Repository: ant
Updated Branches:
  refs/heads/1.9.x 49793c937 -> 50a1d4aea


Merge from master


Project: http://git-wip-us.apache.org/repos/asf/ant/repo
Commit: http://git-wip-us.apache.org/repos/asf/ant/commit/6745574e
Tree: http://git-wip-us.apache.org/repos/asf/ant/tree/6745574e
Diff: http://git-wip-us.apache.org/repos/asf/ant/diff/6745574e

Branch: refs/heads/1.9.x
Commit: 6745574ec864d4fe9d832f4ede26da8a4191d689
Parents: 49793c9
Author: Maarten Coene <maart...@apache.org>
Authored: Thu Apr 5 16:45:23 2018 +0200
Committer: Maarten Coene <maart...@apache.org>
Committed: Thu Apr 5 16:45:23 2018 +0200

----------------------------------------------------------------------
 WHATSNEW                                        |  9 ++++
 .../optional/junit/JUnitTestRunner.java         |  5 +-
 .../optional/junit/JUnitTestRunnerTest.java     | 56 ++++++++++++++++++++
 3 files changed, 68 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ant/blob/6745574e/WHATSNEW
----------------------------------------------------------------------
diff --git a/WHATSNEW b/WHATSNEW
index d5081bf..c9757b6 100644
--- a/WHATSNEW
+++ b/WHATSNEW
@@ -1,6 +1,15 @@
 Changes from Ant 1.9.11 TO Ant 1.9.12
 =====================================
 
+Fixed bugs:
+-----------
+
+ * Delay the class initialization of the test classes untill they are
+   passed to JUnit. This way we can avoid that failing static initializers
+   from non-test classes are reported as error when the 'skipNonTests' option
+   is 'true'.
+   Bugzilla Report 60062
+
 Changes from Ant 1.9.10 TO Ant 1.9.11
 =====================================
 

http://git-wip-us.apache.org/repos/asf/ant/blob/6745574e/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java
----------------------------------------------------------------------
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 2eb2316..d4217ce 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
@@ -383,9 +383,10 @@ public class JUnitTestRunner implements TestListener, 
JUnitTaskMirror.JUnitTestR
             try {
                 Class testClass = null;
                 if (loader == null) {
-                    testClass = Class.forName(junitTest.getName());
+                    testClass = Class.forName(junitTest.getName(), false,
+                                              getClass().getClassLoader());
                 } else {
-                    testClass = Class.forName(junitTest.getName(), true,
+                    testClass = Class.forName(junitTest.getName(), false,
                                               loader);
                 }
 

http://git-wip-us.apache.org/repos/asf/ant/blob/6745574e/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunnerTest.java
----------------------------------------------------------------------
diff --git 
a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunnerTest.java
 
b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunnerTest.java
index e8544ce..19d127c 100644
--- 
a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunnerTest.java
+++ 
b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunnerTest.java
@@ -25,6 +25,7 @@ import java.io.OutputStream;
 import java.io.PrintWriter;
 import java.io.StringWriter;
 
+import javafx.scene.control.Control;
 import junit.framework.AssertionFailedError;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
@@ -99,6 +100,35 @@ public class JUnitTestRunnerTest {
         //assertEquals(runner.getFormatter().getError(), 
JUnitTestRunner.FAILURES, runner.getRetCode());
     }
 
+    // check that something which is not a testcase doesn't generate an error
+    // when skipping non-test classes
+    @Test
+    public void testSkipNonTestsNoTestCase() {
+        TestRunner runner = createRunner(NoTestCase.class, true);
+        runner.run();
+        assertEquals(runner.getFormatter().getError(), 
JUnitTestRunner.SUCCESS, runner.getRetCode());
+    }
+
+    // check that something which is not a testcase with a failing static init 
doesn't generate an error
+    // when skipping non-test classes
+    @Test
+    public void testSkipNonTestsNoTestCaseFailingStaticInit() {
+        TestRunner runner = 
createRunner(NoTestCaseStaticInitializerError.class, true);
+        runner.run();
+        assertEquals(runner.getFormatter().getError(), 
JUnitTestRunner.SUCCESS, runner.getRetCode());
+    }
+
+    @Test
+    public void testStaticInitializerErrorTestCase() {
+        TestRunner runner = createRunner(StaticInitializerErrorTestCase.class);
+        runner.run();
+        // On junit3 this is a FAILURE, on junit4 this is an ERROR
+        int ret = runner.getRetCode();
+        if (ret != JUnitTestRunner.FAILURES && ret != JUnitTestRunner.ERRORS) {
+            fail("Unexpected result " + ret + " from junit runner");
+        }
+    }
+
     // check that an exception in the constructor is noticed
     @Test
     public void testInvalidTestCase() {
@@ -134,6 +164,12 @@ public class JUnitTestRunnerTest {
                                             true, true, true);
     }
 
+    protected TestRunner createRunner(Class<?> clazz, boolean skipNonTests) {
+        JUnitTest test = new JUnitTest(clazz.getName());
+        test.setSkipNonTests(skipNonTests);
+        return new TestRunner(test, null, true, true, true);
+    }
+
     protected TestRunner createRunnerForTestMethod(Class<?> clazz, String 
method) {
         return new TestRunner(new JUnitTest(clazz.getName()), new String[] 
{method},
                                             true, true, true);
@@ -196,6 +232,15 @@ public class JUnitTestRunnerTest {
     public static class NoTestCase {
     }
 
+    public static class NoTestCaseStaticInitializerError {
+        static {
+            error();
+        }
+        private static void error() {
+            throw new NullPointerException("thrown on purpose");
+        }
+    }
+
     public static class InvalidMethodTestCase extends TestCase {
         public InvalidMethodTestCase(String name) {
             super(name);
@@ -251,6 +296,17 @@ public class JUnitTestRunnerTest {
         }
     }
 
+    public static class StaticInitializerErrorTestCase extends TestCase {
+        static {
+            error();
+        }
+        private static void error() {
+            throw new NullPointerException("thrown on purpose");
+        }
+        public void testA() {
+        }
+    }
+
     public static class AssertionErrorTest {
         @Test
         public void throwsAssertionError() {

Reply via email to