You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by kr...@apache.org on 2012/08/08 19:40:44 UTC

svn commit: r1370857 - in /maven/surefire/trunk: maven-failsafe-plugin/src/main/java/org/apache/maven/plugin/failsafe/ maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/ maven-surefire-common/src/main/java/org/apache/maven/plugin/sur...

Author: krosenvold
Date: Wed Aug  8 17:40:44 2012
New Revision: 1370857

URL: http://svn.apache.org/viewvc?rev=1370857&view=rev
Log:
O Further refactoring to contain mutable state

Modified:
    maven/surefire/trunk/maven-failsafe-plugin/src/main/java/org/apache/maven/plugin/failsafe/IntegrationTestMojo.java
    maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
    maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireExecutionParameters.java
    maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkConfiguration.java
    maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkStarter.java
    maven/surefire/trunk/maven-surefire-plugin/src/main/java/org/apache/maven/plugin/surefire/SurefirePlugin.java

Modified: maven/surefire/trunk/maven-failsafe-plugin/src/main/java/org/apache/maven/plugin/failsafe/IntegrationTestMojo.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/maven-failsafe-plugin/src/main/java/org/apache/maven/plugin/failsafe/IntegrationTestMojo.java?rev=1370857&r1=1370856&r2=1370857&view=diff
==============================================================================
--- maven/surefire/trunk/maven-failsafe-plugin/src/main/java/org/apache/maven/plugin/failsafe/IntegrationTestMojo.java (original)
+++ maven/surefire/trunk/maven-failsafe-plugin/src/main/java/org/apache/maven/plugin/failsafe/IntegrationTestMojo.java Wed Aug  8 17:40:44 2012
@@ -219,6 +219,7 @@ public class IntegrationTestMojo
         File summaryFile = getSummaryFile();
         if ( !summaryFile.getParentFile().isDirectory() )
         {
+            //noinspection ResultOfMethodCallIgnored
             summaryFile.getParentFile().mkdirs();
         }
 
@@ -372,12 +373,12 @@ public class IntegrationTestMojo
         this.project = project;
     }
 
-    public List getClasspathDependencyExcludes()
+    public List<String> getClasspathDependencyExcludes()
     {
         return classpathDependencyExcludes;
     }
 
-    public void setClasspathDependencyExcludes( List classpathDependencyExcludes )
+    public void setClasspathDependencyExcludes( List<String> classpathDependencyExcludes )
     {
         this.classpathDependencyExcludes = classpathDependencyExcludes;
     }
@@ -392,12 +393,12 @@ public class IntegrationTestMojo
         this.classpathDependencyScopeExclude = classpathDependencyScopeExclude;
     }
 
-    public List getAdditionalClasspathElements()
+    public List<String> getAdditionalClasspathElements()
     {
         return additionalClasspathElements;
     }
 
-    public void setAdditionalClasspathElements( List additionalClasspathElements )
+    public void setAdditionalClasspathElements( List<String> additionalClasspathElements )
     {
         this.additionalClasspathElements = additionalClasspathElements;
     }
@@ -519,16 +520,6 @@ public class IntegrationTestMojo
         this.forkedProcessTimeoutInSeconds = forkedProcessTimeoutInSeconds;
     }
 
-    public Properties getOriginalSystemProperties()
-    {
-        return originalSystemProperties;
-    }
-
-    public void setOriginalSystemProperties( Properties originalSystemProperties )
-    {
-        this.originalSystemProperties = originalSystemProperties;
-    }
-
     public boolean isUseSystemClassLoader()
     {
         return useSystemClassLoader;

Modified: maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java?rev=1370857&r1=1370856&r2=1370857&view=diff
==============================================================================
--- maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java (original)
+++ maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java Wed Aug  8 17:40:44 2012
@@ -621,9 +621,22 @@ public abstract class AbstractSurefireMo
         throws MojoExecutionException, MojoFailureException
     {
         createDependencyResolver();
-        Summary summary = executeAllProviders( scanResult );
-        restoreOriginalSystemPropertiesWhenNotForking( summary );
-        handleSummary( summary );
+
+        Properties originalSystemProperties =
+            ForkConfiguration.isInProcess( getForkMode() ) ? (Properties) System.getProperties().clone() : null;
+        try
+        {
+            Summary summary = executeAllProviders( scanResult );
+
+            handleSummary( summary );
+        }
+        finally
+        {
+            if ( originalSystemProperties != null )
+            {
+                System.setProperties( originalSystemProperties );
+            }
+        }
     }
 
     private Artifact surefireBooterArtifact;
@@ -668,29 +681,30 @@ public abstract class AbstractSurefireMo
         return summary;
     }
 
