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

2019-08-14 Thread tibordigana
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 
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 disableXmlReport 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 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 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 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 artifacts )
+{
+Collection files = new 

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

2019-08-14 Thread tibordigana
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 cae25506a71bbdf196efbe0173d636bb83b7d7e4
Author: Andy Wilkinson 
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 +
 2 files changed, 36 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..3d03c10 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
@@ -158,6 +158,8 @@ public abstract class AbstractSurefireMojo
 private static final Platform PLATFORM = new Platform();
 
 private final ProviderDetector providerDetector = new ProviderDetector();
+
+private final ClasspathCache classpathCache = new ClasspathCache();
 
 /**
  * Note: use the legacy system property disableXmlReport 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 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 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 ConcurrentHashMap classpaths = new 
ConcurrentHashMap<>( 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 artifacts )
+{
+Collection files