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

@@ -24,8 +24,6 @@ import javax.lang.model.element.Modifier;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.Test;
import org.springframework.aot.generate.DefaultGenerationContext;
import org.springframework.aot.generate.InMemoryGeneratedFiles;
import org.springframework.aot.generate.MethodReference;
import org.springframework.aot.hint.ResourcePatternHint;
import org.springframework.aot.test.generator.compile.Compiled;
@@ -54,16 +52,13 @@ import static org.assertj.core.api.Assertions.entry;
*/
class ConfigurationClassPostProcessorAotContributionTests {
private final InMemoryGeneratedFiles generatedFiles;
private final DefaultGenerationContext generationContext;
private final TestGenerationContext generationContext;
private final MockBeanFactoryInitializationCode beanFactoryInitializationCode;
ConfigurationClassPostProcessorAotContributionTests() {
this.generatedFiles = new InMemoryGeneratedFiles();
this.generationContext = new TestGenerationContext(this.generatedFiles);
this.generationContext = new TestGenerationContext();
this.beanFactoryInitializationCode = new MockBeanFactoryInitializationCode(this.generationContext);
}
@@ -123,7 +118,7 @@ class ConfigurationClassPostProcessorAotContributionTests {
.build());
});
this.generationContext.writeGeneratedContent();
TestCompiler.forSystem().withFiles(this.generatedFiles).compile(compiled ->
TestCompiler.forSystem().withFiles(this.generationContext.getGeneratedFiles()).compile(compiled ->
result.accept(compiled.getInstance(Consumer.class), compiled));
}

View File

@@ -20,8 +20,6 @@ import java.util.function.BiConsumer;
import org.junit.jupiter.api.Test;
import org.springframework.aot.generate.DefaultGenerationContext;
import org.springframework.aot.generate.InMemoryGeneratedFiles;
import org.springframework.aot.test.generator.compile.Compiled;
import org.springframework.aot.test.generator.compile.TestCompiler;
import org.springframework.beans.BeansException;
@@ -187,11 +185,10 @@ class ApplicationContextAotGeneratorTests {
private void testCompiledResult(GenericApplicationContext applicationContext,
BiConsumer<ApplicationContextInitializer<GenericApplicationContext>, Compiled> result) {
ApplicationContextAotGenerator generator = new ApplicationContextAotGenerator();
InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles();
DefaultGenerationContext generationContext = new TestGenerationContext(generatedFiles);
TestGenerationContext generationContext = new TestGenerationContext();
generator.generateApplicationContext(applicationContext, generationContext);
generationContext.writeGeneratedContent();
TestCompiler.forSystem().withFiles(generatedFiles).compile(compiled ->
TestCompiler.forSystem().withFiles(generationContext.getGeneratedFiles()).compile(compiled ->
result.accept(compiled.getInstance(ApplicationContextInitializer.class), compiled));
}

View File

@@ -25,7 +25,6 @@ import java.lang.annotation.Target;
import org.junit.jupiter.api.Test;
import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.generate.InMemoryGeneratedFiles;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.TypeReference;
@@ -54,7 +53,7 @@ class ReflectiveProcessorBeanRegistrationAotProcessorTests {
private final ReflectiveProcessorBeanRegistrationAotProcessor processor = new ReflectiveProcessorBeanRegistrationAotProcessor();
private final GenerationContext generationContext = new TestGenerationContext(new InMemoryGeneratedFiles());
private final GenerationContext generationContext = new TestGenerationContext();
@Test
void shouldIgnoreNonAnnotatedType() {

View File

@@ -26,7 +26,6 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.generate.InMemoryGeneratedFiles;
import org.springframework.aot.hint.ResourceBundleHint;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
@@ -56,7 +55,7 @@ class RuntimeHintsBeanFactoryInitializationAotProcessorTests {
@BeforeEach
void setup() {
this.generationContext = new TestGenerationContext(new InMemoryGeneratedFiles());
this.generationContext = new TestGenerationContext();
this.generator = new ApplicationContextAotGenerator();
}

View File

@@ -20,8 +20,6 @@ import java.util.function.BiConsumer;
import org.junit.jupiter.api.Test;
import org.springframework.aot.generate.DefaultGenerationContext;
import org.springframework.aot.generate.InMemoryGeneratedFiles;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
@@ -110,11 +108,10 @@ class ApplicationContextAotGeneratorRuntimeHintsTests {
@SuppressWarnings({"rawtypes", "unchecked"})
private void compile(GenericApplicationContext applicationContext, BiConsumer<RuntimeHints, RuntimeHintsInvocations> initializationResult) {
ApplicationContextAotGenerator generator = new ApplicationContextAotGenerator();
InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles();
DefaultGenerationContext generationContext = new TestGenerationContext(generatedFiles);
TestGenerationContext generationContext = new TestGenerationContext();
generator.generateApplicationContext(applicationContext, generationContext);
generationContext.writeGeneratedContent();
TestCompiler.forSystem().withFiles(generatedFiles).compile(compiled -> {
TestCompiler.forSystem().withFiles(generationContext.getGeneratedFiles()).compile(compiled -> {
ApplicationContextInitializer instance = compiled.getInstance(ApplicationContextInitializer.class);
GenericApplicationContext freshContext = new GenericApplicationContext();
RuntimeHintsInvocations recordedInvocations = RuntimeHintsRecorder.record(() -> {