Browse Source
Prior to this commit, the AOT runtime support in the Spring TestContext
Framework (TCF) relied on the MergedContextConfiguration for a given
test class being the same as during the AOT processing phase. However,
this is not always the case. For example, Spring Boot "disables"
selected `ContextCustomizer` implementations during AOT runtime
execution.
See 0f325f98b5
To address that, this commit ensures that context caching works
properly during AOT runtime execution even if the
MergedContextConfiguration differs from what was produced during the
AOT processing phase. Specifically, this commit introduces
AotMergedContextConfiguration which is a MergedContextConfiguration
implementation based on an AOT-generated ApplicationContextInitializer.
AotMergedContextConfiguration wraps the MergedContextConfiguration
built during AOT runtime execution.
Interactions with the ContextCache are performed using the
AotMergedContextConfiguration; whereas, the ApplicationContext is
loaded using the original MergedContextConfiguration.
This commit also introduces a ContextCustomizerFactory that emulates
the ImportsContextCustomizerFactory in Spring Boot's testing support.
BasicSpringJupiterImportedConfigTests uses @Import to verify that the
context customizer works, and AotIntegrationTests has been updated to
execute BasicSpringJupiterImportedConfigTests after test classes whose
MergedContextConfiguration is identical during AOT runtime execution.
Without the fix in this commit, BasicSpringJupiterImportedConfigTests
would fail in AOT runtime mode since its ApplicationContext would be
pulled from the cache using an inappropriate cache key.
Closes gh-29289
pull/29292/head
11 changed files with 411 additions and 32 deletions
@ -0,0 +1,112 @@
@@ -0,0 +1,112 @@
|
||||
/* |
||||
* Copyright 2002-2022 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.test.context.cache; |
||||
|
||||
import java.util.Collections; |
||||
|
||||
import org.springframework.context.ApplicationContextInitializer; |
||||
import org.springframework.core.style.ToStringCreator; |
||||
import org.springframework.lang.Nullable; |
||||
import org.springframework.test.context.CacheAwareContextLoaderDelegate; |
||||
import org.springframework.test.context.MergedContextConfiguration; |
||||
|
||||
/** |
||||
* {@link MergedContextConfiguration} implementation based on an AOT-generated |
||||
* {@link ApplicationContextInitializer} that is used to load an AOT-optimized |
||||
* {@link org.springframework.context.ApplicationContext ApplicationContext}. |
||||
* |
||||
* <p>An {@code ApplicationContext} should not be loaded using the metadata in |
||||
* this {@code AotMergedContextConfiguration}. Rather the metadata from the |
||||
* {@linkplain #getOriginal() original} {@code MergedContextConfiguration} must |
||||
* be used. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.0 |
||||
*/ |
||||
class AotMergedContextConfiguration extends MergedContextConfiguration { |
||||
|
||||
private static final long serialVersionUID = 1963364911008547843L; |
||||
|
||||
private final Class<? extends ApplicationContextInitializer<?>> contextInitializerClass; |
||||
|
||||
private final MergedContextConfiguration original; |
||||
|
||||
|
||||
AotMergedContextConfiguration(Class<?> testClass, |
||||
Class<? extends ApplicationContextInitializer<?>> contextInitializerClass, |
||||
MergedContextConfiguration original, |
||||
CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate) { |
||||
|
||||
super(testClass, null, null, Collections.singleton(contextInitializerClass), null, |
||||
original.getContextLoader(), cacheAwareContextLoaderDelegate, original.getParent()); |
||||
this.contextInitializerClass = contextInitializerClass; |
||||
this.original = original; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Get the original {@link MergedContextConfiguration} that this |
||||
* {@code AotMergedContextConfiguration} was created for. |
||||
*/ |
||||
MergedContextConfiguration getOriginal() { |
||||
return this.original; |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(@Nullable Object other) { |
||||
if (this == other) { |
||||
return true; |
||||
} |
||||
if (other == null || other.getClass() != getClass()) { |
||||
return false; |
||||
} |
||||
AotMergedContextConfiguration that = (AotMergedContextConfiguration) other; |
||||
if (!this.contextInitializerClass.equals(that.contextInitializerClass)) { |
||||
return false; |
||||
} |
||||
if (!nullSafeClassName(getContextLoader()).equals(nullSafeClassName(that.getContextLoader()))) { |
||||
return false; |
||||
} |
||||
if (getParent() == null) { |
||||
if (that.getParent() != null) { |
||||
return false; |
||||
} |
||||
} |
||||
else if (!getParent().equals(that.getParent())) { |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
@Override |
||||
public int hashCode() { |
||||
int result = this.contextInitializerClass.hashCode(); |
||||
result = 31 * result + nullSafeClassName(getContextLoader()).hashCode(); |
||||
result = 31 * result + (getParent() != null ? getParent().hashCode() : 0); |
||||
return result; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return new ToStringCreator(this) |
||||
.append("testClass", getTestClass().getName()) |
||||
.append("contextInitializerClass", this.contextInitializerClass.getName()) |
||||
.append("original", this.original) |
||||
.toString(); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,75 @@
@@ -0,0 +1,75 @@
|
||||
/* |
||||
* Copyright 2002-2022 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.test.context.aot.samples.basic; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.beans.factory.annotation.Value; |
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.context.annotation.Import; |
||||
import org.springframework.test.context.TestPropertySource; |
||||
import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterImportedConfigTests.ImportedConfig; |
||||
import org.springframework.test.context.aot.samples.common.MessageService; |
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Uses configuration identical to {@link BasicSpringJupiterTests} and |
||||
* {@link BasicSpringJupiterImportedConfigTests} EXCEPT that this class is |
||||
* annotated with {@link Import @Import} to register an additional bean. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.0 |
||||
*/ |
||||
@SpringJUnitConfig(BasicTestConfiguration.class) |
||||
@Import(ImportedConfig.class) |
||||
@TestPropertySource(properties = "test.engine = jupiter") |
||||
public class BasicSpringJupiterImportedConfigTests { |
||||
|
||||
@Autowired |
||||
ApplicationContext context; |
||||
|
||||
@Autowired |
||||
MessageService messageService; |
||||
|
||||
@Autowired |
||||
String enigma; |
||||
|
||||
@Value("${test.engine}") |
||||
String testEngine; |
||||
|
||||
@org.junit.jupiter.api.Test |
||||
void test() { |
||||
assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!"); |
||||
assertThat(enigma).isEqualTo("imported!"); |
||||
assertThat(testEngine).isEqualTo("jupiter"); |
||||
assertThat(context.getEnvironment().getProperty("test.engine")) |
||||
.as("@TestPropertySource").isEqualTo("jupiter"); |
||||
} |
||||
|
||||
@Configuration(proxyBeanMethods = false) |
||||
static class ImportedConfig { |
||||
|
||||
@Bean |
||||
String enigma() { |
||||
return "imported!"; |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,74 @@
@@ -0,0 +1,74 @@
|
||||
/* |
||||
* Copyright 2002-2022 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.test.context.aot.samples.basic; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.List; |
||||
|
||||
import org.springframework.aot.AotDetector; |
||||
import org.springframework.context.ConfigurableApplicationContext; |
||||
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader; |
||||
import org.springframework.context.annotation.Import; |
||||
import org.springframework.context.support.GenericApplicationContext; |
||||
import org.springframework.test.context.ContextConfigurationAttributes; |
||||
import org.springframework.test.context.ContextCustomizer; |
||||
import org.springframework.test.context.ContextCustomizerFactory; |
||||
import org.springframework.test.context.MergedContextConfiguration; |
||||
|
||||
/** |
||||
* Emulates {@code ImportsContextCustomizerFactory} from Spring Boot's testing support. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.0 |
||||
*/ |
||||
class ImportsContextCustomizerFactory implements ContextCustomizerFactory { |
||||
|
||||
@Override |
||||
public ContextCustomizer createContextCustomizer(Class<?> testClass, |
||||
List<ContextConfigurationAttributes> configAttributes) { |
||||
|
||||
if (AotDetector.useGeneratedArtifacts()) { |
||||
return null; |
||||
} |
||||
if (testClass.getName().startsWith("org.springframework.test.context.aot.samples") && |
||||
testClass.isAnnotationPresent(Import.class)) { |
||||
return new ImportsContextCustomizer(testClass); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* Emulates {@code ImportsContextCustomizer} from Spring Boot's testing support. |
||||
*/ |
||||
private static class ImportsContextCustomizer implements ContextCustomizer { |
||||
|
||||
private final Class<?> testClass; |
||||
|
||||
ImportsContextCustomizer(Class<?> testClass) { |
||||
this.testClass = testClass; |
||||
} |
||||
|
||||
@Override |
||||
public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) { |
||||
AnnotatedBeanDefinitionReader annotatedBeanDefinitionReader = |
||||
new AnnotatedBeanDefinitionReader((GenericApplicationContext) context); |
||||
Arrays.stream(this.testClass.getAnnotation(Import.class).value()) |
||||
.forEach(annotatedBeanDefinitionReader::register); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,79 @@
@@ -0,0 +1,79 @@
|
||||
/* |
||||
* Copyright 2002-2022 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.test.context.cache; |
||||
|
||||
import java.util.Set; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.context.ApplicationContextInitializer; |
||||
import org.springframework.context.support.GenericApplicationContext; |
||||
import org.springframework.test.context.CacheAwareContextLoaderDelegate; |
||||
import org.springframework.test.context.ContextLoader; |
||||
import org.springframework.test.context.MergedContextConfiguration; |
||||
import org.springframework.test.context.support.DelegatingSmartContextLoader; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.Mockito.mock; |
||||
|
||||
/** |
||||
* Tests for {@link AotMergedContextConfiguration}. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.0 |
||||
*/ |
||||
class AotMergedContextConfigurationTests { |
||||
|
||||
private final CacheAwareContextLoaderDelegate delegate = |
||||
new DefaultCacheAwareContextLoaderDelegate(mock(ContextCache.class)); |
||||
|
||||
private final ContextLoader contextLoader = new DelegatingSmartContextLoader(); |
||||
|
||||
private final MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(), null, null, |
||||
Set.of(DemoApplicationContextInitializer.class), null, contextLoader); |
||||
|
||||
private final AotMergedContextConfiguration aotMergedConfig1 = new AotMergedContextConfiguration(getClass(), |
||||
DemoApplicationContextInitializer.class, mergedConfig, delegate); |
||||
|
||||
private final AotMergedContextConfiguration aotMergedConfig2 = new AotMergedContextConfiguration(getClass(), |
||||
DemoApplicationContextInitializer.class, mergedConfig, delegate); |
||||
|
||||
|
||||
@Test |
||||
void testEquals() { |
||||
assertThat(aotMergedConfig1).isEqualTo(aotMergedConfig1); |
||||
assertThat(aotMergedConfig1).isEqualTo(aotMergedConfig2); |
||||
|
||||
assertThat(mergedConfig).isNotEqualTo(aotMergedConfig1); |
||||
assertThat(aotMergedConfig1).isNotEqualTo(mergedConfig); |
||||
} |
||||
|
||||
@Test |
||||
void testHashCode() { |
||||
assertThat(aotMergedConfig1).hasSameHashCodeAs(aotMergedConfig2); |
||||
|
||||
assertThat(aotMergedConfig1).doesNotHaveSameHashCodeAs(mergedConfig); |
||||
} |
||||
|
||||
|
||||
static class DemoApplicationContextInitializer implements ApplicationContextInitializer<GenericApplicationContext> { |
||||
@Override |
||||
public void initialize(GenericApplicationContext applicationContext) { |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -1,2 +1,6 @@
@@ -1,2 +1,6 @@
|
||||
# Test configuration file containing a non-existent default TestExecutionListener. |
||||
# Test configuration file containing a non-existent default TestExecutionListener and a demo ContextCustomizerFactory. |
||||
|
||||
org.springframework.test.context.TestExecutionListener = org.example.FooListener |
||||
|
||||
org.springframework.test.context.ContextCustomizerFactory =\ |
||||
org.springframework.test.context.aot.samples.basic.ImportsContextCustomizerFactory |
||||
|
||||
Loading…
Reference in new issue