View Javadoc

1   package org.apache.maven.plugins.surefire.report;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.apache.maven.artifact.handler.ArtifactHandler;
20  import org.apache.maven.model.ReportPlugin;
21  import org.apache.maven.project.MavenProject;
22  import org.apache.maven.reporting.AbstractMavenReport;
23  import org.apache.maven.reporting.MavenReportException;
24  import org.codehaus.doxia.site.renderer.SiteRenderer;
25  import org.codehaus.plexus.util.PathTool;
26  import org.codehaus.plexus.util.StringUtils;
27  
28  import java.io.File;
29  import java.util.Iterator;
30  import java.util.Locale;
31  import java.util.ResourceBundle;
32  
33  
34  /**
35   * Creates a nicely formatted Surefire Test Report in html format.
36   *
37   * @author <a href="mailto:jruiz@exist.com">Johnny R. Ruiz III</a>
38   * @version $Id: SurefireReportMojo.java 433277 2006-08-21 16:28:06Z vsiveton $
39   * @goal report
40   * @execute phase="test" lifecycle="surefire"
41   */
42  public class SurefireReportMojo
43      extends AbstractMavenReport
44  {
45      /**
46       * Location where generated html will be created.
47       *
48       * @parameter expression="${project.reporting.outputDirectory}"
49       */
50      private String outputDirectory;
51  
52      /**
53       * Doxia Site Renderer
54       *
55       * @parameter expression="${component.org.codehaus.doxia.site.renderer.SiteRenderer}"
56       * @required @readonly
57       */
58      private SiteRenderer siteRenderer;
59  
60      /**
61       * Maven Project
62       *
63       * @parameter expression="${project}"
64       * @required @readonly
65       */
66      private MavenProject project;
67  
68      /**
69       * If set to false, only failures are shown.
70       *
71       * @parameter expression="${showSuccess}" default-value="true"
72       * @required
73       */
74      private boolean showSuccess;
75  
76      /**
77       * This directory contains the XML Report files that will be parsed and rendered to HTML format.
78       *
79       * @parameter expression="${project.build.directory}/surefire-reports"
80       * @required
81       */
82      private File reportsDirectory;
83  
84      /**
85       * The filename to use for the report.
86       *
87       * @parameter expression="${outputName}" default-value="surefire-report"
88       * @required
89       */
90      private String outputName;
91  
92      /**
93       * Location of the Xrefs to link.
94       *
95       * @parameter default-value="${project.reporting.outputDirectory}/xref-test"
96       */
97      private File xrefLocation;
98  
99      /**
100      * Whether to link the XRef if found.
101      *
102      * @parameter expression="${linkXRef}" default-value="true"
103      */
104     private boolean linkXRef;
105 
106     public void executeReport( Locale locale )
107         throws MavenReportException
108     {
109         SurefireReportGenerator report =
110             new SurefireReportGenerator( reportsDirectory, locale, showSuccess, determineXrefLocation() );
111 
112         report.doGenerateReport( getBundle( locale ), getSink() );
113     }
114 
115     private String determineXrefLocation()
116     {
117         String location = null;
118 
119         if ( linkXRef )
120         {
121             String relativePath = PathTool.getRelativePath( outputDirectory, xrefLocation.getAbsolutePath() );
122             if ( StringUtils.isEmpty( relativePath ) )
123             {
124                 relativePath = ".";
125             }
126             relativePath = relativePath + "/" + xrefLocation.getName();
127             if ( xrefLocation.exists() )
128             {
129                 // XRef was already generated by manual execution of a lifecycle binding
130                 location = relativePath;
131             }
132             else
133             {
134                 // Not yet generated - check if the report is on its way
135                 for ( Iterator reports = project.getReportPlugins().iterator(); reports.hasNext(); )
136                 {
137                     ReportPlugin report = (ReportPlugin) reports.next();
138 
139                     String artifactId = report.getArtifactId();
140                     if ( "maven-jxr-plugin".equals( artifactId ) || "jxr-maven-plugin".equals( artifactId ) )
141                     {
142                         location = relativePath;
143                     }
144                 }
145             }
146 
147             if ( location == null )
148             {
149                 getLog().warn( "Unable to locate Test Source XRef to link to - DISABLED" );
150             }
151         }
152         return location;
153     }
154 
155     public String getName( Locale locale )
156     {
157         return getBundle( locale ).getString( "report.surefire.name" );
158     }
159 
160     public String getDescription( Locale locale )
161     {
162         return getBundle( locale ).getString( "report.surefire.description" );
163     }
164 
165     protected SiteRenderer getSiteRenderer()
166     {
167         return siteRenderer;
168     }
169 
170     protected MavenProject getProject()
171     {
172         return project;
173     }
174 
175     public String getOutputName()
176     {
177         return outputName;
178     }
179 
180     protected String getOutputDirectory()
181     {
182         return outputDirectory;
183     }
184 
185     private ResourceBundle getBundle( Locale locale )
186     {
187         return ResourceBundle.getBundle( "surefire-report", locale, this.getClass().getClassLoader() );
188     }
189 
190     /**
191      * @see org.apache.maven.reporting.AbstractMavenReport#canGenerateReport()
192      */
193     public boolean canGenerateReport()
194     {
195         // Only execute reports for java projects
196         ArtifactHandler artifactHandler = this.project.getArtifact().getArtifactHandler();
197         return "java".equals( artifactHandler.getLanguage() );
198     }
199 }