Adapt to changes in DefaultGenerationContext

See https://github.com/spring-projects/spring-framework/issues/28877
pull/31887/head
Stephane Nicoll 2 years ago
parent d6cce1f441
commit 193c1d7acf

@ -25,9 +25,6 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.aot.generate.ClassNameGenerator;
import org.springframework.aot.generate.DefaultGenerationContext;
import org.springframework.aot.generate.InMemoryGeneratedFiles;
import org.springframework.aot.test.generator.compile.CompileWithTargetClassAccess; import org.springframework.aot.test.generator.compile.CompileWithTargetClassAccess;
import org.springframework.aot.test.generator.compile.TestCompiler; import org.springframework.aot.test.generator.compile.TestCompiler;
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
@ -45,6 +42,7 @@ import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.aot.ApplicationContextAotGenerator; import org.springframework.context.aot.ApplicationContextAotGenerator;
import org.springframework.context.support.GenericApplicationContext; import org.springframework.context.support.GenericApplicationContext;
import org.springframework.javapoet.ClassName; import org.springframework.javapoet.ClassName;
import org.springframework.test.aot.generate.TestGenerationContext;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -76,14 +74,12 @@ class ChildManagementContextInitializerAotTests {
ServletManagementContextAutoConfiguration.class, WebEndpointAutoConfiguration.class, ServletManagementContextAutoConfiguration.class, WebEndpointAutoConfiguration.class,
EndpointAutoConfiguration.class)); EndpointAutoConfiguration.class));
contextRunner.withPropertyValues("server.port=0", "management.server.port=0").prepare((context) -> { contextRunner.withPropertyValues("server.port=0", "management.server.port=0").prepare((context) -> {
InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles(); TestGenerationContext generationContext = new TestGenerationContext(TestTarget.class);
DefaultGenerationContext generationContext = new DefaultGenerationContext(
new ClassNameGenerator(TestTarget.class), generatedFiles);
ClassName className = new ApplicationContextAotGenerator().generateApplicationContext( ClassName className = new ApplicationContextAotGenerator().generateApplicationContext(
(GenericApplicationContext) context.getSourceApplicationContext(), generationContext); (GenericApplicationContext) context.getSourceApplicationContext(), generationContext);
generationContext.writeGeneratedContent(); generationContext.writeGeneratedContent();
TestCompiler compiler = TestCompiler.forSystem(); TestCompiler compiler = TestCompiler.forSystem();
compiler.withFiles(generatedFiles).compile((compiled) -> { compiler.withFiles(generationContext.getGeneratedFiles()).compile((compiled) -> {
ServletWebServerApplicationContext freshApplicationContext = new ServletWebServerApplicationContext(); ServletWebServerApplicationContext freshApplicationContext = new ServletWebServerApplicationContext();
TestPropertyValues.of("server.port=0", "management.server.port=0").applyTo(freshApplicationContext); TestPropertyValues.of("server.port=0", "management.server.port=0").applyTo(freshApplicationContext);
ApplicationContextInitializer<GenericApplicationContext> initializer = compiled ApplicationContextInitializer<GenericApplicationContext> initializer = compiled

@ -101,7 +101,7 @@ public class AotProcessor {
/** /**
* Trigger the processing of the application managed by this instance. * Trigger the processing of the application managed by this instance.
*/ */
public void process() { public void process() throws IOException {
deleteExistingOutput(); deleteExistingOutput();
AotProcessorHook hook = new AotProcessorHook(); AotProcessorHook hook = new AotProcessorHook();
SpringApplicationHooks.withHook(hook, this::callApplicationMainMethod); SpringApplicationHooks.withHook(hook, this::callApplicationMainMethod);
@ -142,7 +142,7 @@ public class AotProcessor {
} }
} }
private void performAotProcessing(GenericApplicationContext applicationContext) { private void performAotProcessing(GenericApplicationContext applicationContext) throws IOException {
FileSystemGeneratedFiles generatedFiles = new FileSystemGeneratedFiles(this::getRoot); FileSystemGeneratedFiles generatedFiles = new FileSystemGeneratedFiles(this::getRoot);
DefaultGenerationContext generationContext = new DefaultGenerationContext( DefaultGenerationContext generationContext = new DefaultGenerationContext(
new ClassNameGenerator(this.application), generatedFiles); new ClassNameGenerator(this.application), generatedFiles);

@ -45,7 +45,7 @@ class AotProcessorTests {
} }
@Test @Test
void processApplicationInvokesRunMethod(@TempDir Path directory) { void processApplicationInvokesRunMethod(@TempDir Path directory) throws IOException {
String[] arguments = new String[] { "1", "2" }; String[] arguments = new String[] { "1", "2" };
AotProcessor processor = new AotProcessor(SampleApplication.class, arguments, directory.resolve("source"), AotProcessor processor = new AotProcessor(SampleApplication.class, arguments, directory.resolve("source"),
directory.resolve("resource"), directory.resolve("class"), "com.example", "example"); directory.resolve("resource"), directory.resolve("class"), "com.example", "example");

Loading…
Cancel
Save