forked from junit-team/junit5
/
XmlReportData.java
143 lines (119 loc) · 4.66 KB
/
XmlReportData.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
/*
* Copyright 2015-2021 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 v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/
package org.junit.platform.reporting.legacy.xml;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
import static org.junit.platform.engine.TestExecutionResult.Status.ABORTED;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;
import org.junit.platform.commons.util.ExceptionUtils;
import org.junit.platform.engine.TestExecutionResult;
import org.junit.platform.engine.reporting.ReportEntry;
import org.junit.platform.launcher.TestIdentifier;
import org.junit.platform.launcher.TestPlan;
/**
* @since 1.4
*/
class XmlReportData {
private static final int MILLIS_PER_SECOND = 1000;
private final Map<TestIdentifier, TestExecutionResult> finishedTests = new ConcurrentHashMap<>();
private final Map<TestIdentifier, String> skippedTests = new ConcurrentHashMap<>();
private final Map<TestIdentifier, Instant> startInstants = new ConcurrentHashMap<>();
private final Map<TestIdentifier, Instant> endInstants = new ConcurrentHashMap<>();
private final Map<TestIdentifier, List<ReportEntry>> reportEntries = new ConcurrentHashMap<>();
private final TestPlan testPlan;
private final Clock clock;
XmlReportData(TestPlan testPlan, Clock clock) {
this.testPlan = testPlan;
this.clock = clock;
}
TestPlan getTestPlan() {
return this.testPlan;
}
Clock getClock() {
return this.clock;
}
void markSkipped(TestIdentifier testIdentifier, String reason) {
this.skippedTests.put(testIdentifier, reason == null ? "" : reason);
}
void markStarted(TestIdentifier testIdentifier) {
this.startInstants.put(testIdentifier, this.clock.instant());
}
void markFinished(TestIdentifier testIdentifier, TestExecutionResult result) {
this.endInstants.put(testIdentifier, this.clock.instant());
if (result.getStatus() == ABORTED) {
String reason = result.getThrowable().map(ExceptionUtils::readStackTrace).orElse("");
this.skippedTests.put(testIdentifier, reason);
}
else {
this.finishedTests.put(testIdentifier, result);
}
}
void addReportEntry(TestIdentifier testIdentifier, ReportEntry entry) {
List<ReportEntry> entries = this.reportEntries.computeIfAbsent(testIdentifier, key -> new ArrayList<>());
entries.add(entry);
}
boolean wasSkipped(TestIdentifier testIdentifier) {
return findSkippedAncestor(testIdentifier).isPresent();
}
double getDurationInSeconds(TestIdentifier testIdentifier) {
Instant startInstant = this.startInstants.getOrDefault(testIdentifier, Instant.EPOCH);
Instant endInstant = this.endInstants.getOrDefault(testIdentifier, startInstant);
return Duration.between(startInstant, endInstant).toMillis() / (double) MILLIS_PER_SECOND;
}
String getSkipReason(TestIdentifier testIdentifier) {
return findSkippedAncestor(testIdentifier).map(skippedTestIdentifier -> {
String reason = this.skippedTests.get(skippedTestIdentifier);
if (!testIdentifier.equals(skippedTestIdentifier)) {
reason = "parent was skipped: " + reason;
}
return reason;
}).orElse(null);
}
List<TestExecutionResult> getResults(TestIdentifier testIdentifier) {
return getAncestors(testIdentifier).stream() //
.map(this.finishedTests::get) //
.filter(Objects::nonNull) //
.collect(toList());
}
List<ReportEntry> getReportEntries(TestIdentifier testIdentifier) {
return this.reportEntries.getOrDefault(testIdentifier, emptyList());
}
private Optional<TestIdentifier> findSkippedAncestor(TestIdentifier testIdentifier) {
return findAncestor(testIdentifier, this.skippedTests::containsKey);
}
private Optional<TestIdentifier> findAncestor(TestIdentifier testIdentifier, Predicate<TestIdentifier> predicate) {
Optional<TestIdentifier> current = Optional.of(testIdentifier);
while (current.isPresent()) {
if (predicate.test(current.get())) {
return current;
}
current = this.testPlan.getParent(current.get());
}
return Optional.empty();
}
private List<TestIdentifier> getAncestors(TestIdentifier testIdentifier) {
TestIdentifier current = testIdentifier;
List<TestIdentifier> ancestors = new ArrayList<>();
while (current != null) {
ancestors.add(current);
current = this.testPlan.getParent(current).orElse(null);
}
return ancestors;
}
}