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/08/14 15:48:01 UTC

[maven-surefire] branch SUREFIRE-1679 updated (cae2550 -> 0cf43c9)

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

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


 discard cae2550  [SUREFIRE-1679] Prevent classpath caching from causing pollution
     new 0cf43c9  [SUREFIRE-1679] Prevent classpath caching from causing pollution

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (cae2550)
            \
             N -- N -- N   refs/heads/SUREFIRE-1679 (0cf43c9)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../plugin/surefire/AbstractSurefireMojo.java      |  8 +--
 .../plugin/surefire/AbstractSurefireMojoTest.java  | 61 ++++++++++++++++++++++
 2 files changed, 65 insertions(+), 4 deletions(-)


[maven-surefire] 01/01: [SUREFIRE-1679] Prevent classpath caching from causing pollution

Posted by ti...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 0cf43c99128f9dffa86874e6de97a8c11266c77b
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.
---
 .../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" );