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