View Javadoc

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