source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/testcases/org/apache/tools/ant/taskdefs/ParallelTest.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 5.4 KB
Line 
1/*
2 * Copyright 2002-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17package org.apache.tools.ant.taskdefs;
18import java.io.PrintStream;
19import junit.framework.AssertionFailedError;
20import org.apache.tools.ant.BuildException;
21
22import org.apache.tools.ant.BuildFileTest;
23import org.apache.tools.ant.DemuxOutputStream;
24import org.apache.tools.ant.Project;
25import org.apache.tools.ant.Task;
26
27/**
28 * Test of the parallel TaskContainer
29 *
30 * @created 21 February 2002
31 */
32public class ParallelTest extends BuildFileTest {
33 /** Standard property value for the basic test */
34 public final static String DIRECT_MESSAGE = "direct";
35 /** Standard property value for the basic and fail test */
36 public final static String DELAYED_MESSAGE = "delayed";
37 /** Standard property value for the fail test */
38 public final static String FAILURE_MESSAGE = "failure";
39
40 /** the build fiel associated with this test */
41 public final static String TEST_BUILD_FILE
42 = "src/etc/testcases/taskdefs/parallel.xml";
43
44 /**
45 * Constructor for the ParallelTest object
46 *
47 * @param name name of the test
48 */
49 public ParallelTest(String name) {
50 super(name);
51 }
52
53 /** The JUnit setup method */
54 public void setUp() {
55 configureProject(TEST_BUILD_FILE);
56 }
57
58 /** tests basic operation of the parallel task */
59 public void testBasic() {
60 // should get no output at all
61 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 /** tests basic operation of the parallel task */
72 public void testThreadCount() {
73 // should get no output at all
74 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 /**
86 * the test result string should match the regex
87 * <code>^(\|\d+\/(+-)*)+\|$</code> for someting like
88 * <code>|3/++--+-|5/+++++-----|</code>
89 *
90 *@returns -1 no more tests
91 * # start pos of next test
92 *@throws AssertionFailedException when a constraint is invalid
93 */
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 /** tests the failure of a task within a parallel construction */
127 public void testFail() {
128 // should get no output at all
129 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 /** tests the demuxing of output streams in a multithreaded situation */
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
Note: See TracBrowser for help on using the repository browser.