-    private SurefireProperties setupProperties( boolean isForking )
+    private SurefireProperties setupProperties()
     {
-        storeOriginalSystemProperties();
-        SurefireProperties effectiveProperties = createEffectiveProperties();
-        if ( !isForking )
-        {
-            effectiveProperties.copyToSystemProperties();
-        }
+        SurefireProperties result =
+            SurefireProperties.calculateEffectiveProperties( getSystemProperties(), getSystemPropertiesFile(),
+                                                             getSystemPropertyVariables(), getUserProperties(),
+                                                             getLog() );
 
-        effectiveProperties.verifyLegalSystemProperties( getLog() );
+        result.setProperty( "basedir", getBasedir().getAbsolutePath() );
+        result.setProperty( "user.dir", getWorkingDirectory().getAbsolutePath() );
+        result.setProperty( "localRepository", getLocalRepository().getBasedir() );
+
+        result.verifyLegalSystemProperties( getLog() );
         if ( getLog().isDebugEnabled() )
         {
-            effectiveProperties.showToLog( getLog(), "system property" );
+            result.showToLog( getLog(), "system property" );
         }
-        return effectiveProperties;
-
+        return result;
     }
 
     private void executeProvider( ProviderInfo provider, DefaultScanResult scanResult, Summary summary )
         throws MojoExecutionException, MojoFailureException
     {
-        SurefireProperties effectiveProperties = setupProperties( ForkConfiguration.isForking( getForkMode() ) );
-        ForkConfiguration forkConfiguration = getForkConfiguration( effectiveProperties );
+        SurefireProperties effectiveProperties = setupProperties();
+        ForkConfiguration forkConfiguration = getForkConfiguration();
         summary.reportForkConfiguration( forkConfiguration );
         ClassLoaderConfiguration classLoaderConfiguration = getClassLoaderConfiguration( forkConfiguration );
 
@@ -702,6 +716,7 @@ public abstract class AbstractSurefireMo
             final RunResult result;
             if ( ForkConfiguration.FORK_NEVER.equals( forkConfiguration.getForkMode() ) )
             {
+                effectiveProperties.copyToSystemProperties();
                 InPluginVMSurefireStarter surefireStarter =
                     createInprocessStarter( provider, forkConfiguration, classLoaderConfiguration, runOrderParameters );
                 result = surefireStarter.runSuitesInProcess( scanResult );
@@ -709,7 +724,7 @@ public abstract class AbstractSurefireMo
             else
             {
                 ForkStarter forkStarter =
-                    createForkStarter( provider, forkConfiguration, classLoaderConfiguration, runOrderParameters );
+                    createForkStarter( provider, forkConfiguration, classLoaderConfiguration, runOrderParameters, effectiveProperties );
                 result = forkStarter.run( scanResult );
             }
             summary.registerRunResult( result );
@@ -747,14 +762,6 @@ public abstract class AbstractSurefireMo
     protected abstract void handleSummary( Summary summary )
         throws MojoExecutionException, MojoFailureException;
 
-    protected void restoreOriginalSystemPropertiesWhenNotForking( Summary summary )
-    {
-        if ( ( getOriginalSystemProperties() != null ) && ( summary.isForking() ) )
-        {
-            System.setProperties( getOriginalSystemProperties() );
-        }
-    }
-
     protected void logReportsDirectory()
     {
         getLog().info(
@@ -1165,7 +1172,8 @@ public abstract class AbstractSurefireMo
 
     protected ForkStarter createForkStarter( ProviderInfo provider, ForkConfiguration forkConfiguration,
                                              ClassLoaderConfiguration classLoaderConfiguration,
-                                             RunOrderParameters runOrderParameters )
+                                             RunOrderParameters runOrderParameters,
+                                             SurefireProperties effectiveSystemProperties )
         throws MojoExecutionException, MojoFailureException
     {
         StartupConfiguration startupConfiguration =
@@ -1174,7 +1182,7 @@ public abstract class AbstractSurefireMo
         StartupReportConfiguration startupReportConfiguration = getStartupReportConfiguration( configChecksum );
         ProviderConfiguration providerConfiguration = createProviderConfiguration( runOrderParameters );
         return new ForkStarter( providerConfiguration, startupConfiguration, forkConfiguration,
-                                getForkedProcessTimeoutInSeconds(), startupReportConfiguration );
+                                getForkedProcessTimeoutInSeconds(), startupReportConfiguration, effectiveSystemProperties );
     }
 
     protected InPluginVMSurefireStarter createInprocessStarter( ProviderInfo provider,
@@ -1192,7 +1200,7 @@ public abstract class AbstractSurefireMo
 
     }
 
-    protected ForkConfiguration getForkConfiguration( SurefireProperties effectiveProperties )
+    protected ForkConfiguration getForkConfiguration()
     {
         File tmpDir = getSurefireTempDir();
         //noinspection ResultOfMethodCallIgnored
@@ -1238,8 +1246,6 @@ public abstract class AbstractSurefireMo
         {
             setUseSystemClassLoader( isUseSystemClassLoader() );
 
-            fork.setSystemProperties( effectiveProperties );
-
             if ( "true".equals( getDebugForkedProcess() ) )
             {
                 setDebugForkedProcess(
@@ -1298,11 +1304,6 @@ public abstract class AbstractSurefireMo
         return fork;
     }
 
-    private void storeOriginalSystemProperties()
-    {
-        setOriginalSystemProperties( (Properties) System.getProperties().clone() );
-    }
-
 
     /**
      * Where surefire stores its own temp files

Modified: maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireExecutionParameters.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireExecutionParameters.java?rev=1370857&r1=1370856&r2=1370857&view=diff
==============================================================================
--- maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireExecutionParameters.java (original)
+++ maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireExecutionParameters.java Wed Aug  8 17:40:44 2012
@@ -241,10 +241,6 @@ public interface SurefireExecutionParame
 
     void setMetadataSource( ArtifactMetadataSource metadataSource );
 
-    Properties getOriginalSystemProperties();
-
-    void setOriginalSystemProperties( Properties originalSystemProperties );
-
     boolean isDisableXmlReport();
 
     void setDisableXmlReport( boolean disableXmlReport );

Modified: maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkConfiguration.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkConfiguration.java?rev=1370857&r1=1370856&r2=1370857&view=diff
==============================================================================
--- maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkConfiguration.java (original)
+++ maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkConfiguration.java Wed Aug  8 17:40:44 2012
@@ -121,6 +121,11 @@ public class ForkConfiguration
         return !FORK_NEVER.equals( getForkMode( forkMode ) );
     }
 
+    public static boolean isInProcess( String forkMode )
+    {
+        return !isForking( forkMode );
+    }
+
     public void setSystemProperties( Properties systemProperties )
     {
         this.systemProperties = (Properties) systemProperties.clone();
@@ -162,11 +167,6 @@ public class ForkConfiguration
         return forkMode;
     }
 
-    public Properties getSystemProperties()
-    {
-        return systemProperties;
-    }
-
     /**
      * @param classPath              cla the classpath arguments
      * @param classpathConfiguration the classpath configuration

Modified: maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkStarter.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkStarter.java?rev=1370857&r1=1370856&r2=1370857&view=diff
==============================================================================
--- maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkStarter.java (original)
+++ maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkStarter.java Wed Aug  8 17:40:44 2012
@@ -33,6 +33,7 @@ import java.util.concurrent.ThreadPoolEx
 import java.util.concurrent.TimeUnit;
 import org.apache.maven.plugin.surefire.CommonReflector;
 import org.apache.maven.plugin.surefire.StartupReportConfiguration;
+import org.apache.maven.plugin.surefire.SurefireProperties;
 import org.apache.maven.plugin.surefire.booterclient.output.ForkClient;
 import org.apache.maven.plugin.surefire.booterclient.output.ThreadedStreamConsumer;
 import org.apache.maven.plugin.surefire.report.FileReporterFactory;
@@ -81,22 +82,24 @@ public class ForkStarter
 
     private final StartupReportConfiguration startupReportConfiguration;
 
+    private final SurefireProperties effectiveSystemProperties;
+
     private final FileReporterFactory fileReporterFactory;
 
     private static volatile int systemPropertiesFileCounter = 0;
 
-    private static final Object lock = new Object();
-
 
     public ForkStarter( ProviderConfiguration providerConfiguration, StartupConfiguration startupConfiguration,
                         ForkConfiguration forkConfiguration, int forkedProcessTimeoutInSeconds,
-                        StartupReportConfiguration startupReportConfiguration )
+                        StartupReportConfiguration startupReportConfiguration,
+                        SurefireProperties effectiveSystemProperties )
     {
         this.forkConfiguration = forkConfiguration;
         this.providerConfiguration = providerConfiguration;
         this.forkedProcessTimeoutInSeconds = forkedProcessTimeoutInSeconds;
         this.startupConfiguration = startupConfiguration;
         this.startupReportConfiguration = startupReportConfiguration;
+        this.effectiveSystemProperties = effectiveSystemProperties;
         fileReporterFactory = new FileReporterFactory( startupReportConfiguration );
     }
 
@@ -220,7 +223,7 @@ public class ForkStarter
         throws SurefireBooterForkException
     {
         File surefireProperties;
-        File systemProperties = null;
+        File systPropsFile = null;
         try
         {
             BooterSerializer booterSerializer = new BooterSerializer( forkConfiguration, properties );
@@ -228,9 +231,9 @@ public class ForkStarter
             surefireProperties = booterSerializer.serialize( providerConfiguration, startupConfiguration, testSet,
                                                              forkConfiguration.getForkMode() );
 
-            if ( forkConfiguration.getSystemProperties() != null )
+            if ( effectiveSystemProperties != null )
             {
-                systemProperties = SystemPropertyManager.writePropertiesFile( forkConfiguration.getSystemProperties(),
+                systPropsFile = SystemPropertyManager.writePropertiesFile( effectiveSystemProperties,
                                                                               forkConfiguration.getTempDirectory(),
                                                                               "surefire_"
                                                                                   + systemPropertiesFileCounter++,
@@ -252,15 +255,16 @@ public class ForkStarter
         // Surefire-booter if !useSystemClassLoader
         Classpath bootClasspath = Classpath.join( bootClasspathConfiguration, additionlClassPathUrls );
 
+        @SuppressWarnings( "unchecked" )
         Commandline cli = forkConfiguration.createCommandLine( bootClasspath.getClassPath(),
                                                                startupConfiguration.getClassLoaderConfiguration(),
                                                                startupConfiguration.isShadefire() );
 
         cli.createArg().setFile( surefireProperties );
 
-        if ( systemProperties != null )
+        if ( systPropsFile != null )
         {
-            cli.createArg().setFile( systemProperties );
+            cli.createArg().setFile( systPropsFile );
         }
 
         ThreadedStreamConsumer threadedStreamConsumer = new ThreadedStreamConsumer( forkClient );

Modified: maven/surefire/trunk/maven-surefire-plugin/src/main/java/org/apache/maven/plugin/surefire/SurefirePlugin.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/maven-surefire-plugin/src/main/java/org/apache/maven/plugin/surefire/SurefirePlugin.java?rev=1370857&r1=1370856&r2=1370857&view=diff
==============================================================================
--- maven/surefire/trunk/maven-surefire-plugin/src/main/java/org/apache/maven/plugin/surefire/SurefirePlugin.java (original)
+++ maven/surefire/trunk/maven-surefire-plugin/src/main/java/org/apache/maven/plugin/surefire/SurefirePlugin.java Wed Aug  8 17:40:44 2012
@@ -21,7 +21,6 @@ package org.apache.maven.plugin.surefire
 
 import java.io.File;
 import java.util.List;
-import java.util.Properties;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.plugins.annotations.LifecyclePhase;
@@ -121,13 +120,6 @@ public class SurefirePlugin
     @Parameter( property = "surefire.timeout" )
     private int forkedProcessTimeoutInSeconds;
 
-    private Properties originalSystemProperties;
-
-    /**
-     * systemPropertyVariables + systemProperties
-     */
-    private Properties internalSystemProperties = new Properties();
-
     /**
      * Option to pass dependencies to the system's classloader instead of using an isolated class loader when forking.
      * Prevents problems with JDKs which implement the service provider lookup mechanism by using the system's
@@ -345,10 +337,10 @@ public class SurefirePlugin
             return null;
         }
         String[] testArray = StringUtils.split( test, "," );
-        StringBuffer tests = new StringBuffer();
-        for ( int i = 0; i < testArray.length; i++ )
+        StringBuilder tests = new StringBuilder();
+        for ( String aTestArray : testArray )
         {
-            String singleTest = testArray[i];
+            String singleTest = aTestArray;
             int index = singleTest.indexOf( '#' );
             if ( index >= 0 )
             {// the way version 2.7.3.  support single test method
@@ -377,7 +369,7 @@ public class SurefirePlugin
             if ( index2 < 0 )
             {
                 String testStrAfterFirstSharp = this.test.substring( index + 1, this.test.length() );
-                if ( testStrAfterFirstSharp.indexOf( "+" ) < 0 )
+                if ( !testStrAfterFirstSharp.contains( "+" ) )
                 {//the original way
                     return testStrAfterFirstSharp;
                 }
@@ -394,16 +386,6 @@ public class SurefirePlugin
         return null;
     }
 
-    public Properties getOriginalSystemProperties()
-    {
-        return originalSystemProperties;
-    }
-
-    public void setOriginalSystemProperties( Properties originalSystemProperties )
-    {
-        this.originalSystemProperties = originalSystemProperties;
-    }
-
     public boolean isUseSystemClassLoader()
     {
         return useSystemClassLoader;