Polish and sync TestGenerationContext implementations

- new constructor for providing InMemoryGeneratedFiles
- writeGeneratedContent() now throws UncheckedIOException
This commit is contained in:
Sam Brannen
2022-08-06 14:08:49 +03:00
parent 1de0207874
commit a466179bc8
3 changed files with 63 additions and 19 deletions

View File

@@ -17,6 +17,7 @@
package org.springframework.core.testfixture.aot.generate;
import java.io.IOException;
import java.io.UncheckedIOException;
import org.springframework.aot.generate.ClassNameGenerator;
import org.springframework.aot.generate.DefaultGenerationContext;
@@ -24,20 +25,49 @@ import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.generate.InMemoryGeneratedFiles;
/**
* Test {@link GenerationContext} implementation that uses
* {@link InMemoryGeneratedFiles} and provides a convenient
* {@link TestTarget} by default.
* {@link GenerationContext} test implementation that uses
* {@link InMemoryGeneratedFiles} by default, with a convenient override of
* {@link #writeGeneratedContent()} that throws {@link UncheckedIOException}
* instead of {@link IOException}.
*
* @author Stephane Nicoll
* @author Sam Brannen
* @since 6.0
*/
public class TestGenerationContext extends DefaultGenerationContext {
public TestGenerationContext(ClassNameGenerator classNameGenerator) {
super(classNameGenerator, new InMemoryGeneratedFiles());
/**
* Create an instance using {@link TestTarget} as the default target class.
*/
public TestGenerationContext() {
this(TestTarget.class);
}
public TestGenerationContext() {
this(new ClassNameGenerator(TestTarget.class));
/**
* Create an instance using the specified {@code target}.
* @param target the default target class to use
*/
public TestGenerationContext(Class<?> target) {
this(new ClassNameGenerator(target));
}
/**
* Create an instance using the specified {@link ClassNameGenerator}.
* @param classNameGenerator the class name generator to use
*/
public TestGenerationContext(ClassNameGenerator classNameGenerator) {
this(classNameGenerator, new InMemoryGeneratedFiles());
}
/**
* Create an instance using the specified {@link ClassNameGenerator} and
* {@link InMemoryGeneratedFiles}.
* @param classNameGenerator the class name generator to use
* @param generatedFiles the generated files
*/
public TestGenerationContext(ClassNameGenerator classNameGenerator,
InMemoryGeneratedFiles generatedFiles) {
super(classNameGenerator, generatedFiles);
}
@Override
@@ -51,7 +81,7 @@ public class TestGenerationContext extends DefaultGenerationContext {
super.writeGeneratedContent();
}
catch (IOException ex) {
throw new IllegalStateException(ex);
throw new UncheckedIOException(ex);
}
}

View File

@@ -17,7 +17,7 @@
package org.springframework.core.testfixture.aot.generate;
/**
* A target used by tests of code generation.
* A <em>default target class</em> used by tests of code generation.
*
* @author Stephane Nicoll
*/

View File

@@ -17,6 +17,7 @@
package org.springframework.test.aot.generate;
import java.io.IOException;
import java.io.UncheckedIOException;
import org.springframework.aot.generate.ClassNameGenerator;
import org.springframework.aot.generate.DefaultGenerationContext;
@@ -26,21 +27,15 @@ import org.springframework.aot.generate.InMemoryGeneratedFiles;
/**
* {@link GenerationContext} test implementation that uses
* {@link InMemoryGeneratedFiles} by default, with a convenient override of
* {@link #writeGeneratedContent()} that does not throw {@link IOException}.
* {@link #writeGeneratedContent()} that throws {@link UncheckedIOException}
* instead of {@link IOException}.
*
* @author Stephane Nicoll
* @author Sam Brannen
* @since 6.0
*/
public class TestGenerationContext extends DefaultGenerationContext {
/**
* Create an instance using the specified {@link ClassNameGenerator}.
* @param classNameGenerator the class name generator to use.
*/
public TestGenerationContext(ClassNameGenerator classNameGenerator) {
super(classNameGenerator, new InMemoryGeneratedFiles());
}
/**
* Create an instance using the specified {@code target}.
* @param target the default target class to use
@@ -49,6 +44,25 @@ public class TestGenerationContext extends DefaultGenerationContext {
this(new ClassNameGenerator(target));
}
/**
* Create an instance using the specified {@link ClassNameGenerator}.
* @param classNameGenerator the class name generator to use
*/
public TestGenerationContext(ClassNameGenerator classNameGenerator) {
this(classNameGenerator, new InMemoryGeneratedFiles());
}
/**
* Create an instance using the specified {@link ClassNameGenerator} and
* {@link InMemoryGeneratedFiles}.
* @param classNameGenerator the class name generator to use
* @param generatedFiles the generated files
*/
public TestGenerationContext(ClassNameGenerator classNameGenerator,
InMemoryGeneratedFiles generatedFiles) {
super(classNameGenerator, generatedFiles);
}
@Override
public InMemoryGeneratedFiles getGeneratedFiles() {
return (InMemoryGeneratedFiles) super.getGeneratedFiles();
@@ -60,7 +74,7 @@ public class TestGenerationContext extends DefaultGenerationContext {
super.writeGeneratedContent();
}
catch (IOException ex) {
throw new IllegalStateException(ex);
throw new UncheckedIOException(ex);
}
}