Revise configurable refresh support in SmartContextLoader SPI

This commit revises 903e9f2a02 based on feedback from the Spring Boot
team.

- The original loadContext(MergedContextConfiguration) method is no
  longer deprecated.

- loadContext(MergedContextConfiguration, boolean) has been replaced by
  loadContextForAotProcessing(MergedContextConfiguration) which is
  implemented as a `default` method that throws an
  UnsupportedOperationException.

- Affected code has been refactored to adjust to these changes.

See gh-28906
This commit is contained in:
Sam Brannen
2022-08-03 16:54:32 +03:00
parent 3a890033b7
commit e3f4d810f6
11 changed files with 178 additions and 135 deletions

View File

@@ -20,6 +20,7 @@ import java.util.Arrays;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.MergedContextConfiguration;
@@ -48,7 +49,7 @@ class AnnotationConfigContextLoaderTests {
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(),
new String[] { "config.xml" }, EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, contextLoader);
assertThatIllegalStateException()
.isThrownBy(() -> contextLoader.loadContext(mergedConfig, true))
.isThrownBy(() -> contextLoader.loadContext(mergedConfig))
.withMessageContaining("does not support resource locations");
}
@@ -56,30 +57,34 @@ class AnnotationConfigContextLoaderTests {
* @since 6.0
*/
@Test
void loadContextHonorsRefreshTrue() throws Exception {
void loadContextRefreshesContext() throws Exception {
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(
AnnotatedFooConfigInnerClassTestCase.class, EMPTY_STRING_ARRAY,
new Class<?>[] {AnnotatedFooConfigInnerClassTestCase.FooConfig.class},
EMPTY_STRING_ARRAY, contextLoader);
ConfigurableApplicationContext context = contextLoader.loadContext(mergedConfig, true);
assertThat(context).isNotNull();
assertThat(context.isActive()).as("ApplicationContext is active").isTrue();
ApplicationContext context = contextLoader.loadContext(mergedConfig);
assertThat(context).isInstanceOf(ConfigurableApplicationContext.class);
ConfigurableApplicationContext cac = (ConfigurableApplicationContext) context;
assertThat(cac.isActive()).as("ApplicationContext is active").isTrue();
assertThat(context.getBean(String.class)).isEqualTo("foo");
cac.close();
}
/**
* @since 6.0
*/
@Test
void loadContextHonorsRefreshFalse() throws Exception {
void loadContextForAotProcessingDoesNotRefreshContext() throws Exception {
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(
AnnotatedFooConfigInnerClassTestCase.class, EMPTY_STRING_ARRAY,
new Class<?>[] {AnnotatedFooConfigInnerClassTestCase.FooConfig.class},
EMPTY_STRING_ARRAY, contextLoader);
ConfigurableApplicationContext context = contextLoader.loadContext(mergedConfig, false);
assertThat(context).isNotNull();
assertThat(context.isActive()).as("ApplicationContext is active").isFalse();
ApplicationContext context = contextLoader.loadContextForAotProcessing(mergedConfig);
assertThat(context).isInstanceOf(ConfigurableApplicationContext.class);
ConfigurableApplicationContext cac = (ConfigurableApplicationContext) context;
assertThat(cac.isActive()).as("ApplicationContext is active").isFalse();
assertThat(Arrays.stream(context.getBeanDefinitionNames())).anyMatch(name -> name.contains("FooConfig"));
cac.close();
}
@Test

View File

@@ -53,7 +53,7 @@ class CustomizedGenericXmlContextLoaderTests {
MergedContextConfiguration mergedConfig =
new MergedContextConfiguration(getClass(), null, null, null, null);
customLoader.loadContext(mergedConfig, true);
customLoader.loadContext(mergedConfig);
assertThat(customizeInvoked).as("customizeContext() should have been invoked").isTrue();
}

View File

@@ -100,7 +100,7 @@ class DelegatingSmartContextLoaderTests {
@Test
void loadContextWithNullConfig() throws Exception {
assertThatIllegalArgumentException().isThrownBy(() -> loader.loadContext(null, true));
assertThatIllegalArgumentException().isThrownBy(() -> loader.loadContext((MergedContextConfiguration) null));
}
@Test
@@ -108,7 +108,7 @@ class DelegatingSmartContextLoaderTests {
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(
getClass(), EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, loader);
assertThatIllegalStateException()
.isThrownBy(() -> loader.loadContext(mergedConfig, true))
.isThrownBy(() -> loader.loadContext(mergedConfig))
.withMessageStartingWith("Neither")
.withMessageContaining("was able to load an ApplicationContext from");
}
@@ -121,7 +121,7 @@ class DelegatingSmartContextLoaderTests {
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(),
new String[] {"test.xml"}, new Class<?>[] {getClass()}, EMPTY_STRING_ARRAY, loader);
assertThatIllegalStateException()
.isThrownBy(() -> loader.loadContext(mergedConfig, true))
.isThrownBy(() -> loader.loadContext(mergedConfig))
.withMessageStartingWith("Neither")
.withMessageContaining("declare either 'locations' or 'classes' but not both.");
}
@@ -147,7 +147,7 @@ class DelegatingSmartContextLoaderTests {
private void assertApplicationContextLoadsAndContainsFooString(MergedContextConfiguration mergedConfig)
throws Exception {
ApplicationContext applicationContext = loader.loadContext(mergedConfig, true);
ApplicationContext applicationContext = loader.loadContext(mergedConfig);
assertThat(applicationContext).isInstanceOf(ConfigurableApplicationContext.class);
assertThat(applicationContext.getBean(String.class)).isEqualTo("foo");
ConfigurableApplicationContext cac = (ConfigurableApplicationContext) applicationContext;
@@ -164,7 +164,7 @@ class DelegatingSmartContextLoaderTests {
new String[] {"classpath:/org/springframework/test/context/support/DelegatingSmartContextLoaderTests$XmlTestCase-context.xml"},
EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, loader);
assertApplicationContextLoadsWithoutRefresh(mergedConfig, "foo");
assertApplicationContextLoadsForAotProcessing(mergedConfig, "foo");
}
/**
@@ -175,13 +175,13 @@ class DelegatingSmartContextLoaderTests {
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(ConfigClassTestCase.class,
EMPTY_STRING_ARRAY, new Class<?>[] {ConfigClassTestCase.Config.class}, EMPTY_STRING_ARRAY, loader);
assertApplicationContextLoadsWithoutRefresh(mergedConfig, "ConfigClassTestCase.Config");
assertApplicationContextLoadsForAotProcessing(mergedConfig, "ConfigClassTestCase.Config");
}
private void assertApplicationContextLoadsWithoutRefresh(MergedContextConfiguration mergedConfig,
private void assertApplicationContextLoadsForAotProcessing(MergedContextConfiguration mergedConfig,
String expectedBeanDefName) throws Exception {
ApplicationContext context = loader.loadContext(mergedConfig, false);
ApplicationContext context = loader.loadContextForAotProcessing(mergedConfig);
assertThat(context).isInstanceOf(ConfigurableApplicationContext.class);
ConfigurableApplicationContext cac = (ConfigurableApplicationContext) context;
assertThat(cac.isActive()).as("ApplicationContext is active").isFalse();

View File

@@ -40,7 +40,7 @@ class GenericXmlContextLoaderTests {
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
new Class<?>[] { getClass() }, EMPTY_STRING_ARRAY, loader);
assertThatIllegalStateException()
.isThrownBy(() -> loader.loadContext(mergedConfig, true))
.isThrownBy(() -> loader.loadContext(mergedConfig))
.withMessageContaining("does not support annotated classes");
}

View File

@@ -39,7 +39,7 @@ class AnnotationConfigWebContextLoaderTests {
new String[] { "config.xml" }, EMPTY_CLASS_ARRAY, null, EMPTY_STRING_ARRAY, EMPTY_STRING_ARRAY,
EMPTY_STRING_ARRAY, "resource/path", loader, null, null);
assertThatIllegalStateException()
.isThrownBy(() -> loader.loadContext(mergedConfig, true))
.isThrownBy(() -> loader.loadContext(mergedConfig))
.withMessageContaining("does not support resource locations");
}

View File

@@ -38,7 +38,7 @@ class GenericXmlWebContextLoaderTests {
new Class<?>[] { getClass() }, null, EMPTY_STRING_ARRAY, EMPTY_STRING_ARRAY, EMPTY_STRING_ARRAY,
"resource/path", loader, null, null);
assertThatIllegalStateException()
.isThrownBy(() -> loader.loadContext(mergedConfig, true))
.isThrownBy(() -> loader.loadContext(mergedConfig))
.withMessageContaining("does not support annotated classes");
}