16
16
17
17
package org .springframework .context .annotation .jsr330 ;
18
18
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 ;
20
29
import org .atinject .tck .Tck ;
21
30
import org .atinject .tck .auto .Car ;
22
31
import org .atinject .tck .auto .Convertible ;
28
37
import org .atinject .tck .auto .V8Engine ;
29
38
import org .atinject .tck .auto .accessories .Cupholder ;
30
39
import org .atinject .tck .auto .accessories .SpareTire ;
40
+ import org .junit .jupiter .api .DynamicNode ;
41
+ import org .junit .jupiter .api .TestFactory ;
31
42
32
43
import org .springframework .context .annotation .AnnotatedBeanDefinitionReader ;
33
44
import org .springframework .context .annotation .Jsr330ScopeMetadataResolver ;
34
45
import org .springframework .context .annotation .Primary ;
35
46
import org .springframework .context .support .GenericApplicationContext ;
36
47
48
+ import static org .junit .jupiter .api .DynamicContainer .dynamicContainer ;
49
+ import static org .junit .jupiter .api .DynamicTest .dynamicTest ;
50
+
37
51
/**
52
+ * {@code @Inject} Technology Compatibility Kit (TCK) tests.
53
+ *
38
54
* @author Juergen Hoeller
55
+ * @author Sam Brannen
39
56
* @since 3.0
57
+ * @see org.atinject.tck.Tck
40
58
*/
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
+
43
67
44
68
@ SuppressWarnings ("unchecked" )
45
- public static Test suite () {
69
+ private static Car buildCar () {
46
70
GenericApplicationContext ac = new GenericApplicationContext ();
47
71
AnnotatedBeanDefinitionReader bdr = new AnnotatedBeanDefinitionReader (ac );
48
72
bdr .setScopeMetadataResolver (new Jsr330ScopeMetadataResolver ());
@@ -57,9 +81,48 @@ public static Test suite() {
57
81
bdr .registerBean (FuelTank .class );
58
82
59
83
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
+ }
61
121
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 );
63
126
}
64
127
65
128
}
0 commit comments