1 | |
package org.apache.maven.plugin.invoker; |
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.io.IOException; |
24 | |
import java.text.DecimalFormat; |
25 | |
import java.text.DecimalFormatSymbols; |
26 | |
import java.text.NumberFormat; |
27 | |
import java.util.ArrayList; |
28 | |
import java.util.Iterator; |
29 | |
import java.util.List; |
30 | |
import java.util.Locale; |
31 | |
|
32 | |
import org.apache.maven.doxia.sink.Sink; |
33 | |
import org.apache.maven.doxia.siterenderer.Renderer; |
34 | |
import org.apache.maven.plugin.invoker.model.BuildJob; |
35 | |
import org.apache.maven.plugin.invoker.model.io.xpp3.BuildJobXpp3Reader; |
36 | |
import org.apache.maven.project.MavenProject; |
37 | |
import org.apache.maven.reporting.AbstractMavenReport; |
38 | |
import org.apache.maven.reporting.MavenReportException; |
39 | |
import org.codehaus.plexus.i18n.I18N; |
40 | |
import org.codehaus.plexus.util.ReaderFactory; |
41 | |
import org.codehaus.plexus.util.StringUtils; |
42 | |
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 0 | public class InvokerReport |
54 | |
extends AbstractMavenReport |
55 | |
{ |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
protected MavenProject project; |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
protected Renderer siteRenderer; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
protected I18N i18n; |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
protected File outputDirectory; |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
private File reportsDirectory; |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
private NumberFormat percentFormat; |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
private NumberFormat secondsFormat; |
106 | |
|
107 | |
protected void executeReport( Locale locale ) |
108 | |
throws MavenReportException |
109 | |
{ |
110 | 0 | DecimalFormatSymbols symbols = new DecimalFormatSymbols( locale ); |
111 | 0 | percentFormat = new DecimalFormat( getText( locale, "report.invoker.format.percent" ), symbols ); |
112 | 0 | secondsFormat = new DecimalFormat( getText( locale, "report.invoker.format.seconds" ), symbols ); |
113 | |
|
114 | 0 | Sink sink = getSink(); |
115 | |
|
116 | 0 | sink.head(); |
117 | |
|
118 | 0 | sink.title(); |
119 | 0 | sink.text( getText( locale, "report.invoker.result.title" ) ); |
120 | 0 | sink.title_(); |
121 | |
|
122 | 0 | sink.head_(); |
123 | |
|
124 | 0 | sink.body(); |
125 | |
|
126 | 0 | sink.section1(); |
127 | 0 | sink.sectionTitle1(); |
128 | 0 | sink.text( getText( locale, "report.invoker.result.title" ) ); |
129 | 0 | sink.sectionTitle1_(); |
130 | 0 | sink.paragraph(); |
131 | 0 | sink.text( getText( locale, "report.invoker.result.description" ) ); |
132 | 0 | sink.paragraph_(); |
133 | 0 | sink.section1_(); |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | 0 | File[] reportFiles = ReportUtils.getReportFiles( reportsDirectory ); |
139 | 0 | if ( reportFiles.length <= 0 ) |
140 | |
{ |
141 | 0 | getLog().info( "no invoker report files found, skip report generation" ); |
142 | 0 | return; |
143 | |
} |
144 | |
|
145 | 0 | List buildJobs = new ArrayList( reportFiles.length ); |
146 | 0 | for ( int i = 0, size = reportFiles.length; i < size; i++ ) |
147 | |
{ |
148 | 0 | File reportFile = reportFiles[i]; |
149 | |
try |
150 | |
{ |
151 | 0 | BuildJobXpp3Reader reader = new BuildJobXpp3Reader(); |
152 | 0 | buildJobs.add( reader.read( ReaderFactory.newXmlReader( reportFile ) ) ); |
153 | |
} |
154 | 0 | catch ( XmlPullParserException e ) |
155 | |
{ |
156 | 0 | throw new MavenReportException( "Failed to parse report file: " + reportFile, e ); |
157 | |
} |
158 | 0 | catch ( IOException e ) |
159 | |
{ |
160 | 0 | throw new MavenReportException( "Failed to read report file: " + reportFile, e ); |
161 | 0 | } |
162 | |
} |
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | 0 | constructSummarySection( buildJobs, locale ); |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | 0 | sink.section2(); |
175 | 0 | sink.sectionTitle2(); |
176 | |
|
177 | 0 | sink.text( getText( locale, "report.invoker.detail.title" ) ); |
178 | |
|
179 | 0 | sink.sectionTitle2_(); |
180 | |
|
181 | 0 | sink.section2_(); |
182 | |
|
183 | |
|
184 | 0 | sink.table(); |
185 | |
|
186 | 0 | sink.tableRow(); |
187 | |
|
188 | |
|
189 | |
|
190 | 0 | sinkTableHeader( sink, getText( locale, "report.invoker.detail.name" ) ); |
191 | 0 | sinkTableHeader( sink, getText( locale, "report.invoker.detail.result" ) ); |
192 | 0 | sinkTableHeader( sink, getText( locale, "report.invoker.detail.time" ) ); |
193 | 0 | sinkTableHeader( sink, getText( locale, "report.invoker.detail.message" ) ); |
194 | |
|
195 | 0 | sink.tableRow_(); |
196 | |
|
197 | 0 | for ( Iterator iterator = buildJobs.iterator(); iterator.hasNext(); ) |
198 | |
{ |
199 | 0 | BuildJob buildJob = (BuildJob) iterator.next(); |
200 | 0 | renderBuildJob( buildJob, locale ); |
201 | |
} |
202 | |
|
203 | 0 | sink.table_(); |
204 | |
|
205 | 0 | sink.body_(); |
206 | |
|
207 | 0 | sink.flush(); |
208 | 0 | sink.close(); |
209 | 0 | } |
210 | |
|
211 | |
private void constructSummarySection( List buildJobs, Locale locale ) |
212 | |
{ |
213 | 0 | Sink sink = getSink(); |
214 | |
|
215 | 0 | sink.section2(); |
216 | 0 | sink.sectionTitle2(); |
217 | |
|
218 | 0 | sink.text( getText( locale, "report.invoker.summary.title" ) ); |
219 | |
|
220 | 0 | sink.sectionTitle2_(); |
221 | 0 | sink.section2_(); |
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | 0 | sink.table(); |
229 | 0 | sink.tableRow(); |
230 | |
|
231 | 0 | sinkTableHeader( sink, getText( locale, "report.invoker.summary.number" ) ); |
232 | 0 | sinkTableHeader( sink, getText( locale, "report.invoker.summary.success" ) ); |
233 | 0 | sinkTableHeader( sink, getText( locale, "report.invoker.summary.failed" ) ); |
234 | 0 | sinkTableHeader( sink, getText( locale, "report.invoker.summary.success.rate" ) ); |
235 | 0 | sinkTableHeader( sink, getText( locale, "report.invoker.summary.time.total" ) ); |
236 | 0 | sinkTableHeader( sink, getText( locale, "report.invoker.summary.time.avg" ) ); |
237 | |
|
238 | 0 | int number = buildJobs.size(); |
239 | 0 | int success = 0; |
240 | 0 | int failed = 0; |
241 | 0 | double totalTime = 0; |
242 | |
|
243 | 0 | for ( Iterator iterator = buildJobs.iterator(); iterator.hasNext(); ) |
244 | |
{ |
245 | 0 | BuildJob buildJob = (BuildJob) iterator.next(); |
246 | 0 | if ( BuildJob.Result.SUCCESS.equals( buildJob.getResult() ) ) |
247 | |
{ |
248 | 0 | success++; |
249 | |
} |
250 | 0 | else if ( !BuildJob.Result.SKIPPED.equals( buildJob.getResult() ) ) |
251 | |
{ |
252 | 0 | failed++; |
253 | |
} |
254 | 0 | totalTime += buildJob.getTime(); |
255 | |
} |
256 | |
|
257 | 0 | sink.tableRow_(); |
258 | 0 | sink.tableRow(); |
259 | |
|
260 | 0 | sinkCell( sink, Integer.toString( number ) ); |
261 | 0 | sinkCell( sink, Integer.toString( success ) ); |
262 | 0 | sinkCell( sink, Integer.toString( failed ) ); |
263 | |
|
264 | 0 | if ( success + failed > 0 ) |
265 | |
{ |
266 | 0 | sinkCell( sink, percentFormat.format( (double) success / ( success + failed ) ) ); |
267 | |
} |
268 | |
else |
269 | |
{ |
270 | 0 | sinkCell( sink, "" ); |
271 | |
} |
272 | |
|
273 | 0 | sinkCell( sink, secondsFormat.format( totalTime ) ); |
274 | |
|
275 | 0 | sinkCell( sink, secondsFormat.format( totalTime / number ) ); |
276 | |
|
277 | 0 | sink.tableRow_(); |
278 | 0 | sink.table_(); |
279 | |
|
280 | 0 | } |
281 | |
|
282 | |
private void renderBuildJob( BuildJob buildJob, Locale locale ) |
283 | |
{ |
284 | 0 | Sink sink = getSink(); |
285 | 0 | sink.tableRow(); |
286 | 0 | StringBuffer buffer = new StringBuffer(); |
287 | 0 | if ( !StringUtils.isEmpty( buildJob.getName() ) && !StringUtils.isEmpty( buildJob.getDescription() ) ) |
288 | |
{ |
289 | 0 | buffer.append( buildJob.getName() ); |
290 | 0 | buffer.append( " : " ); |
291 | 0 | buffer.append( buildJob.getDescription() ); |
292 | |
} |
293 | |
else |
294 | |
{ |
295 | 0 | buffer.append( buildJob.getProject() ); |
296 | |
} |
297 | 0 | sinkCell( sink, buffer.toString() ); |
298 | |
|
299 | 0 | sinkCell( sink, buildJob.getResult() ); |
300 | 0 | sinkCell( sink, secondsFormat.format( buildJob.getTime() ) ); |
301 | 0 | sinkCell( sink, buildJob.getFailureMessage() ); |
302 | 0 | sink.tableRow_(); |
303 | 0 | } |
304 | |
|
305 | |
protected String getOutputDirectory() |
306 | |
{ |
307 | 0 | return outputDirectory.getAbsolutePath(); |
308 | |
} |
309 | |
|
310 | |
protected MavenProject getProject() |
311 | |
{ |
312 | 0 | return project; |
313 | |
} |
314 | |
|
315 | |
protected Renderer getSiteRenderer() |
316 | |
{ |
317 | 0 | return siteRenderer; |
318 | |
} |
319 | |
|
320 | |
public String getDescription( Locale locale ) |
321 | |
{ |
322 | 0 | return getText( locale, "report.invoker.result.description" ); |
323 | |
} |
324 | |
|
325 | |
public String getName( Locale locale ) |
326 | |
{ |
327 | 0 | return getText( locale, "report.invoker.result.name" ); |
328 | |
} |
329 | |
|
330 | |
public String getOutputName() |
331 | |
{ |
332 | 0 | return "invoker-report"; |
333 | |
} |
334 | |
|
335 | |
public boolean canGenerateReport() |
336 | |
{ |
337 | 0 | return ReportUtils.getReportFiles( reportsDirectory ).length > 0; |
338 | |
} |
339 | |
|
340 | |
private String getText( Locale locale, String key ) |
341 | |
{ |
342 | 0 | return i18n.getString( "invoker-report", locale, key ); |
343 | |
} |
344 | |
|
345 | |
private void sinkTableHeader( Sink sink, String header ) |
346 | |
{ |
347 | 0 | sink.tableHeaderCell(); |
348 | 0 | sink.text( header ); |
349 | 0 | sink.tableHeaderCell_(); |
350 | 0 | } |
351 | |
|
352 | |
private void sinkCell( Sink sink, String text ) |
353 | |
{ |
354 | 0 | sink.tableCell(); |
355 | 0 | sink.text( text ); |
356 | 0 | sink.tableCell_(); |
357 | 0 | } |
358 | |
|
359 | |
} |