1 17 package org.apache.tools.ant.taskdefs; 18 import java.io.PrintStream ; 19 import junit.framework.AssertionFailedError; 20 import org.apache.tools.ant.BuildException; 21 22 import org.apache.tools.ant.BuildFileTest; 23 import org.apache.tools.ant.DemuxOutputStream; 24 import org.apache.tools.ant.Project; 25 import org.apache.tools.ant.Task; 26 27 32 public class ParallelTest extends BuildFileTest { 33 34 public final static String DIRECT_MESSAGE = "direct"; 35 36 public final static String DELAYED_MESSAGE = "delayed"; 37 38 public final static String FAILURE_MESSAGE = "failure"; 39 40 41 public final static String TEST_BUILD_FILE 42 = "src/etc/testcases/taskdefs/parallel.xml"; 43 44 49 public ParallelTest(String name) { 50 super(name); 51 } 52 53 54 public void setUp() { 55 configureProject(TEST_BUILD_FILE); 56 } 57 58 59 public void testBasic() { 60 Project project = getProject(); 62 project.setUserProperty("test.direct", DIRECT_MESSAGE); 63 project.setUserProperty("test.delayed", DELAYED_MESSAGE); 64 expectOutputAndError("testBasic", "", ""); 65 String log = getLog(); 66 assertEquals("parallel tasks didn't output correct data", log, 67 DIRECT_MESSAGE + DELAYED_MESSAGE); 68 69 } 70 71 72 public void testThreadCount() { 73 Project project = getProject(); 75 project.setUserProperty("test.direct", DIRECT_MESSAGE); 76 project.setUserProperty("test.delayed", DELAYED_MESSAGE); 77 expectOutputAndError("testThreadCount", "", ""); 78 String log = getLog(); 79 int pos = 0; 80 while (pos > -1) { 81 pos = countThreads(log, pos); 82 } 83 } 84 85 94 static int countThreads(String s, int start) { 95 int firstPipe = s.indexOf('|', start); 96 int beginSlash = s.indexOf('/', firstPipe); 97 int lastPipe = s.indexOf('|', beginSlash); 98 if ((firstPipe == -1) || (beginSlash == -1) || (lastPipe == -1)) { 99 return -1; 100 } 101 102 int max = Integer.parseInt(s.substring(firstPipe + 1, beginSlash)); 103 int current = 0; 104 int pos = beginSlash + 1; 105 while (pos < lastPipe) { 106 switch (s.charAt(pos++)) { 107 case '+': 108 current++; 109 break; 110 case '-': 111 current--; 112 break; 113 default: 114 throw new AssertionFailedError("Only expect '+-' in result count, found " 115 + s.charAt(--pos) + " at position " + pos); 116 } 117 if (current > max) { 118 throw new AssertionFailedError("Number of executing threads exceeded number allowed: " 119 + current + " > " + max); 120 } 121 } 122 return lastPipe; 123 } 124 125 126 127 public void testFail() { 128 Project project = getProject(); 130 project.setUserProperty("test.failure", FAILURE_MESSAGE); 131 project.setUserProperty("test.delayed", DELAYED_MESSAGE); 132 expectBuildExceptionContaining("testFail", 133 "fail task in one parallel branch", FAILURE_MESSAGE); 134 } 135 136 137 public void testDemux() { 138 Project project = getProject(); 139 project.addTaskDefinition("demuxtest", DemuxOutputTask.class); 140 PrintStream out = System.out; 141 PrintStream err = System.err; 142 System.setOut(new PrintStream (new DemuxOutputStream(project, false))); 143 System.setErr(new PrintStream (new DemuxOutputStream(project, true))); 144 145 try { 146 project.executeTarget("testDemux"); 147 } finally { 148 System.setOut(out); 149 System.setErr(err); 150 } 151 } 152 153 } 154 155 | Popular Tags |