Add more tests for @ComponentScan as a meta-annotation

Issue: SPR-11557
This commit is contained in:
Sam Brannen 2014-03-15 15:28:17 +01:00
parent 189cd663c5
commit e7b8a657b4
4 changed files with 158 additions and 10 deletions

View File

@ -109,7 +109,7 @@ public class ComponentScanAnnotationIntegrationTests {
}
@Test
public void viaContextRegistration_WithComposedAnnotation() {
public void viaContextRegistration_WithLocallyDeclaredComposedAnnotation() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(ComposedAnnotationConfig.class);
ctx.refresh();
@ -122,6 +122,19 @@ public class ComponentScanAnnotationIntegrationTests {
ctx.containsBean("simpleComponent"), is(true));
}
@Test
public void viaContextRegistration_WithExternallyDeclaredComposedAnnotation() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(org.springframework.context.annotation.componentscan.meta.ComposedAnnotationConfig.class);
ctx.refresh();
ctx.getBean(org.springframework.context.annotation.componentscan.meta.ComposedAnnotationConfig.class);
ctx.getBean(SimpleComponent.class);
assertThat("config class bean not found", ctx.containsBeanDefinition("composedAnnotationConfig"), is(true));
assertThat("@ComponentScan annotated @Configuration class registered directly against "
+ "AnnotationConfigApplicationContext did not trigger component scanning as expected",
ctx.containsBean("simpleComponent"), is(true));
}
@Test
public void viaBeanRegistration() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
@ -236,10 +249,7 @@ public class ComponentScanAnnotationIntegrationTests {
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public static @interface ComposedConfiguration {
String[] basePackages() default {};
String[] bundles() default {};
}
@ComposedConfiguration(basePackages = "org.springframework.context.annotation.componentscan.simple")

View File

@ -111,8 +111,50 @@ public class ConfigurationClassPostProcessorTests {
}
@Test
public void postProcessorWorksWithComposedAnnotations() {
beanFactory.registerBeanDefinition("config", new RootBeanDefinition(ComposedAnnotationConfig.class));
public void postProcessorWorksWithLocallyDeclaredComposedConfiguration() {
beanFactory.registerBeanDefinition("config", new RootBeanDefinition(ComposedConfigurationClass.class));
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
pp.setEnvironment(new StandardEnvironment());
pp.postProcessBeanFactory(beanFactory);
SimpleComponent simpleComponent = beanFactory.getBean(SimpleComponent.class);
assertNotNull(simpleComponent);
}
@Test
public void postProcessorWorksWithLocallyDeclaredComposedComposedConfiguration() {
beanFactory.registerBeanDefinition("config", new RootBeanDefinition(ComposedComposedConfigurationClass.class));
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
pp.setEnvironment(new StandardEnvironment());
pp.postProcessBeanFactory(beanFactory);
SimpleComponent simpleComponent = beanFactory.getBean(SimpleComponent.class);
assertNotNull(simpleComponent);
}
@Test
public void postProcessorWorksWithLocallyDeclaredMetaComponentScanConfiguration() {
beanFactory.registerBeanDefinition("config", new RootBeanDefinition(MetaComponentScanConfigurationClass.class));
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
pp.setEnvironment(new StandardEnvironment());
pp.postProcessBeanFactory(beanFactory);
SimpleComponent simpleComponent = beanFactory.getBean(SimpleComponent.class);
assertNotNull(simpleComponent);
}
@Test
public void postProcessorWorksWithLocallyDeclaredMetaComponentScanConfigurationSubclass() {
beanFactory.registerBeanDefinition("config", new RootBeanDefinition(
SubMetaComponentScanConfigurationClass.class));
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
pp.setEnvironment(new StandardEnvironment());
pp.postProcessBeanFactory(beanFactory);
SimpleComponent simpleComponent = beanFactory.getBean(SimpleComponent.class);
assertNotNull(simpleComponent);
}
@Test
public void postProcessorWorksWithExternallyDeclaredComposedAnnotation() {
beanFactory.registerBeanDefinition("config", new RootBeanDefinition(
org.springframework.context.annotation.componentscan.meta.ComposedAnnotationConfig.class));
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
pp.setEnvironment(new StandardEnvironment());
pp.postProcessBeanFactory(beanFactory);
@ -632,14 +674,44 @@ public class ConfigurationClassPostProcessorTests {
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public static @interface ComposedConfiguration {
String[] basePackages() default {};
String[] bundles() default {};
}
@ComposedConfiguration(basePackages = "org.springframework.context.annotation.componentscan.simple")
public static class ComposedAnnotationConfig {
public static class ComposedConfigurationClass {
}
@ComposedConfiguration
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public static @interface ComposedComposedConfiguration {
String[] basePackages() default {};
}
@ComposedComposedConfiguration(basePackages = "org.springframework.context.annotation.componentscan.simple")
public static class ComposedComposedConfigurationClass {
}
@ComponentScan
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public static @interface MetaComponentScan {
}
@MetaComponentScan
@Configuration
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public static @interface MetaComponentScanConfiguration {
String[] basePackages() default {};
}
@MetaComponentScanConfiguration(basePackages = "org.springframework.context.annotation.componentscan.simple")
public static class MetaComponentScanConfigurationClass {
}
@Configuration
public static class SubMetaComponentScanConfigurationClass extends MetaComponentScanConfigurationClass {
}
}

View File

@ -0,0 +1,26 @@
/*
* Copyright 2002-2014 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
*
* http://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.context.annotation.componentscan.meta;
/**
* @author Sam Brannen
* @since 4.0.3
*/
@ComposedConfiguration(basePackages = "org.springframework.context.annotation.componentscan.simple")
public class ComposedAnnotationConfig {
}

View File

@ -0,0 +1,40 @@
/*
* Copyright 2002-2014 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
*
* http://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.context.annotation.componentscan.meta;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* Composed annotation, combining {@link Configuration @Configuration} and
* {@link ComponentScan @ComponentScan}.
*
* @author Sam Brannen
* @since 4.0.3
*/
@Configuration
@ComponentScan
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ComposedConfiguration {
String[] basePackages() default {};
}