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 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | 4 | public abstract class AbstractSurefireReportMojo |
26 | |
extends AbstractMavenReport |
27 | |
{ |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
private File outputDirectory; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
private Renderer siteRenderer; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
private MavenProject project; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
private boolean showSuccess; |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
private File[] reportsDirectories; |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
private File reportsDirectory; |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
private List reactorProjects; |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
private File xrefLocation; |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
private boolean linkXRef; |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
private boolean aggregate; |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
protected boolean isSkipped() |
119 | |
{ |
120 | 0 | return false; |
121 | |
} |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
protected boolean isGeneratedWhenNoResults() |
130 | |
{ |
131 | 0 | return false; |
132 | |
} |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public void executeReport( Locale locale ) |
138 | |
throws MavenReportException |
139 | |
{ |
140 | 4 | if ( isSkipped() ) |
141 | |
{ |
142 | 0 | return; |
143 | |
} |
144 | |
|
145 | 4 | final List reportsDirectoryList = getReportsDirectories(); |
146 | |
|
147 | 4 | if ( reportsDirectoryList == null ) |
148 | |
{ |
149 | 0 | return; |
150 | |
} |
151 | |
|
152 | 4 | if ( !isGeneratedWhenNoResults() ) |
153 | |
{ |
154 | 4 | boolean atLeastOneDirectoryExists = false; |
155 | 4 | for ( Iterator i = reportsDirectoryList.iterator(); i.hasNext() && !atLeastOneDirectoryExists; ) |
156 | |
{ |
157 | 4 | atLeastOneDirectoryExists = SurefireReportParser.hasReportFiles( (File) i.next() ); |
158 | |
} |
159 | 4 | if ( !atLeastOneDirectoryExists ) |
160 | |
{ |
161 | 0 | return; |
162 | |
} |
163 | |
} |
164 | |
|
165 | 4 | SurefireReportGenerator report = |
166 | |
new SurefireReportGenerator( reportsDirectoryList, locale, showSuccess, determineXrefLocation() ); |
167 | |
|
168 | 4 | report.doGenerateReport( getBundle( locale ), getSink() ); |
169 | 4 | } |
170 | |
|
171 | |
public boolean canGenerateReport() |
172 | |
{ |
173 | 8 | if ( isSkipped() ) |
174 | |
{ |
175 | 0 | return false; |
176 | |
} |
177 | |
|
178 | 8 | final List reportsDirectoryList = getReportsDirectories(); |
179 | |
|
180 | 8 | if ( reportsDirectoryList == null ) |
181 | |
{ |
182 | 0 | return false; |
183 | |
} |
184 | |
|
185 | 8 | if ( !isGeneratedWhenNoResults() ) |
186 | |
{ |
187 | 8 | boolean atLeastOneDirectoryExists = false; |
188 | 8 | for ( Iterator i = reportsDirectoryList.iterator(); i.hasNext() && !atLeastOneDirectoryExists; ) |
189 | |
{ |
190 | 8 | atLeastOneDirectoryExists = SurefireReportParser.hasReportFiles( (File) i.next() ); |
191 | |
} |
192 | 8 | if ( !atLeastOneDirectoryExists ) |
193 | |
{ |
194 | 0 | return false; |
195 | |
} |
196 | |
} |
197 | |
|
198 | 8 | return super.canGenerateReport(); |
199 | |
} |
200 | |
|
201 | |
private List getReportsDirectories() |
202 | |
{ |
203 | 12 | final List reportsDirectoryList = new ArrayList(); |
204 | |
|
205 | 12 | if ( reportsDirectories != null ) |
206 | |
{ |
207 | 0 | reportsDirectoryList.addAll( Arrays.asList( reportsDirectories ) ); |
208 | |
} |
209 | |
|
210 | 12 | if ( reportsDirectory != null ) |
211 | |
{ |
212 | |
|
213 | 12 | reportsDirectoryList.add( reportsDirectory ); |
214 | |
} |
215 | 12 | if ( aggregate ) |
216 | |
{ |
217 | 0 | if ( !project.isExecutionRoot() ) |
218 | |
{ |
219 | 0 | return null; |
220 | |
} |
221 | 0 | if ( reportsDirectories == null ) |
222 | |
{ |
223 | 0 | for ( Iterator i = getProjectsWithoutRoot().iterator(); i.hasNext(); ) |
224 | |
{ |
225 | 0 | reportsDirectoryList.add( getSurefireReportsDirectory( (MavenProject) i.next() ) ); |
226 | |
} |
227 | |
} |
228 | |
else |
229 | |
{ |
230 | |
|
231 | |
|
232 | 0 | String parentBaseDir = getProject().getBasedir().getAbsolutePath(); |
233 | 0 | for ( Iterator i = getProjectsWithoutRoot().iterator(); i.hasNext(); ) |
234 | |
{ |
235 | 0 | MavenProject subProject = (MavenProject) i.next(); |
236 | 0 | String moduleBaseDir = subProject.getBasedir().getAbsolutePath(); |
237 | 0 | for ( int d = 0; d < reportsDirectories.length; d++ ) |
238 | |
{ |
239 | 0 | String reportDir = reportsDirectories[d].getPath(); |
240 | 0 | if ( reportDir.startsWith( parentBaseDir ) ) |
241 | |
{ |
242 | 0 | reportDir = reportDir.substring( parentBaseDir.length() ); |
243 | |
} |
244 | 0 | File reportsDirectory = new File( moduleBaseDir, reportDir ); |
245 | 0 | if ( reportsDirectory.exists() && reportsDirectory.isDirectory() ) |
246 | |
{ |
247 | 0 | getLog().debug( "Adding report dir : " + moduleBaseDir + reportDir ); |
248 | 0 | reportsDirectoryList.add( reportsDirectory ); |
249 | |
} |
250 | |
} |
251 | 0 | } |
252 | 0 | } |
253 | |
} |
254 | |
else |
255 | |
{ |
256 | 12 | if ( reportsDirectoryList.size() == 0 ) |
257 | |
{ |
258 | |
|
259 | 0 | reportsDirectoryList.add( getSurefireReportsDirectory( project ) ); |
260 | |
} |
261 | |
} |
262 | 12 | return reportsDirectoryList; |
263 | |
} |
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
protected abstract File getSurefireReportsDirectory( MavenProject subProject ); |
271 | |
|
272 | |
private List getProjectsWithoutRoot() |
273 | |
{ |
274 | 0 | List result = new ArrayList(); |
275 | 0 | for ( Iterator i = reactorProjects.iterator(); i.hasNext(); ) |
276 | |
{ |
277 | 0 | MavenProject subProject = (MavenProject) i.next(); |
278 | 0 | if ( !project.equals( subProject ) ) |
279 | |
{ |
280 | 0 | result.add( subProject ); |
281 | |
} |
282 | 0 | } |
283 | 0 | return result; |
284 | |
|
285 | |
} |
286 | |
|
287 | |
private String determineXrefLocation() |
288 | |
{ |
289 | 4 | String location = null; |
290 | |
|
291 | 4 | if ( linkXRef ) |
292 | |
{ |
293 | 3 | String relativePath = PathTool.getRelativePath( getOutputDirectory(), xrefLocation.getAbsolutePath() ); |
294 | 3 | if ( StringUtils.isEmpty( relativePath ) ) |
295 | |
{ |
296 | 1 | relativePath = "."; |
297 | |
} |
298 | 3 | relativePath = relativePath + "/" + xrefLocation.getName(); |
299 | 3 | if ( xrefLocation.exists() ) |
300 | |
{ |
301 | |
|
302 | 0 | location = relativePath; |
303 | |
} |
304 | |
else |
305 | |
{ |
306 | |
|
307 | 3 | for ( Iterator reports = project.getReportPlugins().iterator(); reports.hasNext(); ) |
308 | |
{ |
309 | 2 | ReportPlugin report = (ReportPlugin) reports.next(); |
310 | |
|
311 | 2 | String artifactId = report.getArtifactId(); |
312 | 2 | if ( "maven-jxr-plugin".equals( artifactId ) || "jxr-maven-plugin".equals( artifactId ) ) |
313 | |
{ |
314 | 2 | location = relativePath; |
315 | |
} |
316 | 2 | } |
317 | |
} |
318 | |
|
319 | 3 | if ( location == null ) |
320 | |
{ |
321 | 1 | getLog().warn( "Unable to locate Test Source XRef to link to - DISABLED" ); |
322 | |
} |
323 | |
} |
324 | 4 | return location; |
325 | |
} |
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
public String getName( Locale locale ) |
331 | |
{ |
332 | 0 | return getBundle( locale ).getString( "report.surefire.name" ); |
333 | |
} |
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
public String getDescription( Locale locale ) |
339 | |
{ |
340 | 0 | return getBundle( locale ).getString( "report.surefire.description" ); |
341 | |
} |
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
protected Renderer getSiteRenderer() |
347 | |
{ |
348 | 8 | return siteRenderer; |
349 | |
} |
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
protected MavenProject getProject() |
355 | |
{ |
356 | 0 | return project; |
357 | |
} |
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
public abstract String getOutputName(); |
363 | |
|
364 | |
|
365 | |
|
366 | |
|
367 | |
protected String getOutputDirectory() |
368 | |
{ |
369 | 7 | return outputDirectory.getAbsolutePath(); |
370 | |
} |
371 | |
|
372 | |
private ResourceBundle getBundle( Locale locale ) |
373 | |
{ |
374 | 4 | return ResourceBundle.getBundle( "surefire-report", locale, this.getClass().getClassLoader() ); |
375 | |
} |
376 | |
} |