mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-07-15 01:07:30 +08:00
Adapt to Spring AOT changes
This commit is contained in:
parent
d478a020df
commit
3702c71fc1
@ -27,8 +27,6 @@ import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.aot.AotDetector;
|
||||
import org.springframework.aot.test.generate.TestGenerationContext;
|
||||
import org.springframework.aot.test.generate.compile.CompileWithTargetClassAccess;
|
||||
import org.springframework.aot.test.generate.compile.TestCompiler;
|
||||
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementContextAutoConfiguration;
|
||||
@ -43,6 +41,8 @@ import org.springframework.boot.web.servlet.context.ServletWebServerApplicationC
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.aot.ApplicationContextAotGenerator;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.core.test.tools.CompileWithForkedClassLoader;
|
||||
import org.springframework.core.test.tools.TestCompiler;
|
||||
import org.springframework.javapoet.ClassName;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
@ -65,7 +65,7 @@ class ChildManagementContextInitializerAotTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@CompileWithTargetClassAccess
|
||||
@CompileWithForkedClassLoader
|
||||
@SuppressWarnings("unchecked")
|
||||
void aotContributedInitializerStartsManagementContext(CapturedOutput output) {
|
||||
WebApplicationContextRunner contextRunner = new WebApplicationContextRunner(
|
||||
@ -80,7 +80,7 @@ class ChildManagementContextInitializerAotTests {
|
||||
(GenericApplicationContext) context.getSourceApplicationContext(), generationContext);
|
||||
generationContext.writeGeneratedContent();
|
||||
TestCompiler compiler = TestCompiler.forSystem();
|
||||
compiler.withFiles(generationContext.getGeneratedFiles()).compile((compiled) -> {
|
||||
compiler.with(generationContext).compile((compiled) -> {
|
||||
ServletWebServerApplicationContext freshApplicationContext = new ServletWebServerApplicationContext();
|
||||
TestPropertyValues.of("server.port=0", "management.server.port=0").applyTo(freshApplicationContext);
|
||||
ApplicationContextInitializer<GenericApplicationContext> initializer = compiled
|
||||
|
@ -22,8 +22,7 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.aot.AotDetector;
|
||||
import org.springframework.aot.generate.InMemoryGeneratedFiles;
|
||||
import org.springframework.aot.test.generate.compile.CompileWithTargetClassAccess;
|
||||
import org.springframework.aot.test.generate.compile.TestCompiler;
|
||||
import org.springframework.aot.test.generate.CompilerFiles;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.support.GenericBeanDefinition;
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
@ -32,6 +31,8 @@ import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.core.test.tools.CompileWithForkedClassLoader;
|
||||
import org.springframework.core.test.tools.TestCompiler;
|
||||
import org.springframework.test.context.BootstrapUtils;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
import org.springframework.test.context.TestContextBootstrapper;
|
||||
@ -49,7 +50,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@CompileWithTargetClassAccess
|
||||
@CompileWithForkedClassLoader
|
||||
class SpringBootContextLoaderAotTests {
|
||||
|
||||
@Test
|
||||
@ -58,7 +59,7 @@ class SpringBootContextLoaderAotTests {
|
||||
TestContextAotGenerator generator = new TestContextAotGenerator(generatedFiles);
|
||||
Class<?> testClass = ExampleTest.class;
|
||||
generator.processAheadOfTime(Stream.of(testClass));
|
||||
TestCompiler.forSystem().withFiles(generatedFiles).printFiles(System.out)
|
||||
TestCompiler.forSystem().with(CompilerFiles.from(generatedFiles))
|
||||
.compile(ThrowingConsumer.of((compiled) -> assertCompiledTest(testClass)));
|
||||
}
|
||||
|
||||
|
@ -125,7 +125,7 @@ public class AotProcessor {
|
||||
private void performAotProcessing(GenericApplicationContext applicationContext) {
|
||||
FileSystemGeneratedFiles generatedFiles = new FileSystemGeneratedFiles(this::getRoot);
|
||||
DefaultGenerationContext generationContext = new DefaultGenerationContext(
|
||||
new ClassNameGenerator(this.application), generatedFiles);
|
||||
new ClassNameGenerator(ClassName.get(this.application)), generatedFiles);
|
||||
ApplicationContextAotGenerator generator = new ApplicationContextAotGenerator();
|
||||
ClassName generatedInitializerClassName = generator.processAheadOfTime(applicationContext, generationContext);
|
||||
registerEntryPointHint(generationContext, generatedInitializerClassName);
|
||||
|
@ -28,6 +28,7 @@ import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
|
||||
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor;
|
||||
import org.springframework.beans.factory.aot.BeanRegistrationCode;
|
||||
import org.springframework.beans.factory.aot.BeanRegistrationCodeFragments;
|
||||
import org.springframework.beans.factory.aot.BeanRegistrationCodeFragmentsDecorator;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.support.InstanceSupplier;
|
||||
import org.springframework.beans.factory.support.RegisteredBean;
|
||||
@ -48,7 +49,7 @@ class ConfigurationPropertiesBeanRegistrationAotProcessor implements BeanRegistr
|
||||
if (!isImmutableConfigurationPropertiesBeanDefinition(registeredBean.getMergedBeanDefinition())) {
|
||||
return null;
|
||||
}
|
||||
return BeanRegistrationAotContribution.ofBeanRegistrationCodeFragmentsCustomizer(
|
||||
return BeanRegistrationAotContribution.withCustomCodeFragments(
|
||||
(codeFragments) -> new ConfigurationPropertiesBeanRegistrationCodeFragments(codeFragments,
|
||||
registeredBean));
|
||||
|
||||
@ -59,7 +60,8 @@ class ConfigurationPropertiesBeanRegistrationAotProcessor implements BeanRegistr
|
||||
&& BindMethod.VALUE_OBJECT.equals(beanDefinition.getAttribute(BindMethod.class.getName()));
|
||||
}
|
||||
|
||||
private static class ConfigurationPropertiesBeanRegistrationCodeFragments extends BeanRegistrationCodeFragments {
|
||||
private static class ConfigurationPropertiesBeanRegistrationCodeFragments
|
||||
extends BeanRegistrationCodeFragmentsDecorator {
|
||||
|
||||
private static final Predicate<String> INCLUDE_BIND_METHOD_ATTRIBUTE_FILTER = (name) -> name
|
||||
.equals(BindMethod.class.getName());
|
||||
|
@ -110,9 +110,8 @@ class AotProcessorTests {
|
||||
assertThat(directory.resolve(
|
||||
"source/org/springframework/boot/AotProcessorTests_SampleApplication__ApplicationContextInitializer.java"))
|
||||
.exists().isRegularFile();
|
||||
assertThat(directory.resolve(
|
||||
"source/org/springframework/boot/AotProcessorTests_SampleApplication__BeanDefinitions.java"))
|
||||
.exists().isRegularFile();
|
||||
assertThat(directory.resolve("source/org/springframework/boot/AotProcessorTests__BeanDefinitions.java"))
|
||||
.exists().isRegularFile();
|
||||
assertThat(directory.resolve(
|
||||
"source/org/springframework/boot/AotProcessorTests_SampleApplication__BeanFactoryRegistrations.java"))
|
||||
.exists().isRegularFile();
|
||||
|
@ -33,7 +33,7 @@ import org.springframework.boot.ConfigurableBootstrapContext;
|
||||
import org.springframework.boot.DefaultBootstrapContext;
|
||||
import org.springframework.boot.logging.DeferredLogFactory;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.core.mock.MockSpringFactoriesLoader;
|
||||
import org.springframework.core.test.io.support.MockSpringFactoriesLoader;
|
||||
import org.springframework.mock.env.MockPropertySource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
@ -37,7 +37,7 @@ import org.springframework.boot.logging.DeferredLogFactory;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.core.mock.MockSpringFactoriesLoader;
|
||||
import org.springframework.core.test.io.support.MockSpringFactoriesLoader;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
@ -29,7 +29,7 @@ import org.springframework.boot.env.PropertiesPropertySourceLoader;
|
||||
import org.springframework.boot.env.PropertySourceLoader;
|
||||
import org.springframework.boot.env.YamlPropertySourceLoader;
|
||||
import org.springframework.core.io.support.SpringFactoriesLoader;
|
||||
import org.springframework.core.mock.MockSpringFactoriesLoader;
|
||||
import org.springframework.core.test.io.support.MockSpringFactoriesLoader;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
@ -26,15 +26,13 @@ import java.util.function.Consumer;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.aot.generate.ClassNameGenerator;
|
||||
import org.springframework.aot.generate.DefaultGenerationContext;
|
||||
import org.springframework.aot.generate.GenerationContext;
|
||||
import org.springframework.aot.generate.InMemoryGeneratedFiles;
|
||||
import org.springframework.aot.hint.ExecutableHint;
|
||||
import org.springframework.aot.hint.RuntimeHints;
|
||||
import org.springframework.aot.hint.TypeHint;
|
||||
import org.springframework.aot.hint.TypeReference;
|
||||
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
|
||||
import org.springframework.aot.test.generate.TestGenerationContext;
|
||||
import org.springframework.beans.factory.aot.AotServices;
|
||||
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution;
|
||||
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor;
|
||||
@ -278,8 +276,7 @@ class ConfigurationPropertiesBeanFactoryInitializationAotProcessorTests {
|
||||
private RuntimeHints process(ConfigurableListableBeanFactory beanFactory) {
|
||||
BeanFactoryInitializationAotContribution contribution = this.processor.processAheadOfTime(beanFactory);
|
||||
assertThat(contribution).isNotNull();
|
||||
GenerationContext generationContext = new DefaultGenerationContext(new ClassNameGenerator(Object.class),
|
||||
new InMemoryGeneratedFiles());
|
||||
GenerationContext generationContext = new TestGenerationContext();
|
||||
contribution.applyTo(generationContext, mock(BeanFactoryInitializationCode.class));
|
||||
return generationContext.getRuntimeHints();
|
||||
}
|
||||
|
@ -22,8 +22,6 @@ import java.util.function.Consumer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.aot.test.generate.TestGenerationContext;
|
||||
import org.springframework.aot.test.generate.compile.CompileWithTargetClassAccess;
|
||||
import org.springframework.aot.test.generate.compile.TestCompiler;
|
||||
import org.springframework.beans.factory.aot.AotServices;
|
||||
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
|
||||
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor;
|
||||
@ -38,6 +36,8 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.aot.ApplicationContextAotGenerator;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.core.test.tools.CompileWithForkedClassLoader;
|
||||
import org.springframework.core.test.tools.TestCompiler;
|
||||
import org.springframework.javapoet.ClassName;
|
||||
import org.springframework.test.context.support.TestPropertySourceUtils;
|
||||
|
||||
@ -90,7 +90,7 @@ class ConfigurationPropertiesBeanRegistrationAotProcessorTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@CompileWithTargetClassAccess
|
||||
@CompileWithForkedClassLoader
|
||||
void aotContributedInitializerBindsValueObject() {
|
||||
compile(createContext(ValueObjectSampleBeanConfiguration.class), (freshContext) -> {
|
||||
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(freshContext, "test.name=Hello");
|
||||
@ -101,7 +101,7 @@ class ConfigurationPropertiesBeanRegistrationAotProcessorTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@CompileWithTargetClassAccess
|
||||
@CompileWithForkedClassLoader
|
||||
void aotContributedInitializerBindsJavaBean() {
|
||||
compile(createContext(JavaBeanSampleBeanConfiguration.class), (freshContext) -> {
|
||||
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(freshContext, "test.name=Hello");
|
||||
@ -112,7 +112,7 @@ class ConfigurationPropertiesBeanRegistrationAotProcessorTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@CompileWithTargetClassAccess
|
||||
@CompileWithForkedClassLoader
|
||||
void aotContributedInitializerBindsScannedValueObject() {
|
||||
compile(createContext(ScanTestConfiguration.class), (freshContext) -> {
|
||||
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(freshContext, "b.first.name=Hello");
|
||||
@ -123,7 +123,7 @@ class ConfigurationPropertiesBeanRegistrationAotProcessorTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@CompileWithTargetClassAccess
|
||||
@CompileWithForkedClassLoader
|
||||
void aotContributedInitializerBindsScannedJavaBean() {
|
||||
compile(createContext(ScanTestConfiguration.class), (freshContext) -> {
|
||||
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(freshContext, "b.second.number=42");
|
||||
@ -145,7 +145,7 @@ class ConfigurationPropertiesBeanRegistrationAotProcessorTests {
|
||||
TestGenerationContext generationContext = new TestGenerationContext(TestTarget.class);
|
||||
ClassName className = new ApplicationContextAotGenerator().processAheadOfTime(context, generationContext);
|
||||
generationContext.writeGeneratedContent();
|
||||
TestCompiler.forSystem().withFiles(generationContext.getGeneratedFiles()).compile((compiled) -> {
|
||||
TestCompiler.forSystem().with(generationContext).compile((compiled) -> {
|
||||
GenericApplicationContext freshApplicationContext = new GenericApplicationContext();
|
||||
ApplicationContextInitializer<GenericApplicationContext> initializer = compiled
|
||||
.getInstance(ApplicationContextInitializer.class, className.toString());
|
||||
|
@ -27,7 +27,7 @@ import org.springframework.boot.testsupport.system.OutputCaptureExtension;
|
||||
import org.springframework.context.EnvironmentAware;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.mock.MockSpringFactoriesLoader;
|
||||
import org.springframework.core.test.io.support.MockSpringFactoriesLoader;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.same;
|
||||
|
Loading…
Reference in New Issue
Block a user