Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit d9d03db

Browse files
committed
Rename AotContextLoader to AotTestMappings and update Javadoc
See gh-1262
1 parent 3ef445f commit d9d03db

File tree

5 files changed

+74
-50
lines changed

5 files changed

+74
-50
lines changed

spring-native/src/main/java/org/springframework/aot/test/AotCacheAwareContextLoaderDelegate.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,20 @@ public class AotCacheAwareContextLoaderDelegate extends DefaultCacheAwareContext
4040

4141
private static final Log logger = LogFactory.getLog(AotCacheAwareContextLoaderDelegate.class);
4242

43-
private final AotContextLoader aotContextLoader;
43+
private final AotTestMappings aotTestMappings;
4444

4545
public AotCacheAwareContextLoaderDelegate() {
46-
this.aotContextLoader = new AotContextLoader();
46+
this.aotTestMappings = new AotTestMappings();
4747
}
4848

49-
AotCacheAwareContextLoaderDelegate(AotContextLoader aotContextLoader, ContextCache contextCache) {
49+
AotCacheAwareContextLoaderDelegate(AotTestMappings aotTestMappings, ContextCache contextCache) {
5050
super(contextCache);
51-
this.aotContextLoader = aotContextLoader;
51+
this.aotTestMappings = aotTestMappings;
5252
}
5353

5454
@Override
5555
protected ApplicationContext loadContextInternal(MergedContextConfiguration config) throws Exception {
56-
SmartContextLoader contextLoader = this.aotContextLoader.getContextLoader(config.getTestClass());
56+
SmartContextLoader contextLoader = this.aotTestMappings.getContextLoader(config.getTestClass());
5757
if (contextLoader != null) {
5858
Assert.isInstanceOf(AotMergedContextConfiguration.class, config);
5959
logger.info("Loading test ApplicationContext in AOT mode using " + contextLoader);
@@ -94,7 +94,7 @@ private MergedContextConfiguration replaceIfNecessary(MergedContextConfiguration
9494

9595
Class<?> testClass = mergedContextConfiguration.getTestClass();
9696
Class<? extends ApplicationContextInitializer<?>> contextInitializerClass =
97-
this.aotContextLoader.getContextInitializerClass(testClass);
97+
this.aotTestMappings.getContextInitializerClass(testClass);
9898

9999
if (contextInitializerClass != null) {
100100
return new AotMergedContextConfiguration(testClass, contextInitializerClass, mergedContextConfiguration,

spring-native/src/main/java/org/springframework/aot/test/AotDependencyInjectionTestExecutionListener.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@
3939
*
4040
* @author Sam Brannen
4141
* @see DependencyInjectionTestExecutionListener
42-
* @see AotContextLoader
42+
* @see AotTestMappings
4343
* @see AotCacheAwareContextLoaderDelegate
4444
*/
4545
public class AotDependencyInjectionTestExecutionListener extends DependencyInjectionTestExecutionListener {
4646

4747
private static final Log logger = LogFactory.getLog(AotDependencyInjectionTestExecutionListener.class);
4848

49-
private static final AotContextLoader aotContextLoader = getAotContextLoader();
49+
private static final AotTestMappings aotTestMappings = getAotTestMappings();
5050

5151

5252
@Override
@@ -96,16 +96,16 @@ private void injectDependenciesInAotMode(TestContext testContext) throws Excepti
9696
}
9797

9898
private boolean isSupportedTestClass(TestContext testContext) {
99-
return aotContextLoader != null && aotContextLoader.isSupportedTestClass(testContext.getTestClass());
99+
return aotTestMappings != null && aotTestMappings.isSupportedTestClass(testContext.getTestClass());
100100
}
101101

102-
private static AotContextLoader getAotContextLoader() {
102+
private static AotTestMappings getAotTestMappings() {
103103
if (AotModeDetector.isRunningAotTests()) {
104104
try {
105-
return new AotContextLoader();
105+
return new AotTestMappings();
106106
}
107107
catch (Exception ex) {
108-
throw new IllegalStateException("Failed to instantiate AotContextLoader", ex);
108+
throw new IllegalStateException("Failed to instantiate AotTestMappings", ex);
109109
}
110110
}
111111
return null;

spring-native/src/main/java/org/springframework/aot/test/AotContextLoader.java spring-native/src/main/java/org/springframework/aot/test/AotTestMappings.java

+44-20
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,77 @@
2626
import org.springframework.util.ReflectionUtils;
2727

2828
/**
29-
* Load generated application contexts for supported test classes.
29+
* {@code AotTestMappings} provides mappings from test classes to AOT context loaders
30+
* and context initializers.
31+
* <p/>
32+
* If a test class is not {@linkplain #isSupportedTestClass(Class) supported} in
33+
* AOT mode, {@link #getContextLoader(Class)} and {@link #getContextInitializerClass(Class)}
34+
* will return {@code null}.
3035
* <p/>
3136
* Reflectively accesses {@value #INITIALIZER_NAME} generated by the
32-
* {@code TestContextAotProcessor} in the {@code spring-aot-test} module to retrieve the
33-
* mapping of all supported test classes.
37+
* {@code TestContextAotProcessor} in the {@code spring-aot-test} module to retrieve
38+
* the mappings.
3439
*
3540
* @author Stephane Nicoll
3641
* @author Sam Brannen
3742
*/
38-
class AotContextLoader {
43+
class AotTestMappings {
3944

4045
private static final String INITIALIZER_NAME = "org.springframework.aot.TestContextBootstrapInitializer";
4146

47+
4248
private final Map<String, Supplier<SmartContextLoader>> contextLoaders;
4349

4450
private final Map<String, Class<? extends ApplicationContextInitializer<?>>> contextInitializers;
4551

4652

47-
AotContextLoader(Map<String, Supplier<SmartContextLoader>> contextLoaders,
53+
AotTestMappings(Map<String, Supplier<SmartContextLoader>> contextLoaders,
4854
Map<String, Class<? extends ApplicationContextInitializer<?>>> contextInitializers) {
4955

5056
this.contextLoaders = contextLoaders;
5157
this.contextInitializers = contextInitializers;
5258
}
5359

5460
@SuppressWarnings("unchecked")
55-
AotContextLoader(String initializerClassName) {
61+
AotTestMappings(String initializerClassName) {
5662
this(loadMap(initializerClassName, "getContextLoaders"), loadMap(initializerClassName, "getContextInitializers"));
5763
}
5864

59-
AotContextLoader() {
65+
AotTestMappings() {
6066
this(INITIALIZER_NAME);
6167
}
6268

69+
/**
70+
* Determine if the specified test class has an AOT-generated application context.
71+
* <p>If this method returns {@code true}, {@link #getContextLoader(Class)} and
72+
* {@link #getContextInitializerClass(Class)} should return non-null values.
73+
*/
74+
boolean isSupportedTestClass(Class<?> testClass) {
75+
return this.contextLoaders.containsKey(testClass.getName());
76+
}
77+
78+
/**
79+
* Get the AOT {@link SmartContextLoader} for the specified test class.
80+
* @return the AOT context loader, or {@code null} if there is no AOT context
81+
* loader for the specified test class
82+
* @see #isSupportedTestClass(Class)
83+
*/
84+
SmartContextLoader getContextLoader(Class<?> testClass) {
85+
Supplier<SmartContextLoader> supplier = this.contextLoaders.get(testClass.getName());
86+
return (supplier != null) ? supplier.get() : null;
87+
}
88+
89+
/**
90+
* Get the AOT {@link ApplicationContextInitializer} for the specified test class.
91+
* @return the AOT context initializer, or {@code null} if there is no AOT context
92+
* initializer for the specified test class
93+
* @see #isSupportedTestClass(Class)
94+
*/
95+
Class<? extends ApplicationContextInitializer<?>> getContextInitializerClass(Class<?> testClass) {
96+
return this.contextInitializers.get(testClass.getName());
97+
}
98+
99+
63100
@SuppressWarnings("rawtypes")
64101
private static Map loadMap(String className, String methodName) {
65102
try {
@@ -78,17 +115,4 @@ private static Map loadMap(String className, String methodName) {
78115
}
79116
}
80117

81-
SmartContextLoader getContextLoader(Class<?> testClass) {
82-
Supplier<SmartContextLoader> supplier = this.contextLoaders.get(testClass.getName());
83-
return (supplier != null) ? supplier.get() : null;
84-
}
85-
86-
boolean isSupportedTestClass(Class<?> testClass) {
87-
return this.contextLoaders.containsKey(testClass.getName());
88-
}
89-
90-
Class<? extends ApplicationContextInitializer<?>> getContextInitializerClass(Class<?> testClass) {
91-
return this.contextInitializers.get(testClass.getName());
92-
}
93-
94118
}

spring-native/src/test/java/org/springframework/aot/test/AotCacheAwareContextLoaderDelegateTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import java.util.function.Supplier;
2121

2222
import org.junit.jupiter.api.Test;
23-
2423
import org.mockito.ArgumentCaptor;
24+
2525
import org.springframework.boot.SpringBootConfiguration;
2626
import org.springframework.boot.test.context.SpringBootTest;
2727
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
@@ -143,8 +143,8 @@ private AotCacheAwareContextLoaderDelegate createDemoDelegate() {
143143
private AotCacheAwareContextLoaderDelegate createAotCacheAwareContextLoaderDelegate(
144144
Map<String, Supplier<SmartContextLoader>> contextLoaders,
145145
Map<String, Class<? extends ApplicationContextInitializer<?>>> contextInitializers) {
146-
AotContextLoader aotContextLoader = new AotContextLoader(contextLoaders, contextInitializers);
147-
return new AotCacheAwareContextLoaderDelegate(aotContextLoader, this.contextCache);
146+
AotTestMappings aotTestMappings = new AotTestMappings(contextLoaders, contextInitializers);
147+
return new AotCacheAwareContextLoaderDelegate(aotTestMappings, this.contextCache);
148148
}
149149

150150
private void verifyCached(Class<? extends MergedContextConfiguration> type) {

spring-native/src/test/java/org/springframework/aot/test/AotContextLoaderTests.java spring-native/src/test/java/org/springframework/aot/test/AotTestMappingsTests.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -32,56 +32,56 @@
3232
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
3333

3434
/**
35-
* Tests for {@link AotContextLoader}.
35+
* Tests for {@link AotTestMappings}.
3636
*
3737
* @author Stephane Nicoll
3838
* @author Sam Brannen
3939
*/
40-
class AotContextLoaderTests {
40+
class AotTestMappingsTests {
4141

4242
@Test
4343
void loadWithClassNameFindsMatchingContextLoader() {
44-
AotContextLoader aotContextLoader = new AotContextLoader(TestMapping.class.getName());
45-
assertThat(aotContextLoader.getContextLoader(AotContextLoaderTests.class))
44+
AotTestMappings aotTestMappings = new AotTestMappings(TestMapping.class.getName());
45+
assertThat(aotTestMappings.getContextLoader(AotTestMappingsTests.class))
4646
.isInstanceOf(AotSpringBootConfigContextLoader.class);
4747
}
4848

4949
@Test
5050
void loadWithClassNameReturnsNullContextLoaderForUnregisteredTest() {
51-
AotContextLoader aotContextLoader = new AotContextLoader(TestMapping.class.getName());
52-
assertThat(aotContextLoader.getContextLoader(Map.class)).isNull();
51+
AotTestMappings aotTestMappings = new AotTestMappings(TestMapping.class.getName());
52+
assertThat(aotTestMappings.getContextLoader(Map.class)).isNull();
5353
}
5454

5555
@Test
5656
void loadWithClassNameFindsMatchingContextInitializerClass() {
57-
AotContextLoader aotContextLoader = new AotContextLoader(TestMapping.class.getName());
58-
assertThat(aotContextLoader.getContextInitializerClass(AotContextLoaderTests.class))
57+
AotTestMappings aotTestMappings = new AotTestMappings(TestMapping.class.getName());
58+
assertThat(aotTestMappings.getContextInitializerClass(AotTestMappingsTests.class))
5959
.isEqualTo(TestApplicationContextInitializer.class);
6060
}
6161

6262
@Test
6363
void loadWithClassNameReturnsNullContextInitializerClassForUnregisteredTest() {
64-
AotContextLoader aotContextLoader = new AotContextLoader(TestMapping.class.getName());
65-
assertThat(aotContextLoader.getContextInitializerClass(Map.class)).isNull();
64+
AotTestMappings aotTestMappings = new AotTestMappings(TestMapping.class.getName());
65+
assertThat(aotTestMappings.getContextInitializerClass(Map.class)).isNull();
6666
}
6767

6868
@Test
6969
void loadWithClassNameWithoutContextLoadersMethod() {
70-
assertThatIllegalStateException().isThrownBy(() -> new AotContextLoader(Map.class.getName()))
70+
assertThatIllegalStateException().isThrownBy(() -> new AotTestMappings(Map.class.getName()))
7171
.withMessage("No getContextLoaders() method found on java.util.Map");
7272
}
7373

7474
@Test
7575
void loadWithClassNameWithoutContextInitializersMethod() {
7676
String className = HalfBakedTestMapping.class.getName();
77-
assertThatIllegalStateException().isThrownBy(() -> new AotContextLoader(className))
77+
assertThatIllegalStateException().isThrownBy(() -> new AotTestMappings(className))
7878
.withMessage("No getContextInitializers() method found on " + className);
7979
}
8080

8181
@Test
8282
void loadWithClassNameThatDoesNotExist() {
8383
String className = "com.example.DoesNotExist";
84-
assertThatIllegalStateException().isThrownBy(() -> new AotContextLoader(className))
84+
assertThatIllegalStateException().isThrownBy(() -> new AotTestMappings(className))
8585
.withMessageMatching("Failed to load .+ method in " + Pattern.quote(className))
8686
.withCauseInstanceOf(ClassNotFoundException.class);
8787
}
@@ -91,14 +91,14 @@ public static class TestMapping {
9191

9292
public static Map<String, Supplier<SmartContextLoader>> getContextLoaders() {
9393
Map<String, Supplier<SmartContextLoader>> entries = new HashMap<>();
94-
entries.put(AotContextLoaderTests.class.getName(), () -> new AotSpringBootConfigContextLoader(TestApplicationContextInitializer.class));
94+
entries.put(AotTestMappingsTests.class.getName(), () -> new AotSpringBootConfigContextLoader(TestApplicationContextInitializer.class));
9595
entries.put("com.example.SampleTests", () -> new AotSpringBootConfigContextLoader(TestApplicationContextInitializer.class));
9696
return entries;
9797
}
9898

9999
public static Map<String, Class<? extends ApplicationContextInitializer<?>>> getContextInitializers() {
100100
Map<String, Class<? extends ApplicationContextInitializer<?>>> map = new HashMap<>();
101-
map.put(AotContextLoaderTests.class.getName(), TestApplicationContextInitializer.class);
101+
map.put(AotTestMappingsTests.class.getName(), TestApplicationContextInitializer.class);
102102
map.put("com.example.SampleTests", TestApplicationContextInitializer.class);
103103
return map;
104104
}

0 commit comments

Comments
 (0)