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