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 2020/05/16 10:46:12 UTC

[maven-surefire] branch SUREFIRE-1570 updated: early initialized Platform and ResolvePathResultWrapper to gain the performance

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

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


The following commit(s) were added to refs/heads/SUREFIRE-1570 by this push:
     new 7170a78  early initialized Platform and ResolvePathResultWrapper to gain the performance
7170a78 is described below

commit 7170a78d16b8f664a6a9eb40985c72d45ae6adb7
Author: tibordigana <ti...@apache.org>
AuthorDate: Sat May 16 12:46:04 2020 +0200

    early initialized Platform and ResolvePathResultWrapper to gain the performance
---
 .../plugin/surefire/AbstractSurefireMojo.java      | 59 ++++++++++++----------
 .../plugin/surefire/AbstractSurefireMojoTest.java  | 28 +++++-----
 2 files changed, 46 insertions(+), 41 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 490c6da..0dfb95d 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
@@ -910,6 +910,7 @@ public abstract class AbstractSurefireMojo
         cli = commandLineOptions();
         // Stuff that should have been final
         setupStuff();
+        Platform platform = PLATFORM.withJdkExecAttributesForTests( getEffectiveJvm() );
 
         if ( verifyParameters() && !hasExecutedBefore() )
         {
@@ -925,7 +926,7 @@ public abstract class AbstractSurefireMojo
                 return;
             }
             logReportsDirectory();
-            executeAfterPreconditionsChecked( scan );
+            executeAfterPreconditionsChecked( scan, platform );
         }
     }
 
@@ -1137,11 +1138,12 @@ public abstract class AbstractSurefireMojo
         }
     }
 
