From 7fd46296e105c7a7abc57b5cf2aee10a3466a9e9 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Thu, 19 Aug 2010 12:21:03 +0000 Subject: [PATCH] allow test listener events to be enabled by an attribute or a magic property - disable them by default git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@987139 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 6 +++ docs/manual/Tasks/junit.html | 27 ++++++++++++ src/etc/testcases/taskdefs/optional/junit.xml | 4 +- .../taskdefs/optional/junit/JUnitTask.java | 43 +++++++++++++++++-- .../optional/junit/JUnitTestListenerTest.java | 26 +++++++++++ 5 files changed, 101 insertions(+), 5 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index b963f3bf4..ebf7115a9 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -46,6 +46,12 @@ Changes that could break older environments: store their information are now excluded by the defaultexcludes. Bugzilla Report 49624. + * The task no longer generates TestListener events - which + have been introduced in ant 1.7.0 - by default. The task has a new + attribute enableTestListenerEvents and a new "magic" property + ant.junit.enabletestlistenerevents has been added that can be used + to reinstate the old behavior. + Fixed bugs: ----------- diff --git a/docs/manual/Tasks/junit.html b/docs/manual/Tasks/junit.html index ada4e3c85..762074a26 100644 --- a/docs/manual/Tasks/junit.html +++ b/docs/manual/Tasks/junit.html @@ -235,6 +235,19 @@ elements).

since Ant 1.8.0 No + + enableTestListenerEvents + Whether Ant should send fine grained information + about the running tests to Ant's logging system at the verbose + level. Such events may be used by custom test listeners to show + the progress of tests.
+ Defaults to false.
+ Can be overridden by a magic + property.
+ since Ant 1.8.2 - Ant 1.7.0 to 1.8.1 behave as + if this attribute was true by default. + No +

By using the errorproperty and failureproperty @@ -635,6 +648,20 @@ supported.

to your junit task.

+

ant.junit.enabletestlistenerevents + magic property

+ +

Since 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.

+

Examples

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; }