maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From krosenv...@apache.org
Subject git commit: [SUREFIRE-933] Tests not matching categories would fail build
Date Sun, 09 Dec 2012 12:30:14 GMT
Updated Branches:
  refs/heads/master 39a67f583 -> 0cb28a6de


[SUREFIRE-933] Tests not matching categories would fail build

Fixed in a way that does not break forkMode=onceperthread (enhanced the IT for that), refined
the logic of JUnitCoreProvider.canRunClass (to also detect the BasicTest class from the IT)
Tests are now filtered right before handing them over to JUnit. In case of a NoTestsRemainException
while filtering the test request, nothing will be handed over to JUnit.


Project: http://git-wip-us.apache.org/repos/asf/maven-surefire/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven-surefire/commit/0cb28a6d
Tree: http://git-wip-us.apache.org/repos/asf/maven-surefire/tree/0cb28a6d
Diff: http://git-wip-us.apache.org/repos/asf/maven-surefire/diff/0cb28a6d

Branch: refs/heads/master
Commit: 0cb28a6de72ee273b0eea02555513513bc213724
Parents: 39a67f5
Author: agudian <andreas.gudian@gmail.com>
Authored: Sat Dec 8 21:02:02 2012 +0100
Committer: Kristian Rosenvold <krosenvold@apache.org>
Committed: Sun Dec 9 13:24:33 2012 +0100

----------------------------------------------------------------------
 .../jiras/Surefire839TestWithoutCategoriesIT.java  |    9 ++-
 .../surefire/junitcore/JUnitCoreProvider.java      |   91 ++-------------
 .../maven/surefire/junitcore/JUnitCoreWrapper.java |   50 +++++++-
 3 files changed, 63 insertions(+), 87 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/0cb28a6d/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire839TestWithoutCategoriesIT.java
----------------------------------------------------------------------
diff --git a/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire839TestWithoutCategoriesIT.java
b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire839TestWithoutCategoriesIT.java
index 6c03349..ebb6672 100755
--- a/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire839TestWithoutCategoriesIT.java
+++ b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire839TestWithoutCategoriesIT.java
@@ -28,6 +28,13 @@ public class Surefire839TestWithoutCategoriesIT
     @Test
     public void classWithoutCategory()
     {
-        unpack( "junit48-categories" ).setJUnitVersion( "4.11" ).executeTest().verifyErrorFree(
2 );
+        unpack( "junit48-categories" ).setJUnitVersion( "4.11" ).executeTest().verifyErrorFree(
3 );
+    }
+
+    @Test
+    public void classWithoutCategoryForked()
+    {
+        unpack( "junit48-categories" ).setJUnitVersion( "4.11" ).forkOncePerThread().threadCount(
+            2 ).executeTest().verifyErrorFree( 3 );
     }
 }

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/0cb28a6d/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/JUnitCoreProvider.java
----------------------------------------------------------------------
diff --git a/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/JUnitCoreProvider.java
b/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/JUnitCoreProvider.java
index 81ea524..d857aff 100644
--- a/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/JUnitCoreProvider.java
+++ b/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/JUnitCoreProvider.java
@@ -19,8 +19,6 @@ package org.apache.maven.surefire.junitcore;
  * under the License.
  */
 
-import java.lang.reflect.Method;
-import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -45,7 +43,6 @@ import org.apache.maven.surefire.util.ScannerFilter;
 import org.apache.maven.surefire.util.TestsToRun;
 import org.apache.maven.surefire.util.internal.StringUtils;
 
