Polish ComponentScanParserTests and clean up warnings

This commit is contained in:
Sam Brannen 2014-03-20 01:28:59 +01:00
parent ad09f1b5ec
commit 9cee70ff66
1 changed files with 37 additions and 33 deletions

View File

@ -16,13 +16,6 @@
package org.springframework.context.annotation; 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.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
@ -30,7 +23,6 @@ import java.lang.annotation.Target;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext; import org.springframework.context.support.GenericXmlApplicationContext;
@ -41,6 +33,9 @@ import org.springframework.core.type.filter.TypeFilter;
import example.profilescan.ProfileAnnotatedComponent; import example.profilescan.ProfileAnnotatedComponent;
import example.scannable.AutowiredQualifierFooService; import example.scannable.AutowiredQualifierFooService;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
/** /**
* @author Mark Fisher * @author Mark Fisher
* @author Juergen Hoeller * @author Juergen Hoeller
@ -49,62 +44,67 @@ import example.scannable.AutowiredQualifierFooService;
*/ */
public class ComponentScanParserTests { public class ComponentScanParserTests {
private ClassPathXmlApplicationContext loadContext(String path) {
return new ClassPathXmlApplicationContext(path, getClass());
}
@Test @Test
public void testAspectJTypeFilter() { public void aspectJTypeFilter() {
ApplicationContext context = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext context = loadContext("aspectjTypeFilterTests.xml");
"org/springframework/context/annotation/aspectjTypeFilterTests.xml");
assertTrue(context.containsBean("fooServiceImpl")); assertTrue(context.containsBean("fooServiceImpl"));
assertTrue(context.containsBean("stubFooDao")); assertTrue(context.containsBean("stubFooDao"));
assertFalse(context.containsBean("scopedProxyTestBean")); assertFalse(context.containsBean("scopedProxyTestBean"));
context.close();
} }
@Test @Test
public void testNonMatchingResourcePattern() { public void nonMatchingResourcePattern() {
ApplicationContext context = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext context = loadContext("nonMatchingResourcePatternTests.xml");
"org/springframework/context/annotation/nonMatchingResourcePatternTests.xml");
assertFalse(context.containsBean("fooServiceImpl")); assertFalse(context.containsBean("fooServiceImpl"));
context.close();
} }
@Test @Test
public void testMatchingResourcePattern() { public void matchingResourcePattern() {
ApplicationContext context = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext context = loadContext("matchingResourcePatternTests.xml");
"org/springframework/context/annotation/matchingResourcePatternTests.xml");
assertTrue(context.containsBean("fooServiceImpl")); assertTrue(context.containsBean("fooServiceImpl"));
context.close();
} }
@Test @Test
public void testComponentScanWithAutowiredQualifier() { public void componentScanWithAutowiredQualifier() {
ApplicationContext context = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext context = loadContext("componentScanWithAutowiredQualifierTests.xml");
"org/springframework/context/annotation/componentScanWithAutowiredQualifierTests.xml");
AutowiredQualifierFooService fooService = (AutowiredQualifierFooService) context.getBean("fooService"); AutowiredQualifierFooService fooService = (AutowiredQualifierFooService) context.getBean("fooService");
assertTrue(fooService.isInitCalled()); assertTrue(fooService.isInitCalled());
assertEquals("bar", fooService.foo(123)); assertEquals("bar", fooService.foo(123));
context.close();
} }
@Test @Test
public void testCustomAnnotationUsedForBothComponentScanAndQualifier() { public void customAnnotationUsedForBothComponentScanAndQualifier() {
ApplicationContext context = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext context = loadContext("customAnnotationUsedForBothComponentScanAndQualifierTests.xml");
"org/springframework/context/annotation/customAnnotationUsedForBothComponentScanAndQualifierTests.xml");
KustomAnnotationAutowiredBean testBean = (KustomAnnotationAutowiredBean) context.getBean("testBean"); KustomAnnotationAutowiredBean testBean = (KustomAnnotationAutowiredBean) context.getBean("testBean");
assertNotNull(testBean.getDependency()); assertNotNull(testBean.getDependency());
context.close();
} }
@Test @Test
public void testCustomTypeFilter() { public void customTypeFilter() {
ApplicationContext context = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext context = loadContext("customTypeFilterTests.xml");
"org/springframework/context/annotation/customTypeFilterTests.xml");
KustomAnnotationAutowiredBean testBean = (KustomAnnotationAutowiredBean) context.getBean("testBean"); KustomAnnotationAutowiredBean testBean = (KustomAnnotationAutowiredBean) context.getBean("testBean");
assertNotNull(testBean.getDependency()); assertNotNull(testBean.getDependency());
context.close();
} }
@Test @Test
public void testComponentScanRespectsProfileAnnotation() { public void componentScanRespectsProfileAnnotation() {
String xmlLocation = "org/springframework/context/annotation/componentScanRespectsProfileAnnotationTests.xml"; String xmlLocation = "org/springframework/context/annotation/componentScanRespectsProfileAnnotationTests.xml";
{ // should exclude the profile-annotated bean if active profiles remains unset { // should exclude the profile-annotated bean if active profiles remains unset
GenericXmlApplicationContext context = new GenericXmlApplicationContext(); GenericXmlApplicationContext context = new GenericXmlApplicationContext();
context.load(xmlLocation); context.load(xmlLocation);
context.refresh(); context.refresh();
assertThat(context.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(false)); assertThat(context.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(false));
context.close();
} }
{ // should include the profile-annotated bean with active profiles set { // should include the profile-annotated bean with active profiles set
GenericXmlApplicationContext context = new GenericXmlApplicationContext(); GenericXmlApplicationContext context = new GenericXmlApplicationContext();
@ -112,13 +112,15 @@ public class ComponentScanParserTests {
context.load(xmlLocation); context.load(xmlLocation);
context.refresh(); context.refresh();
assertThat(context.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(true)); assertThat(context.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(true));
context.close();
} }
{ // ensure the same works for AbstractRefreshableApplicationContext impls too { // ensure the same works for AbstractRefreshableApplicationContext impls too
ConfigurableApplicationContext context = ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(new String[] { xmlLocation },
new ClassPathXmlApplicationContext(new String[]{xmlLocation}, false); false);
context.getEnvironment().setActiveProfiles(ProfileAnnotatedComponent.PROFILE_NAME); context.getEnvironment().setActiveProfiles(ProfileAnnotatedComponent.PROFILE_NAME);
context.refresh(); context.refresh();
assertThat(context.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(true)); assertThat(context.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(true));
context.close();
} }
} }
@ -128,7 +130,6 @@ public class ComponentScanParserTests {
public static @interface CustomAnnotation { public static @interface CustomAnnotation {
} }
/** /**
* Intentionally spelling "custom" with a "k" since there are numerous * Intentionally spelling "custom" with a "k" since there are numerous
* classes in this package named *Custom*. * classes in this package named *Custom*.
@ -139,17 +140,20 @@ public class ComponentScanParserTests {
@CustomAnnotation @CustomAnnotation
private KustomAnnotationDependencyBean dependency; private KustomAnnotationDependencyBean dependency;
public KustomAnnotationDependencyBean getDependency() { public KustomAnnotationDependencyBean getDependency() {
return this.dependency; return this.dependency;
} }
} }
/**
* Intentionally spelling "custom" with a "k" since there are numerous
* classes in this package named *Custom*.
*/
@CustomAnnotation @CustomAnnotation
public static class KustomAnnotationDependencyBean { public static class KustomAnnotationDependencyBean {
} }
public static class CustomTypeFilter implements TypeFilter { public static class CustomTypeFilter implements TypeFilter {
/** /**