Author: stephenc
Date: Tue Nov 22 10:35:17 2011
New Revision: 1204901

URL: http://svn.apache.org/viewvc?rev=1204901&view=rev
Log:
[SUREFIRE-772] Skip Maven Failsafe Report

o Adds some "smarts" to the reports so that they can be skipped always or 
skipped only if the reports they are generating are missing and allows these 
smarts to be specified per report type.

o Todo: put in some integration tests for the report smart skipping

Modified:
    
maven/surefire/trunk/maven-surefire-report-plugin/src/main/java/org/apache/maven/plugins/surefire/report/AbstractSurefireReportMojo.java
    
maven/surefire/trunk/maven-surefire-report-plugin/src/main/java/org/apache/maven/plugins/surefire/report/FailsafeReportMojo.java
    
maven/surefire/trunk/maven-surefire-report-plugin/src/main/java/org/apache/maven/plugins/surefire/report/SurefireReportMojo.java
    
maven/surefire/trunk/maven-surefire-report-plugin/src/main/java/org/apache/maven/plugins/surefire/report/SurefireReportParser.java

Modified: 
maven/surefire/trunk/maven-surefire-report-plugin/src/main/java/org/apache/maven/plugins/surefire/report/AbstractSurefireReportMojo.java
URL: 
http://svn.apache.org/viewvc/maven/surefire/trunk/maven-surefire-report-plugin/src/main/java/org/apache/maven/plugins/surefire/report/AbstractSurefireReportMojo.java?rev=1204901&r1=1204900&r2=1204901&view=diff
==============================================================================
--- 
maven/surefire/trunk/maven-surefire-report-plugin/src/main/java/org/apache/maven/plugins/surefire/report/AbstractSurefireReportMojo.java
 (original)
+++ 
maven/surefire/trunk/maven-surefire-report-plugin/src/main/java/org/apache/maven/plugins/surefire/report/AbstractSurefireReportMojo.java
 Tue Nov 22 10:35:17 2011
@@ -110,11 +110,38 @@ public abstract class AbstractSurefireRe
     private boolean aggregate;
 
     /**
+     * Whether the report should be generated or not.
+     *
+     * @return {@code true} if and only if the report should be generated.
+     * @since 2.11
+     */
+    protected boolean isSkipped()
+    {
+        return false;
+    }
+
+    /**
+     * Whether the report should be generated when there are no test results.
+     *
+     * @return {@code true} if and only if the report should be generated when 
there are no result files at all.
+     * @since 2.11
+     */
+    protected boolean isGeneratedWhenNoResults()
+    {
+        return false;
+    }
+
+    /**
      * {@inheritDoc}
      */
     public void executeReport( Locale locale )
         throws MavenReportException
     {
+        if ( isSkipped() )
+        {
+            getLog().info( getOutputName() + " generation skipped." );
+            return;
+        }
         final List reportsDirectoryList = new ArrayList();
 
         if ( reportsDirectories != null )
@@ -175,6 +202,19 @@ public abstract class AbstractSurefireRe
             }
         }
 
+        if ( isGeneratedWhenNoResults() )
+        {
+            boolean atLeastOneDirectoryExists = false;
+            for ( Iterator i = reportsDirectoryList.iterator(); i.hasNext() && 
!atLeastOneDirectoryExists; )
+            {
+                atLeastOneDirectoryExists = 
SurefireReportParser.hasReportFiles( (File) i.next() );
+            }
+            if ( !atLeastOneDirectoryExists )
+            {
+                getLog().info( getOutputName() + " generation skipped as there 
are no report files to report." );
+            }
+        }
+
         SurefireReportGenerator report =
             new SurefireReportGenerator( reportsDirectoryList, locale, 
showSuccess, determineXrefLocation() );
 

Modified: 
maven/surefire/trunk/maven-surefire-report-plugin/src/main/java/org/apache/maven/plugins/surefire/report/FailsafeReportMojo.java
URL: 
http://svn.apache.org/viewvc/maven/surefire/trunk/maven-surefire-report-plugin/src/main/java/org/apache/maven/plugins/surefire/report/FailsafeReportMojo.java?rev=1204901&r1=1204900&r2=1204901&view=diff
==============================================================================
--- 
maven/surefire/trunk/maven-surefire-report-plugin/src/main/java/org/apache/maven/plugins/surefire/report/FailsafeReportMojo.java
 (original)
+++ 
maven/surefire/trunk/maven-surefire-report-plugin/src/main/java/org/apache/maven/plugins/surefire/report/FailsafeReportMojo.java
 Tue Nov 22 10:35:17 2011
@@ -48,6 +48,25 @@ public class FailsafeReportMojo
      */
     private String outputName;
 
+    /**
+     * If set to true the failsafe report will be generated even when there 
are no failsafe result files.
+     * Defaults to <code>false</code> to preserve legacy behaviour pre 2.10
+     *
+     * @parameter expression="${alwaysGenerateFailsafeReport}" 
default-value="false"
+     * @noinspection UnusedDeclaration
+     * @since 2.11
+     */
+    private boolean alwaysGenerateFailsafeReport;
+
+    /**
+     * If set to true the failsafe report generation will be skipped.
+     *
+     * @parameter expression="${skipFailsafeReport}" default-value="false"
+     * @noinspection UnusedDeclaration
+     * @since 2.11
+     */
+    private boolean skipFailsafeReport;
+
     protected File getSurefireReportsDirectory( MavenProject subProject )
     {
         String buildDir = subProject.getBuild().getDirectory();
@@ -59,4 +78,13 @@ public class FailsafeReportMojo
         return outputName;
     }
 
+    protected boolean isSkipped()
+    {
+        return skipFailsafeReport;
+    }
+
+    protected boolean isGeneratedWhenNoResults()
+    {
+        return alwaysGenerateFailsafeReport;
+    }
 }

