From e7b8a657b477ddf88e737ee8b7806f0338464365 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 15 Mar 2014 15:28:17 +0100 Subject: [PATCH] Add more tests for @ComponentScan as a meta-annotation Issue: SPR-11557 --- ...mponentScanAnnotationIntegrationTests.java | 18 +++- .../ConfigurationClassPostProcessorTests.java | 82 +++++++++++++++++-- .../meta/ComposedAnnotationConfig.java | 26 ++++++ .../meta/ComposedConfiguration.java | 40 +++++++++ 4 files changed, 157 insertions(+), 9 deletions(-) create mode 100644 spring-context/src/test/java/org/springframework/context/annotation/componentscan/meta/ComposedAnnotationConfig.java create mode 100644 spring-context/src/test/java/org/springframework/context/annotation/componentscan/meta/ComposedConfiguration.java diff --git a/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanAnnotationIntegrationTests.java b/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanAnnotationIntegrationTests.java index d5f81b9b777..ff27755c049 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanAnnotationIntegrationTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanAnnotationIntegrationTests.java @@ -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") diff --git a/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java b/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java index 23fe04c210f..fa378bf2c2b 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java @@ -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 {}; + } + + @ComposedConfiguration(basePackages = "org.springframework.context.annotation.componentscan.simple") + public static class ComposedConfigurationClass { + } + @ComposedConfiguration + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.TYPE) + public static @interface ComposedComposedConfiguration { String[] basePackages() default {}; + } - String[] bundles() default {}; + @ComposedComposedConfiguration(basePackages = "org.springframework.context.annotation.componentscan.simple") + public static class ComposedComposedConfigurationClass { } - @ComposedConfiguration(basePackages = "org.springframework.context.annotation.componentscan.simple") - public static class ComposedAnnotationConfig { + @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 { } } diff --git a/spring-context/src/test/java/org/springframework/context/annotation/componentscan/meta/ComposedAnnotationConfig.java b/spring-context/src/test/java/org/springframework/context/annotation/componentscan/meta/ComposedAnnotationConfig.java new file mode 100644 index 00000000000..57a77ccab69 --- /dev/null +++ b/spring-context/src/test/java/org/springframework/context/annotation/componentscan/meta/ComposedAnnotationConfig.java @@ -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 { + +} diff --git a/spring-context/src/test/java/org/springframework/context/annotation/componentscan/meta/ComposedConfiguration.java b/spring-context/src/test/java/org/springframework/context/annotation/componentscan/meta/ComposedConfiguration.java new file mode 100644 index 00000000000..bdaedbcd8c7 --- /dev/null +++ b/spring-context/src/test/java/org/springframework/context/annotation/componentscan/meta/ComposedConfiguration.java @@ -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 {}; +}