/
SuiteHTMLReporter.java
733 lines (635 loc) · 26.7 KB
/
SuiteHTMLReporter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
package org.testng.reporters;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestClass;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.Reporter;
import org.testng.internal.Utils;
import org.testng.xml.XmlSuite;
/**
* This class implements an HTML reporter for suites.
*
* @author cbeust
* @author <a href='mailto:the_mindstorm@evolva.ro'>Alexandru Popescu</a>
*/
public class SuiteHTMLReporter implements IReporter {
public static final String METHODS_CHRONOLOGICAL = "methods.html";
public static final String METHODS_ALPHABETICAL = "methods-alphabetical.html";
public static final String GROUPS = "groups.html";
public static final String CLASSES = "classes.html";
public static final String REPORTER_OUTPUT = "reporter-output.html";
public static final String METHODS_NOT_RUN = "methods-not-run.html";
public static final String TESTNG_XML = "testng.xml.html";
private Map<String, ITestClass> m_classes = new HashMap<String, ITestClass>();
private String m_outputDirectory;
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
m_outputDirectory = outputDirectory;
try {
HtmlHelper.generateStylesheet(outputDirectory);
} catch (IOException e) {
// TODO Propagate the exception properly.
e.printStackTrace();
}
for (int i = 0; i < xmlSuites.size(); i++) {
//
// Generate the various reports
//
XmlSuite xmlSuite = xmlSuites.get(i);
if (xmlSuite.getTests().size() == 0) continue;
ISuite suite = suites.get(i);
generateTableOfContents(xmlSuite, suite);
generateSuites(xmlSuite, suite);
generateIndex(xmlSuite, suite);
generateMain(xmlSuite, suite);
generateMethodsAndGroups(xmlSuite, suite);
generateMethodsChronologically(xmlSuite, suite, METHODS_CHRONOLOGICAL, false);
generateMethodsChronologically(xmlSuite, suite, METHODS_ALPHABETICAL, true);
generateClasses(xmlSuite, suite);
generateReporterOutput(xmlSuite, suite);
generateExcludedMethodsReport(xmlSuite, suite);
generateXmlFile(xmlSuite, suite);
}
generateIndex(suites);
}
private void generateXmlFile(XmlSuite xmlSuite, ISuite suite) {
String content =
new String(xmlSuite.toXml().replaceAll("<", "<").replaceAll(">", ">"))
.replaceAll(" ", " ").replaceAll("\n", "<br/>");
StringBuffer sb = new StringBuffer("<html>");
sb.append("<head><title>").append("testng.xml for ")
.append(xmlSuite.getName()).append("</title></head><body><tt>")
.append(content)
.append("</tt></body></html>");
Utils.writeFile(getOutputDirectory(xmlSuite), TESTNG_XML, sb);
}
/**
* Generate the main index.html file that lists all the suites
* and their result
*/
private void generateIndex(List<ISuite> suites) {
StringBuffer sb = new StringBuffer();
String title = "Test results";
sb.append("<html>\n<head><title>" + title + "</title>")
.append(HtmlHelper.getCssString("."))
.append("</head><body>\n")
.append("<h2><p align='center'>").append(title).append("</p></h2>\n")
.append("<table border='1' width='100%' class='main-page'>")
.append("<tr><th>Suite</th><th>Passed</th><th>Failed</th><th>Skipped</th><th>testng.xml</th></tr>\n");
int totalFailedTests = 0;
int totalPassedTests = 0;
int totalSkippedTests = 0;
StringBuffer suiteBuf= new StringBuffer();
for (ISuite suite : suites) {
if (suite.getResults().size() == 0) continue;
String name = suite.getName();
int failedTests= 0;
int passedTests= 0;
int skippedTests= 0;
Map<String, ISuiteResult> results = suite.getResults();
for (String suiteName : results.keySet()) {
ISuiteResult result = results.get(suiteName);
ITestContext context = result.getTestContext();
failedTests += context.getFailedTests().size();
totalFailedTests += context.getFailedTests().size();
passedTests += context.getPassedTests().size();
totalPassedTests += context.getPassedTests().size();
skippedTests += context.getSkippedTests().size();
totalSkippedTests += context.getSkippedTests().size();
}
String cls = failedTests > 0 ? "invocation-failed"
: (passedTests > 0 ? "invocation-passed" : "invocation-failed");
suiteBuf.append("<tr align='center' class='").append(cls).append("'>")
.append("<td><a href='").append(name).append("/index.html'>")
.append(name).append("</a></td>\n");
suiteBuf.append("<td>" + passedTests + "</td>")
.append("<td>" + failedTests + "</td>")
.append("<td>" + skippedTests + "</td>")
.append("<td><a href='").append(name).append("/").append(TESTNG_XML).append("'>Link").append("</a></td>")
.append("</tr>");
}
String cls= totalFailedTests > 0 ? "invocation-failed"
: (totalPassedTests > 0 ? "invocation-passed" : "invocation-failed");
sb.append("<tr align='center' class='").append(cls).append("'>")
.append("<td><em>Total</em></td>")
.append("<td><em>").append(totalPassedTests).append("</em></td>")
.append("<td><em>").append(totalFailedTests).append("</em></td>")
.append("<td><em>").append(totalSkippedTests).append("</em></td>")
.append("<td> </td>")
.append("</tr>\n");
sb.append(suiteBuf);
sb.append("</table>").append("</body></html>\n");
Utils.writeFile(m_outputDirectory, "index.html", sb.toString());
}
private void generateExcludedMethodsReport(XmlSuite xmlSuite, ISuite suite) {
Collection<ITestNGMethod> excluded = suite.getExcludedMethods();
StringBuffer sb2 = new StringBuffer("<h2>Methods that were not run</h2><table>\n");
for (ITestNGMethod method : excluded) {
Method m = method.getMethod();
if (m != null) {
sb2.append("<tr><td>")
.append(m.getDeclaringClass().getName() + "." + m.getName());
if(null != method.getDescription() && !"".equals(method.getDescription())) {
sb2.append("<br/>").append(SP2).append("<i>").append(method.getDescription()).append("</i>");
}
sb2.append("</td></tr>\n");
}
}
sb2.append("</table>");
Utils.writeFile(getOutputDirectory(xmlSuite), METHODS_NOT_RUN, sb2);
}
private void generateReporterOutput(XmlSuite xmlSuite, ISuite suite) {
StringBuffer sb = new StringBuffer();
List<ITestNGMethod> methodsNotRun = new ArrayList<ITestNGMethod>();
//
// Reporter output
//
sb.append("<h2>Reporter output</h2>")
.append("<table>");
List<String> output = Reporter.getOutput();
for (String line : output) {
sb.append("<tr><td>").append(line).append("</td></tr>\n");
}
sb.append("</table>");
Utils.writeFile(getOutputDirectory(xmlSuite), REPORTER_OUTPUT, sb);
}
private void generateClasses(XmlSuite xmlSuite, ISuite suite) {
StringBuffer sb = new StringBuffer();
sb.append("<table border='1'>\n")
.append("<tr>\n")
.append("<th>Class name</th>\n")
.append("<th>Method name</th>\n")
.append("<th>Groups</th>\n")
.append("</tr>")
;
for (ITestClass tc : m_classes.values()) {
sb.append(generateClass(tc));
}
sb.append("</table>\n");
Utils.writeFile(getOutputDirectory(xmlSuite), CLASSES, sb);
}
private final static String SP = " ";
private final static String SP2 = SP + SP + SP + SP;
private final static String SP3 = SP2 + SP2;
private final static String SP4 = SP3 + SP3;
private String generateClass(ITestClass cls) {
StringBuffer sb = new StringBuffer();
sb.append("<tr>\n")
.append("<td>").append(cls.getRealClass().getName()).append("</td>\n")
.append("<td> </td>")
.append("<td> </td>")
.append("</tr>\n")
;
String[] tags = new String[] {
"@Test",
"@BeforeClass",
"@BeforeMethod",
"@AfterMethod",
"@AfterClass"
};
ITestNGMethod[][] methods = new ITestNGMethod[][] {
cls.getTestMethods(),
cls.getBeforeClassMethods(),
cls.getBeforeTestMethods(),
cls.getAfterTestMethods(),
cls.getAfterClassMethods()
};
for (int i = 0; i < tags.length; i++) {
sb.append("<tr>\n")
.append("<td align='center' colspan='3'>").append(tags[i]).append("</td>\n")
.append("</tr>\n")
.append(dumpMethods(methods[i]))
;
}
// sb.append("<hr width='100%'/>")
// .append("<h3>").append(cls.getRealClass().getName()).append("</h3>\n");
//
// sb.append("<div>").append(SP3).append("Test methods\n")
// .append(dumpMethods(cls.getTestMethods())).append("</div>\n")
// .append("<div>").append(SP3).append("@BeforeClass\n")
// .append(dumpMethods(cls.getBeforeClassMethods())).append("</div>\n")
// .append("<div>").append(SP3).append("@BeforeMethod\n")
// .append(dumpMethods(cls.getBeforeTestMethods())).append("</div>\n")
// .append("<div>").append(SP3).append("@AfterMethod\n")
// .append(dumpMethods(cls.getAfterTestMethods())).append("</div>\n")
// .append("<div>").append(SP3).append("@AfterClass\n")
// .append(dumpMethods(cls.getAfterClassMethods())).append("</div>\n")
// ;
String result = sb.toString();
return result;
}
private String dumpMethods(ITestNGMethod[] testMethods) {
StringBuffer sb = new StringBuffer("");
if(null == testMethods || testMethods.length == 0) {
return "";
}
for (ITestNGMethod m : testMethods) {
sb.append("<tr>\n");
sb.append("<td> </td>\n")
.append("<td>").append(m.getMethodName()).append("</td>\n")
;
String[] groups = m.getGroups();
if (groups != null && groups.length > 0) {
sb.append("<td>");
for (String g : groups) {
sb.append(g).append(" ");
}
sb.append("</td>\n");
}
else {
sb.append("<td> </td>");
}
sb.append("</tr>\n");
}
// StringBuffer sb = new StringBuffer("<br/>"); //"<table bgcolor=\"#c0c0c0\"/>");
// for (ITestNGMethod tm : testMethods) {
// sb
// .append(SP4).append(tm.getMethodName()).append("()\n")
// .append(dumpGroups(tm.getGroups()))
// .append("<br/>");
// ;
// }
String result = sb.toString();
return result;
}
private String dumpGroups(String[] groups) {
StringBuffer sb = new StringBuffer();
if (null != groups && groups.length > 0) {
sb.append(SP4).append("<em>[");
for (String g : groups) {
sb.append(g).append(" ");
}
sb.append("]</em><br/>\n");
}
String result = sb.toString();
return result;
}
/**
* Generate information about the methods that were run
*/
public static final String AFTER= "<<";
public static final String BEFORE = ">>";
private void generateMethodsChronologically(XmlSuite xmlSuite, ISuite suite,
String outputFileName, boolean alphabetical)
{
StringBuffer sb = new StringBuffer();
sb.append("<h2>Methods run, sorted chronologically</h2>");
sb.append("<h3>" + BEFORE + " means before, " + AFTER + " means after</h3><p/>");
long startDate = -1;
Map<Long, StringBuffer> tables = new HashMap<Long, StringBuffer>();
sb.append("<br/><em>").append(suite.getName()).append("</em><p/>");
sb.append("<small><i>(Hover the method name to see the test class name)</i></small><p/>\n");
Collection<ITestNGMethod> invokedMethods = suite.getInvokedMethods();
if (alphabetical) {
@SuppressWarnings({"unchecked"})
Comparator<? super ITestNGMethod> alphabeticalComparator = new Comparator(){
public int compare(Object o1, Object o2) {
ITestNGMethod m1 = (ITestNGMethod) o1;
ITestNGMethod m2 = (ITestNGMethod) o2;
return m1.getMethodName().compareTo(m2.getMethodName());
}
};
Collections.sort((List) invokedMethods, alphabeticalComparator);
}
for (ITestNGMethod tm : invokedMethods) {
Long id = new Long(0);
StringBuffer table = tables.get(id);
if (null == table) {
table = new StringBuffer();
tables.put(id, table);
table.append("<table border=\"1\">\n")
.append("<tr>")
.append("<th>Time</th>")
.append("<th>Delta (ms)</th>")
.append("<th>Suite<br>configuration</th>")
.append("<th>Test<br>configuration</th>")
.append("<th>Class<br>configuration</th>")
.append("<th>Groups<br>configuration</th>")
.append("<th>Method<br>configuration</th>")
.append("<th>Test<br>method</th>")
.append("<th>Thread</th>")
.append("<th>Instances</th>")
.append("</tr>\n");
}
String methodName = tm.toString();
boolean bc = tm.isBeforeClassConfiguration();
boolean ac = tm.isAfterClassConfiguration();
boolean bt = tm.isBeforeTestConfiguration();
boolean at = tm.isAfterTestConfiguration();
boolean bs = tm.isBeforeSuiteConfiguration();
boolean as = tm.isAfterSuiteConfiguration();
boolean bg = tm.isBeforeGroupsConfiguration();
boolean ag = tm.isAfterGroupsConfiguration();
boolean setUp = tm.isBeforeMethodConfiguration();
boolean tearDown = tm.isAfterMethodConfiguration();
boolean isClassConfiguration = bc || ac;
boolean isGroupsConfiguration = bg || ag;
boolean isTestConfiguration = bt || at;
boolean isSuiteConfiguration = bs || as;
boolean isSetupOrTearDown = setUp || tearDown;
String configurationClassMethod = isClassConfiguration ? (bc ? BEFORE : AFTER) + methodName : SP;
String configurationTestMethod = isTestConfiguration ? (bt ? BEFORE : AFTER) + methodName : SP;
String configurationGroupsMethod = isGroupsConfiguration ? (bg ? BEFORE : AFTER) + methodName : SP;
String configurationSuiteMethod = isSuiteConfiguration ? (bs ? BEFORE : AFTER) + methodName : SP;
String setUpOrTearDownMethod = isSetupOrTearDown ? (setUp ? BEFORE : AFTER) + methodName : SP;
String testMethod = tm.isTest() ? methodName : SP;
StringBuffer instances = new StringBuffer();
for (long o : tm.getInstanceHashCodes()) {
instances.append(o).append(" ");
}
if (startDate == -1) startDate = tm.getDate();
SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
String date = format.format(tm.getDate());
table.append("<tr bgcolor=\"" + createColor(tm) + "\">")
.append(" <td>").append(date).append("</td> ")
.append(" <td>").append(tm.getDate() - startDate).append("</td> ")
.append(td(configurationSuiteMethod))
.append(td(configurationTestMethod))
.append(td(configurationClassMethod))
.append(td(configurationGroupsMethod))
.append(td(setUpOrTearDownMethod))
.append(td(testMethod))
.append(" <td>").append(tm.getId()).append("</td> ")
.append(" <td>").append(instances).append("</td> ")
.append("</tr>\n")
;
}
/// Close all the tables
for (StringBuffer table : tables.values()) {
table.append("</table>\n");
sb.append(table.toString());
}
Utils.writeFile(getOutputDirectory(xmlSuite), outputFileName, sb);
}
private static String toHex(int n) {
return Integer.toHexString(0x10 | n).substring(1).toUpperCase();
}
/**
* Generate a HTML color based on the class of the method
*/
private String createColor(ITestNGMethod tm) {
// real class can be null if this client is remote (not serializable)
long color = tm.getRealClass() != null ? tm.getRealClass().hashCode() & 0xffffff: 0xffffff;
long[] rgb = {
((color & 0xff0000) >> 16) & 0xff,
((color & 0x00ff00) >> 8) & 0xff,
color & 0xff
};
// Not too dark
for (int i = 0; i < rgb.length; i++) {
if (rgb[i] < 0x60) rgb[i] += 0x60;
}
long adjustedColor = (rgb[0] << 16) | (rgb[1] << 8) | rgb[2];
String result = Long.toHexString(adjustedColor);
return result;
}
private String td(String s) {
StringBuffer result = new StringBuffer();
String prefix = "";
if (s.startsWith(BEFORE)) {
prefix = BEFORE;
}
else if (s.startsWith(AFTER)) {
prefix = AFTER;
}
if (! s.equals(SP)) {
result.append("<td title=\"").append(s).append("\">");
int open = s.lastIndexOf("(");
int start = s.substring(0, open).lastIndexOf(".");
int end = s.lastIndexOf(")");
if (start >= 0) {
result.append(prefix + s.substring(start + 1, open));
}
else {
result.append(prefix + s);
}
result.append("</td> \n");
}
else {
result.append("<td>").append(SP).append("</td>");
}
return result.toString();
}
private void ppp(String s) {
System.out.println("[SuiteHTMLReporter] " + s);
}
/**
* Generate information about methods and groups
*/
private void generateMethodsAndGroups(XmlSuite xmlSuite, ISuite suite) {
StringBuffer sb = new StringBuffer();
Map<String, Collection<ITestNGMethod>> groups = suite.getMethodsByGroups();
sb.append("<h2>Groups used for this test run</h2>");
if (groups.size() > 0) {
sb.append("<table border=\"1\">\n")
.append("<tr> <td align=\"center\"><b>Group name</b></td>")
.append("<td align=\"center\"><b>Methods</b></td></tr>");
String[] groupNames = groups.keySet().toArray(new String[groups.size()]);
Arrays.sort(groupNames);
for (String group : groupNames) {
Collection<ITestNGMethod> methods = groups.get(group);
sb.append("<tr><td>").append(group).append("</td>");
StringBuffer methodNames = new StringBuffer();
Map<ITestNGMethod, ITestNGMethod> uniqueMethods = new HashMap<ITestNGMethod, ITestNGMethod>();
for (ITestNGMethod tm : methods) {
uniqueMethods.put(tm, tm);
}
for (ITestNGMethod tm : uniqueMethods.values()) {
methodNames.append(tm.toString()).append("<br/>");
}
sb.append("<td>" + methodNames.toString() + "</td></tr>\n");
}
sb.append("</table>\n");
}
Utils.writeFile(getOutputDirectory(xmlSuite), GROUPS, sb);
}
private void generateIndex(XmlSuite xmlSuite, ISuite sr) {
StringBuffer index = new StringBuffer()
.append("<html><head><title>Results for " + sr.getName() + "</title></head>\n")
.append("<frameset cols=\"26%,74%\">\n")
.append("<frame src=\"toc.html\" name=\"navFrame\">\n")
.append("<frame src=\"main.html\" name=\"mainFrame\">\n")
.append("</frameset>\n")
.append("</html>\n")
;
Utils.writeFile(getOutputDirectory(xmlSuite), "index.html", index);
}
private String makeTitle(ISuite suite) {
return "Results for<br/><em>" + suite.getName() + "</em>";
}
private void generateMain(XmlSuite xmlSuite, ISuite sr) {
StringBuffer index = new StringBuffer()
.append("<html><head><title>Results for " + sr.getName() + "</title></head>\n")
.append("<body>Select a result on the left-hand pane.</body>")
.append("</html>\n")
;
Utils.writeFile(getOutputDirectory(xmlSuite), "main.html", index);
}
/**
*
*/
private void generateTableOfContents(XmlSuite xmlSuite, ISuite suite) {
StringBuffer tableOfContents = new StringBuffer();
//
// Generate methods and groups hyperlinks
//
Map<String, ISuiteResult> suiteResults = suite.getResults();
int groupCount = suite.getMethodsByGroups().size();
int methodCount = 0;
for (ISuiteResult sr : suiteResults.values()) {
ITestNGMethod[] methods = sr.getTestContext().getAllTestMethods();
methodCount += Utils.calculateInvokedMethodCount(methods);
// Collect testClasses
for (ITestNGMethod tm : methods) {
ITestClass tc = tm.getTestClass();
m_classes.put(tc.getRealClass().getName(), tc);
}
}
String name = "Results for " + suite.getName();
tableOfContents
.append("<html>\n")
.append("<head>\n")
.append("<title>" + name + "</title>\n")
.append(HtmlHelper.getCssString())
.append("</head>\n")
;
tableOfContents
.append("<body>\n")
.append("<h3><p align=\"center\">" + makeTitle(suite) + "</p></h3>\n")
.append("<table border='1' width='100%'>\n")
.append("<tr valign='top'>\n")
.append("<td>")
.append(suiteResults.size()).append(" ").append(pluralize(suiteResults.size(), "test"))
.append("</td>\n")
.append("<td>")
.append("<a target='mainFrame' href='").append(CLASSES).append("'>")
.append(m_classes.size() + " " + pluralize(m_classes.size(), "class"))
.append("</a>")
.append("</td>\n")
.append("<td>" + methodCount + " " + pluralize(methodCount, "method") + ":<br/>\n")
.append(" <a target='mainFrame' href='").append(METHODS_CHRONOLOGICAL).append("'>").append("chronological</a><br/>\n")
.append(" <a target='mainFrame' href='").append(METHODS_ALPHABETICAL).append("\'>").append("alphabetical</a><br/>\n")
.append(" <a target='mainFrame' href='").append(METHODS_NOT_RUN).append("'>not run (" + suite.getExcludedMethods().size() + ")</a>")
.append("</td>\n")
.append("</tr>\n")
.append("<tr>\n")
.append("<td><a target='mainFrame' href='").append(GROUPS).append("'>").append(groupCount + pluralize(groupCount, " group") + "</a></td>\n")
.append("<td><a target='mainFrame' href='").append(REPORTER_OUTPUT).append("'>reporter output</a></td>\n")
.append("<td><a target='mainFrame' href='").append(TESTNG_XML).append("'>testng.xml</a></td>\n")
.append("</tr>")
.append("</table>");
//
// Generate results for individual tests
//
// Order the results so we can show the failures first, then the skip and
// finally the successes
Map<String, ISuiteResult> redResults = new HashMap<String, ISuiteResult>();
Map<String, ISuiteResult> yellowResults = new HashMap<String, ISuiteResult>();
Map<String, ISuiteResult> greenResults = new HashMap<String, ISuiteResult>();
for (String suiteName : suiteResults.keySet()) {
ISuiteResult sr = suiteResults.get(suiteName);
ITestContext tc = sr.getTestContext();
int failed = tc.getFailedTests().size();
int skipped = tc.getSkippedTests().size();
int passed = tc.getPassedTests().size();
if (failed > 0) {
redResults.put(suiteName, sr);
}
else if (skipped > 0) {
yellowResults.put(suiteName, sr);
}
else if (passed > 0) {
greenResults.put(suiteName, sr);
}
else {
redResults.put(suiteName, sr);
}
}
ISuiteResult[][] results = new ISuiteResult[][] {
sortResults(redResults.values()), sortResults(yellowResults.values()), sortResults(greenResults.values())
};
String[] colors = {"failed", "skipped", "passed"};
for (int i = 0; i < colors.length; i++) {
ISuiteResult[] r = results[i];
for (ISuiteResult sr: r) {
String suiteName = sr.getTestContext().getName();
generateSuiteResult(suiteName, sr, colors[i], tableOfContents, m_outputDirectory);
}
}
tableOfContents.append("</body></html>");
Utils.writeFile(getOutputDirectory(xmlSuite), "toc.html", tableOfContents);
}
private String pluralize(int count, String singular) {
return count > 1 ? (singular.endsWith("s") ? singular + "es" : singular + "s") : singular;
}
private String getOutputDirectory(XmlSuite xmlSuite) {
return m_outputDirectory + File.separatorChar + xmlSuite.getName();
}
private ISuiteResult[] sortResults(Collection<ISuiteResult> r) {
ISuiteResult[] result = r.toArray(new ISuiteResult[r.size()]);
Arrays.sort(result);
return result;
}
private void generateSuiteResult(String suiteName,
ISuiteResult sr,
String cssClass,
StringBuffer tableOfContents,
String outputDirectory)
{
ITestContext tc = sr.getTestContext();
int passed = tc.getPassedTests().size();
int failed = tc.getFailedTests().size();
int skipped = tc.getSkippedTests().size();
String baseFile = tc.getName();
tableOfContents
.append("\n<table width='100%' class='test-").append(cssClass).append("'>\n")
.append("<tr><td>\n")
.append("<table style='width: 100%'><tr>")
.append("<td valign='top'>")
.append(suiteName).append(" (").append(passed).append("/").append(failed).append("/").append(skipped).append(")")
.append("</td>")
.append("<td valign='top' align='right'>\n")
.append(" <a href='" + baseFile + ".html' target='mainFrame'>Results</a>\n")
// .append(" <a href=\"" + baseFile + ".out\" target=\"mainFrame\"\">Output</a>\n")
// .append(" <a href=\"file://" + baseFile + ".properties\" target=\"mainFrame\"\">Property file</a><br>\n")
.append("</td>")
.append("</tr></table>\n")
.append("</td></tr><p/>\n")
;
tableOfContents.append("</table>\n");
}
/**
* Writes a property file for each suite result.
*
* @param xmlSuite
* @param suite
*/
private void generateSuites(XmlSuite xmlSuite, ISuite suite) {
Map<String, ISuiteResult> suiteResults = suite.getResults();
// TODO CQ why not iterate over entry set if were going to use the key to get the value?
for (String propertyFileName : suiteResults.keySet()) {
ISuiteResult sr = suiteResults.get(propertyFileName);
ITestContext testContext = sr.getTestContext();
StringBuffer sb = new StringBuffer();
// TODO CQ why not iterate over entry set if were going to use the key to get the value?
for (String name : suiteResults.keySet()) {
ISuiteResult suiteResult = suiteResults.get(name);
sb.append(suiteResult.toString());
}
Utils.writeFile(getOutputDirectory(xmlSuite), testContext.getName() + ".properties", sb);
}
}
}