|
16 | 16 |
|
17 | 17 | package org.springframework.test.context.aot;
|
18 | 18 |
|
| 19 | + |
| 20 | +import java.nio.file.Path; |
| 21 | +import java.nio.file.Paths; |
| 22 | +import java.util.Arrays; |
19 | 23 | import java.util.List;
|
| 24 | +import java.util.Set; |
20 | 25 | import java.util.stream.Stream;
|
21 | 26 |
|
22 | 27 | import org.junit.jupiter.api.Test;
|
| 28 | +import org.junit.platform.launcher.LauncherDiscoveryRequest; |
| 29 | +import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder; |
| 30 | +import org.junit.platform.launcher.core.LauncherFactory; |
| 31 | +import org.junit.platform.launcher.listeners.SummaryGeneratingListener; |
| 32 | +import org.junit.platform.launcher.listeners.TestExecutionSummary; |
| 33 | +import org.junit.platform.launcher.listeners.TestExecutionSummary.Failure; |
| 34 | +import org.opentest4j.MultipleFailuresError; |
23 | 35 |
|
| 36 | +import org.springframework.aot.AotDetector; |
24 | 37 | import org.springframework.aot.generate.GeneratedFiles.Kind;
|
25 | 38 | import org.springframework.aot.generate.InMemoryGeneratedFiles;
|
| 39 | +import org.springframework.aot.test.generator.compile.CompileWithTargetClassAccess; |
26 | 40 | import org.springframework.aot.test.generator.compile.TestCompiler;
|
| 41 | +import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterSharedConfigTests; |
| 42 | +import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterTests; |
27 | 43 |
|
28 | 44 | import static org.assertj.core.api.Assertions.assertThat;
|
| 45 | +import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass; |
| 46 | +import static org.junit.platform.launcher.EngineFilter.includeEngines; |
29 | 47 |
|
30 | 48 | /**
|
31 | 49 | * Smoke tests for AOT support in the TestContext framework.
|
32 | 50 | *
|
33 | 51 | * @author Sam Brannen
|
34 | 52 | * @since 6.0
|
35 | 53 | */
|
| 54 | +@CompileWithTargetClassAccess |
36 | 55 | class AotSmokeTests extends AbstractAotTests {
|
37 | 56 |
|
| 57 | + private static final String CLASSPATH_ROOT = "AotSmokeTests.classpath_root"; |
| 58 | + |
| 59 | + // We have to determine the classpath root and store it in a system property |
| 60 | + // since @CompileWithTargetClassAccess uses a custom ClassLoader that does |
| 61 | + // not support CodeSource. |
| 62 | + // |
| 63 | + // The system property will only be set when this class is loaded by the |
| 64 | + // original ClassLoader used to launch the JUnit Platform. The attempt to |
| 65 | + // access the CodeSource will fail when the tests are executed in the |
| 66 | + // nested JUnit Platform launched by the CompileWithTargetClassAccessExtension. |
| 67 | + static { |
| 68 | + try { |
| 69 | + Path classpathRoot = Paths.get(AotSmokeTests.class.getProtectionDomain().getCodeSource().getLocation().toURI()); |
| 70 | + System.setProperty(CLASSPATH_ROOT, classpathRoot.toFile().getCanonicalPath()); |
| 71 | + } |
| 72 | + catch (Exception ex) { |
| 73 | + // ignore |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + |
38 | 78 | @Test
|
39 |
| - // Using @CompileWithTargetClassAccess results in the following exception in classpathRoots(): |
40 |
| - // java.lang.NullPointerException: Cannot invoke "java.net.URL.toURI()" because the return |
41 |
| - // value of "java.security.CodeSource.getLocation()" is null |
42 |
| - void scanClassPathThenGenerateSourceFilesAndCompileThem() { |
43 |
| - Stream<Class<?>> testClasses = scan("org.springframework.test.context.aot.samples.basic"); |
| 79 | + void endToEndTests() { |
| 80 | + // AOT BUILD-TIME: CLASSPATH SCANNING |
| 81 | + Stream<Class<?>> testClasses = createTestClassScanner() |
| 82 | + .scan("org.springframework.test.context.aot.samples.basic") |
| 83 | + // This test focuses solely on JUnit Jupiter tests |
| 84 | + .filter(sourceFile -> sourceFile.getName().contains("Jupiter")); |
| 85 | + |
| 86 | + // AOT BUILD-TIME: PROCESSING |
44 | 87 | InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles();
|
45 | 88 | TestContextAotGenerator generator = new TestContextAotGenerator(generatedFiles);
|
46 |
| - |
47 | 89 | generator.processAheadOfTime(testClasses);
|
48 | 90 |
|
49 | 91 | List<String> sourceFiles = generatedFiles.getGeneratedFiles(Kind.SOURCE).keySet().stream().toList();
|
50 |
| - assertThat(sourceFiles).containsExactlyInAnyOrder(expectedSourceFilesForBasicSpringTests); |
| 92 | + assertThat(sourceFiles).containsExactlyInAnyOrder(expectedSourceFilesForBasicSpringJupiterTests); |
51 | 93 |
|
| 94 | + // AOT BUILD-TIME: COMPILATION |
52 | 95 | TestCompiler.forSystem().withFiles(generatedFiles)
|
53 | 96 | // .printFiles(System.out)
|
54 |
| - .compile(compiled -> { |
55 |
| - // just make sure compilation completes without errors |
56 |
| - }); |
| 97 | + .compile(compiled -> |
| 98 | + // AOT RUN-TIME: EXECUTION |
| 99 | + runTestsInAotMode(BasicSpringJupiterTests.class, BasicSpringJupiterSharedConfigTests.class)); |
57 | 100 | }
|
58 | 101 |
|
| 102 | + |
| 103 | + private static void runTestsInAotMode(Class<?>... testClasses) { |
| 104 | + try { |
| 105 | + System.setProperty(AotDetector.AOT_ENABLED, "true"); |
| 106 | + |
| 107 | + LauncherDiscoveryRequestBuilder builder = LauncherDiscoveryRequestBuilder.request() |
| 108 | + .filters(includeEngines("junit-jupiter")); |
| 109 | + Arrays.stream(testClasses).forEach(testClass -> builder.selectors(selectClass(testClass))); |
| 110 | + LauncherDiscoveryRequest request = builder.build(); |
| 111 | + SummaryGeneratingListener listener = new SummaryGeneratingListener(); |
| 112 | + LauncherFactory.create().execute(request, listener); |
| 113 | + TestExecutionSummary summary = listener.getSummary(); |
| 114 | + if (summary.getTotalFailureCount() > 0) { |
| 115 | + List<Throwable> exceptions = summary.getFailures().stream().map(Failure::getException).toList(); |
| 116 | + throw new MultipleFailuresError("Test execution failures", exceptions); |
| 117 | + } |
| 118 | + } |
| 119 | + finally { |
| 120 | + System.clearProperty(AotDetector.AOT_ENABLED); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private static TestClassScanner createTestClassScanner() { |
| 125 | + String classpathRoot = System.getProperty(CLASSPATH_ROOT); |
| 126 | + assertThat(classpathRoot).as(CLASSPATH_ROOT).isNotNull(); |
| 127 | + Set<Path> classpathRoots = Set.of(Paths.get(classpathRoot)); |
| 128 | + return new TestClassScanner(classpathRoots); |
| 129 | + } |
| 130 | + |
| 131 | + private static final String[] expectedSourceFilesForBasicSpringJupiterTests = { |
| 132 | + // Global |
| 133 | + "org/springframework/test/context/aot/AotTestMappings__Generated.java", |
| 134 | + // BasicSpringJupiterSharedConfigTests |
| 135 | + "org/springframework/context/event/DefaultEventListenerFactory__TestContext001_BeanDefinitions.java", |
| 136 | + "org/springframework/context/event/EventListenerMethodProcessor__TestContext001_BeanDefinitions.java", |
| 137 | + "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterSharedConfigTests__TestContext001_ApplicationContextInitializer.java", |
| 138 | + "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterSharedConfigTests__TestContext001_BeanFactoryRegistrations.java", |
| 139 | + "org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext001_BeanDefinitions.java", |
| 140 | + // BasicSpringJupiterTests -- not generated b/c already generated for BasicSpringJupiterSharedConfigTests. |
| 141 | + // "org/springframework/context/event/DefaultEventListenerFactory__TestContext00?_BeanDefinitions.java", |
| 142 | + // "org/springframework/context/event/EventListenerMethodProcessor__TestContext00?_BeanDefinitions.java", |
| 143 | + // "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests__TestContext00?_ApplicationContextInitializer.java", |
| 144 | + // "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests__TestContext00?_BeanFactoryRegistrations.java", |
| 145 | + // "org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext00?_BeanDefinitions.java", |
| 146 | + // BasicSpringJupiterTests.NestedTests |
| 147 | + "org/springframework/context/event/DefaultEventListenerFactory__TestContext002_BeanDefinitions.java", |
| 148 | + "org/springframework/context/event/EventListenerMethodProcessor__TestContext002_BeanDefinitions.java", |
| 149 | + "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests_NestedTests__TestContext002_ApplicationContextInitializer.java", |
| 150 | + "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests_NestedTests__TestContext002_BeanFactoryRegistrations.java", |
| 151 | + "org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext002_BeanDefinitions.java", |
| 152 | + }; |
| 153 | + |
59 | 154 | }
|
0 commit comments