Modified: 
maven/surefire/trunk/maven-surefire-report-plugin/src/main/java/org/apache/maven/plugins/surefire/report/SurefireReportMojo.java
URL: 
http://svn.apache.org/viewvc/maven/surefire/trunk/maven-surefire-report-plugin/src/main/java/org/apache/maven/plugins/surefire/report/SurefireReportMojo.java?rev=1204901&r1=1204900&r2=1204901&view=diff
==============================================================================
--- 
maven/surefire/trunk/maven-surefire-report-plugin/src/main/java/org/apache/maven/plugins/surefire/report/SurefireReportMojo.java
 (original)
+++ 
maven/surefire/trunk/maven-surefire-report-plugin/src/main/java/org/apache/maven/plugins/surefire/report/SurefireReportMojo.java
 Tue Nov 22 10:35:17 2011
@@ -57,6 +57,25 @@ public class SurefireReportMojo
      */
     private String outputName;
 
+    /**
+     * If set to true the surefire report will be generated even when there 
are no surefire result files.
+     * Defaulyts to <code>true</code> to preserve legacy behaviour pre 2.10.
+     *
+     * @parameter expression="${alwaysGenerateSurefireReport}" 
default-value="true"
+     * @noinspection UnusedDeclaration
+     * @since 2.11
+     */
+    private boolean alwaysGenerateSurefireReport;
+
+    /**
+     * If set to true the surefire report generation will be skipped.
+     *
+     * @parameter expression="${skipSurefireReport}" default-value="false"
+     * @noinspection UnusedDeclaration
+     * @since 2.11
+     */
+    private boolean skipSurefireReport;
+
     protected File getSurefireReportsDirectory( MavenProject subProject )
     {
         String buildDir = subProject.getBuild().getDirectory();
@@ -68,4 +87,13 @@ public class SurefireReportMojo
         return outputName;
     }
 
+    protected boolean isSkipped()
+    {
+        return skipSurefireReport;
+    }
+
+    protected boolean isGeneratedWhenNoResults()
+    {
+        return alwaysGenerateSurefireReport;
+    }
 }

Modified: 
maven/surefire/trunk/maven-surefire-report-plugin/src/main/java/org/apache/maven/plugins/surefire/report/SurefireReportParser.java
URL: 
http://svn.apache.org/viewvc/maven/surefire/trunk/maven-surefire-report-plugin/src/main/java/org/apache/maven/plugins/surefire/report/SurefireReportParser.java?rev=1204901&r1=1204900&r2=1204901&view=diff
==============================================================================
--- 
maven/surefire/trunk/maven-surefire-report-plugin/src/main/java/org/apache/maven/plugins/surefire/report/SurefireReportParser.java
 (original)
+++ 
maven/surefire/trunk/maven-surefire-report-plugin/src/main/java/org/apache/maven/plugins/surefire/report/SurefireReportParser.java
 Tue Nov 22 10:35:17 2011
@@ -43,6 +43,10 @@ import org.xml.sax.SAXException;
  */
 public class SurefireReportParser
 {
+    private static final String INCLUDES = "*.xml";
+
+    private static final String EXCLUDES = "*.txt, testng-failed.xml, 
testng-failures.xml, testng-results.xml";
+
     private NumberFormat numberFormat = NumberFormat.getInstance();
 
     private List reportsDirectories;
@@ -74,8 +78,7 @@ public class SurefireReportParser
                 continue;
             }
             String[] xmlReportFiles =
-                getIncludedFiles( reportsDirectory, "*.xml",
-                                  "*.txt, testng-failed.xml, 
testng-failures.xml, testng-results.xml" );
+                getIncludedFiles( reportsDirectory, INCLUDES, EXCLUDES );
             for ( int j = 0; j < xmlReportFiles.length; j++ )
             {
                 File xmlReport = new File( reportsDirectory, xmlReportFiles[j] 
);
@@ -262,7 +265,19 @@ public class SurefireReportParser
         return failureDetailList;
     }
 
-    private String[] getIncludedFiles( File directory, String includes, String 
excludes )
+    /**
+     * Returns {@code true} if the specified directory contains at least one 
report file.
+     *
+     * @param directory the directory
+     * @return {@code true} if the specified directory contains at least one 
report file.
+     */
+    public static boolean hasReportFiles( File directory )
+    {
+        return directory != null && directory.isDirectory()
+            && getIncludedFiles( directory, INCLUDES, EXCLUDES ).length > 0;
+    }
+
+    private static String[] getIncludedFiles( File directory, String includes, 
String excludes )
     {
         DirectoryScanner scanner = new DirectoryScanner();
 


Reply via email to