This is an automated email from the ASF dual-hosted git repository.

tibordigana pushed a commit to branch SUREFIRE-1585-tibor2
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git


The following commit(s) were added to refs/heads/SUREFIRE-1585-tibor2 by this 
push:
     new 47d4ff3  [SUREFIRE-1585] Align JUnit Platform version at runtime
47d4ff3 is described below

commit 47d4ff397b3754785a6a18bce23658a2c70a264d
Author: tibordigana <tibordig...@apache.org>
AuthorDate: Sat Feb 23 17:08:35 2019 +0100

    [SUREFIRE-1585] Align JUnit Platform version at runtime
---
 .../plugin/surefire/AbstractSurefireMojo.java      |  96 ++++----
 .../plugin/surefire/AbstractSurefireMojoTest.java  | 251 +++++++++++++++++++--
 .../src/site/apt/examples/junit-platform.apt.vm    |  81 ++++++-
 .../maven/surefire/its/jiras/Surefire1585IT.java   |  11 +-
 .../pom.xml                                        |  17 +-
 .../src/test/java/JUnit4Test.java}                 |  12 +-
 .../pom.xml                                        |   2 +-
 .../src/test/java/JupiterTest.java                 |   2 -
 8 files changed, 386 insertions(+), 86 deletions(-)

