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

@@ -27,6 +27,10 @@ import org.springframework.util.Assert;
/**
* Default {@link GenerationContext} implementation.
*
* <p>Generated classes are flushed out using {@link #writeGeneratedContent()}
* and should be called once the generation process using this instance has
* completed.
*
* @author Phillip Webb
* @author Stephane Nicoll
* @since 6.0
@@ -104,13 +108,8 @@ public class DefaultGenerationContext implements GenerationContext {
/**
* Write any generated content out to the generated files.
*/
public void writeGeneratedContent() {
try {
this.generatedClasses.writeTo(this.generatedFiles);
}
catch (IOException ex) {
throw new IllegalStateException(ex);
}
public void writeGeneratedContent() throws IOException {
this.generatedClasses.writeTo(this.generatedFiles);
}
}

View File

@@ -16,6 +16,7 @@
package org.springframework.aot.generate;
import java.io.IOException;
import java.util.function.Consumer;
import org.junit.jupiter.api.Test;
@@ -118,7 +119,7 @@ class DefaultGenerationContextTests {
}
@Test
void withNameKeepsTrackOfAllGeneratedFiles() {
void withNameKeepsTrackOfAllGeneratedFiles() throws IOException {
DefaultGenerationContext context = new DefaultGenerationContext(
new ClassNameGenerator(TestTarget.class), this.generatedFiles);
context.getGeneratedClasses().addForFeature("Test", typeSpecCustomizer);
@@ -132,7 +133,7 @@ class DefaultGenerationContextTests {
}
@Test
void withNameGeneratesUniqueName() {
void withNameGeneratesUniqueName() throws IOException {
DefaultGenerationContext context = new DefaultGenerationContext(
new ClassNameGenerator(Object.class), this.generatedFiles);
context.withName("Test").getGeneratedClasses()

View File

@@ -16,25 +16,43 @@
package org.springframework.core.testfixture.aot.generate;
import java.io.IOException;
import org.springframework.aot.generate.ClassNameGenerator;
import org.springframework.aot.generate.DefaultGenerationContext;
import org.springframework.aot.generate.GeneratedFiles;
import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.generate.InMemoryGeneratedFiles;
/**
* Test {@link GenerationContext} implementation that uses
* {@link TestTarget} as the main target.
* {@link InMemoryGeneratedFiles} and provides a convenient
* {@link TestTarget} by default.
*
* @author Stephane Nicoll
*/
public class TestGenerationContext extends DefaultGenerationContext {
public TestGenerationContext(GeneratedFiles generatedFiles) {
super(new ClassNameGenerator(TestTarget.class), generatedFiles);
public TestGenerationContext(ClassNameGenerator classNameGenerator) {
super(classNameGenerator, new InMemoryGeneratedFiles());
}
public TestGenerationContext() {
this(new InMemoryGeneratedFiles());
this(new ClassNameGenerator(TestTarget.class));
}
@Override
public InMemoryGeneratedFiles getGeneratedFiles() {
return (InMemoryGeneratedFiles) super.getGeneratedFiles();
}
@Override
public void writeGeneratedContent() {
try {
super.writeGeneratedContent();
}
catch (IOException ex) {
throw new IllegalStateException(ex);
}
}
}