This is an automated email from the ASF dual-hosted git repository. tibordigana pushed a commit to branch 1564 in repository https://gitbox.apache.org/repos/asf/maven-surefire.git
The following commit(s) were added to refs/heads/1564 by this push: new 6a9495a refactoring done, added avoidArtifactDuplicates() 6a9495a is described below commit 6a9495af2bba7e6b024d69e1207cbc0bb50d068f Author: Tibor17 <tibordig...@apache.org> AuthorDate: Tue Sep 25 23:45:53 2018 +0200 refactoring done, added avoidArtifactDuplicates() --- .../plugin/surefire/AbstractSurefireMojo.java | 110 +++++++++++---------- .../maven/plugin/surefire/ClasspathCache.java | 17 ++++ .../apache/maven/plugin/surefire/ProviderInfo.java | 3 - .../surefire/SurefireDependencyResolver.java | 64 ++++++------ .../maven/plugin/surefire/TestClassPath.java | 6 ++ 5 files changed, 110 insertions(+), 90 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 25f3443..c407ffe 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 @@ -1712,62 +1712,59 @@ public abstract class AbstractSurefireMojo private StartupConfiguration createStartupConfiguration( @Nonnull ProviderInfo provider, boolean isInprocess, @Nonnull ClassLoaderConfiguration classLoaderConfiguration, @Nonnull DefaultScanResult scanResult ) - throws MojoExecutionException, MojoFailureException + throws MojoExecutionException { try { - // cache the provider lookup - String providerName = provider.getProviderName(); - Classpath providerClasspath = ClasspathCache.getCachedClassPath( providerName ); - if ( providerClasspath == null ) - { - providerClasspath = provider.getProviderClasspath(); - ClasspathCache.setCachedClasspath( providerName, providerClasspath ); - } - File moduleDescriptor = getModuleDescriptor(); - + Set<Artifact> providerArtifacts = provider.getProviderClasspath(); + String providerName = provider.getProviderName(); if ( moduleDescriptor.exists() && !isInprocess ) { - return newStartupConfigForModularClasspath( classLoaderConfiguration, providerClasspath, providerName, + return newStartupConfigWithModularPath( classLoaderConfiguration, providerArtifacts, providerName, moduleDescriptor, scanResult ); } else { - Artifact surefireCommonArtifact = getCommonArtifact(); - Classpath inprocClassPath = - providerClasspath.addClassPathElementUrl( surefireCommonArtifact.getFile().getAbsolutePath() ) - .addClassPathElementUrl( getApiArtifact().getFile().getAbsolutePath() ); - return newStartupConfigForNonModularClasspath( classLoaderConfiguration, providerClasspath, - inprocClassPath, providerName ); + return newStartupConfigWithClasspath( classLoaderConfiguration, providerArtifacts, providerName ); } } catch ( AbstractArtifactResolutionException e ) { throw new MojoExecutionException( "Unable to generate classpath: " + e, e ); } - catch ( InvalidVersionSpecificationException e ) - { - throw new MojoExecutionException( "Unable to generate classpath: " + e, e ); - } catch ( IOException e ) { throw new MojoExecutionException( e.getMessage(), e ); } } - private StartupConfiguration newStartupConfigForNonModularClasspath( - @Nonnull ClassLoaderConfiguration classLoaderConfiguration, @Nonnull Set<Artifact> providerClasspath, - @Nonnull Classpath inprocClasspath, @Nonnull String providerName ) + private StartupConfiguration newStartupConfigWithClasspath( + @Nonnull ClassLoaderConfiguration classLoaderConfiguration, @Nonnull Set<Artifact> providerArtifacts, + @Nonnull String providerName ) { TestClassPath testClasspathWrapper = generateTestClasspath(); Classpath testClasspath = testClasspathWrapper.toClasspath(); + testClasspathWrapper.avoidArtifactDuplicates( providerArtifacts ); + + Classpath providerClasspath = ClasspathCache.getCachedClassPath( providerName ); + if ( providerClasspath == null ) + { + providerClasspath = ClasspathCache.setCachedClasspath( providerName, providerArtifacts ); + } + getConsoleLogger().debug( testClasspath.getLogMessage( "test classpath:" ) ); getConsoleLogger().debug( providerClasspath.getLogMessage( "provider classpath:" ) ); getConsoleLogger().debug( testClasspath.getCompactLogMessage( "test(compact) classpath:" ) ); getConsoleLogger().debug( providerClasspath.getCompactLogMessage( "provider(compact) classpath:" ) ); + String surefireCommonArtifactPath = getCommonArtifact().getFile().getAbsolutePath(); // todo: why added? + String surefireApiArtifactPath = getApiArtifact().getFile().getAbsolutePath(); + // todo: surefireApiArtifactPath is always in every provider - why added here? + Classpath inprocClasspath = providerClasspath.addClassPathElementUrl( surefireCommonArtifactPath ) + .addClassPathElementUrl( surefireApiArtifactPath ); + ClasspathConfiguration classpathConfiguration = new ClasspathConfiguration( testClasspath, providerClasspath, inprocClasspath, effectiveIsEnableAssertions(), isChildDelegation() ); @@ -1780,17 +1777,28 @@ public abstract class AbstractSurefireMojo return new LocationManager(); } - private StartupConfiguration newStartupConfigForModularClasspath( - @Nonnull ClassLoaderConfiguration classLoaderConfiguration, @Nonnull Set<Artifact> providerClasspath, + private StartupConfiguration newStartupConfigWithModularPath( + @Nonnull ClassLoaderConfiguration classLoaderConfiguration, @Nonnull Set<Artifact> providerArtifacts, @Nonnull String providerName, @Nonnull File moduleDescriptor, @Nonnull DefaultScanResult scanResult ) - throws AbstractArtifactResolutionException, IOException + throws IOException { - ResolvePathsRequest<String> req = ResolvePathsRequest.withStrings( generateTestClasspath().getClassPath() ) + TestClassPath testClasspathWrapper = generateTestClasspath(); + Classpath testClasspath = testClasspathWrapper.toClasspath(); + + testClasspathWrapper.avoidArtifactDuplicates( providerArtifacts ); + + Classpath providerClasspath = ClasspathCache.getCachedClassPath( providerName ); + if ( providerClasspath == null ) + { + providerClasspath = ClasspathCache.setCachedClasspath( providerName, providerArtifacts ); + } + + ResolvePathsRequest<String> req = ResolvePathsRequest.withStrings( testClasspath.getClassPath() ) .setMainModuleDescriptor( moduleDescriptor.getAbsolutePath() ); ResolvePathsResult<String> result = ( (LocationManager) getLocationManager() ).resolvePaths( req ); - Classpath testClasspath = new Classpath( result.getClasspathElements() ); + testClasspath = new Classpath( result.getClasspathElements() ); Classpath testModulepath = new Classpath( result.getModulepathElements().keySet() ); SortedSet<String> packages = new TreeSet<String>(); @@ -2091,13 +2099,13 @@ public abstract class AbstractSurefireMojo } private InPluginVMSurefireStarter createInprocessStarter( @Nonnull ProviderInfo provider, - @Nonnull ClassLoaderConfiguration classLoaderConfiguration, + @Nonnull ClassLoaderConfiguration classLoaderConfig, @Nonnull RunOrderParameters runOrderParameters, @Nonnull DefaultScanResult scanResult ) throws MojoExecutionException, MojoFailureException { StartupConfiguration startupConfiguration = - createStartupConfiguration( provider, true, classLoaderConfiguration, scanResult ); + createStartupConfiguration( provider, true, classLoaderConfig, scanResult ); String configChecksum = getConfigChecksum(); StartupReportConfiguration startupReportConfiguration = getStartupReportConfiguration( configChecksum, false ); ProviderConfiguration providerConfiguration = createProviderConfiguration( runOrderParameters ); @@ -2443,7 +2451,8 @@ public abstract class AbstractSurefireMojo classpathArtifacts = filterArtifacts( classpathArtifacts, dependencyFilter ); } - return new TestClassPath( classpathArtifacts, getClassesDirectory(), getTestClassesDirectory(), getAdditionalClasspathElements() ); + return new TestClassPath( classpathArtifacts, getClassesDirectory(), + getTestClassesDirectory(), getAdditionalClasspathElements() ); // adding TestNG MethodSelector to the classpath // Todo: move @@ -2797,12 +2806,12 @@ public abstract class AbstractSurefireMojo @Override @Nonnull - public Classpath getProviderClasspath() + public Set<Artifact> getProviderClasspath() throws ArtifactResolutionException, ArtifactNotFoundException { Artifact surefireArtifact = getPluginArtifactMap().get( "org.apache.maven.surefire:surefire-booter" ); - return dependencyResolver.getProviderClasspath( "surefire-testng", surefireArtifact.getBaseVersion(), - testNgArtifact ); + String version = surefireArtifact.getBaseVersion(); + return dependencyResolver.getProviderClasspath( "surefire-testng", version, testNgArtifact ); } } @@ -2828,14 +2837,13 @@ public abstract class AbstractSurefireMojo @Override @Nonnull - public Classpath getProviderClasspath() + public Set<Artifact> getProviderClasspath() throws ArtifactResolutionException, ArtifactNotFoundException { // add the JUnit provider as default - it doesn't require JUnit to be present, // since it supports POJO tests. - return dependencyResolver.getProviderClasspath( "surefire-junit3", surefireBooterArtifact.getBaseVersion(), - null ); - + String version = surefireBooterArtifact.getBaseVersion(); + return dependencyResolver.getProviderClasspath( "surefire-junit3", version, null ); } } @@ -2871,13 +2879,12 @@ public abstract class AbstractSurefireMojo @Override @Nonnull - public Classpath getProviderClasspath() + public Set<Artifact> getProviderClasspath() throws ArtifactResolutionException, ArtifactNotFoundException { - return dependencyResolver.getProviderClasspath( "surefire-junit4", surefireBooterArtifact.getBaseVersion(), - null ); + String version = surefireBooterArtifact.getBaseVersion(); + return dependencyResolver.getProviderClasspath( "surefire-junit4", version, null ); } - } final class JUnitPlatformProviderInfo @@ -2911,12 +2918,11 @@ public abstract class AbstractSurefireMojo @Override @Nonnull - public Classpath getProviderClasspath() + public Set<Artifact> getProviderClasspath() throws ArtifactResolutionException, ArtifactNotFoundException { - return dependencyResolver.getProviderClasspath( "surefire-junit-platform", - surefireBooterArtifact.getBaseVersion(), - null ); + String version = surefireBooterArtifact.getBaseVersion(); + return dependencyResolver.getProviderClasspath( "surefire-junit-platform", version, null ); } } @@ -2961,11 +2967,11 @@ public abstract class AbstractSurefireMojo @Override @Nonnull - public Classpath getProviderClasspath() + public Set<Artifact> getProviderClasspath() throws ArtifactResolutionException, ArtifactNotFoundException { - return dependencyResolver.getProviderClasspath( "surefire-junit47", surefireBooterArtifact.getBaseVersion(), - null ); + String version = surefireBooterArtifact.getBaseVersion(); + return dependencyResolver.getProviderClasspath( "surefire-junit47", version, null ); } } @@ -3011,7 +3017,7 @@ public abstract class AbstractSurefireMojo @Override @Nonnull - public Classpath getProviderClasspath() + public Set<Artifact> getProviderClasspath() throws ArtifactResolutionException, ArtifactNotFoundException { return dependencyResolver.addProviderToClasspath( pluginArtifactMap, getMojoArtifact() ); diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/ClasspathCache.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/ClasspathCache.java index b8b2e2d..3bb1ac2 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/ClasspathCache.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/ClasspathCache.java @@ -19,7 +19,12 @@ package org.apache.maven.plugin.surefire; * under the License. */ +import java.util.ArrayList; +import java.util.Collection; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; + +import org.apache.maven.artifact.Artifact; import org.apache.maven.surefire.booter.Classpath; import javax.annotation.Nonnull; @@ -41,4 +46,16 @@ public class ClasspathCache { CLASSPATHS.put( key, classpath ); } + + public static Classpath setCachedClasspath( @Nonnull String key, @Nonnull Set<Artifact> artifacts ) + { + Collection<String> files = new ArrayList<String>(); + for ( Artifact artifact : artifacts ) + { + files.add( artifact.getFile().getAbsolutePath() ); + } + Classpath classpath = new Classpath( files ); + setCachedClasspath( key, classpath ); + return classpath; + } } diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/ProviderInfo.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/ProviderInfo.java index 5f00cab..e99cc82 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/ProviderInfo.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/ProviderInfo.java @@ -21,10 +21,7 @@ package org.apache.maven.plugin.surefire; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.resolver.AbstractArtifactResolutionException; -import org.apache.maven.artifact.resolver.ArtifactNotFoundException; -import org.apache.maven.artifact.resolver.ArtifactResolutionException; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.surefire.booter.Classpath; import javax.annotation.Nonnull; import java.util.Set; diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireDependencyResolver.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireDependencyResolver.java index a43c4de..f33a316 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireDependencyResolver.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireDependencyResolver.java @@ -19,10 +19,6 @@ package org.apache.maven.plugin.surefire; * under the License. */ -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.metadata.ArtifactMetadataSource; @@ -37,11 +33,19 @@ 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.surefire.booter.Classpath; import org.apache.maven.plugin.surefire.log.api.ConsoleLogger; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static java.util.Collections.singleton; +import static org.apache.maven.artifact.Artifact.SCOPE_TEST; +import static org.apache.maven.artifact.versioning.VersionRange.createFromVersion; /** * Does dependency resolution and artifact handling for the surefire plugin. @@ -123,65 +127,55 @@ public class SurefireDependencyResolver Artifact originatingArtifact = artifactFactory.createBuildArtifact( "dummy", "dummy", "1.0", "jar" ); - return artifactResolver.resolveTransitively( Collections.singleton( providerArtifact ), originatingArtifact, + return artifactResolver.resolveTransitively( singleton( providerArtifact ), originatingArtifact, localRepository, remoteRepositories, artifactMetadataSource, filter ); } @Nonnull - public Classpath getProviderClasspath( String provider, String version, Artifact filteredArtifact ) + @SuppressWarnings( "unchecked" ) + public Set<Artifact> getProviderClasspath( String provider, String version, Artifact filteredArtifact ) throws ArtifactNotFoundException, ArtifactResolutionException { - Classpath classPath = ClasspathCache.getCachedClassPath( provider ); - if ( classPath == null ) - { - Artifact providerArtifact = artifactFactory.createDependencyArtifact( "org.apache.maven.surefire", provider, - VersionRange.createFromVersion( - version ), "jar", null, - Artifact.SCOPE_TEST ); - ArtifactResolutionResult result = resolveArtifact( filteredArtifact, providerArtifact ); - List<String> files = new ArrayList<String>(); + Artifact providerArtifact = artifactFactory.createDependencyArtifact( "org.apache.maven.surefire", + provider, createFromVersion( version ), "jar", null, SCOPE_TEST ); + + ArtifactResolutionResult result = resolveArtifact( filteredArtifact, providerArtifact ); + if ( log.isDebugEnabled() ) + { for ( Object o : result.getArtifacts() ) { Artifact artifact = (Artifact) o; - - log.debug( - "Adding to " + pluginName + " test classpath: " + artifact.getFile().getAbsolutePath() + " Scope: " - + artifact.getScope() ); - - files.add( artifact.getFile().getAbsolutePath() ); + String artifactPath = artifact.getFile().getAbsolutePath(); + String scope = artifact.getScope(); + log.debug( "Adding to " + pluginName + " test classpath: " + artifactPath + " Scope: " + scope ); } - classPath = new Classpath( files ); - ClasspathCache.setCachedClasspath( provider, classPath ); } - return classPath; + + return result.getArtifacts(); } - public Classpath addProviderToClasspath( Map<String, Artifact> pluginArtifactMap, Artifact surefireArtifact ) + public Set<Artifact> addProviderToClasspath( Map<String, Artifact> pluginArtifactMap, Artifact surefireArtifact ) throws ArtifactResolutionException, ArtifactNotFoundException { - List<String> files = new ArrayList<String>(); + Set<Artifact> providerArtifacts = new LinkedHashSet<Artifact>(); if ( surefireArtifact != null ) { - final ArtifactResolutionResult artifactResolutionResult = resolveArtifact( null, surefireArtifact ); + ArtifactResolutionResult artifactResolutionResult = resolveArtifact( null, surefireArtifact ); for ( Artifact artifact : pluginArtifactMap.values() ) { if ( !artifactResolutionResult.getArtifacts().contains( artifact ) ) { - files.add( artifact.getFile().getAbsolutePath() ); + providerArtifacts.add( artifact ); } } } else { // Bit of a brute force strategy if not found. Should probably be improved - for ( Artifact artifact : pluginArtifactMap.values() ) - { - files.add( artifact.getFile().getPath() ); - } + providerArtifacts.addAll( pluginArtifactMap.values() ); } - return new Classpath( files ); + return providerArtifacts; } - } diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/TestClassPath.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/TestClassPath.java index b4f65ef..8727106 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/TestClassPath.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/TestClassPath.java @@ -25,6 +25,7 @@ import org.apache.maven.surefire.booter.Classpath; import java.io.File; import java.util.ArrayList; import java.util.List; +import java.util.Set; import static java.util.Collections.addAll; import static org.apache.maven.shared.utils.StringUtils.split; @@ -47,6 +48,11 @@ final class TestClassPath this.additionalClasspathElements = additionalClasspathElements; } + void avoidArtifactDuplicates( Set<Artifact> providerClasspath ) + { + + } + Iterable<Artifact> getArtifacts() { return artifacts;