From 9cee70ff66c4762fd2ffc3ade71d322765bdeca0 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Thu, 20 Mar 2014 01:28:59 +0100 Subject: [PATCH] Polish ComponentScanParserTests and clean up warnings --- .../annotation/ComponentScanParserTests.java | 70 ++++++++++--------- 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanParserTests.java b/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanParserTests.java index f9e2435a34c..8b1068b65fb 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanParserTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanParserTests.java @@ -16,13 +16,6 @@ package org.springframework.context.annotation; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -30,7 +23,6 @@ import java.lang.annotation.Target; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.GenericXmlApplicationContext; @@ -41,6 +33,9 @@ import org.springframework.core.type.filter.TypeFilter; import example.profilescan.ProfileAnnotatedComponent; import example.scannable.AutowiredQualifierFooService; +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + /** * @author Mark Fisher * @author Juergen Hoeller @@ -49,62 +44,67 @@ import example.scannable.AutowiredQualifierFooService; */ public class ComponentScanParserTests { + private ClassPathXmlApplicationContext loadContext(String path) { + return new ClassPathXmlApplicationContext(path, getClass()); + } + @Test - public void testAspectJTypeFilter() { - ApplicationContext context = new ClassPathXmlApplicationContext( - "org/springframework/context/annotation/aspectjTypeFilterTests.xml"); + public void aspectJTypeFilter() { + ClassPathXmlApplicationContext context = loadContext("aspectjTypeFilterTests.xml"); assertTrue(context.containsBean("fooServiceImpl")); assertTrue(context.containsBean("stubFooDao")); assertFalse(context.containsBean("scopedProxyTestBean")); + context.close(); } @Test - public void testNonMatchingResourcePattern() { - ApplicationContext context = new ClassPathXmlApplicationContext( - "org/springframework/context/annotation/nonMatchingResourcePatternTests.xml"); + public void nonMatchingResourcePattern() { + ClassPathXmlApplicationContext context = loadContext("nonMatchingResourcePatternTests.xml"); assertFalse(context.containsBean("fooServiceImpl")); + context.close(); } @Test - public void testMatchingResourcePattern() { - ApplicationContext context = new ClassPathXmlApplicationContext( - "org/springframework/context/annotation/matchingResourcePatternTests.xml"); + public void matchingResourcePattern() { + ClassPathXmlApplicationContext context = loadContext("matchingResourcePatternTests.xml"); assertTrue(context.containsBean("fooServiceImpl")); + context.close(); } @Test - public void testComponentScanWithAutowiredQualifier() { - ApplicationContext context = new ClassPathXmlApplicationContext( - "org/springframework/context/annotation/componentScanWithAutowiredQualifierTests.xml"); + public void componentScanWithAutowiredQualifier() { + ClassPathXmlApplicationContext context = loadContext("componentScanWithAutowiredQualifierTests.xml"); AutowiredQualifierFooService fooService = (AutowiredQualifierFooService) context.getBean("fooService"); assertTrue(fooService.isInitCalled()); assertEquals("bar", fooService.foo(123)); + context.close(); } @Test - public void testCustomAnnotationUsedForBothComponentScanAndQualifier() { - ApplicationContext context = new ClassPathXmlApplicationContext( - "org/springframework/context/annotation/customAnnotationUsedForBothComponentScanAndQualifierTests.xml"); + public void customAnnotationUsedForBothComponentScanAndQualifier() { + ClassPathXmlApplicationContext context = loadContext("customAnnotationUsedForBothComponentScanAndQualifierTests.xml"); KustomAnnotationAutowiredBean testBean = (KustomAnnotationAutowiredBean) context.getBean("testBean"); assertNotNull(testBean.getDependency()); + context.close(); } @Test - public void testCustomTypeFilter() { - ApplicationContext context = new ClassPathXmlApplicationContext( - "org/springframework/context/annotation/customTypeFilterTests.xml"); + public void customTypeFilter() { + ClassPathXmlApplicationContext context = loadContext("customTypeFilterTests.xml"); KustomAnnotationAutowiredBean testBean = (KustomAnnotationAutowiredBean) context.getBean("testBean"); assertNotNull(testBean.getDependency()); + context.close(); } @Test - public void testComponentScanRespectsProfileAnnotation() { + public void componentScanRespectsProfileAnnotation() { String xmlLocation = "org/springframework/context/annotation/componentScanRespectsProfileAnnotationTests.xml"; { // should exclude the profile-annotated bean if active profiles remains unset GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.load(xmlLocation); context.refresh(); assertThat(context.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(false)); + context.close(); } { // should include the profile-annotated bean with active profiles set GenericXmlApplicationContext context = new GenericXmlApplicationContext(); @@ -112,23 +112,24 @@ public class ComponentScanParserTests { context.load(xmlLocation); context.refresh(); assertThat(context.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(true)); + context.close(); } { // ensure the same works for AbstractRefreshableApplicationContext impls too - ConfigurableApplicationContext context = - new ClassPathXmlApplicationContext(new String[]{xmlLocation}, false); + ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(new String[] { xmlLocation }, + false); context.getEnvironment().setActiveProfiles(ProfileAnnotatedComponent.PROFILE_NAME); context.refresh(); assertThat(context.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(true)); + context.close(); } } - @Target({ElementType.TYPE, ElementType.FIELD}) + @Target({ ElementType.TYPE, ElementType.FIELD }) @Retention(RetentionPolicy.RUNTIME) public static @interface CustomAnnotation { } - /** * Intentionally spelling "custom" with a "k" since there are numerous * classes in this package named *Custom*. @@ -139,17 +140,20 @@ public class ComponentScanParserTests { @CustomAnnotation private KustomAnnotationDependencyBean dependency; + public KustomAnnotationDependencyBean getDependency() { return this.dependency; } } - + /** + * Intentionally spelling "custom" with a "k" since there are numerous + * classes in this package named *Custom*. + */ @CustomAnnotation public static class KustomAnnotationDependencyBean { } - public static class CustomTypeFilter implements TypeFilter { /**