Skip to content

Commit 3d5003a

Browse files
committed
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
1 parent 60d2d16 commit 3d5003a

File tree

18 files changed

+144
-100
lines changed

18 files changed

+144
-100
lines changed

spring-aop/src/test/java/org/springframework/aop/scope/ScopedProxyBeanRegistrationAotProcessorTests.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
import org.junit.jupiter.api.Test;
2626

2727
import org.springframework.aop.framework.AopInfrastructureBean;
28-
import org.springframework.aot.generate.DefaultGenerationContext;
29-
import org.springframework.aot.generate.InMemoryGeneratedFiles;
3028
import org.springframework.aot.generate.MethodReference;
3129
import org.springframework.aot.test.generator.compile.Compiled;
3230
import org.springframework.aot.test.generator.compile.TestCompiler;
@@ -64,18 +62,15 @@ class ScopedProxyBeanRegistrationAotProcessorTests {
6462

6563
private final TestBeanRegistrationsAotProcessor processor;
6664

67-
private final InMemoryGeneratedFiles generatedFiles;
68-
69-
private final DefaultGenerationContext generationContext;
65+
private final TestGenerationContext generationContext;
7066

7167
private final MockBeanFactoryInitializationCode beanFactoryInitializationCode;
7268

7369

7470
ScopedProxyBeanRegistrationAotProcessorTests() {
7571
this.beanFactory = new DefaultListableBeanFactory();
7672
this.processor = new TestBeanRegistrationsAotProcessor();
77-
this.generatedFiles = new InMemoryGeneratedFiles();
78-
this.generationContext = new TestGenerationContext(this.generatedFiles);
73+
this.generationContext = new TestGenerationContext();
7974
this.beanFactoryInitializationCode = new MockBeanFactoryInitializationCode(this.generationContext);
8075
}
8176

@@ -152,7 +147,7 @@ private void compile(BiConsumer<DefaultListableBeanFactory, Compiled> result) {
152147
.build());
153148
});
154149
this.generationContext.writeGeneratedContent();
155-
TestCompiler.forSystem().withFiles(this.generatedFiles).compile(compiled -> {
150+
TestCompiler.forSystem().withFiles(this.generationContext.getGeneratedFiles()).compile(compiled -> {
156151
DefaultListableBeanFactory freshBeanFactory = new DefaultListableBeanFactory();
157152
freshBeanFactory.setBeanClassLoader(compiled.getClassLoader());
158153
compiled.getInstance(Consumer.class).accept(freshBeanFactory);

spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanRegistrationAotContributionTests.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323

2424
import org.junit.jupiter.api.Test;
2525

26-
import org.springframework.aot.generate.DefaultGenerationContext;
27-
import org.springframework.aot.generate.InMemoryGeneratedFiles;
2826
import org.springframework.aot.generate.MethodReference;
2927
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
3028
import org.springframework.aot.test.generator.compile.CompileWithTargetClassAccess;
@@ -53,18 +51,15 @@
5351
*/
5452
class AutowiredAnnotationBeanRegistrationAotContributionTests {
5553

56-
private final InMemoryGeneratedFiles generatedFiles;
57-
58-
private final DefaultGenerationContext generationContext;
54+
private final TestGenerationContext generationContext;
5955

6056
private final MockBeanRegistrationCode beanRegistrationCode;
6157

6258
private final DefaultListableBeanFactory beanFactory;
6359

6460

6561
AutowiredAnnotationBeanRegistrationAotContributionTests() {
66-
this.generatedFiles = new InMemoryGeneratedFiles();
67-
this.generationContext = new TestGenerationContext(this.generatedFiles);
62+
this.generationContext = new TestGenerationContext();
6863
this.beanRegistrationCode = new MockBeanRegistrationCode(this.generationContext);
6964
this.beanFactory = new DefaultListableBeanFactory();
7065
}
@@ -177,7 +172,7 @@ private void compile(RegisteredBean registeredBean,
177172

178173
});
179174
this.generationContext.writeGeneratedContent();
180-
TestCompiler.forSystem().withFiles(this.generatedFiles).compile(compiled ->
175+
TestCompiler.forSystem().withFiles(this.generationContext.getGeneratedFiles()).compile(compiled ->
181176
result.accept(compiled.getInstance(BiFunction.class), compiled));
182177
}
183178

spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionMethodGeneratorTests.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@
2727

2828
import org.junit.jupiter.api.Test;
2929

30-
import org.springframework.aot.generate.DefaultGenerationContext;
3130
import org.springframework.aot.generate.GeneratedMethod;
3231
import org.springframework.aot.generate.GenerationContext;
33-
import org.springframework.aot.generate.InMemoryGeneratedFiles;
3432
import org.springframework.aot.generate.MethodReference;
3533
import org.springframework.aot.test.generator.compile.CompileWithTargetClassAccess;
3634
import org.springframework.aot.test.generator.compile.Compiled;
@@ -64,9 +62,7 @@
6462
*/
6563
class BeanDefinitionMethodGeneratorTests {
6664

67-
private final InMemoryGeneratedFiles generatedFiles;
68-
69-
private final DefaultGenerationContext generationContext;
65+
private final TestGenerationContext generationContext;
7066

7167
private final DefaultListableBeanFactory beanFactory;
7268

@@ -76,8 +72,7 @@ class BeanDefinitionMethodGeneratorTests {
7672

7773

7874
BeanDefinitionMethodGeneratorTests() {
79-
this.generatedFiles = new InMemoryGeneratedFiles();
80-
this.generationContext = new TestGenerationContext(this.generatedFiles);
75+
this.generationContext = new TestGenerationContext();
8176
this.beanFactory = new DefaultListableBeanFactory();
8277
this.methodGeneratorFactory = new BeanDefinitionMethodGeneratorFactory(
8378
new AotFactoriesLoader(this.beanFactory, new MockSpringFactoriesLoader()));
@@ -412,7 +407,7 @@ private void compile(MethodReference method,
412407
.addCode("return $L;", method.toInvokeCodeBlock()).build());
413408
});
414409
this.generationContext.writeGeneratedContent();
415-
TestCompiler.forSystem().withFiles(this.generatedFiles).printFiles(System.out).compile(compiled ->
410+
TestCompiler.forSystem().withFiles(this.generationContext.getGeneratedFiles()).compile(compiled ->
416411
result.accept((RootBeanDefinition) compiled.getInstance(Supplier.class).get(), compiled));
417412
}
418413

spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionPropertiesCodeGeneratorTests.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727

2828
import org.junit.jupiter.api.Test;
2929

30-
import org.springframework.aot.generate.DefaultGenerationContext;
3130
import org.springframework.aot.generate.GeneratedClass;
32-
import org.springframework.aot.generate.InMemoryGeneratedFiles;
3331
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
3432
import org.springframework.aot.test.generator.compile.Compiled;
3533
import org.springframework.aot.test.generator.compile.TestCompiler;
@@ -63,9 +61,7 @@ class BeanDefinitionPropertiesCodeGeneratorTests {
6361

6462
private final RootBeanDefinition beanDefinition = new RootBeanDefinition();
6563

66-
private final InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles();
67-
68-
private final DefaultGenerationContext generationContext = new TestGenerationContext(this.generatedFiles);
64+
private final TestGenerationContext generationContext = new TestGenerationContext();
6965

7066
@Test
7167
void setPrimaryWhenFalse() {
@@ -434,7 +430,7 @@ private void compile(
434430
.addStatement("return beanDefinition").build());
435431
});
436432
this.generationContext.writeGeneratedContent();
437-
TestCompiler.forSystem().withFiles(this.generatedFiles).compile(compiled -> {
433+
TestCompiler.forSystem().withFiles(this.generationContext.getGeneratedFiles()).compile(compiled -> {
438434
RootBeanDefinition suppliedBeanDefinition = (RootBeanDefinition) compiled
439435
.getInstance(Supplier.class).get();
440436
result.accept(suppliedBeanDefinition, compiled);

spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGeneratorTests.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@
3333
import org.junit.jupiter.api.Nested;
3434
import org.junit.jupiter.api.Test;
3535

36-
import org.springframework.aot.generate.DefaultGenerationContext;
3736
import org.springframework.aot.generate.GeneratedClass;
38-
import org.springframework.aot.generate.InMemoryGeneratedFiles;
3937
import org.springframework.aot.test.generator.compile.Compiled;
4038
import org.springframework.aot.test.generator.compile.TestCompiler;
4139
import org.springframework.beans.factory.config.BeanReference;
@@ -64,8 +62,7 @@
6462
class BeanDefinitionPropertyValueCodeGeneratorTests {
6563

6664
private void compile(Object value, BiConsumer<Object, Compiled> result) {
67-
InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles();
68-
DefaultGenerationContext generationContext = new TestGenerationContext(generatedFiles);
65+
TestGenerationContext generationContext = new TestGenerationContext();
6966
DeferredTypeBuilder typeBuilder = new DeferredTypeBuilder();
7067
GeneratedClass generatedClass = generationContext.getGeneratedClasses().addForFeature("TestCode", typeBuilder);
7168
CodeBlock generatedCode = new BeanDefinitionPropertyValueCodeGenerator(
@@ -78,7 +75,7 @@ private void compile(Object value, BiConsumer<Object, Compiled> result) {
7875
.returns(Object.class).addStatement("return $L", generatedCode).build());
7976
});
8077
generationContext.writeGeneratedContent();
81-
TestCompiler.forSystem().withFiles(generatedFiles).compile(compiled ->
78+
TestCompiler.forSystem().withFiles(generationContext.getGeneratedFiles()).compile(compiled ->
8279
result.accept(compiled.getInstance(Supplier.class).get(), compiled));
8380
}
8481

@@ -468,7 +465,7 @@ class BeanReferenceTests {
468465
@Test
469466
void generatedWhenBeanNameReference() {
470467
RuntimeBeanNameReference beanReference = new RuntimeBeanNameReference("test");
471-
compile(beanReference, (instance, compiler) -> {
468+
compile(beanReference, (instance, compiler) -> {
472469
RuntimeBeanReference actual = (RuntimeBeanReference) instance;
473470
assertThat(actual.getBeanName()).isEqualTo(beanReference.getBeanName());
474471
});
@@ -477,7 +474,7 @@ void generatedWhenBeanNameReference() {
477474
@Test
478475
void generatedWhenBeanReferenceByName() {
479476
RuntimeBeanReference beanReference = new RuntimeBeanReference("test");
480-
compile(beanReference, (instance, compiler) -> {
477+
compile(beanReference, (instance, compiler) -> {
481478
RuntimeBeanReference actual = (RuntimeBeanReference) instance;
482479
assertThat(actual.getBeanName()).isEqualTo(beanReference.getBeanName());
483480
assertThat(actual.getBeanType()).isEqualTo(beanReference.getBeanType());

spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContributionTests.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@
2929
import org.junit.jupiter.api.Test;
3030

3131
import org.springframework.aot.generate.ClassNameGenerator;
32-
import org.springframework.aot.generate.DefaultGenerationContext;
3332
import org.springframework.aot.generate.GenerationContext;
34-
import org.springframework.aot.generate.InMemoryGeneratedFiles;
3533
import org.springframework.aot.generate.MethodReference;
3634
import org.springframework.aot.test.generator.compile.Compiled;
3735
import org.springframework.aot.test.generator.compile.TestCompiler;
@@ -61,9 +59,7 @@ class BeanRegistrationsAotContributionTests {
6159

6260
private DefaultListableBeanFactory beanFactory;
6361

64-
private final InMemoryGeneratedFiles generatedFiles;
65-
66-
private DefaultGenerationContext generationContext;
62+
private TestGenerationContext generationContext;
6763

6864
private final BeanDefinitionMethodGeneratorFactory methodGeneratorFactory;
6965

@@ -73,8 +69,7 @@ class BeanRegistrationsAotContributionTests {
7369
BeanRegistrationsAotContributionTests() {
7470
this.springFactoriesLoader = new MockSpringFactoriesLoader();
7571
this.beanFactory = new DefaultListableBeanFactory();
76-
this.generatedFiles = new InMemoryGeneratedFiles();
77-
this.generationContext = new TestGenerationContext(this.generatedFiles);
72+
this.generationContext = new TestGenerationContext();
7873
this.methodGeneratorFactory = new BeanDefinitionMethodGeneratorFactory(
7974
new AotFactoriesLoader(this.beanFactory, this.springFactoriesLoader));
8075
this.beanFactoryInitializationCode = new MockBeanFactoryInitializationCode(this.generationContext);
@@ -102,8 +97,8 @@ void applyToAppliesContribution() {
10297

10398
@Test
10499
void applyToWhenHasNameGeneratesPrefixedFeatureName() {
105-
this.generationContext = new DefaultGenerationContext(
106-
new ClassNameGenerator(TestTarget.class, "Management"), this.generatedFiles);
100+
this.generationContext = new TestGenerationContext(
101+
new ClassNameGenerator(TestTarget.class, "Management"));
107102
this.beanFactoryInitializationCode = new MockBeanFactoryInitializationCode(this.generationContext);
108103
Map<String, BeanDefinitionMethodGenerator> registrations = new LinkedHashMap<>();
109104
RegisteredBean registeredBean = registerBean(
@@ -170,7 +165,7 @@ private void compile(
170165
.build());
171166
});
172167
this.generationContext.writeGeneratedContent();
173-
TestCompiler.forSystem().withFiles(this.generatedFiles).printFiles(System.out).compile(compiled ->
168+
TestCompiler.forSystem().withFiles(this.generationContext.getGeneratedFiles()).compile(compiled ->
174169
result.accept(compiled.getInstance(Consumer.class), compiled));
175170
}
176171

spring-beans/src/test/java/org/springframework/beans/factory/aot/InstanceSupplierCodeGeneratorTests.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
import org.assertj.core.api.ThrowingConsumer;
2626
import org.junit.jupiter.api.Test;
2727

28-
import org.springframework.aot.generate.DefaultGenerationContext;
2928
import org.springframework.aot.generate.GeneratedClass;
30-
import org.springframework.aot.generate.InMemoryGeneratedFiles;
3129
import org.springframework.aot.hint.ExecutableHint;
3230
import org.springframework.aot.hint.ExecutableMode;
3331
import org.springframework.aot.hint.ReflectionHints;
@@ -68,14 +66,11 @@
6866
*/
6967
class InstanceSupplierCodeGeneratorTests {
7068

71-
private final InMemoryGeneratedFiles generatedFiles;
72-
73-
private final DefaultGenerationContext generationContext;
69+
private final TestGenerationContext generationContext;
7470

7571

7672
InstanceSupplierCodeGeneratorTests() {
77-
this.generatedFiles = new InMemoryGeneratedFiles();
78-
this.generationContext = new TestGenerationContext(this.generatedFiles);
73+
this.generationContext = new TestGenerationContext();
7974
}
8075

8176

@@ -323,8 +318,8 @@ private void compile(DefaultListableBeanFactory beanFactory,
323318
.addStatement("return $L", generatedCode).build());
324319
});
325320
this.generationContext.writeGeneratedContent();
326-
TestCompiler.forSystem().withFiles(this.generatedFiles).printFiles(System.out).compile(compiled ->
327-
result.accept((InstanceSupplier<?>) compiled.getInstance(Supplier.class).get(), compiled));
321+
TestCompiler.forSystem().withFiles(this.generationContext.getGeneratedFiles()).compile(compiled ->
322+
result.accept((InstanceSupplier<?>) compiled.getInstance(Supplier.class).get(), compiled));
328323
}
329324

330325
}

spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorAotContributionTests.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import org.assertj.core.api.InstanceOfAssertFactories;
2525
import org.junit.jupiter.api.Test;
2626

27-
import org.springframework.aot.generate.DefaultGenerationContext;
28-
import org.springframework.aot.generate.InMemoryGeneratedFiles;
2927
import org.springframework.aot.generate.MethodReference;
3028
import org.springframework.aot.hint.ResourcePatternHint;
3129
import org.springframework.aot.test.generator.compile.Compiled;
@@ -54,16 +52,13 @@
5452
*/
5553
class ConfigurationClassPostProcessorAotContributionTests {
5654

57-
private final InMemoryGeneratedFiles generatedFiles;
58-
59-
private final DefaultGenerationContext generationContext;
55+
private final TestGenerationContext generationContext;
6056

6157
private final MockBeanFactoryInitializationCode beanFactoryInitializationCode;
6258

6359

6460
ConfigurationClassPostProcessorAotContributionTests() {
65-
this.generatedFiles = new InMemoryGeneratedFiles();
66-
this.generationContext = new TestGenerationContext(this.generatedFiles);
61+
this.generationContext = new TestGenerationContext();
6762
this.beanFactoryInitializationCode = new MockBeanFactoryInitializationCode(this.generationContext);
6863
}
6964

@@ -123,7 +118,7 @@ private void compile(BiConsumer<Consumer<DefaultListableBeanFactory>, Compiled>
123118
.build());
124119
});
125120
this.generationContext.writeGeneratedContent();
126-
TestCompiler.forSystem().withFiles(this.generatedFiles).compile(compiled ->
121+
TestCompiler.forSystem().withFiles(this.generationContext.getGeneratedFiles()).compile(compiled ->
127122
result.accept(compiled.getInstance(Consumer.class), compiled));
128123
}
129124

spring-context/src/test/java/org/springframework/context/aot/ApplicationContextAotGeneratorTests.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020

2121
import org.junit.jupiter.api.Test;
2222

23-
import org.springframework.aot.generate.DefaultGenerationContext;
24-
import org.springframework.aot.generate.InMemoryGeneratedFiles;
2523
import org.springframework.aot.test.generator.compile.Compiled;
2624
import org.springframework.aot.test.generator.compile.TestCompiler;
2725
import org.springframework.beans.BeansException;
@@ -187,11 +185,10 @@ void generateApplicationContextWhenHasBeanRegistrationAotProcessorExcludesProces
187185
private void testCompiledResult(GenericApplicationContext applicationContext,
188186
BiConsumer<ApplicationContextInitializer<GenericApplicationContext>, Compiled> result) {
189187
ApplicationContextAotGenerator generator = new ApplicationContextAotGenerator();
190-
InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles();
191-
DefaultGenerationContext generationContext = new TestGenerationContext(generatedFiles);
188+
TestGenerationContext generationContext = new TestGenerationContext();
192189
generator.generateApplicationContext(applicationContext, generationContext);
193190
generationContext.writeGeneratedContent();
194-
TestCompiler.forSystem().withFiles(generatedFiles).compile(compiled ->
191+
TestCompiler.forSystem().withFiles(generationContext.getGeneratedFiles()).compile(compiled ->
195192
result.accept(compiled.getInstance(ApplicationContextInitializer.class), compiled));
196193
}
197194

spring-context/src/test/java/org/springframework/context/aot/ReflectiveProcessorBeanRegistrationAotProcessorTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.junit.jupiter.api.Test;
2626

2727
import org.springframework.aot.generate.GenerationContext;
28-
import org.springframework.aot.generate.InMemoryGeneratedFiles;
2928
import org.springframework.aot.hint.MemberCategory;
3029
import org.springframework.aot.hint.RuntimeHints;
3130
import org.springframework.aot.hint.TypeReference;
@@ -54,7 +53,7 @@ class ReflectiveProcessorBeanRegistrationAotProcessorTests {
5453

5554
private final ReflectiveProcessorBeanRegistrationAotProcessor processor = new ReflectiveProcessorBeanRegistrationAotProcessor();
5655

57-
private final GenerationContext generationContext = new TestGenerationContext(new InMemoryGeneratedFiles());
56+
private final GenerationContext generationContext = new TestGenerationContext();
5857

5958
@Test
6059
void shouldIgnoreNonAnnotatedType() {

spring-context/src/test/java/org/springframework/context/aot/RuntimeHintsBeanFactoryInitializationAotProcessorTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.junit.jupiter.api.Test;
2727

2828
import org.springframework.aot.generate.GenerationContext;
29-
import org.springframework.aot.generate.InMemoryGeneratedFiles;
3029
import org.springframework.aot.hint.ResourceBundleHint;
3130
import org.springframework.aot.hint.RuntimeHints;
3231
import org.springframework.aot.hint.RuntimeHintsRegistrar;
@@ -56,7 +55,7 @@ class RuntimeHintsBeanFactoryInitializationAotProcessorTests {
5655

5756
@BeforeEach
5857
void setup() {
59-
this.generationContext = new TestGenerationContext(new InMemoryGeneratedFiles());
58+
this.generationContext = new TestGenerationContext();
6059
this.generator = new ApplicationContextAotGenerator();
6160
}
6261

0 commit comments

Comments
 (0)