Skip to content

Commit c16129e

Browse files
Include full display name in legacy XML reports (#5524)
Rather than just including the display name of the test, the display names of all ancestors are now included as well, separated by the `>` character. This includes display names of parents otherwise not represented in the legacy XML format (for example, the invocation of a `@ParameterizedClass`). This is a backport of commit ee3a100 Changes were made to compile under Java 8 Co-authored-by: Marc Philipp <mail@marcphilipp.de> Signed-off-by: Anthony Milbourne <18662115+amilbourne@users.noreply.github.com>
1 parent adbff58 commit c16129e

4 files changed

Lines changed: 33 additions & 17 deletions

File tree

documentation/modules/ROOT/partials/release-notes/release-notes-5.14.4.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ No changes.
2424

2525
* Include index of `@ClassTemplate`/`@ParameterizedClass` invocations in test names in
2626
legacy XML reports to make them unique.
27+
* Legacy XML reports now include parent display names to make it easier to distinguish
28+
between invocations for different parameters.
2729

2830

2931
[[v5.14.4-junit-vintage]]

junit-platform-reporting/src/main/java/org/junit/platform/reporting/legacy/xml/XmlReportWriter.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.net.UnknownHostException;
3838
import java.text.NumberFormat;
3939
import java.time.LocalDateTime;
40+
import java.util.ArrayDeque;
4041
import java.util.ArrayList;
4142
import java.util.Collection;
4243
import java.util.EnumSet;
@@ -116,7 +117,7 @@ private void writeXmlReport(TestIdentifier testIdentifier, Map<TestIdentifier, A
116117
Writer out) throws XMLStreamException {
117118

118119
try (XmlReport report = new XmlReport(out)) {
119-
report.write(testIdentifier, tests);
120+
report.write(testIdentifier, tests, this.reportData.getTestPlan());
120121
}
121122
}
122123

@@ -131,16 +132,16 @@ private class XmlReport implements AutoCloseable {
131132
this.xml = factory.createXMLStreamWriter(this.out);
132133
}
133134

134-
void write(TestIdentifier testIdentifier, Map<TestIdentifier, AggregatedTestResult> tests)
135+
void write(TestIdentifier testIdentifier, Map<TestIdentifier, AggregatedTestResult> tests, TestPlan testPlan)
135136
throws XMLStreamException {
136137
xml.writeStartDocument("UTF-8", "1.0");
137138
newLine();
138-
writeTestsuite(testIdentifier, tests);
139+
writeTestsuite(testIdentifier, tests, testPlan);
139140
xml.writeEndDocument();
140141
}
141142

142-
private void writeTestsuite(TestIdentifier testIdentifier, Map<TestIdentifier, AggregatedTestResult> tests)
143-
throws XMLStreamException {
143+
private void writeTestsuite(TestIdentifier testIdentifier, Map<TestIdentifier, AggregatedTestResult> tests,
144+
TestPlan testPlan) throws XMLStreamException {
144145

145146
// NumberFormat is not thread-safe. Thus, we instantiate it here and pass it to
146147
// writeTestcase instead of using a constant
@@ -154,10 +155,10 @@ private void writeTestsuite(TestIdentifier testIdentifier, Map<TestIdentifier, A
154155
writeSystemProperties();
155156

156157
for (Entry<TestIdentifier, AggregatedTestResult> entry : tests.entrySet()) {
157-
writeTestcase(entry.getKey(), entry.getValue(), numberFormat);
158+
writeTestcase(entry.getKey(), entry.getValue(), numberFormat, testPlan);
158159
}
159160

160-
writeOutputElement("system-out", formatNonStandardAttributesAsString(testIdentifier));
161+
writeOutputElement("system-out", formatNonStandardAttributesAsString(testIdentifier, testPlan));
161162

162163
xml.writeEndElement();
163164
newLine();
@@ -198,7 +199,7 @@ private void writeSystemProperties() throws XMLStreamException {
198199
}
199200

200201
private void writeTestcase(TestIdentifier testIdentifier, AggregatedTestResult testResult,
201-
NumberFormat numberFormat) throws XMLStreamException {
202+
NumberFormat numberFormat, TestPlan testPlan) throws XMLStreamException {
202203

203204
xml.writeStartElement("testcase");
204205

@@ -211,7 +212,7 @@ private void writeTestcase(TestIdentifier testIdentifier, AggregatedTestResult t
211212

212213
List<String> systemOutElements = new ArrayList<>();
213214
List<String> systemErrElements = new ArrayList<>();
214-
systemOutElements.add(formatNonStandardAttributesAsString(testIdentifier));
215+
systemOutElements.add(formatNonStandardAttributesAsString(testIdentifier, testPlan));
215216
collectReportEntries(testIdentifier, systemOutElements, systemErrElements);
216217
writeOutputElements("system-out", systemOutElements);
217218
writeOutputElements("system-err", systemErrElements);
@@ -334,9 +335,16 @@ private LocalDateTime getCurrentDateTime() {
334335
return LocalDateTime.now(reportData.getClock()).withNano(0);
335336
}
336337

337-
private String formatNonStandardAttributesAsString(TestIdentifier testIdentifier) {
338+
private String formatNonStandardAttributesAsString(TestIdentifier testIdentifier, TestPlan testPlan) {
339+
ArrayDeque<String> displayNames = new ArrayDeque<>();
340+
TestIdentifier current = testIdentifier;
341+
while (current != null) {
342+
displayNames.push(current.getDisplayName());
343+
current = testPlan.getParent(current).orElse(null);
344+
}
345+
String fullDisplayName = String.join(" > ", displayNames);
338346
return "unique-id: " + testIdentifier.getUniqueId() //
339-
+ "\ndisplay-name: " + testIdentifier.getDisplayName();
347+
+ "\ndisplay-name: " + fullDisplayName;
340348
}
341349

342350
private void writeOutputElements(String elementName, List<String> elements) throws XMLStreamException {

platform-tests/src/test/java/org/junit/platform/reporting/legacy/xml/LegacyXmlReportGeneratingListenerTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ void writesFileForSingleSucceedingTest() throws Exception {
9090
assertThat(testcase.attr("classname")).isEqualTo("dummy");
9191
assertThat(testcase.child("system-out").text()) //
9292
.containsSubsequence("unique-id: [engine:dummy]/[test:succeedingTest]",
93-
"display-name: display<-->Name 😎");
93+
"display-name: dummy > display<-->Name 😎");
9494

9595
assertThat(testsuite.find("skipped")).isEmpty();
9696
assertThat(testsuite.find("failure")).isEmpty();

platform-tests/src/test/java/org/junit/platform/reporting/legacy/xml/XmlReportWriterTests.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,14 @@ void writesReportEntry() throws Exception {
9191

9292
@Test
9393
void writesCapturedOutput() throws Exception {
94-
var uniqueId = engineDescriptor.getUniqueId().append("test", "test");
95-
var testDescriptor = new TestDescriptorStub(uniqueId, "successfulTest");
96-
engineDescriptor.addChild(testDescriptor);
94+
var classDescriptor = new TestDescriptorStub(engineDescriptor.getUniqueId().append("class", "SomeClass"),
95+
"SomeClass");
96+
engineDescriptor.addChild(classDescriptor);
97+
98+
var testDescriptor = new TestDescriptorStub(classDescriptor.getUniqueId().append("test", "successfulTest"),
99+
"successfulTest");
100+
classDescriptor.addChild(testDescriptor);
101+
97102
var testPlan = TestPlan.from(true, Set.of(engineDescriptor), configParams, dummyOutputDirectoryCreator());
98103

99104
var reportData = new XmlReportData(testPlan, Clock.systemDefaultZone());
@@ -103,13 +108,14 @@ void writesCapturedOutput() throws Exception {
103108
"foo", "bar"));
104109
reportData.addReportEntry(TestIdentifier.from(testDescriptor), reportEntry);
105110
reportData.addReportEntry(TestIdentifier.from(testDescriptor), ReportEntry.from(Map.of("baz", "qux")));
106-
reportData.markFinished(testPlan.getTestIdentifier(uniqueId), successful());
111+
reportData.markFinished(testPlan.getTestIdentifier(testDescriptor.getUniqueId()), successful());
107112

108113
var testsuite = writeXmlReport(testPlan, reportData);
109114

110115
assertValidAccordingToJenkinsSchema(testsuite.document());
111116
assertThat(testsuite.find("system-out").text(0)) //
112-
.containsSubsequence("unique-id: ", "test:test", "display-name: successfulTest");
117+
.containsSubsequence("unique-id: [engine:engine]/[class:SomeClass]/[test:successfulTest]",
118+
"display-name: Engine > SomeClass > successfulTest");
113119
assertThat(testsuite.find("system-out").text(1)) //
114120
.containsSubsequence("Report Entry #1 (timestamp: ", "- foo: bar", "Report Entry #2 (timestamp: ",
115121
"- baz: qux");

0 commit comments

Comments
 (0)