diff --git 
a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
 
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
index f986bc1..da68069 100644
--- 
a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
+++ 
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
@@ -96,6 +96,7 @@ import java.util.Collections;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
@@ -1773,8 +1774,8 @@ public abstract class AbstractSurefireMojo
             boolean contains = false;
             for ( Artifact providerArtifact : providerArtifacts )
             {
-                if ( providerArtifact.getGroupId().equals( 
inPluginArtifact.getGroupId() )
-                        && providerArtifact.getArtifactId().equals( 
inPluginArtifact.getArtifactId() ) )
+                if ( hasGroupArtifactId( providerArtifact.getGroupId(), 
providerArtifact.getArtifactId(),
+                        inPluginArtifact ) )
                 {
                     contains = true;
                     break;
@@ -1788,6 +1789,11 @@ public abstract class AbstractSurefireMojo
         return result;
     }
 
+    private static boolean hasGroupArtifactId( String groupId, String 
artifactId, Artifact artifact )
+    {
+        return groupId.equals( artifact.getGroupId() ) && artifactId.equals( 
artifact.getArtifactId() );
+    }
+
     private static Classpath createInProcClasspath( Classpath 
providerClasspath, Set<Artifact> newArtifacts )
     {
         Classpath inprocClasspath = providerClasspath.clone();
@@ -2127,9 +2133,13 @@ public abstract class AbstractSurefireMojo
     private Artifact getJunitPlatformArtifact()
     {
         Artifact artifact = getProjectArtifactMap().get( 
"org.junit.platform:junit-platform-commons" );
+        if ( artifact == null )
+        {
+            artifact = getPluginArtifactMap().get( 
"org.junit.platform:junit-platform-engine" );
+        }
+
         Artifact projectArtifact = project.getArtifact();
         String projectGroupId = projectArtifact.getGroupId();
-
         if ( artifact == null && ( "org.junit.platform".equals( projectGroupId 
)
                 || "org.junit.jupiter".equals( projectGroupId )
                 || "org.junit.vintage".equals( projectGroupId ) ) )
@@ -2910,41 +2920,36 @@ public abstract class AbstractSurefireMojo
             Map<String, Artifact> providerArtifacts =
                     dependencyResolver.getProviderClasspathAsMap( 
"surefire-junit-platform", surefireVersion );
             Map<String, Artifact> testDependencies = 
testClasspath.getTestDependencies();
-            if ( hasDependencyJupiterAPI( testDependencies ) )
+
+            if ( hasDependencyPlatformEngine( testDependencies ) )
             {
-                String engineGroupId = "org.junit.jupiter";
-                String engineArtifactId = "junit-jupiter-engine";
-                String engineCoordinates = engineGroupId + ":" + 
engineArtifactId;
-                if ( testDependencies.containsKey( engineCoordinates ) )
-                {
-                    narrowProviderDependencies( engineCoordinates, 
providerArtifacts, testDependencies );
-                }
-                else
-                {
-                    String api = "org.junit.jupiter:junit-jupiter-api";
-                    logDebugOrCliShowErrors( "Test dependencies contain " + 
api + ". Resolving " + engineCoordinates );
-                    String engineVersion = testDependencies.get( api 
).getBaseVersion();
-                    addEngineByApi( engineGroupId, engineArtifactId, 
engineVersion,
-                            providerArtifacts, testDependencies );
-                }
+                String filterTestDependency = 
"org.junit.platform:junit-platform-engine";
+                logDebugOrCliShowErrors( "Test dependencies contain " + 
filterTestDependency );
+                narrowProviderDependencies( filterTestDependency, 
providerArtifacts, testDependencies );
             }
-            else
+            else if ( hasDependencyPlatformEngine( getPluginArtifactMap() ) )
             {
-                String filterTestDependency = null;
-                if ( hasDependencyPlatformEngine( testDependencies ) )
-                {
-                    filterTestDependency = 
"org.junit.platform:junit-platform-engine";
-                }
-                else if ( hasDependencyPlatformCommons( testDependencies ) )
-                {
-                    filterTestDependency = 
"org.junit.platform:junit-platform-commons";
-                }
-
-                if ( filterTestDependency != null )
+                Map<String, Artifact> engineArtifacts = new HashMap<>( 
getPluginArtifactMap() );
+                for ( Iterator<String> keys = 
engineArtifacts.keySet().iterator(); keys.hasNext(); )
                 {
-                    logDebugOrCliShowErrors( "Test dependencies contain " + 
filterTestDependency );
-                    narrowProviderDependencies( filterTestDependency, 
providerArtifacts, testDependencies );
+                    if ( keys.next().startsWith( "org.apache.maven.surefire:" 
) )
+                    {
+                        keys.remove();
+                    }
                 }
+                providerArtifacts.putAll( engineArtifacts );
+                alignVersions( providerArtifacts, engineArtifacts );
+            }
+            else if ( hasDependencyJupiterAPI( testDependencies ) )
+            {
+                String engineGroupId = "org.junit.jupiter";
+                String engineArtifactId = "junit-jupiter-engine";
+                String engineCoordinates = engineGroupId + ":" + 
engineArtifactId;
+                String api = "org.junit.jupiter:junit-jupiter-api";
+                logDebugOrCliShowErrors( "Test dependencies contain " + api + 
". Resolving " + engineCoordinates );
+                String engineVersion = testDependencies.get( api 
).getBaseVersion();
+                addEngineByApi( engineGroupId, engineArtifactId, engineVersion,
+                        providerArtifacts, testDependencies );
             }
             return new LinkedHashSet<>( providerArtifacts.values() );
         }
@@ -2983,9 +2988,10 @@ public abstract class AbstractSurefireMojo
             alignVersions( providerArtifacts, testDependencies );
         }
 
-        private void alignVersions( Map<String, Artifact> providerArtifacts, 
Map<String, Artifact> testDependencies )
+        private void alignVersions( Map<String, Artifact> providerArtifacts,
+                                    Map<String, Artifact> 
referencedDependencies )
         {
-            String version = testDependencies.get( 
"org.junit.platform:junit-platform-commons" ).getBaseVersion();
+            String version = referencedDependencies.get( 
"org.junit.platform:junit-platform-commons" ).getBaseVersion();
             for ( Artifact launcherArtifact : resolve( PROVIDER_DEP_GID, 
PROVIDER_DEP_AID, version, null, "jar" ) )
             {
                 String key = launcherArtifact.getGroupId() + ":" + 
launcherArtifact.getArtifactId();
@@ -3006,24 +3012,22 @@ public abstract class AbstractSurefireMojo
             return r;
         }
 
-        private boolean hasDependencyJUnit4( Map<String, Artifact> 
providerArtifacts )
-        {
-            return providerArtifacts.containsKey( "junit:junit" );
-        }
-
-        private boolean hasDependencyJupiterAPI( Map<String, Artifact> 
providerArtifacts )
+        private boolean hasDependencyJupiterAPI( Map<String, Artifact> 
dependencies )
         {
-            return providerArtifacts.containsKey( 
"org.junit.jupiter:junit-jupiter-api" );
+            return dependencies.containsKey( 
"org.junit.jupiter:junit-jupiter-api" )
+                    || hasGroupArtifactId( "org.junit.jupiter", 
"junit-jupiter-api", getProject().getArtifact() );
         }
 
-        private boolean hasDependencyPlatformCommons( Map<String, Artifact> 
providerArtifacts )
+        private boolean hasDependencyPlatformCommons( Map<String, Artifact> 
dependencies )
         {
-            return providerArtifacts.containsKey( 
"org.junit.platform:junit-platform-commons" );
+            return dependencies.containsKey( 
"org.junit.platform:junit-platform-commons" )
+                   || hasGroupArtifactId( "org.junit.platform", 
"junit-platform-commons", getProject().getArtifact() );
         }
 
-        private boolean hasDependencyPlatformEngine( Map<String, Artifact> 
providerArtifacts )
+        private boolean hasDependencyPlatformEngine( Map<String, Artifact> 
dependencies )
         {
-            return providerArtifacts.containsKey( 
"org.junit.platform:junit-platform-engine" );
+            return dependencies.containsKey( 
"org.junit.platform:junit-platform-engine" )
+                    || hasGroupArtifactId( "org.junit.platform", 
"junit-platform-engine", getProject().getArtifact() );
         }
     }
 
diff --git 
a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java
 
b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java
index 67a0d98..9d2df22 100644
--- 
a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java
+++ 
b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java
@@ -374,6 +374,11 @@ public class AbstractSurefireMojoTest
     @Test
     public void shouldSmartlyResolveJUnit5ProviderWithJUnit4() throws Exception
     {
+        MavenProject mavenProject = new MavenProject();
+        mavenProject.setArtifact( new DefaultArtifact( "dummy", "pom", 
createFromVersion( "1.0.0" ),
+                null, "jar", null, mock( ArtifactHandler.class ) ) );
+        mojo.setProject( mavenProject );
+
         final VersionRange surefireVersion = createFromVersion( "1" );
 
         Artifact junitPlatformArtifact = new DefaultArtifact( 
"org.apache.maven.surefire",
@@ -505,8 +510,13 @@ public class AbstractSurefireMojoTest
     }
 
     @Test
-    public void shouldSmartlyResolveJUnit5ProviderWithJUnit5Commons() throws 
Exception
+    public void shouldSmartlyResolveJUnit5ProviderWithVintage() throws 
Exception
     {
+        MavenProject mavenProject = new MavenProject();
+        mavenProject.setArtifact( new DefaultArtifact( "dummy", "pom", 
createFromVersion( "1.0.0" ),
+                null, "jar", null, mock( ArtifactHandler.class ) ) );
+        mojo.setProject( mavenProject );
+
         final VersionRange surefireVersion = createFromVersion( "1" );
 
         Artifact junitPlatformArtifact = new DefaultArtifact( 
"org.apache.maven.surefire",
@@ -515,14 +525,30 @@ public class AbstractSurefireMojoTest
         final Artifact testClasspathSomeTestArtifact = new DefaultArtifact( 
"third.party", "artifact",
                 createFromVersion( "1.0" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
 
-        final Artifact testClasspathCommons = new DefaultArtifact( 
"org.junit.platform", "junit-platform-commons",
-                createFromVersion( "1.4.0" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
+        final Artifact testClasspathVintage = new DefaultArtifact( 
"org.junit.vintage", "junit-vintage-engine",
+                createFromVersion( "5.4.0" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
 
         final Artifact testClasspathApiguardian = new DefaultArtifact( 
"org.apiguardian", "apiguardian-api",
                 createFromVersion( "1.0.0" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
 
-        Iterable<Artifact> testArtifacts =
-                asList( testClasspathSomeTestArtifact, 
testClasspathApiguardian, testClasspathCommons );
+        final Artifact testClasspathPlatformEng = new DefaultArtifact( 
"org.junit.platform", "junit-platform-engine",
+                createFromVersion( "1.4.0" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
+
+        Artifact testClasspathJUnit4 = new DefaultArtifact( "junit", "junit",
+                createFromVersion( "4.12" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
+
+        Artifact testClasspathHamcrest = new DefaultArtifact( "org.hamcrest", 
"hamcrest-core",
+                createFromVersion( "1.3" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
+
+        final Artifact testClasspathOpentest4j = new DefaultArtifact( 
"org.opentest4j", "opentest4j",
+                createFromVersion( "1.1.1" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
+
+        final Artifact testClasspathCommons = new DefaultArtifact( 
"org.junit.platform", "junit-platform-commons",
+                createFromVersion( "1.4.0" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
+
+        Iterable<Artifact> testArtifacts = asList( 
testClasspathSomeTestArtifact, testClasspathVintage,
+                testClasspathApiguardian, testClasspathPlatformEng, 
testClasspathJUnit4, testClasspathHamcrest,
+                testClasspathOpentest4j, testClasspathCommons );
 
         File classesDirectory = new File( "target/classes" );
 
@@ -551,7 +577,8 @@ public class AbstractSurefireMojoTest
                 return surefireProvider;
             }
         } );
-
+        final ArtifactResolutionResult surefireProviderResolutionResult = 
mock( ArtifactResolutionResult.class );
+        final ArtifactResolutionResult junit4ResolutionResult = mock( 
ArtifactResolutionResult.class );
         when( repositorySystem.resolve( any( ArtifactResolutionRequest.class ) 
) )
                 .thenAnswer( new Answer<ArtifactResolutionResult>()
                 {
@@ -559,13 +586,16 @@ public class AbstractSurefireMojoTest
                     public ArtifactResolutionResult answer( InvocationOnMock 
invocation )
                     {
                         ArtifactResolutionRequest req = 
(ArtifactResolutionRequest) invocation.getArguments()[0];
-                        if ( req.getArtifact() == surefireProvider )
+                        Artifact artifact = req.getArtifact();
+                        if ( artifact == surefireProvider )
                         {
-                            return createSurefireProviderResolutionResult( 
surefireVersion );
+                            return surefireProviderResolutionResult;
                         }
-                        else if ( req.getArtifact().equals( 
testClasspathCommons )  )
+                        else if ( "org.junit.platform".equals( 
req.getArtifact().getGroupId() )
+                                && "junit-platform-engine".equals( 
req.getArtifact().getArtifactId() )
+                                && "1.4.0".equals( 
req.getArtifact().getVersion() ) )
                         {
-                            return createResolutionResult( 
testClasspathApiguardian, testClasspathCommons );
+                            return createVintageEngineResolutionResult();
                         }
                         else if ( "org.junit.platform".equals( 
req.getArtifact().getGroupId() )
                                 && "junit-platform-launcher".equals( 
req.getArtifact().getArtifactId() )
@@ -581,6 +611,141 @@ public class AbstractSurefireMojoTest
                     }
                 } );
 
+        Artifact java5 = new DefaultArtifact( "org.apache.maven.surefire", 
"common-java5",
+                surefireVersion, null, "jar", null, mock( 
ArtifactHandler.class ) );
+        Artifact launcher = new DefaultArtifact( "org.junit.platform", 
"junit-platform-launcher",
+                createFromVersion( "1.3.2" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
+        Artifact apiguardian = new DefaultArtifact( "org.apiguardian", 
"apiguardian-api",
+                createFromVersion( "1.0.0" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
+        Artifact engine = new DefaultArtifact( "org.junit.platform", 
"junit-platform-engine",
+                createFromVersion( "1.3.2" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
+        Artifact commons = new DefaultArtifact( "org.junit.platform", 
"junit-platform-commons",
+                createFromVersion( "1.3.2" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
+        Artifact opentest4j = new DefaultArtifact( "org.opentest4j", 
"opentest4j",
+                createFromVersion( "1.1.1" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
+        Set<Artifact> providerArtifacts = new HashSet<>();
+        providerArtifacts.add( surefireProvider );
+        providerArtifacts.add( java5 );
+        providerArtifacts.add( launcher );
+        providerArtifacts.add( apiguardian );
+        providerArtifacts.add( engine );
+        providerArtifacts.add( commons );
+        providerArtifacts.add( opentest4j );
+
+        when( surefireProviderResolutionResult.getArtifacts() )
+                .thenReturn( providerArtifacts );
+
+        Artifact junit = new DefaultArtifact( "junit", "junit",
+                createFromVersion( "4.12" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
+        Artifact hamcrest = new DefaultArtifact( "org.hamcrest", 
"hamcrest-core",
+                createFromVersion( "1.3" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
+        Set<Artifact> junitArtifacts = new HashSet<>();
+        junitArtifacts.add( junit );
+        junitArtifacts.add( hamcrest );
+        when( junit4ResolutionResult.getArtifacts() )
+                .thenReturn( junitArtifacts );
+
+        mojo.setRepositorySystem( repositorySystem );
+        mojo.setLogger( mock( Logger.class ) );
+
+        invokeMethod( mojo, "setupStuff" );
+        JUnitPlatformProviderInfo prov =
+                mojo.createJUnitPlatformProviderInfo( junitPlatformArtifact, 
testClasspathWrapper );
+
+        Artifact expectedProvider = new DefaultArtifact( 
"org.apache.maven.surefire", "surefire-junit-platform",
+                surefireVersion, null, "jar", null, mock( 
ArtifactHandler.class ) );
+        Artifact expectedCommonJava5 = new DefaultArtifact( 
"org.apache.maven.surefire", "common-java5",
+                surefireVersion, null, "jar", null, mock( 
ArtifactHandler.class ) );
+        Artifact expectedLauncher = new DefaultArtifact( "org.junit.platform", 
"junit-platform-launcher",
+                createFromVersion( "1.4.0" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
+        assertThat( prov.getProviderClasspath() )
+                .hasSize( 3 )
+                .containsOnly( expectedProvider, expectedCommonJava5, 
expectedLauncher );
+
+        assertThat( testClasspathWrapper.getTestDependencies() )
+                .hasSize( 8 )
+                .includes( entry(  "third.party:artifact", 
testClasspathSomeTestArtifact ),
+                        entry(  "org.junit.vintage:junit-vintage-engine", 
testClasspathVintage ),
+                        entry(  "org.apiguardian:apiguardian-api", 
testClasspathApiguardian ),
+                        entry(  "org.junit.platform:junit-platform-engine", 
testClasspathPlatformEng ),
+                        entry(  "junit:junit", testClasspathJUnit4 ),
+                        entry(  "org.hamcrest:hamcrest-core", 
testClasspathHamcrest ),
+                        entry(  "org.opentest4j:opentest4j", 
testClasspathOpentest4j ),
+                        entry( "org.junit.platform:junit-platform-commons", 
testClasspathCommons ) );
+    }
+
+    @Test
+    public void shouldSmartlyResolveJUnit5ProviderWithJUnit5Commons() throws 
Exception
+    {
+        MavenProject mavenProject = new MavenProject();
+        mavenProject.setArtifact( new DefaultArtifact( "dummy", "pom", 
createFromVersion( "1.0.0" ),
+                null, "jar", null, mock( ArtifactHandler.class ) ) );
+        mojo.setProject( mavenProject );
+
+        final VersionRange surefireVersion = createFromVersion( "1" );
+
+        Artifact junitPlatformArtifact = new DefaultArtifact( 
"org.apache.maven.surefire",
+                "surefire-junit-platform", surefireVersion, null, "jar", null, 
mock( ArtifactHandler.class ) );
+
+        final Artifact testClasspathSomeTestArtifact = new DefaultArtifact( 
"third.party", "artifact",
+                createFromVersion( "1.0" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
+
+        final Artifact testClasspathCommons = new DefaultArtifact( 
"org.junit.platform", "junit-platform-commons",
+                createFromVersion( "1.4.0" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
+
+        final Artifact testClasspathApiguardian = new DefaultArtifact( 
"org.apiguardian", "apiguardian-api",
+                createFromVersion( "1.0.0" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
+
+        Iterable<Artifact> testArtifacts =
+                asList( testClasspathSomeTestArtifact, 
testClasspathApiguardian, testClasspathCommons );
+
+        File classesDirectory = new File( "target/classes" );
+
+        File testClassesDirectory = new File( "target/test-classes" );
+
+        TestClassPath testClasspathWrapper =
+                new TestClassPath( testArtifacts, classesDirectory, 
testClassesDirectory, null );
+
+        Artifact forkedBooter = new DefaultArtifact( 
"org.apache.maven.surefire",
+                "surefire-booter", surefireVersion, null, "jar", null, mock( 
ArtifactHandler.class ) );
+
+        mojo.setPluginArtifactMap( singletonMap( 
"org.apache.maven.surefire:surefire-booter", forkedBooter ) );
+        mojo.setRemoteRepositories( 
Collections.<ArtifactRepository>emptyList() );
+        mojo.setProjectRemoteRepositories( 
Collections.<ArtifactRepository>emptyList() );
+        RepositorySystem repositorySystem = mock( RepositorySystem.class );
+        final Artifact surefireProvider = new DefaultArtifact( 
"org.apache.maven.surefire",
+                "surefire-junit-platform", surefireVersion, null, "jar", null, 
mock( ArtifactHandler.class ) );
+        when( repositorySystem.createDependencyArtifact( any( Dependency.class 
) ) ).thenAnswer( new Answer<Artifact>()
+        {
+            @Override
+            public Artifact answer( InvocationOnMock invocation )
+            {
+                Dependency provider = (Dependency) 
invocation.getArguments()[0];
+                assertThat( provider.getGroupId() ).isEqualTo( 
"org.apache.maven.surefire" );
+                assertThat( provider.getArtifactId() ).isEqualTo( 
"surefire-junit-platform" );
+                return surefireProvider;
+            }
+        } );
+
+        when( repositorySystem.resolve( any( ArtifactResolutionRequest.class ) 
) )
+                .thenAnswer( new Answer<ArtifactResolutionResult>()
+                {
+                    @Override
+                    public ArtifactResolutionResult answer( InvocationOnMock 
invocation )
+                    {
+                        ArtifactResolutionRequest req = 
(ArtifactResolutionRequest) invocation.getArguments()[0];
+                        if ( req.getArtifact() == surefireProvider )
+                        {
+                            return createSurefireProviderResolutionResult( 
surefireVersion );
+                        }
+                        else
+                        {
+                            fail();
+                            return null;
+                        }
+                    }
+                } );
+
         mojo.setRepositorySystem( repositorySystem );
         mojo.setLogger( mock( Logger.class ) );
 
@@ -589,17 +754,23 @@ public class AbstractSurefireMojoTest
                 mojo.createJUnitPlatformProviderInfo( junitPlatformArtifact, 
testClasspathWrapper );
         Set<Artifact> resolvedProviderArtifacts = prov.getProviderClasspath();
 
+        Artifact provider = new DefaultArtifact( "org.apache.maven.surefire", 
"surefire-junit-platform",
+                surefireVersion, null, "jar", null, mock( 
ArtifactHandler.class ) );
         Artifact java5 = new DefaultArtifact( "org.apache.maven.surefire", 
"common-java5",
                 surefireVersion, null, "jar", null, mock( 
ArtifactHandler.class ) );
         Artifact launcher = new DefaultArtifact( "org.junit.platform", 
"junit-platform-launcher",
-                createFromVersion( "1.4.0" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
-        Artifact testClasspathJUnit5 = new DefaultArtifact( 
"org.junit.platform", "junit-platform-engine",
-                createFromVersion( "1.4.0" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
-        Artifact testClasspathOpentest4j = new DefaultArtifact( 
"org.opentest4j", "opentest4j",
+                createFromVersion( "1.3.2" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
+        Artifact apiguardian = new DefaultArtifact( "org.apiguardian", 
"apiguardian-api",
+                createFromVersion( "1.0.0" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
+        Artifact engine = new DefaultArtifact( "org.junit.platform", 
"junit-platform-engine",
+                createFromVersion( "1.3.2" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
+        Artifact commons = new DefaultArtifact( "org.junit.platform", 
"junit-platform-commons",
+                createFromVersion( "1.3.2" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
+        Artifact opentest4j = new DefaultArtifact( "org.opentest4j", 
"opentest4j",
                 createFromVersion( "1.1.1" ), null, "jar", null, mock( 
ArtifactHandler.class ) );
         assertThat( resolvedProviderArtifacts )
-                .hasSize( 5 )
-                .containsOnly( surefireProvider, java5, launcher, 
testClasspathJUnit5, testClasspathOpentest4j );
+                .hasSize( 7 )
+                .containsOnly( provider, java5, launcher, apiguardian, engine, 
commons, opentest4j );
 
         assertThat( testClasspathWrapper.getTestDependencies() )
                 .hasSize( 3 )
@@ -611,6 +782,11 @@ public class AbstractSurefireMojoTest
     @Test
     public void shouldSmartlyResolveJUnit5ProviderWithJUnit5Engine() throws 
Exception
     {
+        MavenProject mavenProject = new MavenProject();
+        mavenProject.setArtifact( new DefaultArtifact( "dummy", "pom", 
createFromVersion( "1.0.0" ),
+                null, "jar", null, mock( ArtifactHandler.class ) ) );
+        mojo.setProject( mavenProject );
+
         final VersionRange surefireVersion = createFromVersion( "1" );
 
         Artifact junitPlatformArtifact = new DefaultArtifact( 
"org.apache.maven.surefire",
@@ -720,6 +896,11 @@ public class AbstractSurefireMojoTest
     @Test
     public void shouldSmartlyResolveJUnit5ProviderWithJupiterApi() throws 
Exception
     {
+        MavenProject mavenProject = new MavenProject();
+        mavenProject.setArtifact( new DefaultArtifact( "dummy", "pom", 
createFromVersion( "1.0.0" ),
+                null, "jar", null, mock( ArtifactHandler.class ) ) );
+        mojo.setProject( mavenProject );
+
         final VersionRange surefireVersion = createFromVersion( "1" );
 
         Artifact junitPlatformArtifact = new DefaultArtifact( 
"org.apache.maven.surefire",
@@ -840,6 +1021,11 @@ public class AbstractSurefireMojoTest
     @Test
     public void shouldSmartlyResolveJUnit5ProviderWithJupiterEngine() throws 
Exception
     {
+        MavenProject mavenProject = new MavenProject();
+        mavenProject.setArtifact( new DefaultArtifact( "dummy", "pom", 
createFromVersion( "1.0.0" ),
+                null, "jar", null, mock( ArtifactHandler.class ) ) );
+        mojo.setProject( mavenProject );
+
         final VersionRange surefireVersion = createFromVersion( "1" );
 
         Artifact junitPlatformArtifact = new DefaultArtifact( 
"org.apache.maven.surefire",
@@ -921,11 +1107,11 @@ public class AbstractSurefireMojoTest
                         {
                             return 
createExpectedJUnitPlatformLauncherResolutionResult();
                         }
-                        else if ( "org.junit.jupiter".equals( 
resolvable.getGroupId() )
-                                && "junit-jupiter-engine".equals( 
resolvable.getArtifactId() )
-                                && "5.4.0".equals( resolvable.getVersion() ) )
+                        else if ( "org.junit.platform".equals( 
req.getArtifact().getGroupId() )
+                                && "junit-platform-engine".equals( 
req.getArtifact().getArtifactId() )
+                                && "1.4.0".equals( 
req.getArtifact().getVersion() ) )
                         {
-                            return createJupiterEngineResolutionResult();
+                            return createVintageEngineResolutionResult();
                         }
                         else
                         {
@@ -980,6 +1166,31 @@ public class AbstractSurefireMojoTest
         return launcherResolutionResult;
     }
 
+    private static ArtifactResolutionResult 
createVintageEngineResolutionResult()
+    {
+        ArtifactResolutionResult launcherResolutionResult = mock( 
ArtifactResolutionResult.class );
+        Set<Artifact> resolvedLauncherArtifacts = new HashSet<>();
+        resolvedLauncherArtifacts.add( new DefaultArtifact( 
"org.junit.vintage", "junit-vintage-engine",
+                createFromVersion( "5.4.0" ), null, "jar", null, mock( 
ArtifactHandler.class ) ) );
+        resolvedLauncherArtifacts.add( new DefaultArtifact( 
"org.junit.jupiter", "junit-jupiter-api",
+                createFromVersion( "5.4.0" ), null, "jar", null, mock( 
ArtifactHandler.class ) ) );
+        resolvedLauncherArtifacts.add( new DefaultArtifact( "junit", "junit",
+                createFromVersion( "4.12" ), null, "jar", null, mock( 
ArtifactHandler.class ) ) );
+        resolvedLauncherArtifacts.add( new DefaultArtifact( "org.hamcrest", 
"hamcrest-core",
+                createFromVersion( "1.3" ), null, "jar", null, mock( 
ArtifactHandler.class ) ) );
+        resolvedLauncherArtifacts.add( new DefaultArtifact( 
"org.junit.platform", "junit-platform-engine",
+                createFromVersion( "1.4.0" ), null, "jar", null, mock( 
ArtifactHandler.class ) ) );
+        resolvedLauncherArtifacts.add( new DefaultArtifact( "org.apiguardian", 
"apiguardian-api",
+                createFromVersion( "1.0.0" ), null, "jar", null, mock( 
ArtifactHandler.class ) ) );
+        resolvedLauncherArtifacts.add( new DefaultArtifact( "org.opentest4j", 
"opentest4j",
+                createFromVersion( "1.1.1" ), null, "jar", null, mock( 
ArtifactHandler.class ) ) );
+        resolvedLauncherArtifacts.add( new DefaultArtifact( 
"org.junit.platform", "junit-platform-commons",
+                createFromVersion( "1.4.0" ), null, "jar", null, mock( 
ArtifactHandler.class ) ) );
+        when( launcherResolutionResult.getArtifacts() )
+                .thenReturn( resolvedLauncherArtifacts );
+        return launcherResolutionResult;
+    }
+
     private static ArtifactResolutionResult 
createJupiterEngineResolutionResult()
     {
         ArtifactResolutionResult launcherResolutionResult = mock( 
ArtifactResolutionResult.class );
diff --git a/maven-surefire-plugin/src/site/apt/examples/junit-platform.apt.vm 
b/maven-surefire-plugin/src/site/apt/examples/junit-platform.apt.vm
index 4258387..497043c 100644
--- a/maven-surefire-plugin/src/site/apt/examples/junit-platform.apt.vm
+++ b/maven-surefire-plugin/src/site/apt/examples/junit-platform.apt.vm
@@ -40,7 +40,7 @@ Using JUnit 5 Platform
     <dependency>
         <groupId>org.junit.jupiter</groupId>
         <artifactId>junit-jupiter-engine</artifactId>
-        <version>5.3.2</version>
+        <version>5.4.0</version>
         <scope>test</scope>
     </dependency>
     [...]
@@ -63,7 +63,7 @@ Using JUnit 5 Platform
     <dependency>
         <groupId>org.junit.vintage</groupId>
         <artifactId>junit-vintage-engine</artifactId>
-        <version>5.3.2</version>
+        <version>5.4.0</version>
         <scope>test</scope>
     </dependency>
     [...]
@@ -74,6 +74,83 @@ Using JUnit 5 Platform
   see the {{{https://junit.org/junit5/}JUnit 5 web site}}
   and the {{{https://junit.org/junit5/docs/current/user-guide/}JUnit 5 User 
Guide}}.
 
+* Smart Resolution of Jupiter Engine and Vintage Engine for JUnit4 (since 
plugin version 3.0.0-M4)
+
+  Normally the developer does not want to access internal classes of JUnit5 
engine (e.g. <<<5.4.0>>>). In this example
+  the POM has only Jupiter API dependency in test classpath. The engine 
artifact appears in plugin dependencies.
+
+  Since the version 3.0.0-M4 you can specify only JUnit5 API artifact and your 
test sources become isolated from engine.
+  The engine is resolved by the plugin and downloaded from a remote repository.
+
++---+
+<dependencies>
+    [...]
+    <dependency>
+        <groupId>org.junit.jupiter</groupId>
+        <artifactId>junit-jupiter-api</artifactId>
+        <version>5.4.0</version>
+        <scope>test</scope>
+    </dependency>
+    [...]
+</dependencies>
+...
+<build>
+    <plugins>
+        ...
+        <plugin>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>${project.artifactId}</artifactId>
+            <version>${project.version}</version>
+            <dependencies>
+                <dependency>
+                    <groupId>org.junit.jupiter</groupId>
+                    <artifactId>junit-jupiter-engine</artifactId>
+                    <version>5.4.0</version>
+                    <scope>test</scope>
+                </dependency>
+            </dependencies>
+        </plugin>
+    </plugins>
+</build>
+...
++---+
+
+
+  Similar with JUnit4 in test dependencies of your project POM. The Vintage 
engine artifact is in plugin dependencies.
+
++---+
+<dependencies>
+    [...]
+    <dependency>
+        <groupId>junit</groupId>
+        <artifactId>junit</artifactId>
+        <version>4.12</version>
+        <scope>test</scope>
+    </dependency>
+    [...]
+</dependencies>
+...
+<build>
+    <plugins>
+        ...
+        <plugin>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>${project.artifactId}</artifactId>
+            <version>${project.version}</version>
+            <dependencies>
+                <dependency>
+                    <groupId>org.junit.vintage</groupId>
+                    <artifactId>junit-vintage-engine</artifactId>
+                    <version>5.4.0</version>
+                    <scope>test</scope>
+                </dependency>
+            </dependencies>
+        </plugin>
+    </plugins>
+</build>
+...
++---+
+
 * Provider Selection
 
    If nothing is configured, Surefire detects which JUnit version to use by 
the following algorithm:
diff --git 
a/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1585IT.java
 
b/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1585IT.java
index 4b852b7..12e175f 100644
--- 
a/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1585IT.java
+++ 
b/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1585IT.java
@@ -37,7 +37,16 @@ public class Surefire1585IT
     @Test
     public void shouldRunWithJupiterApi()
     {
-        unpack( "surefire-1585" )
+        unpack( "surefire-1585-jupiter-api" )
+                .debugLogging()
+                .executeTest()
+                .verifyErrorFree( 1 );
+    }
+
+    @Test
+    public void shouldRunWithVintage()
+    {
+        unpack( "surefire-1585-junit4-vintage" )
                 .debugLogging()
                 .executeTest()
                 .verifyErrorFree( 1 );
diff --git a/surefire-its/src/test/resources/surefire-1585/pom.xml 
b/surefire-its/src/test/resources/surefire-1585-junit4-vintage/pom.xml
similarity index 75%
copy from surefire-its/src/test/resources/surefire-1585/pom.xml
copy to surefire-its/src/test/resources/surefire-1585-junit4-vintage/pom.xml
index c7975b6..7bdf148 100644
--- a/surefire-its/src/test/resources/surefire-1585/pom.xml
+++ b/surefire-its/src/test/resources/surefire-1585-junit4-vintage/pom.xml
@@ -24,20 +24,20 @@
     <modelVersion>4.0.0</modelVersion>
 
     <groupId>org.apache.maven.plugins.surefire</groupId>
-    <artifactId>junit-jupiter-api-it</artifactId>
+    <artifactId>surefire-1585-junit4-vintage-it</artifactId>
     <version>1.0</version>
 
     <properties>
         <maven.compiler.source>1.8</maven.compiler.source>
         <maven.compiler.target>1.8</maven.compiler.target>
-        <junit.jupiter.version>5.4.0</junit.jupiter.version>
+        <junit.vintage.version>5.4.0</junit.vintage.version>
     </properties>
 
     <dependencies>
         <dependency>
-            <groupId>org.junit.jupiter</groupId>
-            <artifactId>junit-jupiter-api</artifactId>
-            <version>${junit.jupiter.version}</version>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.12</version>
             <scope>test</scope>
         </dependency>
     </dependencies>
@@ -48,6 +48,13 @@
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-surefire-plugin</artifactId>
                 <version>${surefire.version}</version>
+                <dependencies>
+                    <dependency>
+                        <groupId>org.junit.vintage</groupId>
+                        <artifactId>junit-vintage-engine</artifactId>
+                        <version>${junit.vintage.version}</version>
+                    </dependency>
+                </dependencies>
             </plugin>
         </plugins>
     </build>
diff --git 
a/surefire-its/src/test/resources/surefire-1585/src/test/java/JupiterTest.java 
b/surefire-its/src/test/resources/surefire-1585-junit4-vintage/src/test/java/JUnit4Test.java
similarity index 72%
copy from 
surefire-its/src/test/resources/surefire-1585/src/test/java/JupiterTest.java
copy to 
surefire-its/src/test/resources/surefire-1585-junit4-vintage/src/test/java/JUnit4Test.java
index 26dbeca..d0c1ff4 100644
--- 
a/surefire-its/src/test/resources/surefire-1585/src/test/java/JupiterTest.java
+++ 
b/surefire-its/src/test/resources/surefire-1585-junit4-vintage/src/test/java/JUnit4Test.java
@@ -1,5 +1,3 @@
-package junitplatformenginejupiter;
-
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -19,16 +17,12 @@ package junitplatformenginejupiter;
  * under the License.
  */
 
-import static org.junit.jupiter.api.Assertions.assertEquals;
-
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.TestInfo;
+import org.junit.Test;
 
-class JupiterTest
+public class JUnit4Test
 {
     @Test
-    void test( TestInfo info )
+    public void test()
     {
-        assertEquals( "test(TestInfo)", info.getDisplayName(), "display name 
mismatch" );
     }
 }
diff --git a/surefire-its/src/test/resources/surefire-1585/pom.xml 
b/surefire-its/src/test/resources/surefire-1585-jupiter-api/pom.xml
similarity index 97%
rename from surefire-its/src/test/resources/surefire-1585/pom.xml
rename to surefire-its/src/test/resources/surefire-1585-jupiter-api/pom.xml
index c7975b6..652ae49 100644
--- a/surefire-its/src/test/resources/surefire-1585/pom.xml
+++ b/surefire-its/src/test/resources/surefire-1585-jupiter-api/pom.xml
@@ -24,7 +24,7 @@
     <modelVersion>4.0.0</modelVersion>
 
     <groupId>org.apache.maven.plugins.surefire</groupId>
-    <artifactId>junit-jupiter-api-it</artifactId>
+    <artifactId>surefire-1585-jupiter-api-it</artifactId>
     <version>1.0</version>
 
     <properties>
diff --git 
a/surefire-its/src/test/resources/surefire-1585/src/test/java/JupiterTest.java 
b/surefire-its/src/test/resources/surefire-1585-jupiter-api/src/test/java/JupiterTest.java
similarity index 96%
rename from 
surefire-its/src/test/resources/surefire-1585/src/test/java/JupiterTest.java
rename to 
surefire-its/src/test/resources/surefire-1585-jupiter-api/src/test/java/JupiterTest.java
index 26dbeca..398141c 100644
--- 
a/surefire-its/src/test/resources/surefire-1585/src/test/java/JupiterTest.java
+++ 
b/surefire-its/src/test/resources/surefire-1585-jupiter-api/src/test/java/JupiterTest.java
@@ -1,5 +1,3 @@
-package junitplatformenginejupiter;
-
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file

Reply via email to