-    private void executeAfterPreconditionsChecked( @Nonnull DefaultScanResult scanResult )
+    private void executeAfterPreconditionsChecked( @Nonnull DefaultScanResult scanResult, @Nonnull Platform platform )
         throws MojoExecutionException, MojoFailureException
     {
         TestClassPath testClasspath = generateTestClasspath();
         List<ProviderInfo> providers = createProviders( testClasspath );
+        ResolvePathResultWrapper wrapper = findModuleDescriptor( platform.getJdkExecAttributesForTests().getJdkHome() );
 
         RunResult current = noTestsRun();
 
@@ -1150,7 +1152,8 @@ public abstract class AbstractSurefireMojo
         {
             try
             {
-                current = current.aggregate( executeProvider( provider, scanResult, testClasspath ) );
+                current =
+                    current.aggregate( executeProvider( provider, scanResult, testClasspath, platform, wrapper ) );
             }
             catch ( SurefireBooterForkException | SurefireExecutionException | TestSetFailedException e )
             {
@@ -1268,7 +1271,8 @@ public abstract class AbstractSurefireMojo
 
     @Nonnull
     private RunResult executeProvider( @Nonnull ProviderInfo provider, @Nonnull DefaultScanResult scanResult,
-                                       @Nonnull TestClassPath testClasspathWrapper )
+                                       @Nonnull TestClassPath testClasspathWrapper, @Nonnull Platform platform,
+                                       @Nonnull ResolvePathResultWrapper resolvedJavaModularityResult )
         throws MojoExecutionException, MojoFailureException, SurefireExecutionException, SurefireBooterForkException,
         TestSetFailedException
     {
@@ -1279,7 +1283,6 @@ public abstract class AbstractSurefireMojo
         RunOrderParameters runOrderParameters =
             new RunOrderParameters( getRunOrder(), getStatisticsFile( getConfigChecksum() ) );
 
-        Platform platform = PLATFORM.withJdkExecAttributesForTests( getEffectiveJvm() );
         if ( isNotForking() )
         {
             createCopyAndReplaceForkNumPlaceholder( effectiveProperties, 1 ).copyToSystemProperties();
@@ -1290,7 +1293,7 @@ public abstract class AbstractSurefireMojo
         }
         else
         {
-            ForkConfiguration forkConfiguration = createForkConfiguration( platform );
+            ForkConfiguration forkConfiguration = createForkConfiguration( platform, resolvedJavaModularityResult );
             if ( getConsoleLogger().isDebugEnabled() )
             {
                 showMap( getEnvironmentVariables(), "environment variable" );
@@ -1302,8 +1305,8 @@ public abstract class AbstractSurefireMojo
             try
             {
                 forkStarter = createForkStarter( provider, forkConfiguration, classLoaderConfiguration,
-                                                       runOrderParameters, getConsoleLogger(), scanResult, platform,
-                                                       testClasspathWrapper );
+                                                       runOrderParameters, getConsoleLogger(), scanResult,
+                                                       testClasspathWrapper, platform, resolvedJavaModularityResult );
 
                 return forkStarter.run( effectiveProperties, scanResult );
             }
@@ -1378,9 +1381,9 @@ public abstract class AbstractSurefireMojo
         this.dependencyResolver = dependencyResolver;
     }
 
-    private boolean existsModuleDescriptor( File jdkHome )
+    private boolean existsModuleDescriptor( ResolvePathResultWrapper resolvedJavaModularityResult )
     {
-        return findModuleDescriptor( jdkHome ).getResolvePathResult() != null;
+        return resolvedJavaModularityResult.getResolvePathResult() != null;
     }
 
     private ResolvePathResultWrapper findModuleDescriptor( File jdkHome )
@@ -1404,11 +1407,12 @@ public abstract class AbstractSurefireMojo
         }
     }
 
-    private boolean canExecuteProviderWithModularPath( Platform platform )
+    private boolean canExecuteProviderWithModularPath( Platform platform,
+                                                       ResolvePathResultWrapper resolvedJavaModularityResult )
     {
         return useModulePath()
                 && platform.getJdkExecAttributesForTests().isJava9AtLeast()
-                && existsModuleDescriptor( platform.getJdkExecAttributesForTests().getJdkHome() );
+                && existsModuleDescriptor( resolvedJavaModularityResult );
     }
 
     /**
@@ -1864,15 +1868,16 @@ public abstract class AbstractSurefireMojo
     private StartupConfiguration createStartupConfiguration( @Nonnull ProviderInfo provider, boolean isForking,
                                                              @Nonnull ClassLoaderConfiguration classLoaderConfiguration,
                                                              @Nonnull DefaultScanResult scanResult,
+                                                             @Nonnull TestClassPath testClasspathWrapper,
                                                              @Nonnull Platform platform,
-                                                             @Nonnull TestClassPath testClasspathWrapper )
+                                                             @Nonnull ResolvePathResultWrapper resolvedJavaModularity )
         throws MojoExecutionException
     {
         try
         {
             Set<Artifact> providerArtifacts = provider.getProviderClasspath();
             String providerName = provider.getProviderName();
-            if ( isForking && canExecuteProviderWithModularPath( platform ) )
+            if ( isForking && canExecuteProviderWithModularPath( platform, resolvedJavaModularity ) )
             {
                 JdkAttributes jdkAttributes = platform.getJdkExecAttributesForTests();
                 File jdkHome = jdkAttributes.getJdkHome();
@@ -2340,12 +2345,13 @@ public abstract class AbstractSurefireMojo
     private ForkStarter createForkStarter( @Nonnull ProviderInfo provider, @Nonnull ForkConfiguration forkConfiguration,
                                            @Nonnull ClassLoaderConfiguration classLoaderConfiguration,
                                            @Nonnull RunOrderParameters runOrderParameters, @Nonnull ConsoleLogger log,
-                                           @Nonnull DefaultScanResult scanResult, @Nonnull Platform platform,
-                                           @Nonnull TestClassPath testClasspathWrapper )
+                                           @Nonnull DefaultScanResult scanResult,
+                                           @Nonnull TestClassPath testClasspathWrapper, @Nonnull Platform platform,
+                                           ResolvePathResultWrapper resolvedJavaModularityResult )
         throws MojoExecutionException, MojoFailureException
     {
         StartupConfiguration startupConfiguration = createStartupConfiguration( provider, true,
-                classLoaderConfiguration, scanResult, platform, testClasspathWrapper );
+                classLoaderConfiguration, scanResult, testClasspathWrapper, platform, resolvedJavaModularityResult );
         String configChecksum = getConfigChecksum();
         StartupReportConfiguration startupReportConfiguration = getStartupReportConfiguration( configChecksum, true );
         ProviderConfiguration providerConfiguration = createProviderConfiguration( runOrderParameters );
@@ -2362,7 +2368,7 @@ public abstract class AbstractSurefireMojo
         throws MojoExecutionException, MojoFailureException
     {
         StartupConfiguration startupConfiguration = createStartupConfiguration( provider, false, classLoaderConfig,
-                scanResult, platform, testClasspathWrapper );
+                scanResult, testClasspathWrapper, platform, new ResolvePathResultWrapper( null, true ) );
         String configChecksum = getConfigChecksum();
         StartupReportConfiguration startupReportConfiguration = getStartupReportConfiguration( configChecksum, false );
         ProviderConfiguration providerConfiguration = createProviderConfiguration( runOrderParameters );
@@ -2379,7 +2385,8 @@ public abstract class AbstractSurefireMojo
     }
 
     @Nonnull
-    private ForkConfiguration createForkConfiguration( Platform platform )
+    private ForkConfiguration createForkConfiguration( @Nonnull Platform platform,
+                                                       @Nonnull ResolvePathResultWrapper resolvedJavaModularityResult )
     {
         File tmpDir = getSurefireTempDir();
 
@@ -2391,7 +2398,7 @@ public abstract class AbstractSurefireMojo
 
         getConsoleLogger().debug( "Found implementation of fork node factory: " + forkNode.getClass().getName() );
 
-        if ( canExecuteProviderWithModularPath( platform ) )
+        if ( canExecuteProviderWithModularPath( platform, resolvedJavaModularityResult ) )
         {
             return new ModularClasspathForkConfiguration( bootClasspath,
                     tmpDir,
@@ -2529,9 +2536,9 @@ public abstract class AbstractSurefireMojo
 
     private JdkAttributes getEffectiveJvm() throws MojoFailureException
     {
-        if ( isNotEmpty( jvm ) )
+        if ( isNotEmpty( getJvm() ) )
         {
-            File pathToJava = new File( jvm ).getAbsoluteFile();
+            File pathToJava = new File( getJvm() ).getAbsoluteFile();
             if ( !endsWithJavaPath( pathToJava.getPath() ) )
             {
                 throw new MojoFailureException( "Given path does not end with java executor \""
@@ -2550,9 +2557,9 @@ public abstract class AbstractSurefireMojo
             {
                 getConsoleLogger().warning( "Cannot determine JAVA_HOME of jvm exec path " + pathToJava );
             }
-            else if ( !environmentVariables.containsKey( "JAVA_HOME" ) )
+            else if ( !getEnvironmentVariables().containsKey( "JAVA_HOME" ) )
             {
-                environmentVariables.put( "JAVA_HOME", jdkHome.getAbsolutePath() );
+                getEnvironmentVariables().put( "JAVA_HOME", jdkHome.getAbsolutePath() );
             }
             BigDecimal version = jdkHome == null ? null : toJdkVersionFromReleaseFile( jdkHome );
             boolean javaVersion9 = version == null ? isJava9AtLeast( pathToJava.getPath() ) : isJava9AtLeast( version );
@@ -2577,10 +2584,10 @@ public abstract class AbstractSurefireMojo
                 if ( toolchain instanceof DefaultJavaToolChain )
                 {
                     DefaultJavaToolChain defaultJavaToolChain = (DefaultJavaToolChain) toolchain;
-                    if ( !environmentVariables.containsKey( "JAVA_HOME" ) )
+                    if ( !getEnvironmentVariables().containsKey( "JAVA_HOME" ) )
                     {
                         jdkHome = defaultJavaToolChain.getJavaHome();
-                        environmentVariables.put( "JAVA_HOME", jdkHome );
+                        getEnvironmentVariables().put( "JAVA_HOME", jdkHome );
                     }
                 }
 
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 bbef6e2..92ef90c 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
@@ -170,7 +170,7 @@ public class AbstractSurefireMojoTest
         assertThat( wrapper.getResolvePathResult() )
             .isNull();
 
-        assertThat( invokeMethod( mojo, "existsModuleDescriptor", jdkHome ) )
+        assertThat( invokeMethod( mojo, "existsModuleDescriptor", wrapper ) )
             .isEqualTo( false );
 
         when( mojo.useModulePath() ).thenReturn( true );
@@ -178,7 +178,7 @@ public class AbstractSurefireMojoTest
         File jvmExecutable = new File( jdkHome, IS_OS_WINDOWS ? "bin\\java.exe" : "bin/java" );
         JdkAttributes jdkAttributes = new JdkAttributes( jvmExecutable, jdkHome, true );
         Platform platform = new Platform().withJdkExecAttributesForTests( jdkAttributes );
-        assertThat( invokeMethod( mojo, "canExecuteProviderWithModularPath", platform ) )
+        assertThat( invokeMethod( mojo, "canExecuteProviderWithModularPath", platform, wrapper ) )
             .isEqualTo( false );
     }
 
@@ -213,7 +213,7 @@ public class AbstractSurefireMojoTest
         assertThat( wrapper.getResolvePathResult().getModuleDescriptor() )
             .isSameAs( descriptor );
 
-        assertThat( invokeMethod( mojo, "existsModuleDescriptor", jdkHome ) )
+        assertThat( invokeMethod( mojo, "existsModuleDescriptor", wrapper ) )
             .isEqualTo( true );
 
         when( mojo.useModulePath() ).thenReturn( true );
@@ -221,19 +221,19 @@ public class AbstractSurefireMojoTest
         File jvmExecutable = new File( jdkHome, IS_OS_WINDOWS ? "bin\\java.exe" : "bin/java" );
         JdkAttributes jdkAttributes = new JdkAttributes( jvmExecutable, jdkHome, true );
         Platform platform = new Platform().withJdkExecAttributesForTests( jdkAttributes );
-        assertThat( invokeMethod( mojo, "canExecuteProviderWithModularPath", platform ) )
+        assertThat( invokeMethod( mojo, "canExecuteProviderWithModularPath", platform, wrapper ) )
             .isEqualTo( true );
 
         jdkAttributes = new JdkAttributes( jvmExecutable, jdkHome, false );
         platform = new Platform().withJdkExecAttributesForTests( jdkAttributes );
-        assertThat( invokeMethod( mojo, "canExecuteProviderWithModularPath", platform ) )
+        assertThat( invokeMethod( mojo, "canExecuteProviderWithModularPath", platform, wrapper ) )
             .isEqualTo( false );
 
         when( mojo.useModulePath() ).thenReturn( false );
 
         jdkAttributes = new JdkAttributes( jvmExecutable, jdkHome, true );
         platform = new Platform().withJdkExecAttributesForTests( jdkAttributes );
-        assertThat( invokeMethod( mojo, "canExecuteProviderWithModularPath", platform ) )
+        assertThat( invokeMethod( mojo, "canExecuteProviderWithModularPath", platform, wrapper ) )
             .isEqualTo( false );
     }
 
@@ -265,7 +265,7 @@ public class AbstractSurefireMojoTest
         assertThat( wrapper.getResolvePathResult() )
             .isNull();
 
-        assertThat( invokeMethod( mojo, "existsModuleDescriptor", jdkHome ) )
+        assertThat( invokeMethod( mojo, "existsModuleDescriptor", wrapper ) )
             .isEqualTo( false );
     }
 
@@ -638,7 +638,8 @@ public class AbstractSurefireMojoTest
 
         doReturn( 1 ).when( mojo, "getEffectiveForkCount" );
 
-        return invokeMethod( mojo, "createStartupConfiguration", providerInfo, false, null, null, null, testClassPath );
+        return invokeMethod( mojo, "createStartupConfiguration",
+            providerInfo, false, null, null, testClassPath, null, null );
     }
 
     @Test
@@ -646,11 +647,6 @@ public class AbstractSurefireMojoTest
     {
         String baseDir = System.getProperty( "user.dir" );
 
-        /*if ( !JAVA_RECENT.atLeast( JAVA_9 ) )
-        {
-            return;
-        }*/
-
         Mojo mojo = new Mojo();
 
         // ### BEGIN
@@ -691,13 +687,15 @@ public class AbstractSurefireMojoTest
         artifacts.put( "org.apache.maven.surefire:surefire-booter", dummyArtifact );
         artifacts.put( "org.apache.maven.surefire:surefire-shared-utils", dummyArtifact );
         mojo.setPluginArtifactMap( artifacts );
+
+        ResolvePathResultWrapper wrapper = new ResolvePathResultWrapper( mock( ResolvePathResult.class ), true );
         // ### END
 
         StartupConfiguration actualConfig = invokeMethod( mojo, "createStartupConfiguration",
             new Class[] {ProviderInfo.class, boolean.class, ClassLoaderConfiguration.class, DefaultScanResult.class,
-                Platform.class, TestClassPath.class},
+                TestClassPath.class, Platform.class, ResolvePathResultWrapper.class},
             providerInfo, true, mock( ClassLoaderConfiguration.class ), defaultScanResult,
-            platform, testClassPath );
+            testClassPath, platform, wrapper );
 
         assertThat( actualConfig )
             .isNotNull();