Skip to content

Commit

Permalink
Add tests for multiple composed @ComponentScan/@propertysource annota…
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Aug 12, 2023
1 parent 967a05e commit 8689395
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ComponentScan.Filter;
Expand Down Expand Up @@ -143,6 +144,15 @@ public void viaContextRegistration_WithComposedAnnotation() {
.isTrue();
}

@Test
void multipleComposedComponentScanAnnotations() { // gh-30941
ApplicationContext ctx = new AnnotationConfigApplicationContext(MultipleComposedAnnotationsConfig.class);
ctx.getBean(MultipleComposedAnnotationsConfig.class);
assertContextContainsBean(ctx, "componentScanAnnotationIntegrationTests.MultipleComposedAnnotationsConfig");
assertContextContainsBean(ctx, "simpleComponent");
assertContextContainsBean(ctx, "barComponent");
}

@Test
public void viaBeanRegistration() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
Expand Down Expand Up @@ -267,6 +277,10 @@ public void withBasePackagesAndValueAlias() {
assertThat(ctx.containsBean("fooServiceImpl")).isTrue();
}

private static void assertContextContainsBean(ApplicationContext ctx, String beanName) {
assertThat(ctx.containsBean(beanName)).as("context contains bean " + beanName).isTrue();
}


@Configuration
@ComponentScan
Expand All @@ -278,10 +292,25 @@ public void withBasePackagesAndValueAlias() {
String[] basePackages() default {};
}

@Configuration
@ComponentScan
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ComposedConfiguration2 {

@AliasFor(annotation = ComponentScan.class)
String[] basePackages() default {};
}

@ComposedConfiguration(basePackages = "org.springframework.context.annotation.componentscan.simple")
public static class ComposedAnnotationConfig {
}

@ComposedConfiguration(basePackages = "org.springframework.context.annotation.componentscan.simple")
@ComposedConfiguration2(basePackages = "example.scannable.sub")
static class MultipleComposedAnnotationsConfig {
}

public static class AwareTypeFilter implements TypeFilter, EnvironmentAware,
ResourceLoaderAware, BeanClassLoaderAware, BeanFactoryAware {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 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.
Expand Down Expand Up @@ -32,6 +32,7 @@
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.testfixture.beans.TestBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.annotation.AliasFor;
import org.springframework.core.env.Environment;
Expand Down Expand Up @@ -239,6 +240,15 @@ void withRepeatedPropertySourcesOnComposedAnnotation() {
}
}

@Test
void multipleComposedPropertySourceAnnotations() { // gh-30941
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(MultipleComposedAnnotationsConfig.class);
ctx.getBean(MultipleComposedAnnotationsConfig.class);
assertEnvironmentContainsProperty(ctx, "from.p1");
assertEnvironmentContainsProperty(ctx, "from.p2");
ctx.close();
}

@Test
void withNamedPropertySources() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNamedPropertySources.class);
Expand Down Expand Up @@ -304,6 +314,10 @@ void orderingDoesntReplaceExisting() throws Exception {
ctxWithoutName.close();
}

private static void assertEnvironmentContainsProperty(ApplicationContext ctx, String name) {
assertThat(ctx.getEnvironment().containsProperty(name)).as("environment contains property " + name).isTrue();
}


@Configuration
@PropertySource("classpath:${unresolvable}/p1.properties")
Expand Down Expand Up @@ -496,6 +510,21 @@ static class ConfigWithRepeatedPropertySourceAnnotations {
static class ConfigWithRepeatedPropertySourceAnnotationsOnComposedAnnotation {
}

@Retention(RetentionPolicy.RUNTIME)
@PropertySource("classpath:org/springframework/context/annotation/p1.properties")
@interface PropertySource1 {
}

@Retention(RetentionPolicy.RUNTIME)
@PropertySource("classpath:org/springframework/context/annotation/p2.properties")
@interface PropertySource2 {
}

@Configuration
@PropertySource1
@PropertySource2
static class MultipleComposedAnnotationsConfig {
}

@Configuration
@PropertySources({
Expand Down

0 comments on commit 8689395

Please sign in to comment.