|
| 1 | +/* |
| 2 | + * Copyright 2012-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.test.context; |
| 18 | + |
| 19 | +import java.util.stream.Stream; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import org.springframework.aot.AotDetector; |
| 24 | +import org.springframework.aot.generate.InMemoryGeneratedFiles; |
| 25 | +import org.springframework.aot.test.generate.compile.CompileWithTargetClassAccess; |
| 26 | +import org.springframework.aot.test.generate.compile.TestCompiler; |
| 27 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 28 | +import org.springframework.beans.factory.support.GenericBeanDefinition; |
| 29 | +import org.springframework.boot.SpringBootConfiguration; |
| 30 | +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
| 31 | +import org.springframework.context.ApplicationContextInitializer; |
| 32 | +import org.springframework.context.ConfigurableApplicationContext; |
| 33 | +import org.springframework.context.annotation.Import; |
| 34 | +import org.springframework.context.support.GenericApplicationContext; |
| 35 | +import org.springframework.test.context.BootstrapUtils; |
| 36 | +import org.springframework.test.context.MergedContextConfiguration; |
| 37 | +import org.springframework.test.context.TestContextBootstrapper; |
| 38 | +import org.springframework.test.context.aot.AotContextLoader; |
| 39 | +import org.springframework.test.context.aot.AotTestContextInitializers; |
| 40 | +import org.springframework.test.context.aot.TestContextAotGenerator; |
| 41 | +import org.springframework.test.util.ReflectionTestUtils; |
| 42 | +import org.springframework.util.ClassUtils; |
| 43 | +import org.springframework.util.function.ThrowingConsumer; |
| 44 | + |
| 45 | +import static org.assertj.core.api.Assertions.assertThat; |
| 46 | + |
| 47 | +/** |
| 48 | + * Tests for {@link SpringBootContextLoader} when used in AOT mode. |
| 49 | + * |
| 50 | + * @author Phillip Webb |
| 51 | + */ |
| 52 | +@CompileWithTargetClassAccess |
| 53 | +class SpringBootContextLoaderAotTests { |
| 54 | + |
| 55 | + @Test |
| 56 | + void loadContextForAotProcessingAndAotRuntime() { |
| 57 | + InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles(); |
| 58 | + TestContextAotGenerator generator = new TestContextAotGenerator(generatedFiles); |
| 59 | + Class<?> testClass = ExampleTest.class; |
| 60 | + generator.processAheadOfTime(Stream.of(testClass)); |
| 61 | + TestCompiler.forSystem().withFiles(generatedFiles).printFiles(System.out) |
| 62 | + .compile(ThrowingConsumer.of((compiled) -> assertCompiledTest(testClass))); |
| 63 | + } |
| 64 | + |
| 65 | + private void assertCompiledTest(Class<?> testClass) throws Exception { |
| 66 | + try { |
| 67 | + System.setProperty(AotDetector.AOT_ENABLED, "true"); |
| 68 | + resetAotClasses(); |
| 69 | + AotTestContextInitializers aotContextInitializers = new AotTestContextInitializers(); |
| 70 | + TestContextBootstrapper testContextBootstrapper = BootstrapUtils.resolveTestContextBootstrapper(testClass); |
| 71 | + MergedContextConfiguration mergedConfig = testContextBootstrapper.buildMergedContextConfiguration(); |
| 72 | + ApplicationContextInitializer<ConfigurableApplicationContext> contextInitializer = aotContextInitializers |
| 73 | + .getContextInitializer(testClass); |
| 74 | + ConfigurableApplicationContext context = (ConfigurableApplicationContext) ((AotContextLoader) mergedConfig |
| 75 | + .getContextLoader()).loadContextForAotRuntime(mergedConfig, contextInitializer); |
| 76 | + assertThat(context).isExactlyInstanceOf(GenericApplicationContext.class); |
| 77 | + String[] beanNames = context.getBeanNamesForType(ExampleBean.class); |
| 78 | + BeanDefinition beanDefinition = context.getBeanFactory().getBeanDefinition(beanNames[0]); |
| 79 | + assertThat(beanDefinition).isNotExactlyInstanceOf(GenericBeanDefinition.class); |
| 80 | + } |
| 81 | + finally { |
| 82 | + System.clearProperty(AotDetector.AOT_ENABLED); |
| 83 | + resetAotClasses(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private void resetAotClasses() { |
| 88 | + reset("org.springframework.test.context.aot.AotTestAttributesFactory"); |
| 89 | + reset("org.springframework.test.context.aot.AotTestContextInitializersFactory"); |
| 90 | + } |
| 91 | + |
| 92 | + private void reset(String className) { |
| 93 | + Class<?> targetClass = ClassUtils.resolveClassName(className, null); |
| 94 | + ReflectionTestUtils.invokeMethod(targetClass, "reset"); |
| 95 | + } |
| 96 | + |
| 97 | + @SpringBootTest(classes = ExampleConfig.class, webEnvironment = WebEnvironment.NONE) |
| 98 | + static class ExampleTest { |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + @SpringBootConfiguration |
| 103 | + @Import(ExampleBean.class) |
| 104 | + static class ExampleConfig { |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + static class ExampleBean { |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments