Coverage Report - org.apache.maven.plugins.surefire.report.SurefireReportMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
SurefireReportMojo
0%
0/50
0%
0/30
2.667
 
 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.Iterator;
 25  
 import java.util.List;
 26  
 import java.util.Locale;
 27  
 import java.util.ResourceBundle;
 28  
 
 29  
 import org.apache.maven.doxia.siterenderer.Renderer;
 30  
 import org.apache.maven.model.ReportPlugin;
 31  
 import org.apache.maven.project.MavenProject;
 32  
 import org.apache.maven.reporting.AbstractMavenReport;
 33  
 import org.apache.maven.reporting.MavenReportException;
 34  
 import org.codehaus.plexus.util.PathTool;
 35  
 import org.codehaus.plexus.util.StringUtils;
 36  
 
 37  
 
 38  
 /**
 39  
  * Creates a nicely formatted Surefire Test Report in html format.
 40  
  *
 41  
  * @author <a href="mailto:jruiz@exist.com">Johnny R. Ruiz III</a>
 42  
  * @version $Id: SurefireReportMojo.java 981261 2010-08-01 16:26:10Z bentmann $
 43  
  * @goal report
 44  
  * @execute phase="test" lifecycle="surefire"
 45  
  */
 46  0
 public class SurefireReportMojo
 47  
     extends AbstractMavenReport
 48  
 {
 49  
     /**
 50  
      * Location where generated html will be created.
 51  
      *
 52  
      * @parameter expression="${project.reporting.outputDirectory}"
 53  
      */
 54  
     private File outputDirectory;
 55  
 
 56  
     /**
 57  
      * Doxia Site Renderer
 58  
      *
 59  
      * @component
 60  
      */
 61  
     private Renderer siteRenderer;
 62  
 
 63  
     /**
 64  
      * Maven Project
 65  
      *
 66  
      * @parameter expression="${project}"
 67  
      * @required @readonly
 68  
      */
 69  
     private MavenProject project;
 70  
 
 71  
     /**
 72  
      * If set to false, only failures are shown.
 73  
      *
 74  
      * @parameter expression="${showSuccess}" default-value="true"
 75  
      * @required
 76  
      */
 77  
     private boolean showSuccess;
 78  
 
 79  
     /**
 80  
      * Directories containing the XML Report files that will be parsed and rendered to HTML format.
 81  
      *
 82  
      * @parameter
 83  
      */
 84  
     private File[] reportsDirectories;
 85  
 
 86  
     /**
 87  
      * (Deprecated, use reportsDirectories) This directory contains the XML Report files that will be parsed and rendered to HTML format.
 88  
      *
 89  
      * @deprecated
 90  
      * @parameter
 91  
      */
 92  
     private File reportsDirectory;
 93  
 
 94  
 
 95  
     /**
 96  
      * The projects in the reactor for aggregation report.
 97  
      *
 98  
      * @parameter expression="${reactorProjects}"
 99  
      * @readonly
 100  
      */
 101  
     private List reactorProjects;
 102  
 
 103  
     /**
 104  
      * The filename to use for the report.
 105  
      *
 106  
      * @parameter expression="${outputName}" default-value="surefire-report"
 107  
      * @required
 108  
      */
 109  
     private String outputName;
 110  
 
 111  
     /**
 112  
      * Location of the Xrefs to link.
 113  
      *
 114  
      * @parameter default-value="${project.reporting.outputDirectory}/xref-test"
 115  
      */
 116  
     private File xrefLocation;
 117  
 
 118  
     /**
 119  
      * Whether to link the XRef if found.
 120  
      *
 121  
      * @parameter expression="${linkXRef}" default-value="true"
 122  
      */
 123  
     private boolean linkXRef;
 124  
 
 125  
     /**
 126  
      * Whether to build an aggregated report at the root, or build individual reports.
 127  
      *
 128  
      * @parameter expression="${aggregate}" default-value="false"
 129  
      */
 130  
     private boolean aggregate;
 131  
 
 132  
     /** {@inheritDoc} */
 133  
     public void executeReport( Locale locale )
 134  
         throws MavenReportException
 135  
     {
 136  0
         if ( reportsDirectory != null )
 137  
         {
 138  0
             if ( reportsDirectories == null )
 139  
             {
 140  0
                 reportsDirectories = new File[] { reportsDirectory };
 141  
             }
 142  
             else
 143  
             {
 144  0
                 File[] oldReports = reportsDirectories;
 145  0
                 reportsDirectories = new File[oldReports.length+1];
 146  0
                 System.arraycopy( oldReports, 0, reportsDirectories, 0, oldReports.length );
 147  0
                 reportsDirectories[oldReports.length] = reportsDirectory;
 148  
             }
 149  
         }
 150  0
         if ( aggregate )
 151  
         {
 152  0
             if ( !project.isExecutionRoot() ) return;
 153  0
             if ( reportsDirectories == null )
 154  
             {
 155  0
                 ArrayList reportsDirectoryList = new ArrayList();
 156  
                 // TODO guess the real location
 157  0
                 for (Iterator i = reactorProjects.iterator(); i.hasNext();)
 158  
                 {
 159  0
                     MavenProject subProject = (MavenProject) i.next();
 160  0
                     if ( project.equals( subProject ) ) continue;
 161  0
                     String buildDir = subProject.getBuild().getDirectory();
 162  0
                     File reportsDirectory = new File( buildDir + "/surefire-reports" );
 163  0
                     reportsDirectoryList.add( reportsDirectory );
 164  0
                 }
 165  0
                 reportsDirectories = (File[]) reportsDirectoryList.toArray( new File[0] );
 166  0
             }
 167  
         }
 168  
         else
 169  
         {
 170  0
             if ( reportsDirectories == null )
 171  
             {
 172  0
                 reportsDirectories = new File[] { new File( project.getBuild().getDirectory() + "/surefire-reports" ) };
 173  
             }
 174  
         }
 175  
 
 176  
 
 177  0
         SurefireReportGenerator report =
 178  
             new SurefireReportGenerator( reportsDirectories, locale, showSuccess, determineXrefLocation() );
 179  
 
 180  0
         report.doGenerateReport( getBundle( locale ), getSink() );
 181  0
     }
 182  
 
 183  
     private String determineXrefLocation()
 184  
     {
 185  0
         String location = null;
 186  
 
 187  0
         if ( linkXRef )
 188  
         {
 189  0
             String relativePath = PathTool.getRelativePath( getOutputDirectory(), xrefLocation.getAbsolutePath() );
 190  0
             if ( StringUtils.isEmpty( relativePath ) )
 191  
             {
 192  0
                 relativePath = ".";
 193  
             }
 194  0
             relativePath = relativePath + "/" + xrefLocation.getName();
 195  0
             if ( xrefLocation.exists() )
 196  
             {
 197  
                 // XRef was already generated by manual execution of a lifecycle binding
 198  0
                 location = relativePath;
 199  
             }
 200  
             else
 201  
             {
 202  
                 // Not yet generated - check if the report is on its way
 203  0
                 for ( Iterator reports = project.getReportPlugins().iterator(); reports.hasNext(); )
 204  
                 {
 205  0
                     ReportPlugin report = (ReportPlugin) reports.next();
 206  
 
 207  0
                     String artifactId = report.getArtifactId();
 208  0
                     if ( "maven-jxr-plugin".equals( artifactId ) || "jxr-maven-plugin".equals( artifactId ) )
 209  
                     {
 210  0
                         location = relativePath;
 211  
                     }
 212  0
                 }
 213  
             }
 214  
 
 215  0
             if ( location == null )
 216  
             {
 217  0
                 getLog().warn( "Unable to locate Test Source XRef to link to - DISABLED" );
 218  
             }
 219  
         }
 220  0
         return location;
 221  
     }
 222  
 
 223  
     /** {@inheritDoc} */
 224  
     public String getName( Locale locale )
 225  
     {
 226  0
         return getBundle( locale ).getString( "report.surefire.name" );
 227  
     }
 228  
 
 229  
     /** {@inheritDoc} */
 230  
     public String getDescription( Locale locale )
 231  
     {
 232  0
         return getBundle( locale ).getString( "report.surefire.description" );
 233  
     }
 234  
 
 235  
     /** {@inheritDoc} */
 236  
     protected Renderer getSiteRenderer()
 237  
     {
 238  0
         return siteRenderer;
 239  
     }
 240  
 
 241  
     /** {@inheritDoc} */
 242  
     protected MavenProject getProject()
 243  
     {
 244  0
         return project;
 245  
     }
 246  
 
 247  
     /** {@inheritDoc} */
 248  
     public String getOutputName()
 249  
     {
 250  0
         return outputName;
 251  
     }
 252  
 
 253  
     /** {@inheritDoc} */
 254  
     protected String getOutputDirectory()
 255  
     {
 256  0
         return outputDirectory.getAbsolutePath();
 257  
     }
 258  
 
 259  
     private ResourceBundle getBundle( Locale locale )
 260  
     {
 261  0
         return ResourceBundle.getBundle( "surefire-report", locale, this.getClass().getClassLoader() );
 262  
     }
 263  
 }