Coverage Report - org.apache.maven.plugins.surefire.report.SurefireReportMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
SurefireReportMojo
47%
30/64
33%
14/42
2.818
 
 1  
 package org.apache.maven.plugins.surefire.report;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *     http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.File;
 23  
 import java.util.ArrayList;
 24  
 import java.util.Arrays;
 25  
 import java.util.Iterator;
 26  
 import java.util.List;
 27  
 import java.util.Locale;
 28  
 import java.util.ResourceBundle;
 29  
 
 30  
 import org.apache.maven.doxia.siterenderer.Renderer;
 31  
 import org.apache.maven.model.ReportPlugin;
 32  
 import org.apache.maven.project.MavenProject;
 33  
 import org.apache.maven.reporting.AbstractMavenReport;
 34  
 import org.apache.maven.reporting.MavenReportException;
 35  
 import org.codehaus.plexus.util.PathTool;
 36  
 import org.codehaus.plexus.util.StringUtils;
 37  
 
 38  
 
 39  
 /**
 40  
  * Creates a nicely formatted Surefire Test Report in html format.
 41  
  *
 42  
  * @author <a href="mailto:jruiz@exist.com">Johnny R. Ruiz III</a>
 43  
  * @version $Id: SurefireReportMojo.java 1098389 2011-05-01 19:13:32Z krosenvold $
 44  
  * @goal report
 45  
  * @execute phase="test" lifecycle="surefire"
 46  
  */
 47  4
 public class SurefireReportMojo
 48  
     extends AbstractMavenReport
 49  
 {
 50  
     /**
 51  
      * Location where generated html will be created.
 52  
      *
 53  
      * @parameter expression="${project.reporting.outputDirectory}"
 54  
      * @noinspection UnusedDeclaration
 55  
      */
 56  
     private File outputDirectory;
 57  
 
 58  
     /**
 59  
      * Doxia Site Renderer
 60  
      *
 61  
      * @component
 62  
      * @noinspection UnusedDeclaration
 63  
      */
 64  
     private Renderer siteRenderer;
 65  
 
 66  
     /**
 67  
      * Maven Project
 68  
      *
 69  
      * @parameter expression="${project}"
 70  
      * @required @readonly
 71  
      * @noinspection UnusedDeclaration
 72  
      */
 73  
     private MavenProject project;
 74  
 
 75  
     /**
 76  
      * If set to false, only failures are shown.
 77  
      *
 78  
      * @parameter expression="${showSuccess}" default-value="true"
 79  
      * @required
 80  
      * @noinspection UnusedDeclaration
 81  
      */
 82  
     private boolean showSuccess;
 83  
 
 84  
     /**
 85  
      * Directories containing the XML Report files that will be parsed and rendered to HTML format.
 86  
      *
 87  
      * @parameter
 88  
      * @noinspection UnusedDeclaration
 89  
      */
 90  
     private File[] reportsDirectories;
 91  
 
 92  
     /**
 93  
      * (Deprecated, use reportsDirectories) This directory contains the XML Report files that will be parsed and rendered to HTML format.
 94  
      *
 95  
      * @parameter
 96  
      * @deprecated
 97  
      * @noinspection UnusedDeclaration
 98  
      */
 99  
     private File reportsDirectory;
 100  
 
 101  
 
 102  
     /**
 103  
      * The projects in the reactor for aggregation report.
 104  
      *
 105  
      * @parameter expression="${reactorProjects}"
 106  
      * @readonly
 107  
      * @noinspection MismatchedQueryAndUpdateOfCollection, UnusedDeclaration
 108  
      */
 109  
     private List reactorProjects;
 110  
 
 111  
     /**
 112  
      * The filename to use for the report.
 113  
      *
 114  
      * @parameter expression="${outputName}" default-value="surefire-report"
 115  
      * @required
 116  
      * @noinspection UnusedDeclaration
 117  
      */
 118  
     private String outputName;
 119  
 
 120  
     /**
 121  
      * Location of the Xrefs to link.
 122  
      *
 123  
      * @parameter default-value="${project.reporting.outputDirectory}/xref-test"
 124  
      * @noinspection UnusedDeclaration
 125  
      */
 126  
     private File xrefLocation;
 127  
 
 128  
     /**
 129  
      * Whether to link the XRef if found.
 130  
      *
 131  
      * @parameter expression="${linkXRef}" default-value="true"
 132  
      * @noinspection UnusedDeclaration
 133  
      */
 134  
     private boolean linkXRef;
 135  
 
 136  
     /**
 137  
      * Whether to build an aggregated report at the root, or build individual reports.
 138  
      *
 139  
      * @parameter expression="${aggregate}" default-value="false"
 140  
      * @noinspection UnusedDeclaration
 141  
      */
 142  
     private boolean aggregate;
 143  
 
 144  
     /**
 145  
      * {@inheritDoc}
 146  
      */
 147  
     public void executeReport( Locale locale )
 148  
         throws MavenReportException
 149  
     {
 150  4
         final List reportsDirectoryList = new ArrayList();
 151  
 
 152  4
         if ( reportsDirectories != null )
 153  
         {
 154  0
             reportsDirectoryList.addAll( Arrays.asList( reportsDirectories ) );
 155  
         }
 156  
         //noinspection deprecation
 157  4
         if ( reportsDirectory != null )
 158  
         {
 159  
             //noinspection deprecation
 160  4
             reportsDirectoryList.add( reportsDirectory );
 161  
         }
 162  4
         if ( aggregate )
 163  
         {
 164  0
             if ( !project.isExecutionRoot() )
 165  
             {
 166  0
                 return;
 167  
             }
 168  0
             if ( reportsDirectories == null )
 169  
             {
 170  0
                 for ( Iterator i = getProjectsWithoutRoot().iterator(); i.hasNext(); )
 171  
                 {
 172  0
                     reportsDirectoryList.add( getSurefireReportsDirectory( (MavenProject) i.next() ) );
 173  
                 }
 174  
             }
 175  
             else
 176  
             {
 177  
                 // Multiple report directories are configured.
 178  
                 // Let's see if those directories exist in each sub-module to fix SUREFIRE-570
 179  0
                 String parentBaseDir = getProject().getBasedir().getAbsolutePath();
 180  0
                 for ( Iterator i = getProjectsWithoutRoot().iterator(); i.hasNext(); )
 181  
                 {
 182  0
                     MavenProject subProject = (MavenProject) i.next();
 183  0
                     String moduleBaseDir = subProject.getBasedir().getAbsolutePath();
 184  0
                     for ( int d = 0; d < reportsDirectories.length; d++ )
 185  
                     {
 186  0
                         String reportDir = reportsDirectories[d].getPath();
 187  0
                         if ( reportDir.startsWith( parentBaseDir ) )
 188  
                         {
 189  0
                             reportDir = reportDir.substring( parentBaseDir.length() );
 190  
                         }
 191  0
                         File reportsDirectory = new File( moduleBaseDir, reportDir );
 192  0
                         if ( reportsDirectory.exists() && reportsDirectory.isDirectory() )
 193  
                         {
 194  0
                             getLog().debug( "Adding report dir : " + moduleBaseDir + reportDir );
 195  0
                             reportsDirectoryList.add( reportsDirectory );
 196  
                         }
 197  
                     }
 198  0
                 }
 199  0
             }
 200  
         }
 201  
         else
 202  
         {
 203  4
             if ( reportsDirectoryList.size() == 0 )
 204  
             {
 205  
 
 206  0
                 reportsDirectoryList.add( getSurefireReportsDirectory( project ) );
 207  
             }
 208  
         }
 209  
 
 210  4
         SurefireReportGenerator report =
 211  
             new SurefireReportGenerator( reportsDirectoryList, locale, showSuccess, determineXrefLocation() );
 212  
 
 213  4
         report.doGenerateReport( getBundle( locale ), getSink() );
 214  4
     }
 215  
 
 216  
     private File getSurefireReportsDirectory( MavenProject subProject )
 217  
     {
 218  0
         String buildDir = subProject.getBuild().getDirectory();
 219  0
         return new File( buildDir + "/surefire-reports" );
 220  
     }
 221  
 
 222  
     private List getProjectsWithoutRoot()
 223  
     {
 224  0
         List result = new ArrayList();
 225  0
         for ( Iterator i = reactorProjects.iterator(); i.hasNext(); )
 226  
         {
 227  0
             MavenProject subProject = (MavenProject) i.next();
 228  0
             if ( !project.equals( subProject ) )
 229  
             {
 230  0
                 result.add( subProject );
 231  
             }
 232  0
         }
 233  0
         return result;
 234  
 
 235  
     }
 236  
 
 237  
     private String determineXrefLocation()
 238  
     {
 239  4
         String location = null;
 240  
 
 241  4
         if ( linkXRef )
 242  
         {
 243  3
             String relativePath = PathTool.getRelativePath( getOutputDirectory(), xrefLocation.getAbsolutePath() );
 244  3
             if ( StringUtils.isEmpty( relativePath ) )
 245  
             {
 246  1
                 relativePath = ".";
 247  
             }
 248  3
             relativePath = relativePath + "/" + xrefLocation.getName();
 249  3
             if ( xrefLocation.exists() )
 250  
             {
 251  
                 // XRef was already generated by manual execution of a lifecycle binding
 252  0
                 location = relativePath;
 253  
             }
 254  
             else
 255  
             {
 256  
                 // Not yet generated - check if the report is on its way
 257  3
                 for ( Iterator reports = project.getReportPlugins().iterator(); reports.hasNext(); )
 258  
                 {
 259  2
                     ReportPlugin report = (ReportPlugin) reports.next();
 260  
 
 261  2
                     String artifactId = report.getArtifactId();
 262  2
                     if ( "maven-jxr-plugin".equals( artifactId ) || "jxr-maven-plugin".equals( artifactId ) )
 263  
                     {
 264  2
                         location = relativePath;
 265  
                     }
 266  2
                 }
 267  
             }
 268  
 
 269  3
             if ( location == null )
 270  
             {
 271  1
                 getLog().warn( "Unable to locate Test Source XRef to link to - DISABLED" );
 272  
             }
 273  
         }
 274  4
         return location;
 275  
     }
 276  
 
 277  
     /**
 278  
      * {@inheritDoc}
 279  
      */
 280  
     public String getName( Locale locale )
 281  
     {
 282  0
         return getBundle( locale ).getString( "report.surefire.name" );
 283  
     }
 284  
 
 285  
     /**
 286  
      * {@inheritDoc}
 287  
      */
 288  
     public String getDescription( Locale locale )
 289  
     {
 290  0
         return getBundle( locale ).getString( "report.surefire.description" );
 291  
     }
 292  
 
 293  
     /**
 294  
      * {@inheritDoc}
 295  
      */
 296  
     protected Renderer getSiteRenderer()
 297  
     {
 298  8
         return siteRenderer;
 299  
     }
 300  
 
 301  
     /**
 302  
      * {@inheritDoc}
 303  
      */
 304  
     protected MavenProject getProject()
 305  
     {
 306  0
         return project;
 307  
     }
 308  
 
 309  
     /**
 310  
      * {@inheritDoc}
 311  
      */
 312  
     public String getOutputName()
 313  
     {
 314  4
         return outputName;
 315  
     }
 316  
 
 317  
     /**
 318  
      * {@inheritDoc}
 319  
      */
 320  
     protected String getOutputDirectory()
 321  
     {
 322  7
         return outputDirectory.getAbsolutePath();
 323  
     }
 324  
 
 325  
     private ResourceBundle getBundle( Locale locale )
 326  
     {
 327  4
         return ResourceBundle.getBundle( "surefire-report", locale, this.getClass().getClassLoader() );
 328  
     }
 329  
 }