Skip to content

Commit adbff58

Browse files
Include class template invocation index in legacy reporting names
This is a backport of commit b9756dd 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 39ab75c commit adbff58

12 files changed

Lines changed: 160 additions & 62 deletions

File tree

documentation/modules/ROOT/pages/release-notes.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Please refer to the xref:overview.adoc[User Guide] for comprehensive
99
reference documentation for programmers writing tests, extension authors, and engine
1010
authors as well as build tool and IDE vendors.
1111

12+
include::partial$release-notes/release-notes-5.14.4.adoc[]
13+
1214
include::partial$release-notes/release-notes-5.14.3.adoc[]
1315

1416
include::partial$release-notes/release-notes-5.14.2.adoc[]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[[v5.14.4]]
2+
== 5.14.4
3+
4+
*Date of Release:* ❓
5+
6+
*Scope:* Bug fix for legacy XML reporting of `@ClassTemplate`/`@ParameterizedClass` tests
7+
8+
For a complete list of all _closed_ issues and pull requests for this release, consult the
9+
link:{junit-framework-repo}+/milestone/MILESTONE_NUMBER?closed=1+[5.14.4] milestone page in the JUnit
10+
repository on GitHub.
11+
12+
13+
[[v5.14.4-junit-platform]]
14+
=== JUnit Platform
15+
16+
No changes.
17+
18+
19+
[[v5.14.4-junit-jupiter]]
20+
=== JUnit Jupiter
21+
22+
[[v5.14.4-junit-jupiter-bug-fixes]]
23+
==== Bug Fixes
24+
25+
* Include index of `@ClassTemplate`/`@ParameterizedClass` invocations in test names in
26+
legacy XML reports to make them unique.
27+
28+
29+
[[v5.14.4-junit-vintage]]
30+
=== JUnit Vintage
31+
32+
No changes.

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassBasedTestDescriptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public final Type getType() {
126126
}
127127

