-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
XmlReportsWritingListener.java
99 lines (81 loc) · 2.7 KB
/
XmlReportsWritingListener.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
/*
* Copyright 2015-2016 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.junit.gen5.console.tasks;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.time.Clock;
import javax.xml.stream.XMLStreamException;
import org.junit.gen5.engine.TestExecutionResult;
import org.junit.gen5.launcher.TestExecutionListener;
import org.junit.gen5.launcher.TestIdentifier;
import org.junit.gen5.launcher.TestPlan;
class XmlReportsWritingListener implements TestExecutionListener {
private final File reportsDir;
private final PrintWriter out;
private final Clock clock;
private XmlReportData reportData;
public XmlReportsWritingListener(String reportsDir, PrintWriter out) {
this(reportsDir, out, Clock.systemDefaultZone());
}
// For tests only
XmlReportsWritingListener(String reportsDir, PrintWriter out, Clock clock) {
this.reportsDir = new File(reportsDir);
this.out = out;
this.clock = clock;
}
@Override
public void testPlanExecutionStarted(TestPlan testPlan) {
this.reportData = new XmlReportData(testPlan, clock);
try {
Files.createDirectories(reportsDir.toPath());
}
catch (IOException e) {
printException("Could not create report directory: " + reportsDir, e);
}
}
@Override
public void testPlanExecutionFinished(TestPlan testPlan) {
this.reportData = null;
}
@Override
public void executionSkipped(TestIdentifier testIdentifier, String reason) {
reportData.markSkipped(testIdentifier, reason);
// TODO #86 write file for roots
}
@Override
public void executionStarted(TestIdentifier testIdentifier) {
reportData.markStarted(testIdentifier);
}
@Override
public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult result) {
reportData.markFinished(testIdentifier, result);
if (isARoot(testIdentifier)) {
writeXmlReportSafely(testIdentifier);
}
}
private void writeXmlReportSafely(TestIdentifier testIdentifier) {
File xmlFile = new File(reportsDir, "TEST-" + testIdentifier.getUniqueId() + ".xml");
try {
new XmlReportWriter(reportData).writeXmlReport(testIdentifier, xmlFile);
}
catch (XMLStreamException | IOException e) {
printException("Could not write file: " + xmlFile, e);
}
}
private boolean isARoot(TestIdentifier testIdentifier) {
return !testIdentifier.getParentId().isPresent();
}
private void printException(String message, Exception exception) {
out.println(message);
exception.printStackTrace(out);
}
}