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