1 package org.apache.maven.plugins.surefire.report;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
130 location = relativePath;
131 }
132 else
133 {
134
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
196 ArtifactHandler artifactHandler = this.project.getArtifact().getArtifactHandler();
197 return "java".equals( artifactHandler.getLanguage() );
198 }
199 }