From e7de779f7c1e189f3d1528d0b26b5af5613648c0 Mon Sep 17 00:00:00 2001
From: Stefan Bodewig
-
-
- JUnit's System.err/.out handling
-
-
-
-
- Currently this is coupled to SummaryResultFormatter -
- no SummaryFormatter, no output. Want to decouple it.
-
-
-
-
- Stefan, others welcome
-
@@ -494,7 +473,28 @@
+
- Stefan, others welcome
+ Stefan
+
+
+
+
diff --git a/docs/manual/OptionalTasks/junit.html b/docs/manual/OptionalTasks/junit.html
index 5d84a81cd..4b4775c08 100644
--- a/docs/manual/OptionalTasks/junit.html
+++ b/docs/manual/OptionalTasks/junit.html
@@ -135,6 +135,13 @@ elements).
+
+ JUnit's System.err/.out handling
+
+
+
+
+ showoutput attribute has been added - doesn't work in
+ some cases yet (same reason as bug PR 7980)
+
+
+
+
+ Stefan
true
.By using the Output will always be passed to the formatters and not by
+ * shown by default. This option should for example be set for
+ * tests that are interactive and prompt the user to do
+ * something. If no such method exists, all public methods starting with "test" and taking no
- * argument will be run.
+ * If no such method exists, all public methods starting with
+ * "test" and taking no argument will be run.
*
* Summary output is generated at the end.
*
* @author Stefan Bodewig
* @author Erik Hatcher
+ *
+ * @since Ant 1.2
*/
public class JUnitTestRunner implements TestListener {
@@ -131,6 +134,11 @@ public class JUnitTestRunner implements TestListener {
*/
private static boolean filtertrace = true;
+ /**
+ * Do we send output to System.out/.err in addition to the formatters?
+ */
+ private boolean showOutput = false;
+
private static final String[] DEFAULT_TRACE_FILTERS = new String[] {
"junit.framework.TestCase",
"junit.framework.TestResult",
@@ -187,20 +195,41 @@ public class JUnitTestRunner implements TestListener {
* Constructor for fork=true or when the user hasn't specified a
* classpath.
*/
- public JUnitTestRunner(JUnitTest test, boolean haltOnError, boolean filtertrace,
- boolean haltOnFailure) {
- this(test, haltOnError, filtertrace, haltOnFailure, null);
+ public JUnitTestRunner(JUnitTest test, boolean haltOnError,
+ boolean filtertrace, boolean haltOnFailure) {
+ this(test, haltOnError, filtertrace, haltOnFailure, false);
+ }
+
+ /**
+ * Constructor for fork=true or when the user hasn't specified a
+ * classpath.
+ */
+ public JUnitTestRunner(JUnitTest test, boolean haltOnError,
+ boolean filtertrace, boolean haltOnFailure,
+ boolean showOutput) {
+ this(test, haltOnError, filtertrace, haltOnFailure, showOutput, null);
}
/**
* Constructor to use when the user has specified a classpath.
*/
- public JUnitTestRunner(JUnitTest test, boolean haltOnError, boolean filtertrace,
- boolean haltOnFailure, ClassLoader loader) {
+ public JUnitTestRunner(JUnitTest test, boolean haltOnError,
+ boolean filtertrace, boolean haltOnFailure,
+ ClassLoader loader) {
+ this(test, haltOnError, filtertrace, haltOnFailure, false, loader);
+ }
+
+ /**
+ * Constructor to use when the user has specified a classpath.
+ */
+ public JUnitTestRunner(JUnitTest test, boolean haltOnError,
+ boolean filtertrace, boolean haltOnFailure,
+ boolean showOutput, ClassLoader loader) {
this.filtertrace = filtertrace;
this.junitTest = test;
this.haltOnError = haltOnError;
this.haltOnFailure = haltOnFailure;
+ this.showOutput = showOutput;
try {
Class testClass = null;
@@ -269,9 +298,26 @@ public class JUnitTestRunner implements TestListener {
if (forked) {
savedOut = System.out;
- System.setOut(systemOut);
savedErr = System.err;
- System.setErr(systemError);
+ if (!showOutput) {
+ System.setOut(systemOut);
+ System.setErr(systemError);
+ } else {
+ System.setOut(new PrintStream(
+ new TeeOutputStream(
+ new OutputStream[] {savedOut,
+ systemOut}
+ )
+ )
+ );
+ System.setErr(new PrintStream(
+ new TeeOutputStream(
+ new OutputStream[] {savedErr,
+ systemError}
+ )
+ )
+ );
+ }
}
@@ -384,13 +430,15 @@ public class JUnitTestRunner implements TestListener {
private void fireStartTestSuite() {
for (int i = 0; i < formatters.size(); i++) {
- ((JUnitResultFormatter) formatters.elementAt(i)).startTestSuite(junitTest);
+ ((JUnitResultFormatter) formatters.elementAt(i))
+ .startTestSuite(junitTest);
}
}
private void fireEndTestSuite() {
for (int i = 0; i < formatters.size(); i++) {
- ((JUnitResultFormatter) formatters.elementAt(i)).endTestSuite(junitTest);
+ ((JUnitResultFormatter) formatters.elementAt(i))
+ .endTestSuite(junitTest);
}
}
@@ -417,6 +465,9 @@ public class JUnitTestRunner implements TestListener {
* classname,filename. If filename is ommitted, System.out is
* assumed.errorproperty
and failureproperty
diff --git a/src/etc/testcases/taskdefs/optional/junit.xml b/src/etc/testcases/taskdefs/optional/junit.xml
index 1a36836a8..766266889 100644
--- a/src/etc/testcases/taskdefs/optional/junit.xml
+++ b/src/etc/testcases/taskdefs/optional/junit.xml
@@ -1,34 +1,39 @@
none
*
+ *
+ *
*
*/
public static void main(String[] args) throws IOException {
@@ -425,6 +476,7 @@ public class JUnitTestRunner implements TestListener {
boolean haltFail = false;
boolean stackfilter = true;
Properties props = new Properties();
+ boolean showOut = false;
if (args.length == 0) {
System.err.println("required argument TestClassName missing");
@@ -446,9 +498,12 @@ public class JUnitTestRunner implements TestListener {
System.exit(ERRORS);
}
} else if (args[i].startsWith("propsfile=")) {
- FileInputStream in = new FileInputStream(args[i].substring(10));
+ FileInputStream in = new FileInputStream(args[i]
+ .substring(10));
props.load(in);
in.close();
+ } else if (args[i].startsWith("showoutput=")) {
+ showOut = Project.toBoolean(args[i].substring(11));
}
}
@@ -462,7 +517,8 @@ public class JUnitTestRunner implements TestListener {
}
t.setProperties(props);
- JUnitTestRunner runner = new JUnitTestRunner(t, haltError, stackfilter, haltFail);
+ JUnitTestRunner runner = new JUnitTestRunner(t, haltError, stackfilter,
+ haltFail, showOut);
runner.forked = true;
transferFormatters(runner);
runner.run();
@@ -473,7 +529,8 @@ public class JUnitTestRunner implements TestListener {
private static void transferFormatters(JUnitTestRunner runner) {
for (int i = 0; i < fromCmdLine.size(); i++) {
- runner.addFormatter((JUnitResultFormatter) fromCmdLine.elementAt(i));
+ runner.addFormatter((JUnitResultFormatter) fromCmdLine
+ .elementAt(i));
}
}
@@ -537,4 +594,25 @@ public class JUnitTestRunner implements TestListener {
return false;
}
+ /**
+ * Helper class that sends output sent to multiple streams.
+ *
+ * @since Ant 1.5
+ */
+ private class TeeOutputStream extends OutputStream {
+
+ private OutputStream[] outs;
+
+ private TeeOutputStream(OutputStream[] outs) {
+ this.outs = outs;
+ }
+
+ public void write(int b) throws IOException {
+ for (int i = 0; i < outs.length; i++) {
+ outs[i].write(b);
+ }
+ }
+
+ }
+
} // JUnitTestRunner
diff --git a/xdocs/ant15_todo.xml b/xdocs/ant15_todo.xml
index 7fc697dd2..8a9413d1d 100644
--- a/xdocs/ant15_todo.xml
+++ b/xdocs/ant15_todo.xml
@@ -89,13 +89,6 @@
showoutput send output to System.err/.out as
+ * well as to the formatters? false Stefan, others welcome
-
-
-
JUnit's System.err/.out handling
- Currently this is coupled to SummaryResultFormatter -
- no SummaryFormatter, no output. Want to decouple it.
- Stefan, others welcome
-
+ Make javadoc a real directory based task
@@ -141,8 +134,15 @@
PGP signing task if possible
Not done, but deemed impossible - at least in the 1.5
time frame.
- Stefan, others welcome
+ Stefan
+
+
+
JUnit's System.err/.out handling
+ showoutput attribute has been added - doesn't work in
+ some cases yet (same reason as bug PR 7980)
+ Stefan