Author: krosenvold
Date: Wed Feb  9 20:36:31 2011
New Revision: 1069073

URL: http://svn.apache.org/viewvc?rev=1069073&view=rev
Log:
[SUREFIRE-695] Avoid duplicate code in SurefirePlugin and IntegrationTestMojo

Patch by Stephan Birkner, applied unchanged.

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-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=1069073&r1=1069072&r2=1069073&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 Feb  9 20:36:31 2011
@@ -28,7 +28,6 @@ import org.apache.maven.plugin.MojoExecu
 import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.plugin.surefire.AbstractSurefireMojo;
 import org.apache.maven.plugin.surefire.ProviderInfo;
-import org.apache.maven.plugin.surefire.SurefireExecutionParameters;
 import org.apache.maven.plugin.surefire.booterclient.ChecksumCalculator;
 import org.apache.maven.plugin.surefire.booterclient.ForkConfiguration;
 import org.apache.maven.plugin.surefire.booterclient.ForkStarter;
@@ -69,7 +68,6 @@ import java.util.Properties;
  */
 public class IntegrationTestMojo
     extends AbstractSurefireMojo
-    implements SurefireExecutionParameters
 {
 
     /**
@@ -538,10 +536,6 @@ public class IntegrationTestMojo
      */
     private ArtifactMetadataSource metadataSource;
 
-    private static final String BRIEF_REPORT_FORMAT = "brief";
-
-    private static final String PLAIN_REPORT_FORMAT = "plain";
-
     private Properties originalSystemProperties;
 
     /**
@@ -638,124 +632,96 @@ public class IntegrationTestMojo
     private ToolchainManager toolchainManager;
 
 
-    public void execute()
+    public void executeAfterPreconditionsChecked()
         throws MojoExecutionException, MojoFailureException
     {
-        if ( verifyParameters() )
+        final List providers = initialize();
+        String exceptionMessage = null;
+        FailsafeSummary result = new FailsafeSummary();
+
+        ForkConfiguration forkConfiguration = null;
+        for ( Iterator iter = providers.iterator(); iter.hasNext(); )
         {
-            if ( hasExecutedBefore() )
+            ProviderInfo provider = (ProviderInfo) iter.next();
+            forkConfiguration = getForkConfiguration();
+            ClassLoaderConfiguration classLoaderConfiguration = 
getClassLoaderConfiguration( forkConfiguration );
+            ForkStarter forkStarter = createForkStarter( provider, 
forkConfiguration, classLoaderConfiguration );
+            try
             {
-                return;
+                result.setResult( forkStarter.run() );
             }
-            logReportsDirectory();
-
-            final List providers = initialize();
-            String exceptionMessage = null;
-            FailsafeSummary result = new FailsafeSummary();
-
-            ForkConfiguration forkConfiguration = null;
-            for ( Iterator iter = providers.iterator(); iter.hasNext(); )
+            catch ( SurefireBooterForkException e )
             {
-                ProviderInfo provider = (ProviderInfo) iter.next();
-                forkConfiguration = getForkConfiguration();
-                ClassLoaderConfiguration classLoaderConfiguration = 
getClassLoaderConfiguration( forkConfiguration );
-                ForkStarter forkStarter = createForkStarter( provider, 
forkConfiguration, classLoaderConfiguration );
-                try
+                if ( exceptionMessage == null )
                 {
-                    result.setResult( forkStarter.run() );
+                    exceptionMessage = e.getMessage();
                 }
-                catch ( SurefireBooterForkException e )
-                {
-                    if ( exceptionMessage == null )
-                    {
-                        exceptionMessage = e.getMessage();
-                    }
-                }
-                catch ( SurefireExecutionException e )
-                {
-                    if ( exceptionMessage == null )
-                    {
-                        exceptionMessage = e.getMessage();
-                    }
-                }
-            }
-
-            if ( exceptionMessage != null )
-            {
-                // Fail no matter what as long as any provider failed
-                result.setResult( ProviderConfiguration.TESTS_FAILED_EXIT_CODE 
);
-                result.setException( exceptionMessage );
-            }
-
-            if ( getOriginalSystemProperties() != null && forkConfiguration != 
null && !forkConfiguration.isForking() )
-            {
-                // restore system properties, only makes sense when not 
forking..
-                System.setProperties( getOriginalSystemProperties() );
             }
-
-            if ( !getSummaryFile().getParentFile().isDirectory() )
+            catch ( SurefireExecutionException e )
             {
-                getSummaryFile().getParentFile().mkdirs();
-            }
-
-            try
-            {
-                String encoding;
-                if ( StringUtils.isEmpty( this.encoding ) )
+                if ( exceptionMessage == null )
                 {
-                    getLog().warn(
-                        "File encoding has not been set, using platform 
encoding " + ReaderFactory.FILE_ENCODING
-                            + ", i.e. build is platform dependent!" );
-                    encoding = ReaderFactory.FILE_ENCODING;
+                    exceptionMessage = e.getMessage();
                 }
-                else
-                {
-                    encoding = this.encoding;
-                }
-
-                FileOutputStream fileOutputStream = new FileOutputStream( 
getSummaryFile() );
-                BufferedOutputStream bufferedOutputStream = new 
BufferedOutputStream( fileOutputStream );
-                Writer writer = new OutputStreamWriter( bufferedOutputStream, 
encoding );
-                FailsafeSummaryXpp3Writer xpp3Writer = new 
FailsafeSummaryXpp3Writer();
-                xpp3Writer.write( writer, result );
-                writer.close();
-                bufferedOutputStream.close();
-                fileOutputStream.close();
-            }
-            catch ( IOException e )
-            {
-                throw new MojoExecutionException( e.getMessage(), e );
             }
         }
-    }
-
 
-    protected boolean verifyParameters()
-        throws MojoFailureException
-    {
-        if ( isSkip() || isSkipTests() || isSkipITs() || isSkipExec() )
+        if ( exceptionMessage != null )
         {
-            getLog().info( "Tests are skipped." );
-            return false;
+            // Fail no matter what as long as any provider failed
+            result.setResult( ProviderConfiguration.TESTS_FAILED_EXIT_CODE );
+            result.setException( exceptionMessage );
         }
 
-        if ( !getTestClassesDirectory().exists() )
+        if ( getOriginalSystemProperties() != null && forkConfiguration != 
null && !forkConfiguration.isForking() )
         {
-            if ( getFailIfNoTests() != null && 
getFailIfNoTests().booleanValue() )
-            {
-                throw new MojoFailureException( "No tests to run!" );
-            }
-            getLog().info( "No tests to run." );
-            return false;
+            // restore system properties, only makes sense when not forking..
+            System.setProperties( getOriginalSystemProperties() );
         }
 
-        ensureWorkingDirectoryExists();
+        writeSummary(result);
+    }
 
-        ensureParallelRunningCompatibility();
+       private void writeSummary(FailsafeSummary summary)
+                       throws MojoExecutionException {
+               File summaryFile = getSummaryFile();
+        if ( !summaryFile.getParentFile().isDirectory() )
+        {
+               summaryFile.getParentFile().mkdirs();
+        }
+               try
+        {
+            String encoding;
+            if ( StringUtils.isEmpty( this.encoding ) )
+            {
+                getLog().warn(
+                    "File encoding has not been set, using platform encoding " 
+ ReaderFactory.FILE_ENCODING
+                        + ", i.e. build is platform dependent!" );
+                encoding = ReaderFactory.FILE_ENCODING;
+            }
+            else
+            {
+                encoding = this.encoding;
+            }
 
-        warnIfUselessUseSystemClassLoaderParameter();
+            FileOutputStream fileOutputStream = new FileOutputStream( 
summaryFile );
+            BufferedOutputStream bufferedOutputStream = new 
BufferedOutputStream( fileOutputStream );
+            Writer writer = new OutputStreamWriter( bufferedOutputStream, 
encoding );
+            FailsafeSummaryXpp3Writer xpp3Writer = new 
FailsafeSummaryXpp3Writer();
+            xpp3Writer.write( writer, summary );
+            writer.close();
+            bufferedOutputStream.close();
+            fileOutputStream.close();
+        }
+        catch ( IOException e )
+        {
+            throw new MojoExecutionException( e.getMessage(), e );
+        }
+       }
 
-        return true;
+    protected boolean isSkipExecution()
+    {
+       return isSkip() || isSkipTests() || isSkipITs() || isSkipExec();
     }
 
     protected String getPluginName()

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=1069073&r1=1069072&r2=1069073&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 Feb  9 20:36:31 2011
@@ -92,8 +92,48 @@ public abstract class AbstractSurefireMo
 
     private SurefireDependencyResolver dependencyResolver;
 
-    protected abstract boolean verifyParameters()
-        throws MojoFailureException;
+    public void execute()
+        throws MojoExecutionException, MojoFailureException
+    {
+
+        if ( verifyParameters() && !hasExecutedBefore() )
+        {
+            logReportsDirectory();
+               executeAfterPreconditionsChecked();
+        }
+    }
+
+    protected boolean verifyParameters()
+        throws MojoFailureException
+       {
+           if ( isSkipExecution() )
+           {
+               getLog().info( "Tests are skipped." );
+               return false;
+           }
+       
+           if ( !getTestClassesDirectory().exists() )
+           {
+               if ( Boolean.TRUE.equals(getFailIfNoTests()) )
+               {
+                   throw new MojoFailureException( "No tests to run!" );
+               }
+               getLog().info( "No tests to run." );
+           }
+           else
+           {
+               ensureWorkingDirectoryExists();
+               ensureParallelRunningCompatibility();
+               warnIfUselessUseSystemClassLoaderParameter();
+           }
+       
+           return true;
+       }
+    
+    protected abstract boolean isSkipExecution();
+    
+    protected abstract void executeAfterPreconditionsChecked()
+        throws MojoExecutionException, MojoFailureException;
 
     private Artifact surefireArtifact;
 

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=1069073&r1=1069072&r2=1069073&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 Feb  9 20:36:31 2011
@@ -55,7 +55,7 @@ import java.util.Properties;
  */
 public class SurefirePlugin
     extends AbstractSurefireMojo
-    implements SurefireExecutionParameters, SurefireReportParameters
+    implements SurefireReportParameters
 {
 
     /**
@@ -592,83 +592,55 @@ public class SurefirePlugin
      */
     private ToolchainManager toolchainManager;
 
-    public void execute()
+    public void executeAfterPreconditionsChecked()
         throws MojoExecutionException, MojoFailureException
     {
-        if ( verifyParameters() )
+        final List providers = initialize();
+        Exception exception = null;
+        ForkConfiguration forkConfiguration = null;
+        int result = 0;
+        for ( Iterator iter = providers.iterator(); iter.hasNext(); )
         {
-            if ( hasExecutedBefore() )
-            {
-                return;
-            }
-            logReportsDirectory();
+            ProviderInfo provider = (ProviderInfo) iter.next();
+            forkConfiguration = getForkConfiguration();
+            ClassLoaderConfiguration classLoaderConfiguration = 
getClassLoaderConfiguration( forkConfiguration );
+            ForkStarter forkStarter = createForkStarter( provider, 
forkConfiguration, classLoaderConfiguration );
 
-            final List providers = initialize();
-            Exception exception = null;
-            ForkConfiguration forkConfiguration = null;
-            int result = 0;
-            for ( Iterator iter = providers.iterator(); iter.hasNext(); )
+            try
             {
-                ProviderInfo provider = (ProviderInfo) iter.next();
-                forkConfiguration = getForkConfiguration();
-                ClassLoaderConfiguration classLoaderConfiguration = 
getClassLoaderConfiguration( forkConfiguration );
-                ForkStarter forkStarter = createForkStarter( provider, 
forkConfiguration, classLoaderConfiguration );
-
-                try
-                {
-                    result = forkStarter.run();
-                }
-                catch ( SurefireBooterForkException e )
-                {
-                    exception = e;
-                }
-                catch ( SurefireExecutionException e )
-                {
-                    exception = e;
-                }
+                result = forkStarter.run();
             }
-
-            if ( exception != null )
+            catch ( SurefireBooterForkException e )
             {
-                throw new MojoExecutionException( exception.getMessage(), 
exception );
+                exception = e;
             }
-
-            if ( getOriginalSystemProperties() != null && forkConfiguration != 
null && !forkConfiguration.isForking() )
+            catch ( SurefireExecutionException e )
             {
-                // restore system properties, only makes sense when not 
forking..
-                System.setProperties( getOriginalSystemProperties() );
+                exception = e;
             }
-
-            SurefireHelper.reportExecution( this, result, getLog() );
         }
-    }
 
-    protected boolean verifyParameters()
-        throws MojoFailureException
-    {
-        if ( isSkip() || isSkipTests() || isSkipExec() )
+        if ( exception != null )
         {
-            getLog().info( "Tests are skipped." );
-            return false;
+            throw new MojoExecutionException( exception.getMessage(), 
exception );
         }
 
-        if ( !getTestClassesDirectory().exists() )
+        if ( getOriginalSystemProperties() != null && forkConfiguration != 
null && !forkConfiguration.isForking() )
         {
-            if ( getFailIfNoTests() != null && 
getFailIfNoTests().booleanValue() )
-            {
-                throw new MojoFailureException( "No tests to run!" );
-            }
-            getLog().info( "No tests to run." );
-            return true;
+            // restore system properties, only makes sense when not forking..
+            System.setProperties( getOriginalSystemProperties() );
         }
 
-        ensureWorkingDirectoryExists();
-
-        ensureParallelRunningCompatibility();
+        writeSummary(result);
+    }
 
-        warnIfUselessUseSystemClassLoaderParameter();
+       private void writeSummary(int result) throws MojoFailureException {
+               SurefireHelper.reportExecution( this, result, getLog() );
+       }
 
-        return true;
+    protected boolean isSkipExecution()
+    {
+       return isSkip() || isSkipTests() || isSkipExec();
     }
 
     protected String getPluginName()


Reply via email to