Support rerunFailingTestsCount for JUnit 4.7 test suites
Project: http://git-wip-us.apache.org/repos/asf/maven-surefire/repo Commit: http://git-wip-us.apache.org/repos/asf/maven-surefire/commit/7552ea92 Tree: http://git-wip-us.apache.org/repos/asf/maven-surefire/tree/7552ea92 Diff: http://git-wip-us.apache.org/repos/asf/maven-surefire/diff/7552ea92 Branch: refs/heads/master Commit: 7552ea926a2df2869aead38a52f4bad49acaea8c Parents: 8cf6e57 Author: Sean Flanigan <sflan...@redhat.com> Authored: Tue Apr 28 14:29:09 2015 +1000 Committer: Sean Flanigan <sflan...@redhat.com> Committed: Tue Apr 28 14:29:09 2015 +1000 ---------------------------------------------------------------------- .../Surefire1152RerunFailingTestsInSuiteIT.java | 27 ++++++++- .../pom.xml | 61 ++++++++++++++++++++ .../common/junit4/JUnit4ProviderUtil.java | 44 ++++++-------- .../common/junit4/JUnit4ProviderUtilTest.java | 8 +-- .../surefire/junitcore/JUnitCoreProvider.java | 2 +- 5 files changed, 108 insertions(+), 34 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/7552ea92/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1152RerunFailingTestsInSuiteIT.java ---------------------------------------------------------------------- diff --git a/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1152RerunFailingTestsInSuiteIT.java b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1152RerunFailingTestsInSuiteIT.java index c17c661..e0bc7de 100644 --- a/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1152RerunFailingTestsInSuiteIT.java +++ b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1152RerunFailingTestsInSuiteIT.java @@ -21,6 +21,7 @@ package org.apache.maven.surefire.its.jiras; import org.apache.maven.surefire.its.fixture.OutputValidator; import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase; +import org.apache.maven.surefire.its.fixture.SurefireLauncher; import org.junit.Test; /** @@ -31,12 +32,32 @@ import org.junit.Test; public class Surefire1152RerunFailingTestsInSuiteIT extends SurefireJUnit4IntegrationTestCase { + private static final String RUNNING_WITH_JUNIT48 = + "Using configured provider org.apache.maven.surefire.junitcore.JUnitCoreProvider"; - @Test - public void rerunsFailingTestInSuite() + public OutputValidator runMethodPattern( String projectName, String... goals ) { - OutputValidator outputValidator = unpack( "surefire-1152-rerunFailingTestsCount-suite" ).executeVerify(); + SurefireLauncher launcher = unpack( projectName ); + for ( String goal : goals ) + { + launcher.addGoal( goal ); + } + OutputValidator outputValidator = launcher.executeTest(); outputValidator.assertTestSuiteResults( 3, 0, 0, 0, 3 ); outputValidator.assertIntegrationTestSuiteResults( 1, 0, 0, 0 ); + return outputValidator; + } + + @Test + public void testJUnit48Provider4() + { + runMethodPattern( "surefire-1152-rerunFailingTestsCount-suite", "-P surefire-junit4" ); } + + @Test + public void testJUnit48Provider47() + { + runMethodPattern( "surefire-1152-rerunFailingTestsCount-suite", "-P surefire-junit47" ).verifyTextInLog( RUNNING_WITH_JUNIT48 ); + } + } http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/7552ea92/surefire-integration-tests/src/test/resources/surefire-1152-rerunFailingTestsCount-suite/pom.xml ---------------------------------------------------------------------- diff --git a/surefire-integration-tests/src/test/resources/surefire-1152-rerunFailingTestsCount-suite/pom.xml b/surefire-integration-tests/src/test/resources/surefire-1152-rerunFailingTestsCount-suite/pom.xml index 5719efd..fd11c14 100644 --- a/surefire-integration-tests/src/test/resources/surefire-1152-rerunFailingTestsCount-suite/pom.xml +++ b/surefire-integration-tests/src/test/resources/surefire-1152-rerunFailingTestsCount-suite/pom.xml @@ -78,6 +78,67 @@ </plugins> </build> + <profiles> + <profile> + <id>surefire-junit47</id> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <dependencies> + <dependency> + <groupId>org.apache.maven.surefire</groupId> + <artifactId>surefire-junit47</artifactId> + <version>${surefire.version}</version> + </dependency> + </dependencies> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-failsafe-plugin</artifactId> + <dependencies> + <dependency> + <groupId>org.apache.maven.surefire</groupId> + <artifactId>surefire-junit47</artifactId> + <version>${surefire.version}</version> + </dependency> + </dependencies> + </plugin> + </plugins> + </build> + </profile> + <profile> + <id>surefire-junit4</id> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <dependencies> + <dependency> + <groupId>org.apache.maven.surefire</groupId> + <artifactId>surefire-junit4</artifactId> + <version>${surefire.version}</version> + </dependency> + </dependencies> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-failsafe-plugin</artifactId> + <dependencies> + <dependency> + <groupId>org.apache.maven.surefire</groupId> + <artifactId>surefire-junit4</artifactId> + <version>${surefire.version}</version> + </dependency> + </dependencies> + </plugin> + </plugins> + </build> + </profile> + </profiles> + <dependencies> <dependency> <groupId>junit</groupId> http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/7552ea92/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/common/junit4/JUnit4ProviderUtil.java ---------------------------------------------------------------------- diff --git a/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/common/junit4/JUnit4ProviderUtil.java b/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/common/junit4/JUnit4ProviderUtil.java index 1e35552..6861a21 100644 --- a/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/common/junit4/JUnit4ProviderUtil.java +++ b/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/common/junit4/JUnit4ProviderUtil.java @@ -19,8 +19,6 @@ package org.apache.maven.surefire.common.junit4; * under the License. */ -import org.apache.maven.surefire.util.TestsToRun; - import java.util.Collection; import java.util.HashMap; import java.util.HashSet; @@ -28,6 +26,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import org.apache.maven.surefire.testset.TestSetFailedException; import org.apache.maven.surefire.util.internal.StringUtils; import org.junit.runner.Description; @@ -52,38 +51,31 @@ public final class JUnit4ProviderUtil * Organize all the failures in previous run into a map between test classes and corresponding failing test methods * * @param allFailures all the failures in previous run - * @param testsToRun all the test classes + * @param testClassLoader ClassLoader used for test classes * @return a map between failing test classes and their corresponding failing test methods */ - public static Map<Class<?>, Set<String>> generateFailingTests( List<Failure> allFailures, TestsToRun testsToRun ) + public static Map<Class<?>, Set<String>> generateFailingTests( List<Failure> allFailures, + ClassLoader testClassLoader ) + throws TestSetFailedException { Map<Class<?>, Set<String>> testClassMethods = new HashMap<Class<?>, Set<String>>(); - - for ( Failure failure : allFailures ) + Set<ClassMethod> failingTests = generateFailingTests( allFailures ); + for ( ClassMethod classMethod: failingTests ) { - Description description = failure.getDescription(); - if ( description.isTest() && !isFailureInsideJUnitItself( description ) ) + try { - ClassMethod classMethod = cutTestClassAndMethod( description ); - if ( classMethod.isValid() ) + Class testClassObj = Class.forName( classMethod.getClazz(), true, testClassLoader ); + Set<String> failingMethods = testClassMethods.get( testClassObj ); + if ( failingMethods == null ) { - Class testClassObj = testsToRun.getClassByName( classMethod.getClazz() ); - - if ( testClassObj != null ) - { - Set<String> failingMethods = testClassMethods.get( testClassObj ); - if ( failingMethods == null ) - { - failingMethods = new HashSet<String>(); - failingMethods.add( classMethod.getMethod() ); - testClassMethods.put( testClassObj, failingMethods ); - } - else - { - failingMethods.add( classMethod.getMethod() ); - } - } + failingMethods = new HashSet<String>(); + testClassMethods.put( testClassObj, failingMethods ); } + failingMethods.add( classMethod.getMethod() ); + } + catch ( ClassNotFoundException e ) + { + throw new TestSetFailedException( "Unable to create test class '" + classMethod.getClazz() + "'", e ); } } return testClassMethods; http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/7552ea92/surefire-providers/common-junit4/src/test/java/org/apache/maven/surefire/common/junit4/JUnit4ProviderUtilTest.java ---------------------------------------------------------------------- diff --git a/surefire-providers/common-junit4/src/test/java/org/apache/maven/surefire/common/junit4/JUnit4ProviderUtilTest.java b/surefire-providers/common-junit4/src/test/java/org/apache/maven/surefire/common/junit4/JUnit4ProviderUtilTest.java index f4fdc95..6a6e2e7 100644 --- a/surefire-providers/common-junit4/src/test/java/org/apache/maven/surefire/common/junit4/JUnit4ProviderUtilTest.java +++ b/surefire-providers/common-junit4/src/test/java/org/apache/maven/surefire/common/junit4/JUnit4ProviderUtilTest.java @@ -20,6 +20,7 @@ package org.apache.maven.surefire.common.junit4; */ import junit.framework.TestCase; +import org.apache.maven.surefire.testset.TestSetFailedException; import org.apache.maven.surefire.util.TestsToRun; import org.junit.runner.Description; import org.junit.runner.notification.Failure; @@ -39,9 +40,8 @@ import static org.apache.maven.surefire.common.junit4.JUnit4ProviderUtil.*; public class JUnit4ProviderUtilTest extends TestCase { - public void testGenerateFailingTests() + public void testGenerateFailingTests() throws TestSetFailedException { - TestsToRun testsToRun = new TestsToRun( Arrays.asList( new Class[]{ T1.class, T2.class } ) ); List<Failure> failures = new ArrayList<Failure>( ); Description test1Description = Description.createTestDescription( T1.class, "testOne" ); @@ -56,7 +56,7 @@ public class JUnit4ProviderUtilTest failures.add( new Failure( test4Description, new AssertionError() ) ); failures.add( new Failure( test5Description, new RuntimeException() ) ); - Map<Class<?>, Set<String>> result = generateFailingTests( failures, testsToRun ); + Map<Class<?>, Set<String>> result = generateFailingTests( failures, getClass().getClassLoader() ); assertEquals( 2, result.size() ); Set<String> resultForT1 = result.get( T1.class ); @@ -108,4 +108,4 @@ public class JUnit4ProviderUtilTest { } -} \ No newline at end of file +} http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/7552ea92/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 3276b4e..bc2dd3b 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 @@ -147,7 +147,7 @@ public class JUnitCoreProvider for ( int i = 0; i < rerunFailingTestsCount && !testFailureListener.getAllFailures().isEmpty(); i++ ) { Map<Class<?>, Set<String>> failingTests = - JUnit4ProviderUtil.generateFailingTests( testFailureListener.getAllFailures(), testsToRun ); + JUnit4ProviderUtil.generateFailingTests( testFailureListener.getAllFailures(), testClassLoader ); testFailureListener.reset(); final FilterFactory filterFactory = new FilterFactory( testClassLoader ); Filter failingMethodsFilter = filterFactory.createFailingMethodFilter( failingTests );