001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs;
019:
020: import java.io.PrintStream;
021:
022: import junit.framework.AssertionFailedError;
023:
024: import org.apache.tools.ant.BuildFileTest;
025: import org.apache.tools.ant.DemuxOutputStream;
026: import org.apache.tools.ant.Project;
027:
028: /**
029: * Test of the parallel TaskContainer
030: *
031: * @created 21 February 2002
032: */
033: public class ParallelTest extends BuildFileTest {
034: /** Standard property value for the basic test */
035: public final static String DIRECT_MESSAGE = "direct";
036: /** Standard property value for the basic and fail test */
037: public final static String DELAYED_MESSAGE = "delayed";
038: /** Standard property value for the fail test */
039: public final static String FAILURE_MESSAGE = "failure";
040:
041: /** the build fiel associated with this test */
042: public final static String TEST_BUILD_FILE = "src/etc/testcases/taskdefs/parallel.xml";
043:
044: /**
045: * Constructor for the ParallelTest object
046: *
047: * @param name name of the test
048: */
049: public ParallelTest(String name) {
050: super (name);
051: }
052:
053: /** The JUnit setup method */
054: public void setUp() {
055: configureProject(TEST_BUILD_FILE);
056: }
057:
058: /** tests basic operation of the parallel task */
059: public void testBasic() {
060: // should get no output at all
061: Project p = getProject();
062: p.setUserProperty("test.direct", DIRECT_MESSAGE);
063: p.setUserProperty("test.delayed", DELAYED_MESSAGE);
064: expectOutputAndError("testBasic", "", "");
065: String log = getLog();
066: assertEquals("parallel tasks didn't output correct data", log,
067: DIRECT_MESSAGE + DELAYED_MESSAGE);
068:
069: }
070:
071: /** tests basic operation of the parallel task */
072: public void testThreadCount() {
073: // should get no output at all
074: Project p = getProject();
075: p.setUserProperty("test.direct", DIRECT_MESSAGE);
076: p.setUserProperty("test.delayed", DELAYED_MESSAGE);
077: expectOutputAndError("testThreadCount", "", "");
078: String log = getLog();
079: int pos = 0;
080: while (pos > -1) {
081: pos = countThreads(log, pos);
082: }
083: }
084:
085: /**
086: * the test result string should match the regex
087: * <code>^(\|\d+\/(+-)*)+\|$</code> for someting like
088: * <code>|3/++--+-|5/+++++-----|</code>
089: *
090: *@returns -1 no more tests
091: * # start pos of next test
092: *@throws AssertionFailedException when a constraint is invalid
093: */
094: static int countThreads(String s, int start) {
095: int firstPipe = s.indexOf('|', start);
096: int beginSlash = s.indexOf('/', firstPipe);
097: int lastPipe = s.indexOf('|', beginSlash);
098: if ((firstPipe == -1) || (beginSlash == -1) || (lastPipe == -1)) {
099: return -1;
100: }
101:
102: int max = Integer.parseInt(s.substring(firstPipe + 1,
103: beginSlash));
104: int current = 0;
105: int pos = beginSlash + 1;
106: while (pos < lastPipe) {
107: switch (s.charAt(pos++)) {
108: case '+':
109: current++;
110: break;
111: case '-':
112: current--;
113: break;
114: default:
115: throw new AssertionFailedError(
116: "Only expect '+-' in result count, found "
117: + s.charAt(--pos) + " at position "
118: + pos);
119: }
120: if (current > max) {
121: throw new AssertionFailedError(
122: "Number of executing threads exceeded number allowed: "
123: + current + " > " + max);
124: }
125: }
126: return lastPipe;
127: }
128:
129: /** tests the failure of a task within a parallel construction */
130: public void testFail() {
131: // should get no output at all
132: Project p = getProject();
133: p.setUserProperty("test.failure", FAILURE_MESSAGE);
134: p.setUserProperty("test.delayed", DELAYED_MESSAGE);
135: expectBuildExceptionContaining("testFail",
136: "fail task in one parallel branch", FAILURE_MESSAGE);
137: }
138:
139: /** tests the demuxing of output streams in a multithreaded situation */
140: public void testDemux() {
141: Project p = getProject();
142: p.addTaskDefinition("demuxtest", DemuxOutputTask.class);
143: PrintStream out = System.out;
144: PrintStream err = System.err;
145: System.setOut(new PrintStream(new DemuxOutputStream(p, false)));
146: System.setErr(new PrintStream(new DemuxOutputStream(p, true)));
147:
148: try {
149: p.executeTarget("testDemux");
150: } finally {
151: System.setOut(out);
152: System.setErr(err);
153: }
154: }
155:
156: }
|