[SUREFIRE-1620] Replaced deprecated component ArtifactFactory with RepositorySystem
authorTibor17 <tibordigana@apache.org>
Fri, 4 Jan 2019 00:14:12 +0000 (01:14 +0100)
committerTibor17 <tibordigana@apache.org>
Fri, 4 Jan 2019 00:14:12 +0000 (01:14 +0100)
maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireDependencyResolver.java
maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/SurefireDependencyResolverTest.java

index 677e3ee..c314fb4 100644 (file)
@@ -21,7 +21,6 @@ package org.apache.maven.plugin.surefire;
  */
 
 import org.apache.maven.artifact.Artifact;
-import org.apache.maven.artifact.factory.ArtifactFactory;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.plugins.annotations.Component;
 import org.apache.maven.repository.RepositorySystem;
@@ -657,12 +656,6 @@ public abstract class AbstractSurefireMojo
     private boolean trimStackTrace;
 
     /**
-     * Creates the artifact.
-     */
-    @Component
-    private ArtifactFactory artifactFactory;
-
-    /**
      * The remote plugin repositories declared in the POM.
      *
      * @since 2.2
@@ -1020,7 +1013,7 @@ public abstract class AbstractSurefireMojo
 
     private void createDependencyResolver()
     {
-        dependencyResolver = new SurefireDependencyResolver( getRepositorySystem(), getArtifactFactory(),
+        dependencyResolver = new SurefireDependencyResolver( getRepositorySystem(),
                                                                    getConsoleLogger(), getLocalRepository(),
                                                                    getRemoteRepositories(),
                                                                    getPluginName() );
@@ -3424,17 +3417,6 @@ public abstract class AbstractSurefireMojo
         this.trimStackTrace = trimStackTrace;
     }
 
-    public ArtifactFactory getArtifactFactory()
-    {
-        return artifactFactory;
-    }
-
-    @SuppressWarnings( "UnusedDeclaration" )
-    public void setArtifactFactory( ArtifactFactory artifactFactory )
-    {
-        this.artifactFactory = artifactFactory;
-    }
-
     public List<ArtifactRepository> getRemoteRepositories()
     {
         return remoteRepositories;
index e403433..b255f38 100644 (file)
@@ -26,7 +26,6 @@ import java.util.Map;
 import java.util.Set;
 
 import org.apache.maven.artifact.Artifact;
-import org.apache.maven.artifact.factory.ArtifactFactory;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
 import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
@@ -35,6 +34,7 @@ import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
 import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
 import org.apache.maven.artifact.versioning.VersionRange;
+import org.apache.maven.model.Dependency;
 import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
 import org.apache.maven.repository.RepositorySystem;
 
@@ -43,7 +43,6 @@ import javax.annotation.Nullable;
 
 import static java.util.Collections.singletonList;
 import static org.apache.maven.artifact.Artifact.SCOPE_TEST;
-import static org.apache.maven.artifact.versioning.VersionRange.createFromVersion;
 import static org.apache.maven.artifact.versioning.VersionRange.createFromVersionSpec;
 
 /**
@@ -73,8 +72,6 @@ final class SurefireDependencyResolver
 
     private final RepositorySystem repositorySystem;
 
-    private final ArtifactFactory artifactFactory;
-
     private final ConsoleLogger log;
 
     private final ArtifactRepository localRepository;
@@ -83,12 +80,11 @@ final class SurefireDependencyResolver
 
     private final String pluginName;
 
-    SurefireDependencyResolver( RepositorySystem repositorySystem, ArtifactFactory artifactFactory, ConsoleLogger log,
-                                          ArtifactRepository localRepository,
-                                          List<ArtifactRepository> remoteRepositories, String pluginName )
+    SurefireDependencyResolver( RepositorySystem repositorySystem, ConsoleLogger log,
+                                ArtifactRepository localRepository,
+                                List<ArtifactRepository> remoteRepositories, String pluginName )
     {
         this.repositorySystem = repositorySystem;
-        this.artifactFactory = artifactFactory;
         this.log = log;
         this.localRepository = localRepository;
         this.remoteRepositories = remoteRepositories;
@@ -142,8 +138,9 @@ final class SurefireDependencyResolver
     @Nonnull
     Set<Artifact> getProviderClasspath( String providerArtifactId, String providerVersion )
     {
-        Artifact providerArtifact = artifactFactory.createDependencyArtifact( PROVIDER_GROUP_ID,
-                providerArtifactId, createFromVersion( providerVersion ), "jar", null, SCOPE_TEST );
+        Dependency provider = toProviderDependency( providerArtifactId, providerVersion );
+
+        Artifact providerArtifact = repositorySystem.createDependencyArtifact( provider );
 
         ArtifactResolutionResult result = resolveArtifact( providerArtifact );
 
@@ -214,4 +211,15 @@ final class SurefireDependencyResolver
         orderedProviderArtifacts.addAll( providerArtifacts );
         return orderedProviderArtifacts;
     }
+
+    private static Dependency toProviderDependency( String providerArtifactId, String providerVersion )
+    {
+        Dependency dependency = new Dependency();
+        dependency.setGroupId( PROVIDER_GROUP_ID );
+        dependency.setArtifactId( providerArtifactId );
+        dependency.setVersion( providerVersion );
+        dependency.setType( "jar" );
+        dependency.setScope( SCOPE_TEST );
+        return dependency;
+    }
 }
index cd874f3..ab57aef 100644 (file)
@@ -26,6 +26,7 @@ import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
 import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
 import org.apache.maven.artifact.versioning.VersionRange;
+import org.apache.maven.model.Dependency;
 import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
 import org.apache.maven.repository.RepositorySystem;
 import org.junit.Rule;
@@ -144,7 +145,7 @@ public class SurefireDependencyResolverTest
                 } );
 
         SurefireDependencyResolver surefireDependencyResolver =
-                new SurefireDependencyResolver( repositorySystem, null, null, null, null, null );
+                new SurefireDependencyResolver( repositorySystem, null, null, null, null );
 
         ArtifactResolutionResult actualResult = surefireDependencyResolver.resolveArtifact( provider );
 
@@ -173,22 +174,14 @@ public class SurefireDependencyResolverTest
         providerArtifacts.add( ext );
         providerArtifacts.add( logger );
 
-        ArtifactFactory artifactFactory = mock( ArtifactFactory.class );
-        VersionRange providerVersion = createFromVersion( "5.3.1" );
-        when( artifactFactory.createDependencyArtifact( eq( "org.apache.maven.surefire" ),
-                eq( "surefire-junit-platform" ),
-                eq( providerVersion ),
-                eq( "jar" ),
-                isNull( String.class ),
-                eq( "test" ) ) )
-                .thenReturn( provider );
+        final String providerVersion = "5.3.1";
 
         final ArtifactResolutionResult result = mock( ArtifactResolutionResult.class );
         when( result.getArtifacts() )
                 .thenReturn( providerArtifacts );
 
         RepositorySystem repositorySystem = mock( RepositorySystem.class );
-        when( repositorySystem.resolve( any(  ArtifactResolutionRequest.class) ) )
+        when( repositorySystem.resolve( any( ArtifactResolutionRequest.class ) ) )
                 .then( new Answer<ArtifactResolutionResult>() {
                     @Override
                     public ArtifactResolutionResult answer( InvocationOnMock invocation )
@@ -224,11 +217,33 @@ public class SurefireDependencyResolverTest
                         return result;
                     }
                 } );
+        when( repositorySystem.createDependencyArtifact( any( Dependency.class ) ) )
+                .then( new Answer<Artifact>() {
+                    @Override
+                    public Artifact answer( InvocationOnMock invocation )
+                    {
+                        Object[] args = invocation.getArguments();
+                        assertThat( args )
+                                .hasSize( 1 );
+                        Dependency request = (Dependency) args[0];
+                        assertThat( request.getGroupId() )
+                                .isEqualTo( "org.apache.maven.surefire" );
+                        assertThat( request.getArtifactId() )
+                                .isEqualTo( "surefire-junit-platform" );
+                        assertThat( request.getVersion() )
+                                .isEqualTo( providerVersion );
+                        assertThat( request.getType() )
+                                .isEqualTo( "jar" );
+                        assertThat( request.getScope() )
+                                .isEqualTo( "test" );
+                        return provider;
+                    }
+                } );
 
         ConsoleLogger log = mock( ConsoleLogger.class );
 
         SurefireDependencyResolver surefireDependencyResolver =
-                new SurefireDependencyResolver( repositorySystem, artifactFactory, log, null, null, null );
+                new SurefireDependencyResolver( repositorySystem, log, null, null, null );
 
         when( log.isDebugEnabled() )
                 .thenReturn( true );
@@ -334,7 +349,7 @@ public class SurefireDependencyResolverTest
                 } );
 
         SurefireDependencyResolver surefireDependencyResolver =
-                new SurefireDependencyResolver( repositorySystem, null, null, null, null, null );
+                new SurefireDependencyResolver( repositorySystem, null, null, null, null );
 
         Map<String, Artifact> pluginArtifactsMapping = new HashMap<>();
         pluginArtifactsMapping.put( plugin.getGroupId() + ":" + plugin.getArtifactId(), plugin );