Skip to content

Commit ec16b03

Browse files
committed
Support navigation to failed tests in @⁠Inject TCK tests
See gh-35126
1 parent ed86daa commit ec16b03

File tree

1 file changed

+20
-32
lines changed

1 file changed

+20
-32
lines changed

spring-context/src/test/java/org/springframework/context/annotation/jsr330/SpringAtInjectTckTests.java

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@
1616

1717
package org.springframework.context.annotation.jsr330;
1818

19-
import java.util.Enumeration;
20-
import java.util.Spliterator;
21-
import java.util.Spliterators;
19+
import java.net.URI;
20+
import java.util.Collections;
2221
import java.util.stream.Stream;
23-
import java.util.stream.StreamSupport;
2422

2523
import junit.framework.TestCase;
26-
import junit.framework.TestFailure;
2724
import junit.framework.TestResult;
2825
import junit.framework.TestSuite;
2926
import org.atinject.tck.Tck;
@@ -44,6 +41,7 @@
4441
import org.springframework.context.annotation.Jsr330ScopeMetadataResolver;
4542
import org.springframework.context.annotation.Primary;
4643
import org.springframework.context.support.GenericApplicationContext;
44+
import org.springframework.util.ClassUtils;
4745

4846
import static org.junit.jupiter.api.DynamicContainer.dynamicContainer;
4947
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
@@ -61,7 +59,8 @@ class SpringAtInjectTckTests {
6159
@TestFactory
6260
Stream<? extends DynamicNode> runTechnologyCompatibilityKit() {
6361
TestSuite testSuite = (TestSuite) Tck.testsFor(buildCar(), false, true);
64-
return generateDynamicTests(testSuite);
62+
Class<?> suiteClass = resolveTestSuiteClass(testSuite);
63+
return generateDynamicTests(testSuite, suiteClass);
6564
}
6665

6766

@@ -84,45 +83,34 @@ private static Car buildCar() {
8483
return ac.getBean(Car.class);
8584
}
8685

87-
private static Stream<? extends DynamicNode> generateDynamicTests(TestSuite testSuite) {
88-
return stream(testSuite.tests()).map(test -> {
86+
private static Stream<? extends DynamicNode> generateDynamicTests(TestSuite testSuite, Class<?> suiteClass) {
87+
return Collections.list(testSuite.tests()).stream().map(test -> {
8988
if (test instanceof TestSuite nestedSuite) {
90-
return dynamicContainer(nestedSuite.getName(), generateDynamicTests(nestedSuite));
89+
Class<?> nestedSuiteClass = resolveTestSuiteClass(nestedSuite);
90+
URI uri = URI.create("class:" + nestedSuiteClass.getName());
91+
return dynamicContainer(nestedSuite.getName(), uri, generateDynamicTests(nestedSuite, nestedSuiteClass));
9192
}
9293
if (test instanceof TestCase testCase) {
93-
return dynamicTest(testCase.getName(), () -> runTestCase(testCase));
94+
URI uri = URI.create("method:" + suiteClass.getName() + "#" + testCase.getName());
95+
return dynamicTest(testCase.getName(), uri, () -> runTestCase(testCase));
9496
}
9597
throw new IllegalStateException("Unsupported Test type: " + test.getClass().getName());
9698
});
9799
}
98100

99-
private static void runTestCase(TestCase testCase) {
101+
private static void runTestCase(TestCase testCase) throws Throwable {
100102
TestResult testResult = new TestResult();
101103
testCase.run(testResult);
102-
assertSuccessfulResults(testResult);
103-
}
104-
105-
private static void assertSuccessfulResults(TestResult testResult) {
106-
if (!testResult.wasSuccessful()) {
107-
Throwable throwable = Stream.concat(stream(testResult.failures()), stream(testResult.errors()))
108-
.map(TestFailure::thrownException)
109-
.findFirst()
110-
.get();
111-
112-
if (throwable instanceof Error error) {
113-
throw error;
114-
}
115-
if (throwable instanceof RuntimeException runtimeException) {
116-
throw runtimeException;
117-
}
118-
throw new AssertionError(throwable);
104+
if (testResult.failureCount() > 0) {
105+
throw testResult.failures().nextElement().thrownException();
106+
}
107+
if (testResult.errorCount() > 0) {
108+
throw testResult.errors().nextElement().thrownException();
119109
}
120110
}
121111

122-
private static <T> Stream<T> stream(Enumeration<T> enumeration) {
123-
Spliterator<T> spliterator = Spliterators.spliteratorUnknownSize(
124-
enumeration.asIterator(), Spliterator.ORDERED);
125-
return StreamSupport.stream(spliterator, false);
112+
private static Class<?> resolveTestSuiteClass(TestSuite testSuite) {
113+
return ClassUtils.resolveClassName(testSuite.getName(), Tck.class.getClassLoader());
126114
}
127115

128116
}

0 commit comments

Comments
 (0)