1616
1717package org .springframework .context .annotation .jsr330 ;
1818
19- import junit .framework .Test ;
19+ import java .util .Enumeration ;
20+ import java .util .Spliterator ;
21+ import java .util .Spliterators ;
22+ import java .util .stream .Stream ;
23+ import java .util .stream .StreamSupport ;
24+
25+ import junit .framework .TestCase ;
26+ import junit .framework .TestFailure ;
27+ import junit .framework .TestResult ;
28+ import junit .framework .TestSuite ;
2029import org .atinject .tck .Tck ;
2130import org .atinject .tck .auto .Car ;
2231import org .atinject .tck .auto .Convertible ;
2837import org .atinject .tck .auto .V8Engine ;
2938import org .atinject .tck .auto .accessories .Cupholder ;
3039import org .atinject .tck .auto .accessories .SpareTire ;
40+ import org .junit .jupiter .api .DynamicNode ;
41+ import org .junit .jupiter .api .TestFactory ;
3142
3243import org .springframework .context .annotation .AnnotatedBeanDefinitionReader ;
3344import org .springframework .context .annotation .Jsr330ScopeMetadataResolver ;
3445import org .springframework .context .annotation .Primary ;
3546import org .springframework .context .support .GenericApplicationContext ;
3647
48+ import static org .junit .jupiter .api .DynamicContainer .dynamicContainer ;
49+ import static org .junit .jupiter .api .DynamicTest .dynamicTest ;
50+
3751/**
52+ * {@code @Inject} Technology Compatibility Kit (TCK) tests.
53+ *
3854 * @author Juergen Hoeller
55+ * @author Sam Brannen
3956 * @since 3.0
57+ * @see org.atinject.tck.Tck
4058 */
41- // WARNING: This class MUST be public, since it is based on JUnit 3.
42- public class SpringAtInjectTckTests {
59+ class SpringAtInjectTckTests {
60+
61+ @ TestFactory
62+ Stream <? extends DynamicNode > runTechnologyCompatibilityKit () {
63+ TestSuite testSuite = (TestSuite ) Tck .testsFor (buildCar (), false , true );
64+ return generateDynamicTests (testSuite );
65+ }
66+
4367
4468 @ SuppressWarnings ("unchecked" )
45- public static Test suite () {
69+ private static Car buildCar () {
4670 GenericApplicationContext ac = new GenericApplicationContext ();
4771 AnnotatedBeanDefinitionReader bdr = new AnnotatedBeanDefinitionReader (ac );
4872 bdr .setScopeMetadataResolver (new Jsr330ScopeMetadataResolver ());
@@ -57,9 +81,48 @@ public static Test suite() {
5781 bdr .registerBean (FuelTank .class );
5882
5983 ac .refresh ();
60- Car car = ac .getBean (Car .class );
84+ return ac .getBean (Car .class );
85+ }
86+
87+ private static Stream <? extends DynamicNode > generateDynamicTests (TestSuite testSuite ) {
88+ return stream (testSuite .tests ()).map (test -> {
89+ if (test instanceof TestSuite nestedSuite ) {
90+ return dynamicContainer (nestedSuite .getName (), generateDynamicTests (nestedSuite ));
91+ }
92+ if (test instanceof TestCase testCase ) {
93+ return dynamicTest (testCase .getName (), () -> runTestCase (testCase ));
94+ }
95+ throw new IllegalStateException ("Unsupported Test type: " + test .getClass ().getName ());
96+ });
97+ }
98+
99+ private static void runTestCase (TestCase testCase ) {
100+ TestResult testResult = new TestResult ();
101+ 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 );
119+ }
120+ }
61121
62- return Tck .testsFor (car , false , true );
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 );
63126 }
64127
65128}
0 commit comments