Introduce TestGenerationContext

This commit polishes DefaultGenerationContext to make the method
that flushes generated classes more explicit. It now throws an
IOException and TestGenerationContext has been updated to handle
that to ease its use in code that can't throw such an exception.

As this use case is likely to happen outside the Spring Framework,
this commit adds such a convenience to spring-test as well.

Closes gh-28877
This commit is contained in:
Stephane Nicoll
2022-07-26 15:58:28 +02:00
parent 60d2d16b2b
commit 3d5003ad63
18 changed files with 144 additions and 100 deletions

View File

@@ -25,8 +25,6 @@ import javax.lang.model.element.Modifier;
import org.junit.jupiter.api.Test;
import org.springframework.aop.framework.AopInfrastructureBean;
import org.springframework.aot.generate.DefaultGenerationContext;
import org.springframework.aot.generate.InMemoryGeneratedFiles;
import org.springframework.aot.generate.MethodReference;
import org.springframework.aot.test.generator.compile.Compiled;
import org.springframework.aot.test.generator.compile.TestCompiler;
@@ -64,9 +62,7 @@ class ScopedProxyBeanRegistrationAotProcessorTests {
private final TestBeanRegistrationsAotProcessor processor;
private final InMemoryGeneratedFiles generatedFiles;
private final DefaultGenerationContext generationContext;
private final TestGenerationContext generationContext;
private final MockBeanFactoryInitializationCode beanFactoryInitializationCode;
@@ -74,8 +70,7 @@ class ScopedProxyBeanRegistrationAotProcessorTests {
ScopedProxyBeanRegistrationAotProcessorTests() {
this.beanFactory = new DefaultListableBeanFactory();
this.processor = new TestBeanRegistrationsAotProcessor();
this.generatedFiles = new InMemoryGeneratedFiles();
this.generationContext = new TestGenerationContext(this.generatedFiles);
this.generationContext = new TestGenerationContext();
this.beanFactoryInitializationCode = new MockBeanFactoryInitializationCode(this.generationContext);
}
@@ -152,7 +147,7 @@ class ScopedProxyBeanRegistrationAotProcessorTests {
.build());
});
this.generationContext.writeGeneratedContent();
TestCompiler.forSystem().withFiles(this.generatedFiles).compile(compiled -> {
TestCompiler.forSystem().withFiles(this.generationContext.getGeneratedFiles()).compile(compiled -> {
DefaultListableBeanFactory freshBeanFactory = new DefaultListableBeanFactory();
freshBeanFactory.setBeanClassLoader(compiled.getClassLoader());
compiled.getInstance(Consumer.class).accept(freshBeanFactory);