Skip to content

Commit

Permalink
Rename TestAotMappings to AotTestContextInitializers
Browse files Browse the repository at this point in the history
- for consistency with AotTestAttributes and similar classes

See gh-28205, gh-28204
  • Loading branch information
sbrannen committed Sep 13, 2022
1 parent a3b6b41 commit e7a297a
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import org.springframework.util.ReflectionUtils;

/**
* {@code TestAotMappings} provides mappings from test classes to AOT-optimized
* context initializers.
* {@code AotTestContextInitializers} provides mappings from test classes to
* AOT-optimized context initializers.
*
* <p>If a test class is not {@linkplain #isSupportedTestClass(Class) supported} in
* AOT mode, {@link #getContextInitializer(Class)} will return {@code null}.
Expand All @@ -42,27 +42,27 @@
* @author Stephane Nicoll
* @since 6.0
*/
public class TestAotMappings {
public class AotTestContextInitializers {

// TODO Add support in ClassNameGenerator for supplying a predefined class name.
// There is a similar issue in Spring Boot where code relies on a generated name.
// Ideally we would generate a class named: org.springframework.test.context.aot.GeneratedTestAotMappings
static final String GENERATED_MAPPINGS_CLASS_NAME = TestAotMappings.class.getName() + "__Generated";
// Ideally we would generate a class named: org.springframework.test.context.aot.GeneratedAotTestContextInitializers
static final String GENERATED_MAPPINGS_CLASS_NAME = AotTestContextInitializers.class.getName() + "__Generated";

static final String GENERATED_MAPPINGS_METHOD_NAME = "getContextInitializers";

private final Map<String, Supplier<ApplicationContextInitializer<ConfigurableApplicationContext>>> contextInitializers;


public TestAotMappings() {
public AotTestContextInitializers() {
this(GENERATED_MAPPINGS_CLASS_NAME);
}

TestAotMappings(String initializerClassName) {
AotTestContextInitializers(String initializerClassName) {
this(loadContextInitializersMap(initializerClassName));
}

TestAotMappings(Map<String, Supplier<ApplicationContextInitializer<ConfigurableApplicationContext>>> contextInitializers) {
AotTestContextInitializers(Map<String, Supplier<ApplicationContextInitializer<ConfigurableApplicationContext>>> contextInitializers) {
this.contextInitializers = contextInitializers;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@
import org.springframework.util.MultiValueMap;

/**
* Internal code generator for mappings used by {@link TestAotMappings}.
* Internal code generator for mappings used by {@link AotTestContextInitializers}.
*
* @author Sam Brannen
* @since 6.0
*/
class TestAotMappingsCodeGenerator {
class AotTestContextInitializersCodeGenerator {

private static final Log logger = LogFactory.getLog(TestAotMappingsCodeGenerator.class);
private static final Log logger = LogFactory.getLog(AotTestContextInitializersCodeGenerator.class);

private static final ParameterizedTypeName CONTEXT_INITIALIZER = ParameterizedTypeName.get(
ClassName.get(ApplicationContextInitializer.class),
Expand All @@ -67,7 +67,7 @@ class TestAotMappingsCodeGenerator {
private final GeneratedClass generatedClass;


TestAotMappingsCodeGenerator(MultiValueMap<ClassName, Class<?>> initializerClassMappings,
AotTestContextInitializersCodeGenerator(MultiValueMap<ClassName, Class<?>> initializerClassMappings,
GeneratedClasses generatedClasses) {

this.initializerClassMappings = initializerClassMappings;
Expand All @@ -82,13 +82,13 @@ GeneratedClass getGeneratedClass() {
private void generateType(TypeSpec.Builder type) {
logger.debug(LogMessage.format("Generating AOT test mappings in %s",
this.generatedClass.getName().reflectionName()));
type.addJavadoc("Generated mappings for {@link $T}.", TestAotMappings.class);
type.addJavadoc("Generated mappings for {@link $T}.", AotTestContextInitializers.class);
type.addModifiers(Modifier.PUBLIC);
type.addMethod(generateMappingMethod());
}

private MethodSpec generateMappingMethod() {
MethodSpec.Builder method = MethodSpec.methodBuilder(TestAotMappings.GENERATED_MAPPINGS_METHOD_NAME);
MethodSpec.Builder method = MethodSpec.methodBuilder(AotTestContextInitializers.GENERATED_MAPPINGS_METHOD_NAME);
method.addModifiers(Modifier.PUBLIC, Modifier.STATIC);
method.returns(CONTEXT_SUPPLIER_MAP);
method.addCode(generateMappingCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,13 @@ private String nextTestContextId() {
}

private void generateTestAotMappings(MultiValueMap<ClassName, Class<?>> initializerClassMappings) {
ClassNameGenerator classNameGenerator = new ClassNameGenerator(TestAotMappings.class);
ClassNameGenerator classNameGenerator = new ClassNameGenerator(AotTestContextInitializers.class);
DefaultGenerationContext generationContext =
new DefaultGenerationContext(classNameGenerator, this.generatedFiles, this.runtimeHints);
GeneratedClasses generatedClasses = generationContext.getGeneratedClasses();

TestAotMappingsCodeGenerator codeGenerator =
new TestAotMappingsCodeGenerator(initializerClassMappings, generatedClasses);
AotTestContextInitializersCodeGenerator codeGenerator =
new AotTestContextInitializersCodeGenerator(initializerClassMappings, generatedClasses);
generationContext.writeGeneratedContent();
String className = codeGenerator.getGeneratedClass().getName().reflectionName();
registerPublicMethods(className);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.SmartContextLoader;
import org.springframework.test.context.aot.AotContextLoader;
import org.springframework.test.context.aot.TestAotMappings;
import org.springframework.test.context.aot.AotTestContextInitializers;
import org.springframework.test.context.aot.TestContextAotException;
import org.springframework.util.Assert;

Expand All @@ -57,7 +57,7 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext
static final ContextCache defaultContextCache = new DefaultContextCache();

@Nullable
private final TestAotMappings testAotMappings = getTestAotMappings();
private final AotTestContextInitializers aotTestContextInitializers = getAotTestContextInitializers();

private final ContextCache contextCache;

Expand Down Expand Up @@ -168,7 +168,7 @@ protected ApplicationContext loadContextInternal(MergedContextConfiguration merg
protected ApplicationContext loadContextInAotMode(MergedContextConfiguration mergedConfig) throws Exception {
Class<?> testClass = mergedConfig.getTestClass();
ApplicationContextInitializer<ConfigurableApplicationContext> contextInitializer =
this.testAotMappings.getContextInitializer(testClass);
this.aotTestContextInitializers.getContextInitializer(testClass);
Assert.state(contextInitializer != null,
() -> "Failed to load AOT ApplicationContextInitializer for test class [%s]"
.formatted(testClass.getName()));
Expand Down Expand Up @@ -200,17 +200,18 @@ private ContextLoader getContextLoader(MergedContextConfiguration mergedConfig)
* Determine if we are running in AOT mode for the supplied test class.
*/
private boolean runningInAotMode(Class<?> testClass) {
return (this.testAotMappings != null && this.testAotMappings.isSupportedTestClass(testClass));
return (this.aotTestContextInitializers != null &&
this.aotTestContextInitializers.isSupportedTestClass(testClass));
}

@Nullable
private static TestAotMappings getTestAotMappings() {
private static AotTestContextInitializers getAotTestContextInitializers() {
if (AotDetector.useGeneratedArtifacts()) {
try {
return new TestAotMappings();
return new AotTestContextInitializers();
}
catch (Exception ex) {
throw new IllegalStateException("Failed to instantiate TestAotMappings", ex);
throw new IllegalStateException("Failed to instantiate AotTestContextInitializers", ex);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.springframework.core.Conventions;
import org.springframework.lang.Nullable;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.aot.TestAotMappings;
import org.springframework.test.context.aot.AotTestContextInitializers;

/**
* {@code TestExecutionListener} which provides support for dependency
Expand Down Expand Up @@ -61,7 +61,7 @@ public class DependencyInjectionTestExecutionListener extends AbstractTestExecut
private static final Log logger = LogFactory.getLog(DependencyInjectionTestExecutionListener.class);

@Nullable
private final TestAotMappings testAotMappings = getTestAotMappings();
private final AotTestContextInitializers aotTestContextInitializers = getAotTestContextInitializers();


/**
Expand Down Expand Up @@ -162,17 +162,18 @@ private void injectDependenciesInAotMode(TestContext testContext) throws Excepti
* Determine if we are running in AOT mode for the supplied test class.
*/
private boolean runningInAotMode(Class<?> testClass) {
return (this.testAotMappings != null && this.testAotMappings.isSupportedTestClass(testClass));
return (this.aotTestContextInitializers != null &&
this.aotTestContextInitializers.isSupportedTestClass(testClass));
}

@Nullable
private static TestAotMappings getTestAotMappings() {
private static AotTestContextInitializers getAotTestContextInitializers() {
if (AotDetector.useGeneratedArtifacts()) {
try {
return new TestAotMappings();
return new AotTestContextInitializers();
}
catch (Exception ex) {
throw new IllegalStateException("Failed to instantiate TestAotMappings", ex);
throw new IllegalStateException("Failed to instantiate AotTestContextInitializers", ex);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ abstract class AbstractAotTests {

static final String[] expectedSourceFilesForBasicSpringTests = {
// Global
"org/springframework/test/context/aot/TestAotMappings__Generated.java",
"org/springframework/test/context/aot/AotTestContextInitializers__Generated.java",
"org/springframework/test/context/aot/AotTestAttributes__Generated.java",
// BasicSpringJupiterSharedConfigTests
"org/springframework/context/event/DefaultEventListenerFactory__TestContext001_BeanDefinitions.java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

/**
* Tests for {@link TestContextAotGenerator}, {@link TestAotMappings},
* Tests for {@link TestContextAotGenerator}, {@link AotTestContextInitializers},
* {@link AotTestAttributes}, {@link AotContextLoader}, and run-time hints.
*
* @author Sam Brannen
Expand Down Expand Up @@ -126,11 +126,11 @@ void endToEndTests() {
assertThat(aotAttributes.getString("bogus")).isNull();
assertThat(aotAttributes.getBoolean("bogus")).isFalse();

TestAotMappings testAotMappings = new TestAotMappings();
AotTestContextInitializers aotContextInitializers = new AotTestContextInitializers();
for (Class<?> testClass : testClasses) {
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
ApplicationContextInitializer<ConfigurableApplicationContext> contextInitializer =
testAotMappings.getContextInitializer(testClass);
aotContextInitializers.getContextInitializer(testClass);
assertThat(contextInitializer).isNotNull();
ApplicationContext context = ((AotContextLoader) mergedConfig.getContextLoader())
.loadContextForAotRuntime(mergedConfig, contextInitializer);
Expand All @@ -153,7 +153,7 @@ else if (testClass.getPackageName().contains("jdbc")) {
}

private static void assertRuntimeHints(RuntimeHints runtimeHints) {
assertReflectionRegistered(runtimeHints, TestAotMappings.GENERATED_MAPPINGS_CLASS_NAME, INVOKE_PUBLIC_METHODS);
assertReflectionRegistered(runtimeHints, AotTestContextInitializers.GENERATED_MAPPINGS_CLASS_NAME, INVOKE_PUBLIC_METHODS);
assertReflectionRegistered(runtimeHints, AotTestAttributesCodeGenerator.GENERATED_ATTRIBUTES_CLASS_NAME, INVOKE_PUBLIC_METHODS);

Stream.of(
Expand Down Expand Up @@ -356,7 +356,7 @@ record Mapping(MergedContextConfiguration mergedConfig, ClassName className) {

private static final String[] expectedSourceFiles = {
// Global
"org/springframework/test/context/aot/TestAotMappings__Generated.java",
"org/springframework/test/context/aot/AotTestContextInitializers__Generated.java",
"org/springframework/test/context/aot/AotTestAttributes__Generated.java",
// BasicSpringJupiterSharedConfigTests
"org/springframework/context/event/DefaultEventListenerFactory__TestContext001_BeanDefinitions.java",
Expand Down

0 comments on commit e7a297a

Please sign in to comment.