From 7fd46296e105c7a7abc57b5cf2aee10a3466a9e9 Mon Sep 17 00:00:00 2001
From: Stefan Bodewig
false
.By using the errorproperty
and failureproperty
@@ -635,6 +648,20 @@ supported.
to your junit
task.
ant.junit.enabletestlistenerevents
+ magic propertySince Ant 1.8.2 the enableTestListenerEvents
+ attribute of the task controls whether fine grained logging messages
+ will be sent to the task's verbose log. In addition to this
+ attribute Ant will consult the
+ property ant.junit.enabletestlistenerevents
and the
+ value of the property overrides the setting of the attribute.
This property exists so that containers running Ant that depend on + the additional logging events can ensure they will be generated even + if the build file disables them.
+diff --git a/src/etc/testcases/taskdefs/optional/junit.xml b/src/etc/testcases/taskdefs/optional/junit.xml index afc665465..dc30e6880 100644 --- a/src/etc/testcases/taskdefs/optional/junit.xml +++ b/src/etc/testcases/taskdefs/optional/junit.xml @@ -104,7 +104,9 @@- + + 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 3b5fbb7ba..9a977310b 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 @@ -165,6 +165,7 @@ public class JUnitTask extends Task { private ForkMode forkMode = new ForkMode("perTest"); private boolean splitJunit = false; + private boolean enableTestListenerEvents = false; private JUnitTaskMirror delegate; private ClassLoader mirrorLoader; @@ -186,6 +187,12 @@ public class JUnitTask extends Task { public static final String TESTLISTENER_PREFIX = "junit.framework.TestListener: "; + /** + * Name of magic property that enables test listener events. + */ + public static final String ENABLE_TESTLISTENER_EVENTS = + "ant.junit.enabletestlistenerevents"; + private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); /** @@ -672,6 +679,32 @@ public class JUnitTask extends Task { this.tmpDir = tmpDir; } + /** + * Whether test listener events shall be generated. + * + * Defaults to false.
+ * + *This value will be overridden by the magic property + * ant.junit.enabletestlistenerevents if it has been set.
+ * + * @since Ant 1.8.2 + */ + public void setEnableTestListenerEvents(boolean b) { + enableTestListenerEvents = b; + } + + /** + * Whether test listener events shall be generated. + * @since Ant 1.8.2 + */ + public boolean getEnableTestListenerEvents() { + String e = getProject().getProperty(ENABLE_TESTLISTENER_EVENTS); + if (e != null) { + return Project.toBoolean(e); + } + return enableTestListenerEvents; + } + /** * Adds the jars or directories containing Ant, this task and * JUnit to the classpath - this should make the forked JVM work @@ -953,8 +986,9 @@ public class JUnitTask extends Task { cmd.createArgument().setValue(Constants.LOG_FAILED_TESTS + String.valueOf(logFailedTests)); - cmd.createArgument().setValue( - Constants.LOGTESTLISTENEREVENTS + "true"); // #31885 + // #31885 + cmd.createArgument().setValue(Constants.LOGTESTLISTENEREVENTS + + String.valueOf(getEnableTestListenerEvents())); StringBuffer formatterArg = new StringBuffer(STRING_BUFFER_SIZE); final FormatterElement[] feArray = mergeFormatters(test); @@ -1209,7 +1243,7 @@ public class JUnitTask extends Task { /** * Pass output sent to System.out to the TestRunner so it can - * collect ot for the formatters. + * collect it for the formatters. * * @param output output coming from System.out * @since Ant 1.5 @@ -1360,7 +1394,8 @@ public class JUnitTask extends Task { runner = delegate.newJUnitTestRunner(test, test.getMethods(), test.getHaltonerror(), test.getFiltertrace(), test.getHaltonfailure(), false, - true, classLoader); + getEnableTestListenerEvents(), + classLoader); if (summary) { JUnitTaskMirror.SummaryJUnitResultFormatterMirror f = diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestListenerTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestListenerTest.java index bdf7143f9..31328763e 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestListenerTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestListenerTest.java @@ -45,18 +45,21 @@ public class JUnitTestListenerTest extends BuildFileTest { } public void testFullLogOutput() { + getProject().setProperty("enableEvents", "true"); executeTarget(PASS_TEST_TARGET); assertTrue("expecting full log to have BuildListener events", hasBuildListenerEvents(getFullLog())); } public void testNoLogOutput() { + getProject().setProperty("enableEvents", "true"); executeTarget(PASS_TEST_TARGET); assertFalse("expecting log to not have BuildListener events", hasBuildListenerEvents(getLog())); } public void testTestCountFired() { + getProject().setProperty("enableEvents", "true"); executeTarget(PASS_TEST_TARGET); assertTrue("expecting test count message", hasEventMessage(JUnitTask.TESTLISTENER_PREFIX + @@ -64,6 +67,7 @@ public class JUnitTestListenerTest extends BuildFileTest { } public void testStartTestFired() { + getProject().setProperty("enableEvents", "true"); executeTarget(PASS_TEST_TARGET); assertTrue("expecting test started message", hasEventMessage(JUnitTask.TESTLISTENER_PREFIX + @@ -71,12 +75,34 @@ public class JUnitTestListenerTest extends BuildFileTest { } public void testEndTestFired() { + getProject().setProperty("enableEvents", "true"); executeTarget(PASS_TEST_TARGET); assertTrue("expecting test ended message", hasEventMessage(JUnitTask.TESTLISTENER_PREFIX + "endTest(" + PASS_TEST + ")")); } + public void testNoFullLogOutputByDefault() { + executeTarget(PASS_TEST_TARGET); + assertFalse("expecting full log to not have BuildListener events", + hasBuildListenerEvents(getFullLog())); + } + + public void testFullLogOutputMagicProperty() { + getProject().setProperty(JUnitTask.ENABLE_TESTLISTENER_EVENTS, "true"); + executeTarget(PASS_TEST_TARGET); + assertTrue("expecting full log to have BuildListener events", + hasBuildListenerEvents(getFullLog())); + } + + public void testNoFullLogOutputMagicPropertyWins() { + getProject().setProperty(JUnitTask.ENABLE_TESTLISTENER_EVENTS, "false"); + getProject().setProperty("enableEvents", "true"); + executeTarget(PASS_TEST_TARGET); + assertFalse("expecting full log to not have BuildListener events", + hasBuildListenerEvents(getFullLog())); + } + private boolean hasBuildListenerEvents(String log) { return log.indexOf(JUnitTask.TESTLISTENER_PREFIX) >= 0; }