diff --git a/src/etc/testcases/taskdefs/optional/junit.xml b/src/etc/testcases/taskdefs/optional/junit.xml
index a8074ddb5..b97e36430 100644
--- a/src/etc/testcases/taskdefs/optional/junit.xml
+++ b/src/etc/testcases/taskdefs/optional/junit.xml
@@ -7,6 +7,10 @@
+
+
+
+
@@ -69,4 +73,23 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java
index 9300bd7a1..1a9161e8b 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java
@@ -805,9 +805,14 @@ public class JUnitTask extends Task {
if (summary) {
log("Running " + test.getName(), Project.MSG_INFO);
+ String prefix = "";
+ if ("withoutanderr".equalsIgnoreCase(summaryValue)) {
+ prefix = "OutErr";
+ }
cmd.createArgument()
.setValue("formatter"
- + "=org.apache.tools.ant.taskdefs.optional.junit.SummaryJUnitResultFormatter");
+ + "=org.apache.tools.ant.taskdefs.optional.junit."
+ + prefix + "SummaryJUnitResultFormatter");
}
cmd.createArgument().setValue("showoutput="
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java
index e999a605f..8ecb4074c 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java
@@ -126,16 +126,6 @@ public class JUnitTestRunner implements TestListener {
*/
private boolean haltOnFailure = false;
- /**
- * The corresponding testsuite.
- */
- private Test suite = null;
-
- /**
- * Exception caught in constructor.
- */
- private Exception exception;
-
/**
* Returncode
*/
@@ -158,6 +148,9 @@ public class JUnitTestRunner implements TestListener {
/** Running more than one test suite? */
private static boolean multipleTests = false;
+ /** ClassLoader passed in in non-forked mode. */
+ private ClassLoader loader;
+
/**
* Constructor for fork=true or when the user hasn't specified a
* classpath.
@@ -197,38 +190,7 @@ public class JUnitTestRunner implements TestListener {
this.haltOnError = haltOnError;
this.haltOnFailure = haltOnFailure;
this.showOutput = showOutput;
-
- try {
- Class testClass = null;
- if (loader == null) {
- testClass = Class.forName(test.getName());
- } else {
- testClass = Class.forName(test.getName(), true, loader);
- }
-
- Method suiteMethod = null;
- try {
- // check if there is a suite method
- suiteMethod = testClass.getMethod("suite", new Class[0]);
- } catch (NoSuchMethodException e) {
- // no appropriate suite method found. We don't report any
- // error here since it might be perfectly normal.
- }
- if (suiteMethod != null) {
- // if there is a suite method available, then try
- // to extract the suite from it. If there is an error
- // here it will be caught below and reported.
- suite = (Test) suiteMethod.invoke(null, new Class[0]);
- } else {
- // try to extract a test suite automatically
- // this will generate warnings if the class is no suitable Test
- suite = new TestSuite(testClass);
- }
-
- } catch (Exception e) {
- retCode = ERRORS;
- exception = e;
- }
+ this.loader = loader;
}
public void run() {
@@ -238,77 +200,114 @@ public class JUnitTestRunner implements TestListener {
res.addListener((TestListener) formatters.elementAt(i));
}
- long start = System.currentTimeMillis();
+ ByteArrayOutputStream errStrm = new ByteArrayOutputStream();
+ systemError = new PrintStream(errStrm);
- fireStartTestSuite();
- if (exception != null) { // had an exception in the constructor
- for (int i = 0; i < formatters.size(); i++) {
- ((TestListener) formatters.elementAt(i)).addError(null,
- exception);
- }
- junitTest.setCounts(1, 0, 1);
- junitTest.setRunTime(0);
- } else {
+ ByteArrayOutputStream outStrm = new ByteArrayOutputStream();
+ systemOut = new PrintStream(outStrm);
+ PrintStream savedOut = null;
+ PrintStream savedErr = null;
- ByteArrayOutputStream errStrm = new ByteArrayOutputStream();
- systemError = new PrintStream(errStrm);
+ if (forked) {
+ savedOut = System.out;
+ savedErr = System.err;
+ if (!showOutput) {
+ System.setOut(systemOut);
+ System.setErr(systemError);
+ } else {
+ System.setOut(new PrintStream(
+ new TeeOutputStream(savedOut, systemOut)
+ )
+ );
+ System.setErr(new PrintStream(
+ new TeeOutputStream(savedErr,
+ systemError)
+ )
+ );
+ }
+ perm = null;
+ } else {
+ if (perm != null) {
+ perm.setSecurityManager();
+ }
+ }
- ByteArrayOutputStream outStrm = new ByteArrayOutputStream();
- systemOut = new PrintStream(outStrm);
+ Test suite = null;
+ Exception exception = null;
- PrintStream savedOut = null;
- PrintStream savedErr = null;
+ try {
- if (forked) {
- savedOut = System.out;
- savedErr = System.err;
- if (!showOutput) {
- System.setOut(systemOut);
- System.setErr(systemError);
+ try {
+ Class testClass = null;
+ if (loader == null) {
+ testClass = Class.forName(junitTest.getName());
} else {
- System.setOut(new PrintStream(
- new TeeOutputStream(savedOut, systemOut)
- )
- );
- System.setErr(new PrintStream(
- new TeeOutputStream(savedErr,
- systemError)
- )
- );
+ testClass = Class.forName(junitTest.getName(), true,
+ loader);
}
- perm = null;
- } else {
- if (perm != null) {
- perm.setSecurityManager();
+
+ Method suiteMethod = null;
+ try {
+ // check if there is a suite method
+ suiteMethod = testClass.getMethod("suite", new Class[0]);
+ } catch (NoSuchMethodException e) {
+ // no appropriate suite method found. We don't report any
+ // error here since it might be perfectly normal.
+ }
+ if (suiteMethod != null) {
+ // if there is a suite method available, then try
+ // to extract the suite from it. If there is an error
+ // here it will be caught below and reported.
+ suite = (Test) suiteMethod.invoke(null, new Class[0]);
+ } else {
+ // try to extract a test suite automatically this
+ // will generate warnings if the class is no
+ // suitable Test
+ suite = new TestSuite(testClass);
}
+
+ } catch (Exception e) {
+ retCode = ERRORS;
+ exception = e;
}
+ long start = System.currentTimeMillis();
- try {
- suite.run(res);
- } finally {
- if (perm != null) {
- perm.restoreSecurityManager();
- }
- if (savedOut != null) {
- System.setOut(savedOut);
+ fireStartTestSuite();
+ if (exception != null) { // had an exception constructing suite
+ for (int i = 0; i < formatters.size(); i++) {
+ ((TestListener) formatters.elementAt(i))
+ .addError(null, exception);
}
- if (savedErr != null) {
- System.setErr(savedErr);
+ junitTest.setCounts(1, 0, 1);
+ junitTest.setRunTime(0);
+ } else {
+ try {
+ suite.run(res);
+ } finally {
+ junitTest.setCounts(res.runCount(), res.failureCount(),
+ res.errorCount());
+ junitTest.setRunTime(System.currentTimeMillis() - start);
}
-
- systemError.close();
- systemError = null;
- systemOut.close();
- systemOut = null;
- sendOutAndErr(new String(outStrm.toByteArray()),
- new String(errStrm.toByteArray()));
-
- junitTest.setCounts(res.runCount(), res.failureCount(),
- res.errorCount());
- junitTest.setRunTime(System.currentTimeMillis() - start);
}
+ } finally {
+ if (perm != null) {
+ perm.restoreSecurityManager();
+ }
+ if (savedOut != null) {
+ System.setOut(savedOut);
+ }
+ if (savedErr != null) {
+ System.setErr(savedErr);
+ }
+
+ systemError.close();
+ systemError = null;
+ systemOut.close();
+ systemOut = null;
+ sendOutAndErr(new String(outStrm.toByteArray()),
+ new String(errStrm.toByteArray()));
}
fireEndTestSuite();
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/OutErrSummaryJUnitResultFormatter.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/OutErrSummaryJUnitResultFormatter.java
new file mode 100644
index 000000000..655a9243d
--- /dev/null
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/OutErrSummaryJUnitResultFormatter.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.tools.ant.taskdefs.optional.junit;
+
+/**
+ * Used instead of SummaryJUnitResultFormatter in forked tests if
+ * withOutAndErr is requested.
+ */
+
+public class OutErrSummaryJUnitResultFormatter
+ extends SummaryJUnitResultFormatter {
+
+ /**
+ * Empty
+ */
+ public OutErrSummaryJUnitResultFormatter() {
+ super();
+ setWithOutAndErr(true);
+ }
+}
diff --git a/src/testcases/org/apache/tools/ant/taskdefs/optional/junit/JUnitTaskTest.java b/src/testcases/org/apache/tools/ant/taskdefs/optional/junit/JUnitTaskTest.java
index 9fb5e5430..f0640d9f6 100644
--- a/src/testcases/org/apache/tools/ant/taskdefs/optional/junit/JUnitTaskTest.java
+++ b/src/testcases/org/apache/tools/ant/taskdefs/optional/junit/JUnitTaskTest.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002,2004 The Apache Software Foundation
+ * Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,17 +16,15 @@
*/
package org.apache.tools.ant.taskdefs.optional.junit;
-import org.apache.tools.ant.Project;
-import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildFileTest;
-import java.lang.reflect.InvocationTargetException;
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
public class JUnitTaskTest extends BuildFileTest {
/**
* Constructor for the JUnitTaskTest object
- *
- * @param name we dont know
*/
public JUnitTaskTest(String name) {
super(name);
@@ -45,7 +43,7 @@ public class JUnitTaskTest extends BuildFileTest {
* The teardown method for JUnit
*/
public void tearDown() {
- //executeTarget("cleanup");
+ executeTarget("cleanup");
}
public void testCrash() {
@@ -64,5 +62,69 @@ public class JUnitTaskTest extends BuildFileTest {
expectPropertyUnset("notimeout", "timeout");
}
+ public void testNonForkedCapture() throws IOException {
+ executeTarget("capture");
+ assertNoPrint(getLog(), "log");
+ assertNoPrint(getFullLog(), "debug log");
+ }
+
+ public void testForkedCapture() throws IOException {
+ getProject().setProperty("fork", "true");
+ testNonForkedCapture();
+ // those would fail because of the way BuildFileTest captures output
+ assertNoPrint(getOutput(), "output");
+ assertNoPrint(getError(), "error output");
+ assertOutput();
+ }
+
+ private void assertNoPrint(String result, String where) {
+ assertTrue(where + " '" + result + "' must not contain print statement",
+ result.indexOf("print to System.") == -1);
+ }
+
+ private void assertOutput() throws IOException {
+ FileReader inner = new FileReader(getProject()
+ .resolveFile("testlog.txt"));
+ BufferedReader reader = new BufferedReader(inner);
+ try {
+ String line = reader.readLine();
+ assertEquals("Testsuite: org.apache.tools.ant.taskdefs.optional.junit.Printer",
+ line);
+ line = reader.readLine();
+ assertNotNull(line);
+ assertTrue(line.startsWith("Tests run: 1, Failures: 0, Errors: 0, Time elapsed:"));
+ line = reader.readLine();
+ assertEquals("------------- Standard Output ---------------",
+ line);
+ assertPrint(reader.readLine(), "static", "out");
+ assertPrint(reader.readLine(), "constructor", "out");
+ assertPrint(reader.readLine(), "method", "out");
+ line = reader.readLine();
+ assertEquals("------------- ---------------- ---------------",
+ line);
+ line = reader.readLine();
+ assertEquals("------------- Standard Error -----------------",
+ line);
+ assertPrint(reader.readLine(), "static", "err");
+ assertPrint(reader.readLine(), "constructor", "err");
+ assertPrint(reader.readLine(), "method", "err");
+ line = reader.readLine();
+ assertEquals("------------- ---------------- ---------------",
+ line);
+ line = reader.readLine();
+ assertEquals("", line);
+ line = reader.readLine();
+ assertNotNull(line);
+ assertTrue(line.startsWith("Testcase: testNoCrash took "));
+ } finally {
+ inner.close();
+ }
+ }
+
+ private void assertPrint(String line, String from, String to) {
+ String search = from + " print to System." + to;
+ assertEquals(search, line);
+ }
+
}
diff --git a/src/testcases/org/apache/tools/ant/taskdefs/optional/junit/NoVmCrash.java b/src/testcases/org/apache/tools/ant/taskdefs/optional/junit/NoVmCrash.java
index 2a540c54c..ee8883226 100644
--- a/src/testcases/org/apache/tools/ant/taskdefs/optional/junit/NoVmCrash.java
+++ b/src/testcases/org/apache/tools/ant/taskdefs/optional/junit/NoVmCrash.java
@@ -16,9 +16,7 @@
*/
package org.apache.tools.ant.taskdefs.optional.junit;
-import junit.framework.Test;
import junit.framework.TestCase;
-import junit.framework.TestResult;
/**
* @version $Revision$
diff --git a/src/testcases/org/apache/tools/ant/taskdefs/optional/junit/Printer.java b/src/testcases/org/apache/tools/ant/taskdefs/optional/junit/Printer.java
new file mode 100644
index 000000000..49d4fe58a
--- /dev/null
+++ b/src/testcases/org/apache/tools/ant/taskdefs/optional/junit/Printer.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.tools.ant.taskdefs.optional.junit;
+
+import junit.framework.TestCase;
+
+/**
+ * @version $Revision$
+ */
+public class Printer extends TestCase {
+
+ public Printer(String name) {
+ super(name);
+ System.err.println("constructor print to System.err");
+ System.out.println("constructor print to System.out");
+ }
+
+ static {
+ System.err.println("static print to System.err");
+ System.out.println("static print to System.out");
+ }
+
+ public void testNoCrash() {
+ System.err.println("method print to System.err");
+ System.out.println("method print to System.out");
+ }
+
+}
diff --git a/src/testcases/org/apache/tools/ant/taskdefs/optional/junit/Sleeper.java b/src/testcases/org/apache/tools/ant/taskdefs/optional/junit/Sleeper.java
index e0cb08862..fd23c27bd 100644
--- a/src/testcases/org/apache/tools/ant/taskdefs/optional/junit/Sleeper.java
+++ b/src/testcases/org/apache/tools/ant/taskdefs/optional/junit/Sleeper.java
@@ -16,9 +16,7 @@
*/
package org.apache.tools.ant.taskdefs.optional.junit;
-import junit.framework.Test;
import junit.framework.TestCase;
-import junit.framework.TestResult;
/**
* @version $Revision$
diff --git a/src/testcases/org/apache/tools/ant/taskdefs/optional/junit/VmCrash.java b/src/testcases/org/apache/tools/ant/taskdefs/optional/junit/VmCrash.java
index b75d6ff21..17d50e002 100644
--- a/src/testcases/org/apache/tools/ant/taskdefs/optional/junit/VmCrash.java
+++ b/src/testcases/org/apache/tools/ant/taskdefs/optional/junit/VmCrash.java
@@ -16,9 +16,7 @@
*/
package org.apache.tools.ant.taskdefs.optional.junit;
-import junit.framework.Test;
import junit.framework.TestCase;
-import junit.framework.TestResult;
/**
* @version $Revision$