128128
@Override
129-
public final String getLegacyReportingName() {
129+
protected final String getLegacyReportingBaseName() {
130130
return getTestClass().getName();
131131
}
132132

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassTemplateInvocationTestDescriptor.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.junit.jupiter.engine.support.JupiterThrowableCollectorFactory.createThrowableCollector;
1919

2020
import java.util.List;
21+
import java.util.OptionalInt;
2122
import java.util.Set;
2223
import java.util.function.Function;
2324
import java.util.function.UnaryOperator;
@@ -80,8 +81,13 @@ public Type getType() {
8081
}
8182

8283
@Override
83-
public String getLegacyReportingName() {
84-
return getTestClass().getName() + "[" + index + "]";
84+
protected String getLegacyReportingBaseName() {
85+
return getTestClass().getName();
86+
}
87+
88+
@Override
89+
protected OptionalInt getLegacyReportingIndex() {
90+
return OptionalInt.of(index);
8591
}
8692

8793
// --- TestClassAware ------------------------------------------------------

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/DynamicNodeTestDescriptor.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
package org.junit.jupiter.engine.descriptor;
1212

13+
import java.util.OptionalInt;
14+
1315
import org.junit.jupiter.api.DynamicNode;
1416
import org.junit.jupiter.api.extension.ExtensionContext;
1517
import org.junit.jupiter.engine.config.JupiterConfiguration;
@@ -34,15 +36,24 @@ abstract class DynamicNodeTestDescriptor extends JupiterTestDescriptor {
3436
}
3537

3638
@Override
37-
public String getLegacyReportingName() {
39+
protected final String getLegacyReportingBaseName() {
3840
// @formatter:off
3941
return getParent()
40-
.map(TestDescriptor::getLegacyReportingName)
41-
.orElseGet(this::getDisplayName)
42-
+ "[" + index + "]";
42+
.map(parent -> {
43+
if (parent instanceof JupiterTestDescriptor) {
44+
return ((JupiterTestDescriptor) parent).getLegacyReportingBaseName();
45+
}
46+
return parent.getLegacyReportingName();
47+
})
48+
.orElseGet(this::getDisplayName);
4349
// @formatter:on
4450
}
4551

52+
@Override
53+
protected OptionalInt getLegacyReportingIndex() {
54+
return OptionalInt.of(index);
55+
}
56+
4657
@Override
4758
public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext context) {
4859
ExtensionContext extensionContext = new DynamicExtensionContext(context.getExtensionContext(),

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/JupiterTestDescriptor.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import static java.util.Collections.emptySet;
1414
import static java.util.stream.Collectors.collectingAndThen;
15+
import static java.util.stream.Collectors.joining;
1516
import static java.util.stream.Collectors.toCollection;
1617
import static java.util.stream.Collectors.toSet;
1718
import static org.apiguardian.api.API.Status.INTERNAL;
@@ -24,11 +25,13 @@
2425
import java.util.LinkedHashSet;
2526
import java.util.List;
2627
import java.util.Optional;
28+
import java.util.OptionalInt;
2729
import java.util.Set;
2830
import java.util.concurrent.atomic.AtomicReference;
2931
import java.util.function.Consumer;
3032
import java.util.function.Supplier;
3133
import java.util.function.UnaryOperator;
34+
import java.util.stream.IntStream;
3235

3336
import org.apiguardian.api.API;
3437
import org.junit.jupiter.api.Tag;
@@ -77,6 +80,35 @@ public abstract class JupiterTestDescriptor extends AbstractTestDescriptor
7780

7881
// --- TestDescriptor ------------------------------------------------------
7982

83+
@Override
84+
public final String getLegacyReportingName() {
85+
return getLegacyReportingBaseName()
86+
+ getLegacyReportingIndexes().mapToObj(i -> "[" + i + "]").collect(joining());
87+
}
88+
89+
protected String getLegacyReportingBaseName() {
90+
return getDisplayName();
91+
}
92+
93+
private IntStream getLegacyReportingIndexes() {
94+
OptionalInt ownIndex = getLegacyReportingIndex();
95+
return getParent() //
96+
.map(it -> it instanceof JupiterTestDescriptor //
97+
? ((JupiterTestDescriptor) it).getLegacyReportingIndexes() //
98+
: null) //
99+
.map(parentIndexes -> IntStream.concat(parentIndexes, asStream(ownIndex))) //
100+
.orElse(asStream(ownIndex));
101+
}
102+
103+
private IntStream asStream(final OptionalInt optInt) {
104+
return optInt.isPresent() ? IntStream.of(optInt.getAsInt()) : IntStream.empty();
105+
106+
}
107+
108+
protected OptionalInt getLegacyReportingIndex() {
109+
return OptionalInt.empty();
110+
}
111+
80112
static Set<TestTag> getTags(AnnotatedElement element, Supplier<String> elementDescription,
81113
Supplier<TestSource> sourceProvider, Consumer<DiscoveryIssue> issueCollector) {
82114
AtomicReference<TestSource> source = new AtomicReference<>();

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/MethodBasedTestDescriptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public final Set<TestTag> getTags() {
8787
}
8888

8989
@Override
90-
public String getLegacyReportingName() {
90+
protected final String getLegacyReportingBaseName() {
9191
return String.format("%s(%s)", getTestMethod().getName(),
9292
ClassUtils.nullSafeToString(Class::getSimpleName, getTestMethod().getParameterTypes()));
9393
}

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestTemplateInvocationTestDescriptor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import static org.apiguardian.api.API.Status.INTERNAL;
1515

1616
import java.lang.reflect.Method;
17+
import java.util.OptionalInt;
1718
import java.util.Set;
1819
import java.util.function.UnaryOperator;
1920

@@ -70,8 +71,8 @@ public Set<ExclusiveResource> getExclusiveResources() {
7071
}
7172

7273
@Override
73-
public String getLegacyReportingName() {
74-
return super.getLegacyReportingName() + "[" + index + "]";
74+
protected OptionalInt getLegacyReportingIndex() {
75+
return OptionalInt.of(index);
7576
}
7677

7778
@Override

jupiter-tests/src/test/java/org/junit/jupiter/params/ParameterizedClassIntegrationTests.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import static org.junit.platform.testkit.engine.EventConditions.event;
3838
import static org.junit.platform.testkit.engine.EventConditions.finishedSuccessfully;
3939
import static org.junit.platform.testkit.engine.EventConditions.finishedWithFailure;
40+
import static org.junit.platform.testkit.engine.EventConditions.legacyReportingName;
4041
import static org.junit.platform.testkit.engine.EventConditions.started;
4142
import static org.junit.platform.testkit.engine.EventConditions.test;
4243
import static org.junit.platform.testkit.engine.EventConditions.uniqueId;
@@ -106,6 +107,7 @@
106107
import org.junit.platform.engine.reporting.ReportEntry;
107108
import org.junit.platform.testkit.engine.EngineExecutionResults;
108109
import org.junit.platform.testkit.engine.Event;
110+
import org.junit.platform.testkit.engine.EventType;
109111
import org.junit.platform.testkit.engine.Events;
110112

111113
@SuppressWarnings("JUnitMalformedDeclaration")
@@ -132,19 +134,19 @@ void injectsParametersIntoClass(Class<?> classTemplateClass) {
132134
event(container("#1"), started()), //
133135
event(dynamicTestRegistered("test1")), //
134136
event(dynamicTestRegistered("test2")), //
135-
event(test("test1"), started()), //
137+
event(test("test1"), legacyReportingName("test1()[1]"), started()), //
136138
event(test("test1"), finishedSuccessfully()), //
137-
event(test("test2"), started()), //
139+
event(test("test2"), legacyReportingName("test2()[1]"), started()), //
138140
event(test("test2"), finishedSuccessfully()), //
139141
event(container("#1"), finishedSuccessfully()), //
140142

141143
event(dynamicTestRegistered("#2"), displayName("[2] %s1".formatted(parameterNamePrefix))), //
142144
event(container("#2"), started()), //
143145
event(dynamicTestRegistered("test1")), //
144146
event(dynamicTestRegistered("test2")), //
145-
event(test("test1"), started()), //
147+
event(test("test1"), legacyReportingName("test1()[2]"), started()), //
146148
event(test("test1"), finishedWithFailure(message(it -> it.contains("negative")))), //
147-
event(test("test2"), started()), //
149+
event(test("test2"), legacyReportingName("test2()[2]"), started()), //
148150
event(test("test2"), finishedWithFailure(message(it -> it.contains("negative")))), //
149151
event(container("#2"), finishedSuccessfully()), //
150152

@@ -461,6 +463,18 @@ void supportsNestedParameterizedClass(Class<?> classTemplateClass) {
461463
"afterAll: %s".formatted(classTemplateClass.getSimpleName())
462464
// @formatter:on
463465
);
466+
var legacyReportingNames = results.testEvents() //
467+
.filter(it -> it.getType() == EventType.STARTED) //
468+
.map(e -> e.getTestDescriptor().getLegacyReportingName());
469+
assertThat(legacyReportingNames).containsExactly( //
470+
"test(boolean, TestReporter)[1][1][1]", //
471+
"test(boolean, TestReporter)[1][1][2]", //
472+
"test(boolean, TestReporter)[1][2][1]", //
473+
"test(boolean, TestReporter)[1][2][2]", //
474+
"test(boolean, TestReporter)[2][1][1]", //
475+
"test(boolean, TestReporter)[2][1][2]", //
476+
"test(boolean, TestReporter)[2][2][1]", //
477+
"test(boolean, TestReporter)[2][2][2]");
464478
}
465479

466480
@ParameterizedTest

0 commit comments

Comments
 (0)