maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tibordig...@apache.org
Subject [maven-surefire] branch master updated: [SUREFIRE-1679] Prevent classpath caching from causing pollution
Date Wed, 14 Aug 2019 19:07:34 GMT
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/master by this push:
     new f7d4310  [SUREFIRE-1679] Prevent classpath caching from causing pollution
f7d4310 is described below

commit f7d43102173568f4272325ad71e334279b91db17
Author: Andy Wilkinson <awilkinson@pivotal.io>
AuthorDate: Wed Aug 14 08:51:42 2019 +0100

    [SUREFIRE-1679] Prevent classpath caching from causing pollution
    
    Previously, classpath caching was performed statically. This resulted
    in the classpath cached by one project for a particular provider
    being used by a subsequent project. As a result any customizations to
    the classpath, such as removing duplicate artifacts, would leak out
    and pollute the classpath used by subsequent projects.
    
    This commit prevents the pollution by making the classpath cache
    instance-scoped so that the cache is only used by a single mojo and,
    therefore, a single project.
---
 .../plugin/surefire/AbstractSurefireMojo.java      | 41 ++++++++++++---
 .../maven/plugin/surefire/ClasspathCache.java      |  1 +
 .../plugin/surefire/AbstractSurefireMojoTest.java  | 61 ++++++++++++++++++++++
 3 files changed, 97 insertions(+), 6 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 2f179e8..e2c69b6 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
@@ -159,6 +159,8 @@ public abstract class AbstractSurefireMojo
 
     private final ProviderDetector providerDetector = new ProviderDetector();
 
+    private final ClasspathCache classpathCache = new ClasspathCache();
+
     /**
      * Note: use the legacy system property <em>disableXmlReport</em> set to
{@code true} to disable the report.
      */
@@ -1787,10 +1789,10 @@ public abstract class AbstractSurefireMojo
     {
         Classpath testClasspath = testClasspathWrapper.toClasspath();
 
-        Classpath providerClasspath = ClasspathCache.getCachedClassPath( providerName );
+        Classpath providerClasspath = classpathCache.getCachedClassPath( providerName );
         if ( providerClasspath == null )
         {
-            providerClasspath = ClasspathCache.setCachedClasspath( providerName, providerArtifacts
);
+            providerClasspath = classpathCache.setCachedClasspath( providerName, providerArtifacts
);
         }
 
         getConsoleLogger().debug( testClasspath.getLogMessage( "test classpath:" ) );
@@ -1868,10 +1870,10 @@ public abstract class AbstractSurefireMojo
     {
         Classpath testClasspath = testClasspathWrapper.toClasspath();
 
-        Classpath providerClasspath = ClasspathCache.getCachedClassPath( providerName );
+        Classpath providerClasspath = classpathCache.getCachedClassPath( providerName );
         if ( providerClasspath == null )
         {
-            providerClasspath = ClasspathCache.setCachedClasspath( providerName, providerArtifacts
);
+            providerClasspath = classpathCache.setCachedClasspath( providerName, providerArtifacts
);
         }
 
         ResolvePathsRequest<String> req = ResolvePathsRequest.ofStrings( testClasspath.getClassPath()
)
@@ -2629,7 +2631,7 @@ public abstract class AbstractSurefireMojo
 
     private Classpath getArtifactClasspath( Artifact surefireArtifact )
     {
-        Classpath existing = ClasspathCache.getCachedClassPath( surefireArtifact.getArtifactId()
);
+        Classpath existing = classpathCache.getCachedClassPath( surefireArtifact.getArtifactId()
);
         if ( existing == null )
         {
             List<String> items = new ArrayList<>();
@@ -2643,7 +2645,7 @@ public abstract class AbstractSurefireMojo
                 items.add( artifact.getFile().getAbsolutePath() );
             }
             existing = new Classpath( items );
-            ClasspathCache.setCachedClasspath( surefireArtifact.getArtifactId(), existing
);
+            classpathCache.setCachedClasspath( surefireArtifact.getArtifactId(), existing
);
         }
         return existing;
     }
@@ -3839,4 +3841,31 @@ public abstract class AbstractSurefireMojo
             throw new IllegalArgumentException( "Fork mode " + forkMode + " is not a legal
value" );
         }
     }
