Polishing
This commit is contained in:
@@ -74,74 +74,59 @@ import static org.springframework.beans.factory.support.BeanDefinitionBuilder.ge
|
||||
* @since 3.1
|
||||
*/
|
||||
@SuppressWarnings("resource")
|
||||
public class ComponentScanAnnotationIntegrationTests {
|
||||
class ComponentScanAnnotationIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void controlScan() {
|
||||
void controlScan() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.scan(example.scannable.PackageMarker.class.getPackage().getName());
|
||||
ctx.refresh();
|
||||
assertThat(ctx.containsBean("fooServiceImpl")).as(
|
||||
"control scan for example.scannable package failed to register FooServiceImpl bean").isTrue();
|
||||
assertContextContainsBean(ctx, "fooServiceImpl");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void viaContextRegistration() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(ComponentScanAnnotatedConfig.class);
|
||||
ctx.refresh();
|
||||
void viaContextRegistration() {
|
||||
AnnotationConfigApplicationContext ctx =
|
||||
new AnnotationConfigApplicationContext(ComponentScanAnnotatedConfig.class);
|
||||
ctx.getBean(ComponentScanAnnotatedConfig.class);
|
||||
ctx.getBean(TestBean.class);
|
||||
assertThat(ctx.containsBeanDefinition("componentScanAnnotatedConfig")).as("config class bean not found")
|
||||
.isTrue();
|
||||
assertThat(ctx.containsBeanDefinition("componentScanAnnotatedConfig")).as("config class bean not found").isTrue();
|
||||
assertThat(ctx.containsBean("fooServiceImpl")).as("@ComponentScan annotated @Configuration class registered directly against " +
|
||||
"AnnotationConfigApplicationContext did not trigger component scanning as expected")
|
||||
.isTrue();
|
||||
"AnnotationConfigApplicationContext did not trigger component scanning as expected").isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void viaContextRegistration_WithValueAttribute() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(ComponentScanAnnotatedConfig_WithValueAttribute.class);
|
||||
ctx.refresh();
|
||||
void viaContextRegistration_WithValueAttribute() {
|
||||
AnnotationConfigApplicationContext ctx =
|
||||
new AnnotationConfigApplicationContext(ComponentScanAnnotatedConfig_WithValueAttribute.class);
|
||||
ctx.getBean(ComponentScanAnnotatedConfig_WithValueAttribute.class);
|
||||
ctx.getBean(TestBean.class);
|
||||
assertThat(ctx.containsBeanDefinition("componentScanAnnotatedConfig_WithValueAttribute")).as("config class bean not found")
|
||||
.isTrue();
|
||||
assertThat(ctx.containsBeanDefinition("componentScanAnnotatedConfig_WithValueAttribute")).as("config class bean not found").isTrue();
|
||||
assertThat(ctx.containsBean("fooServiceImpl")).as("@ComponentScan annotated @Configuration class registered directly against " +
|
||||
"AnnotationConfigApplicationContext did not trigger component scanning as expected")
|
||||
.isTrue();
|
||||
"AnnotationConfigApplicationContext did not trigger component scanning as expected").isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void viaContextRegistration_FromPackageOfConfigClass() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(ComponentScanAnnotatedConfigWithImplicitBasePackage.class);
|
||||
ctx.refresh();
|
||||
void viaContextRegistration_FromPackageOfConfigClass() {
|
||||
AnnotationConfigApplicationContext ctx =
|
||||
new AnnotationConfigApplicationContext(ComponentScanAnnotatedConfigWithImplicitBasePackage.class);
|
||||
ctx.getBean(ComponentScanAnnotatedConfigWithImplicitBasePackage.class);
|
||||
assertThat(ctx.containsBeanDefinition("componentScanAnnotatedConfigWithImplicitBasePackage")).as("config class bean not found")
|
||||
.isTrue();
|
||||
assertThat(ctx.containsBeanDefinition("componentScanAnnotatedConfigWithImplicitBasePackage")).as("config class bean not found").isTrue();
|
||||
assertThat(ctx.containsBean("scannedComponent")).as("@ComponentScan annotated @Configuration class registered directly against " +
|
||||
"AnnotationConfigApplicationContext did not trigger component scanning as expected")
|
||||
.isTrue();
|
||||
assertThat(ctx.getBean(ConfigurableComponent.class).isFlag()).as("@Bean method overrides scanned class")
|
||||
.isTrue();
|
||||
"AnnotationConfigApplicationContext did not trigger component scanning as expected").isTrue();
|
||||
assertThat(ctx.getBean(ConfigurableComponent.class).isFlag()).as("@Bean method overrides scanned class").isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void viaContextRegistration_WithComposedAnnotation() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(ComposedAnnotationConfig.class);
|
||||
ctx.refresh();
|
||||
void viaContextRegistration_WithComposedAnnotation() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ComposedAnnotationConfig.class);
|
||||
ctx.getBean(ComposedAnnotationConfig.class);
|
||||
ctx.getBean(SimpleComponent.class);
|
||||
ctx.getBean(ClassWithNestedComponents.NestedComponent.class);
|
||||
ctx.getBean(ClassWithNestedComponents.OtherNestedComponent.class);
|
||||
assertThat(ctx.containsBeanDefinition("componentScanAnnotationIntegrationTests.ComposedAnnotationConfig")).as("config class bean not found")
|
||||
.isTrue();
|
||||
assertThat(ctx.containsBeanDefinition("componentScanAnnotationIntegrationTests.ComposedAnnotationConfig")).as("config class bean not found").isTrue();
|
||||
assertThat(ctx.containsBean("simpleComponent")).as("@ComponentScan annotated @Configuration class registered directly against " +
|
||||
"AnnotationConfigApplicationContext did not trigger component scanning as expected")
|
||||
.isTrue();
|
||||
"AnnotationConfigApplicationContext did not trigger component scanning as expected").isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -154,7 +139,7 @@ public class ComponentScanAnnotationIntegrationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void viaBeanRegistration() {
|
||||
void viaBeanRegistration() {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
bf.registerBeanDefinition("componentScanAnnotatedConfig",
|
||||
genericBeanDefinition(ComponentScanAnnotatedConfig.class).getBeanDefinition());
|
||||
@@ -164,40 +149,36 @@ public class ComponentScanAnnotationIntegrationTests {
|
||||
ctx.refresh();
|
||||
ctx.getBean(ComponentScanAnnotatedConfig.class);
|
||||
ctx.getBean(TestBean.class);
|
||||
assertThat(ctx.containsBeanDefinition("componentScanAnnotatedConfig")).as("config class bean not found")
|
||||
.isTrue();
|
||||
assertThat(ctx.containsBeanDefinition("componentScanAnnotatedConfig")).as("config class bean not found").isTrue();
|
||||
assertThat(ctx.containsBean("fooServiceImpl")).as("@ComponentScan annotated @Configuration class registered as bean " +
|
||||
"definition did not trigger component scanning as expected")
|
||||
.isTrue();
|
||||
"definition did not trigger component scanning as expected").isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withCustomBeanNameGenerator() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(ComponentScanWithBeanNameGenerator.class);
|
||||
ctx.refresh();
|
||||
assertThat(ctx.containsBean("custom_fooServiceImpl")).isTrue();
|
||||
assertThat(ctx.containsBean("fooServiceImpl")).isFalse();
|
||||
void withCustomBeanNameGenerator() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanWithBeanNameGenerator.class);
|
||||
assertContextContainsBean(ctx, "custom_fooServiceImpl");
|
||||
assertContextDoesNotContainBean(ctx, "fooServiceImpl");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withScopeResolver() {
|
||||
void withScopeResolver() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanWithScopeResolver.class);
|
||||
// custom scope annotation makes the bean prototype scoped. subsequent calls
|
||||
// to getBean should return distinct instances.
|
||||
assertThat(ctx.getBean(CustomScopeAnnotationBean.class)).isNotSameAs(ctx.getBean(CustomScopeAnnotationBean.class));
|
||||
assertThat(ctx.containsBean("scannedComponent")).isFalse();
|
||||
assertContextDoesNotContainBean(ctx, "scannedComponent");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multiComponentScan() {
|
||||
void multiComponentScan() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MultiComponentScan.class);
|
||||
assertThat(ctx.getBean(CustomScopeAnnotationBean.class)).isNotSameAs(ctx.getBean(CustomScopeAnnotationBean.class));
|
||||
assertThat(ctx.containsBean("scannedComponent")).isTrue();
|
||||
assertContextContainsBean(ctx, "scannedComponent");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withCustomTypeFilter() {
|
||||
void withCustomTypeFilter() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanWithCustomTypeFilter.class);
|
||||
assertThat(ctx.getDefaultListableBeanFactory().containsSingleton("componentScanParserTests.KustomAnnotationAutowiredBean")).isFalse();
|
||||
KustomAnnotationAutowiredBean testBean = ctx.getBean("componentScanParserTests.KustomAnnotationAutowiredBean", KustomAnnotationAutowiredBean.class);
|
||||
@@ -205,13 +186,13 @@ public class ComponentScanAnnotationIntegrationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withAwareTypeFilter() {
|
||||
void withAwareTypeFilter() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanWithAwareTypeFilter.class);
|
||||
assertThat(ctx.getEnvironment().matchesProfiles("the-filter-ran")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withScopedProxy() throws IOException, ClassNotFoundException {
|
||||
void withScopedProxy() throws Exception {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(ComponentScanWithScopedProxy.class);
|
||||
ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope());
|
||||
@@ -228,7 +209,7 @@ public class ComponentScanAnnotationIntegrationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withScopedProxyThroughRegex() throws IOException, ClassNotFoundException {
|
||||
void withScopedProxyThroughRegex() throws IOException, ClassNotFoundException {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(ComponentScanWithScopedProxyThroughRegex.class);
|
||||
ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope());
|
||||
@@ -240,7 +221,7 @@ public class ComponentScanAnnotationIntegrationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withScopedProxyThroughAspectJPattern() throws IOException, ClassNotFoundException {
|
||||
void withScopedProxyThroughAspectJPattern() throws IOException, ClassNotFoundException {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(ComponentScanWithScopedProxyThroughAspectJPattern.class);
|
||||
ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope());
|
||||
@@ -252,33 +233,33 @@ public class ComponentScanAnnotationIntegrationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withMultipleAnnotationIncludeFilters1() throws IOException, ClassNotFoundException {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(ComponentScanWithMultipleAnnotationIncludeFilters1.class);
|
||||
ctx.refresh();
|
||||
void withMultipleAnnotationIncludeFilters1() throws IOException, ClassNotFoundException {
|
||||
AnnotationConfigApplicationContext ctx =
|
||||
new AnnotationConfigApplicationContext(ComponentScanWithMultipleAnnotationIncludeFilters1.class);
|
||||
ctx.getBean(DefaultNamedComponent.class); // @CustomStereotype-annotated
|
||||
ctx.getBean(MessageBean.class); // @CustomComponent-annotated
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withMultipleAnnotationIncludeFilters2() throws IOException, ClassNotFoundException {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(ComponentScanWithMultipleAnnotationIncludeFilters2.class);
|
||||
ctx.refresh();
|
||||
void withMultipleAnnotationIncludeFilters2() throws IOException, ClassNotFoundException {
|
||||
AnnotationConfigApplicationContext ctx =
|
||||
new AnnotationConfigApplicationContext(ComponentScanWithMultipleAnnotationIncludeFilters2.class);
|
||||
ctx.getBean(DefaultNamedComponent.class); // @CustomStereotype-annotated
|
||||
ctx.getBean(MessageBean.class); // @CustomComponent-annotated
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withBasePackagesAndValueAlias() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(ComponentScanWithBasePackagesAndValueAlias.class);
|
||||
ctx.refresh();
|
||||
assertThat(ctx.containsBean("fooServiceImpl")).isTrue();
|
||||
void withBasePackagesAndValueAlias() {
|
||||
AnnotationConfigApplicationContext ctx =
|
||||
new AnnotationConfigApplicationContext(ComponentScanWithBasePackagesAndValueAlias.class);
|
||||
assertContextContainsBean(ctx, "fooServiceImpl");
|
||||
}
|
||||
|
||||
private static void assertContextContainsBean(ApplicationContext ctx, String beanName) {
|
||||
assertThat(ctx.containsBean(beanName)).as("context contains bean " + beanName).isTrue();
|
||||
assertThat(ctx.containsBean(beanName)).as("context should contain bean " + beanName).isTrue();
|
||||
}
|
||||
private static void assertContextDoesNotContainBean(ApplicationContext ctx, String beanName) {
|
||||
assertThat(ctx.containsBean(beanName)).as("context should not contain bean " + beanName).isFalse();
|
||||
}
|
||||
|
||||
|
||||
@@ -286,7 +267,7 @@ public class ComponentScanAnnotationIntegrationTests {
|
||||
@ComponentScan
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface ComposedConfiguration {
|
||||
@interface ComposedConfiguration {
|
||||
|
||||
@AliasFor(annotation = ComponentScan.class)
|
||||
String[] basePackages() default {};
|
||||
@@ -296,14 +277,14 @@ public class ComponentScanAnnotationIntegrationTests {
|
||||
@ComponentScan
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface ComposedConfiguration2 {
|
||||
@interface ComposedConfiguration2 {
|
||||
|
||||
@AliasFor(annotation = ComponentScan.class)
|
||||
String[] basePackages() default {};
|
||||
}
|
||||
|
||||
@ComposedConfiguration(basePackages = "org.springframework.context.annotation.componentscan.simple")
|
||||
public static class ComposedAnnotationConfig {
|
||||
static class ComposedAnnotationConfig {
|
||||
}
|
||||
|
||||
@ComposedConfiguration(basePackages = "org.springframework.context.annotation.componentscan.simple")
|
||||
@@ -311,7 +292,7 @@ public class ComponentScanAnnotationIntegrationTests {
|
||||
static class MultipleComposedAnnotationsConfig {
|
||||
}
|
||||
|
||||
public static class AwareTypeFilter implements TypeFilter, EnvironmentAware,
|
||||
static class AwareTypeFilter implements TypeFilter, EnvironmentAware,
|
||||
ResourceLoaderAware, BeanClassLoaderAware, BeanFactoryAware {
|
||||
|
||||
private BeanFactory beanFactory;
|
||||
|
||||
@@ -22,8 +22,7 @@ import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import jakarta.inject.Inject;
|
||||
@@ -57,21 +56,13 @@ class PropertySourceAnnotationTests {
|
||||
|
||||
@Test
|
||||
void withExplicitName() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(ConfigWithExplicitName.class);
|
||||
ctx.refresh();
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithExplicitName.class);
|
||||
assertThat(ctx.getEnvironment().getPropertySources().contains("p1")).as("property source p1 was not added").isTrue();
|
||||
assertThat(ctx.getBean(TestBean.class).getName()).isEqualTo("p1TestBean");
|
||||
|
||||
// assert that the property source was added last to the set of sources
|
||||
String name;
|
||||
MutablePropertySources sources = ctx.getEnvironment().getPropertySources();
|
||||
Iterator<org.springframework.core.env.PropertySource<?>> iterator = sources.iterator();
|
||||
do {
|
||||
name = iterator.next().getName();
|
||||
}
|
||||
while (iterator.hasNext());
|
||||
|
||||
String name = sources.stream().toList().get(sources.size() - 1).getName();
|
||||
assertThat(name).isEqualTo("p1");
|
||||
ctx.close();
|
||||
}
|
||||
@@ -79,7 +70,9 @@ class PropertySourceAnnotationTests {
|
||||
@Test
|
||||
void withImplicitName() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithImplicitName.class);
|
||||
assertThat(ctx.getEnvironment().getPropertySources().contains("class path resource [org/springframework/context/annotation/p1.properties]")).as("property source p1 was not added").isTrue();
|
||||
String name = "class path resource [org/springframework/context/annotation/p1.properties]";
|
||||
assertThat(ctx.getEnvironment().getPropertySources().contains(name))
|
||||
.as("property source p1 was not added").isTrue();
|
||||
assertThat(ctx.getBean(TestBean.class).getName()).isEqualTo("p1TestBean");
|
||||
ctx.close();
|
||||
}
|
||||
@@ -177,40 +170,36 @@ class PropertySourceAnnotationTests {
|
||||
@Test
|
||||
void withNameAndMultipleResourceLocations() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNameAndMultipleResourceLocations.class);
|
||||
assertThat(ctx.getEnvironment().containsProperty("from.p1")).isTrue();
|
||||
assertThat(ctx.getEnvironment().containsProperty("from.p2")).isTrue();
|
||||
assertEnvironmentContainsProperties(ctx, "from.p1", "from.p2");
|
||||
// p2 should 'win' as it was registered last
|
||||
assertThat(ctx.getEnvironment().getProperty("testbean.name")).isEqualTo("p2TestBean");
|
||||
assertEnvironmentProperty(ctx, "testbean.name", "p2TestBean");
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void withMultipleResourceLocations() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithMultipleResourceLocations.class);
|
||||
assertThat(ctx.getEnvironment().containsProperty("from.p1")).isTrue();
|
||||
assertThat(ctx.getEnvironment().containsProperty("from.p2")).isTrue();
|
||||
assertEnvironmentContainsProperties(ctx, "from.p1", "from.p2");
|
||||
// p2 should 'win' as it was registered last
|
||||
assertThat(ctx.getEnvironment().getProperty("testbean.name")).isEqualTo("p2TestBean");
|
||||
assertEnvironmentProperty(ctx, "testbean.name", "p2TestBean");
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void withRepeatedPropertySourcesInContainerAnnotation() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithPropertySources.class);
|
||||
assertThat(ctx.getEnvironment().containsProperty("from.p1")).isTrue();
|
||||
assertThat(ctx.getEnvironment().containsProperty("from.p2")).isTrue();
|
||||
assertEnvironmentContainsProperties(ctx, "from.p1", "from.p2");
|
||||
// p2 should 'win' as it was registered last
|
||||
assertThat(ctx.getEnvironment().getProperty("testbean.name")).isEqualTo("p2TestBean");
|
||||
assertEnvironmentProperty(ctx, "testbean.name", "p2TestBean");
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void withRepeatedPropertySources() {
|
||||
try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithRepeatedPropertySourceAnnotations.class)) {
|
||||
assertThat(ctx.getEnvironment().containsProperty("from.p1")).isTrue();
|
||||
assertThat(ctx.getEnvironment().containsProperty("from.p2")).isTrue();
|
||||
assertEnvironmentContainsProperties(ctx, "from.p1", "from.p2");
|
||||
// p2 should 'win' as it was registered last
|
||||
assertThat(ctx.getEnvironment().getProperty("testbean.name")).isEqualTo("p2TestBean");
|
||||
assertEnvironmentProperty(ctx, "testbean.name", "p2TestBean");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,19 +210,16 @@ class PropertySourceAnnotationTests {
|
||||
|
||||
System.clearProperty(key);
|
||||
try (ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(configClass)) {
|
||||
assertThat(ctx.getEnvironment().containsProperty("from.p1")).isTrue();
|
||||
assertThat(ctx.getEnvironment().containsProperty("from.p2")).isTrue();
|
||||
assertEnvironmentContainsProperties(ctx, "from.p1", "from.p2");
|
||||
// p2 should 'win' as it was registered last
|
||||
assertThat(ctx.getEnvironment().getProperty("testbean.name")).isEqualTo("p2TestBean");
|
||||
assertEnvironmentProperty(ctx, "testbean.name", "p2TestBean");
|
||||
}
|
||||
|
||||
System.setProperty(key, "org/springframework/context/annotation");
|
||||
try (ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(configClass)) {
|
||||
assertThat(ctx.getEnvironment().containsProperty("from.p1")).isTrue();
|
||||
assertThat(ctx.getEnvironment().containsProperty("from.p2")).isTrue();
|
||||
assertThat(ctx.getEnvironment().containsProperty("from.p3")).isTrue();
|
||||
assertEnvironmentContainsProperties(ctx, "from.p1", "from.p2", "from.p3");
|
||||
// p3 should 'win' as it was registered last
|
||||
assertThat(ctx.getEnvironment().getProperty("testbean.name")).isEqualTo("p3TestBean");
|
||||
assertEnvironmentProperty(ctx, "testbean.name", "p3TestBean");
|
||||
}
|
||||
finally {
|
||||
System.clearProperty(key);
|
||||
@@ -251,10 +237,9 @@ class PropertySourceAnnotationTests {
|
||||
@Test
|
||||
void withNamedPropertySources() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNamedPropertySources.class);
|
||||
assertThat(ctx.getEnvironment().containsProperty("from.p1")).isTrue();
|
||||
assertThat(ctx.getEnvironment().containsProperty("from.p2")).isTrue();
|
||||
assertEnvironmentContainsProperties(ctx, "from.p1", "from.p2");
|
||||
// p2 should 'win' as it was registered last
|
||||
assertThat(ctx.getEnvironment().getProperty("testbean.name")).isEqualTo("p2TestBean");
|
||||
assertEnvironmentProperty(ctx, "testbean.name", "p2TestBean");
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@@ -268,17 +253,15 @@ class PropertySourceAnnotationTests {
|
||||
@Test
|
||||
void withIgnoredPropertySource() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithIgnoredPropertySource.class);
|
||||
assertThat(ctx.getEnvironment().containsProperty("from.p1")).isTrue();
|
||||
assertThat(ctx.getEnvironment().containsProperty("from.p2")).isTrue();
|
||||
assertEnvironmentContainsProperties(ctx, "from.p1", "from.p2");
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void withSameSourceImportedInDifferentOrder() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithSameSourceImportedInDifferentOrder.class);
|
||||
assertThat(ctx.getEnvironment().containsProperty("from.p1")).isTrue();
|
||||
assertThat(ctx.getEnvironment().containsProperty("from.p2")).isTrue();
|
||||
assertThat(ctx.getEnvironment().getProperty("testbean.name")).isEqualTo("p2TestBean");
|
||||
assertEnvironmentContainsProperties(ctx, "from.p1", "from.p2");
|
||||
assertEnvironmentProperty(ctx, "testbean.name", "p2TestBean");
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@@ -287,8 +270,8 @@ class PropertySourceAnnotationTests {
|
||||
// SPR-10820: p2 should 'win' as it was registered last
|
||||
AnnotationConfigApplicationContext ctxWithName = new AnnotationConfigApplicationContext(ConfigWithNameAndMultipleResourceLocations.class);
|
||||
AnnotationConfigApplicationContext ctxWithoutName = new AnnotationConfigApplicationContext(ConfigWithMultipleResourceLocations.class);
|
||||
assertThat(ctxWithoutName.getEnvironment().getProperty("testbean.name")).isEqualTo("p2TestBean");
|
||||
assertThat(ctxWithName.getEnvironment().getProperty("testbean.name")).isEqualTo("p2TestBean");
|
||||
assertEnvironmentProperty(ctxWithName, "testbean.name", "p2TestBean");
|
||||
assertEnvironmentProperty(ctxWithoutName, "testbean.name", "p2TestBean");
|
||||
ctxWithName.close();
|
||||
ctxWithoutName.close();
|
||||
}
|
||||
@@ -297,7 +280,7 @@ class PropertySourceAnnotationTests {
|
||||
void orderingWithAndWithoutNameAndFourResourceLocations() {
|
||||
// SPR-12198: p4 should 'win' as it was registered last
|
||||
AnnotationConfigApplicationContext ctxWithoutName = new AnnotationConfigApplicationContext(ConfigWithFourResourceLocations.class);
|
||||
assertThat(ctxWithoutName.getEnvironment().getProperty("testbean.name")).isEqualTo("p4TestBean");
|
||||
assertEnvironmentProperty(ctxWithoutName, "testbean.name", "p4TestBean");
|
||||
ctxWithoutName.close();
|
||||
}
|
||||
|
||||
@@ -305,11 +288,11 @@ class PropertySourceAnnotationTests {
|
||||
void orderingDoesntReplaceExisting() throws Exception {
|
||||
// SPR-12198: mySource should 'win' as it was registered manually
|
||||
AnnotationConfigApplicationContext ctxWithoutName = new AnnotationConfigApplicationContext();
|
||||
MapPropertySource mySource = new MapPropertySource("mine", Collections.singletonMap("testbean.name", "myTestBean"));
|
||||
MapPropertySource mySource = new MapPropertySource("mine", Map.of("testbean.name", "myTestBean"));
|
||||
ctxWithoutName.getEnvironment().getPropertySources().addLast(mySource);
|
||||
ctxWithoutName.register(ConfigWithFourResourceLocations.class);
|
||||
ctxWithoutName.refresh();
|
||||
assertThat(ctxWithoutName.getEnvironment().getProperty("testbean.name")).isEqualTo("myTestBean");
|
||||
assertEnvironmentProperty(ctxWithoutName, "testbean.name", "myTestBean");
|
||||
ctxWithoutName.close();
|
||||
}
|
||||
|
||||
@@ -320,8 +303,8 @@ class PropertySourceAnnotationTests {
|
||||
}
|
||||
}
|
||||
|
||||
private static void assertEnvironmentContainsProperty(ApplicationContext ctx, String name) {
|
||||
assertThat(ctx.getEnvironment().containsProperty(name)).as("environment contains property " + name).isTrue();
|
||||
private static void assertEnvironmentProperty(ApplicationContext ctx, String name, Object value) {
|
||||
assertThat(ctx.getEnvironment().getProperty(name)).isEqualTo(value);
|
||||
}
|
||||
|
||||
|
||||
@@ -582,7 +565,6 @@ class PropertySourceAnnotationTests {
|
||||
})
|
||||
@Configuration
|
||||
static class ConfigWithSameSourceImportedInDifferentOrder {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user