-import org.junit.runner.Description;
 import org.junit.runner.manipulation.Filter;
 
 /**
@@ -99,26 +96,10 @@ public class JUnitCoreProvider
     public Iterator getSuites()
     {
         final Filter filter = jUnit48Reflector.isJUnit48Available() ? createJUnit48Filter()
: null;
-        testsToRun = getSuitesAsList( filter );
+        testsToRun = scanClassPath();
         return testsToRun.iterator();
     }
 
-    private boolean containsSomethingRunnable( TestsToRun testsToRun, Filter filter )
-    {
-        if ( filter == null )
-        {
-            return true;
-        }
-        for ( Class o : testsToRun.getLocatedClasses() )
-        {
-            if ( canRunClass( filter, o ) )
-            {
-                return true;
-            }
-        }
-        return false;
-    }
-
     public RunResult invoke( Object forkTestSet )
         throws TestSetFailedException, ReporterException
     {
@@ -143,76 +124,26 @@ public class JUnitCoreProvider
             }
             else
             {
-                testsToRun = getSuitesAsList( filter );
+                testsToRun = scanClassPath();
             }
         }
 
-        if ( containsSomethingRunnable( testsToRun, filter ) )
-        {
-
-            final Map<String, TestSet> testSetMap = new ConcurrentHashMap<String,
TestSet>();
+        final Map<String, TestSet> testSetMap = new ConcurrentHashMap<String, TestSet>();
 
-            RunListener listener = ConcurrentReporterManager.createInstance( testSetMap,
reporterFactory,
-                                                                             jUnitCoreParameters.isParallelClasses(),
-                                                                             jUnitCoreParameters.isParallelBoth(),
-                                                                             consoleLogger
);
+        RunListener listener = ConcurrentReporterManager.createInstance( testSetMap, reporterFactory,
+                                                                         jUnitCoreParameters.isParallelClasses(),
+                                                                         jUnitCoreParameters.isParallelBoth(),
+                                                                         consoleLogger );
 
-            ConsoleOutputCapture.startCapture( (ConsoleOutputReceiver) listener );
+        ConsoleOutputCapture.startCapture( (ConsoleOutputReceiver) listener );
 
-            org.junit.runner.notification.RunListener jUnit4RunListener =
-                new JUnitCoreRunListener( listener, testSetMap );
-            customRunListeners.add( 0, jUnit4RunListener );
+        org.junit.runner.notification.RunListener jUnit4RunListener = new JUnitCoreRunListener(
listener, testSetMap );
+        customRunListeners.add( 0, jUnit4RunListener );
 
-            JUnitCoreWrapper.execute( testsToRun, jUnitCoreParameters, customRunListeners,
filter );
-        }
+        JUnitCoreWrapper.execute( testsToRun, jUnitCoreParameters, customRunListeners, filter
);
         return reporterFactory.close();
     }
 
-    @SuppressWarnings( "unchecked" )
-    private TestsToRun getSuitesAsList( Filter filter )
-    {
-        List<Class<?>> res = new ArrayList<Class<?>>( 500 );
-        TestsToRun max = scanClassPath();
-        if ( filter == null )
-        {
-            return max;
-        }
-
-        Iterator<Class<?>> it = max.iterator();
-        while ( it.hasNext() )
-        {
-            Class<?> clazz = it.next();
-            if ( canRunClass( filter, clazz ) )
-            {
-                res.add( clazz );
-            }
-        }
-        return new TestsToRun( res );
-    }
-
-    private boolean canRunClass( Filter filter, Class<?> clazz )
-    {
-        boolean isCategoryAnnotatedClass = jUnit48Reflector.isCategoryAnnotationPresent(
clazz );
-        Description d = Description.createSuiteDescription( clazz );
-        if ( filter.shouldRun( d ) )
-        {
-            return true;
-        }
-        else
-        {
-            for ( Method method : clazz.getMethods() )
-            {
-                final Description testDescription =
-                    Description.createTestDescription( clazz, method.getName(), method.getAnnotations()
);
-                if ( filter.shouldRun( testDescription ) )
-                {
-                    return true;
-                }
-            }
-        }
-        return false;
-    }
-
     private Filter createJUnit48Filter()
     {
         final FilterFactory filterFactory = new FilterFactory( testClassLoader );

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/0cb28a6d/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/JUnitCoreWrapper.java
----------------------------------------------------------------------
diff --git a/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/JUnitCoreWrapper.java
b/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/JUnitCoreWrapper.java
index aeda523..99bf6dc 100644
--- a/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/JUnitCoreWrapper.java
+++ b/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/JUnitCoreWrapper.java
@@ -19,6 +19,7 @@ package org.apache.maven.surefire.junitcore;
  * under the License.
  */
 
+import java.util.Iterator;
 import java.util.List;
 import java.util.concurrent.ExecutionException;
 import org.apache.maven.surefire.common.junit4.JUnit4RunListener;
@@ -29,7 +30,9 @@ import org.junit.runner.Computer;
 import org.junit.runner.JUnitCore;
 import org.junit.runner.Request;
 import org.junit.runner.Result;
+import org.junit.runner.Runner;
 import org.junit.runner.manipulation.Filter;
+import org.junit.runner.manipulation.NoTestsRemainException;
 import org.junit.runner.notification.RunListener;
 
 /**
@@ -40,6 +43,32 @@ import org.junit.runner.notification.RunListener;
 
 class JUnitCoreWrapper
 {
+    private static class FilteringRequest
+        extends Request
+    {
+        private Runner filteredRunner;
+
+        public FilteringRequest( Request req, Filter filter )
+        {
+            try
+            {
+                Runner runner = req.getRunner();
+                filter.apply( runner );
+                filteredRunner = runner;
+            }
+            catch ( NoTestsRemainException e )
+            {
+                filteredRunner = null;
+            }
+        }
+
+        @Override
+        public Runner getRunner()
+        {
+            return filteredRunner;
+        }
+    }
+
     public static void execute( TestsToRun testsToRun, JUnitCoreParameters jUnitCoreParameters,
                                 List<RunListener> listeners, Filter filter )
         throws TestSetFailedException
@@ -53,14 +82,23 @@ class JUnitCoreWrapper
 
         try
         {
-            Request req = Request.classes( computer, testsToRun.getLocatedClasses() );
-            if ( filter != null )
+            // in order to support LazyTestsToRun, the iterator must be used
+            Iterator classIter = testsToRun.iterator();
+            while ( classIter.hasNext() )
             {
-                req = req.filterWith( filter );
-            }
+                Request req = Request.classes( computer, new Class[]{ (Class) classIter.next()
} );
+                if ( filter != null )
+                {
+                    req = new FilteringRequest( req, filter );
+                    if ( req.getRunner() == null )
+                    {
+                        continue;
+                    }
+                }
 
-            final Result run = junitCore.run( req );
-            JUnit4RunListener.rethrowAnyTestMechanismFailures( run );
+                final Result run = junitCore.run( req );
+                JUnit4RunListener.rethrowAnyTestMechanismFailures( run );
+            }
         }
         finally
         {


Mime
View raw message