+
+    private static final class ClasspathCache
+    {
+        private final Map<String, Classpath> classpaths = new HashMap<>( 4 );
+
+        private Classpath getCachedClassPath( @Nonnull String artifactId )
+        {
+            return classpaths.get( artifactId );
+        }
+
+        private void setCachedClasspath( @Nonnull String key, @Nonnull Classpath classpath
)
+        {
+            classpaths.put( key, classpath );
+        }
+
+        private Classpath setCachedClasspath( @Nonnull String key, @Nonnull Set<Artifact>
artifacts )
+        {
+            Collection<String> files = new ArrayList<>();
+            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/ClasspathCache.java
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/ClasspathCache.java
index 7ba7b54..6dbaf29 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
@@ -32,6 +32,7 @@ import javax.annotation.Nonnull;
 /**
  * @author Kristian Rosenvold
  */
+@Deprecated
 public class ClasspathCache
 {
     private static final ConcurrentHashMap<String, Classpath> CLASSPATHS =
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 cec6760..bd77143 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
@@ -336,6 +336,67 @@ public class AbstractSurefireMojoTest
     }
 
     @Test
+    public void providerClasspathCachingIsNotSharedAcrossMojoInstances() throws Exception
{
+        ProviderInfo providerInfo = mock( ProviderInfo.class );
+        when( providerInfo.getProviderName() ).thenReturn( "test-provider" );
+        Artifact provider = new DefaultArtifact( "com.example", "provider", createFromVersion(
"1" ), "runtime", "jar", "", handler );
+        provider.setFile( mockFile( "original-test-provider.jar" ) );
+        HashSet<Artifact> providerClasspath = new HashSet<Artifact>( asList(
provider ) );
+        when( providerInfo.getProviderClasspath()).thenReturn( providerClasspath );
+
+        StartupConfiguration startupConfiguration = startupConfigurationForProvider(providerInfo);
+        assertThat( startupConfiguration.getClasspathConfiguration().getProviderClasspath().getClassPath()
)
+                .containsExactly( "original-test-provider.jar" );
+
+        provider.setFile( mockFile( "modified-test-provider.jar" ) );
+        startupConfiguration = startupConfigurationForProvider(providerInfo);
+        assertThat( startupConfiguration.getClasspathConfiguration().getProviderClasspath().getClassPath()
)
+                .containsExactly( "modified-test-provider.jar" );
+    }
+
+    private StartupConfiguration startupConfigurationForProvider(ProviderInfo providerInfo)
throws Exception {
+        AbstractSurefireMojo mojo = spy( new Mojo() );
+
+        Logger logger = mock( Logger.class );
+        when( logger.isDebugEnabled() ).thenReturn( true );
+        doNothing().when( logger ).debug( anyString() );
+        when( mojo.getConsoleLogger() ).thenReturn( new PluginConsoleLogger( logger ) );
+
+        File classesDir = mockFile( "classes" );
+        File testClassesDir = mockFile( "test-classes" );
+        TestClassPath testClassPath = new TestClassPath( new ArrayList<Artifact>(),
classesDir, testClassesDir, new String[0] );
+
+        Artifact common = new DefaultArtifact( "org.apache.maven.surefire", "maven-surefire-common",
+                createFromVersion( "1" ), "runtime", "jar", "", handler );
+        common.setFile( mockFile( "maven-surefire-common.jar" ) );
+
+        Artifact ext = new DefaultArtifact( "org.apache.maven.surefire", "surefire-extensions-api",
+                createFromVersion( "1" ), "runtime", "jar", "", handler );
+        ext.setFile( mockFile( "surefire-extensions-api.jar" ) );
+
+        Artifact api = new DefaultArtifact( "org.apache.maven.surefire", "surefire-api",
+                createFromVersion( "1" ), "runtime", "jar", "", handler );
+        api.setFile( mockFile( "surefire-api.jar" ) );
+
+        Artifact loggerApi = new DefaultArtifact( "org.apache.maven.surefire", "surefire-logger-api",
+                createFromVersion( "1" ), "runtime", "jar", "", handler );
+        loggerApi.setFile( mockFile( "surefire-logger-api.jar" ) );
+
+        Map<String, Artifact> providerArtifactsMap = new HashMap<>();
+        providerArtifactsMap.put( "org.apache.maven.surefire:maven-surefire-common", common
);
+        providerArtifactsMap.put( "org.apache.maven.surefire:surefire-extensions-api", ext
);
+        providerArtifactsMap.put( "org.apache.maven.surefire:surefire-api", api );
+        providerArtifactsMap.put( "org.apache.maven.surefire:surefire-logger-api", loggerApi
);
+
+        when( mojo.getPluginArtifactMap() ).thenReturn( providerArtifactsMap );
+
+        doReturn( 1 ).when( mojo, "getEffectiveForkCount" );
+
+        StartupConfiguration startupConfiguration = invokeMethod( mojo, "createStartupConfiguration",
providerInfo, false, null, null, null, testClassPath);
+        return startupConfiguration;
+    }
+
+    @Test
     public void shouldExistTmpDirectory() throws IOException
     {
         String systemTmpDir = System.getProperty( "java.io.tmpdir" );


Mime
View raw message