You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ti...@apache.org on 2019/10/17 08:51:35 UTC

[maven-surefire] branch release/2.22.2 updated: [SUREFIRE-1679] Prevent classpath caching from causing pollution

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

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


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

commit 7d51f96b5f163b57efa3e8ce555094436f9bc06e
Author: Andy Wilkinson <aw...@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.
    
    Backports f7d431021 to 2.22.x
---
 .../plugin/surefire/AbstractSurefireMojo.java      | 42 ++++++++++++---
 .../maven/plugin/surefire/ClasspathCache.java      |  1 +
 .../plugin/surefire/AbstractSurefireMojoTest.java  | 60 ++++++++++++++++++++++
 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 c976a23..2e912ed 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
@@ -98,6 +98,7 @@ import java.lang.reflect.Array;
 import java.lang.reflect.Method;
 import java.math.BigDecimal;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Enumeration;
 import java.util.HashMap;
@@ -160,6 +161,8 @@ public abstract class AbstractSurefireMojo
 
     private final ProviderDetector providerDetector = new ProviderDetector();
 
+    private final ClasspathCache classpathCache = new ClasspathCache();
+
     /**
      * Information about this plugin, mainly used to lookup this plugin's configuration from the currently executing
      * project.
@@ -1749,10 +1752,10 @@ public abstract class AbstractSurefireMojo
 
         testClasspathWrapper.avoidArtifactDuplicates( providerArtifacts );
 
-        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:" ) );
@@ -1822,10 +1825,10 @@ public abstract class AbstractSurefireMojo
 
         testClasspathWrapper.avoidArtifactDuplicates( providerArtifacts );
 
-        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() )
@@ -2552,7 +2555,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 )
         {
             ArtifactResolutionResult result = resolveArtifact( null, surefireArtifact );
@@ -2569,7 +2572,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;
     }
@@ -3746,4 +3749,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<String, Classpath>( 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<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/ClasspathCache.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/ClasspathCache.java
index 3bb1ac2..37973d9 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 22c3f52..448fb49 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
@@ -30,7 +30,9 @@ import org.apache.maven.surefire.booter.Classpath;
 import org.apache.maven.surefire.booter.StartupConfiguration;
 import org.apache.maven.surefire.suite.RunResult;
 import org.codehaus.plexus.logging.Logger;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
 import org.junit.runner.RunWith;
 import org.mockito.ArgumentCaptor;
 import org.mockito.Mock;
@@ -73,6 +75,9 @@ import static org.powermock.reflect.Whitebox.invokeMethod;
 @PrepareForTest( AbstractSurefireMojo.class )
 public class AbstractSurefireMojoTest
 {
+    @Rule
+    public final TemporaryFolder temp = new TemporaryFolder();
+
     @Mock
     private ArtifactHandler handler;
 
@@ -308,6 +313,61 @@ 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() );
+
+        MavenProject project = mock( MavenProject.class );
+        when( project.getArtifacts() ).thenReturn( new HashSet<Artifact>() );
+        when( mojo.getProject() ).thenReturn( project );
+
+        Logger logger = mock( Logger.class );
+        when( logger.isDebugEnabled() ).thenReturn( true );
+        doNothing().when( logger ).debug( anyString() );
+        when( mojo.getConsoleLogger() ).thenReturn( new PluginConsoleLogger( logger ) );
+
+        File classesDir = temp.newFolder();
+        File testClassesDir = temp.newFolder();
+        when ( mojo.getTestClassesDirectory() ).thenReturn( testClassesDir );
+        when ( mojo.getClassesDirectory() ).thenReturn( classesDir );
+
+        Artifact common = new DefaultArtifact( "org.apache.maven.surefire", "maven-surefire-common",
+                createFromVersion( "1" ), "runtime", "jar", "", handler );
+        common.setFile( mockFile( "maven-surefire-common.jar" ) );
+
+        Artifact api = new DefaultArtifact( "org.apache.maven.surefire", "surefire-api",
+                createFromVersion( "1" ), "runtime", "jar", "", handler );
+        api.setFile( mockFile( "surefire-api.jar" ) );
+
+        Map<String, Artifact> providerArtifactsMap = new HashMap<String, Artifact>();
+        providerArtifactsMap.put( "org.apache.maven.surefire:maven-surefire-common", common );
+        providerArtifactsMap.put( "org.apache.maven.surefire:surefire-api", api );
+        when( mojo.getPluginArtifactMap() ).thenReturn( providerArtifactsMap );
+
+        doReturn( 1 ).when( mojo, "getEffectiveForkCount" );
+
+        StartupConfiguration startupConfiguration = invokeMethod( mojo, "createStartupConfiguration", providerInfo, false, null, null);
+        return startupConfiguration;
+    }
+
+    @Test
     public void shouldExistTmpDirectory() throws IOException
     {
         String systemTmpDir = System.getProperty( "java.io.tmpdir" );