Browse Source

Add tests for ImportSelector meta-data

Issue: SPR-12059
pull/623/merge
Phillip Webb 12 years ago
parent
commit
e142fd11e0
  1. 47
      spring-context/src/test/java/org/springframework/context/annotation/ImportSelectorTests.java

47
spring-context/src/test/java/org/springframework/context/annotation/ImportSelectorTests.java

@ -20,7 +20,11 @@ import java.lang.annotation.ElementType; @@ -20,7 +20,11 @@ import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.HashMap;
import java.util.Map;
import org.hamcrest.Matcher;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.InOrder;
import org.springframework.beans.BeansException;
@ -41,6 +45,7 @@ import org.springframework.core.type.AnnotationMetadata; @@ -41,6 +45,7 @@ import org.springframework.core.type.AnnotationMetadata;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
/**
@ -50,6 +55,13 @@ import static org.mockito.Mockito.*; @@ -50,6 +55,13 @@ import static org.mockito.Mockito.*;
*/
public class ImportSelectorTests {
static Map<Class<?>, String> importFrom = new HashMap<Class<?>, String>();
@BeforeClass
public static void clearImportFrom() {
ImportSelectorTests.importFrom.clear();
}
@Test
public void importSelectors() {
DefaultListableBeanFactory beanFactory = spy(new DefaultListableBeanFactory());
@ -67,16 +79,25 @@ public class ImportSelectorTests { @@ -67,16 +79,25 @@ public class ImportSelectorTests {
@Test
public void invokeAwareMethodsInImportSelector() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AwareConfig.class);
context.getBean(MessageSource.class);
assertThat(SampleRegistrar.beanFactory, is((BeanFactory) context.getBeanFactory()));
assertThat(SampleRegistrar.classLoader, is(context.getBeanFactory().getBeanClassLoader()));
assertThat(SampleRegistrar.resourceLoader, is(notNullValue()));
assertThat(SampleRegistrar.environment, is((Environment) context.getEnvironment()));
}
@Test
public void correctMetaDataOnIndirectImports() throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(IndirectConfig.class);
Matcher<String> isFromIndirect = equalTo(IndirectImport.class.getName());
System.out.println(importFrom);
assertThat(importFrom.get(ImportSelector1.class), isFromIndirect);
assertThat(importFrom.get(ImportSelector2.class), isFromIndirect);
assertThat(importFrom.get(DeferredImportSelector1.class), isFromIndirect);
assertThat(importFrom.get(DeferredImportSelector2.class), isFromIndirect);
}
@Configuration
@Import(SampleImportSelector.class)
static class AwareConfig {
@ -133,6 +154,7 @@ public class ImportSelectorTests { @@ -133,6 +154,7 @@ public class ImportSelectorTests {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
ImportSelectorTests.importFrom.put(getClass(), importingClassMetadata.getClassName());
return new String[] { ImportedSelector1.class.getName() };
}
}
@ -141,6 +163,7 @@ public class ImportSelectorTests { @@ -141,6 +163,7 @@ public class ImportSelectorTests {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
ImportSelectorTests.importFrom.put(getClass(), importingClassMetadata.getClassName());
return new String[] { ImportedSelector2.class.getName() };
}
}
@ -149,6 +172,7 @@ public class ImportSelectorTests { @@ -149,6 +172,7 @@ public class ImportSelectorTests {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
ImportSelectorTests.importFrom.put(getClass(), importingClassMetadata.getClassName());
return new String[] { DeferredImportedSelector1.class.getName() };
}
@ -163,6 +187,7 @@ public class ImportSelectorTests { @@ -163,6 +187,7 @@ public class ImportSelectorTests {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
ImportSelectorTests.importFrom.put(getClass(), importingClassMetadata.getClassName());
return new String[] { DeferredImportedSelector2.class.getName() };
}
@ -204,4 +229,22 @@ public class ImportSelectorTests { @@ -204,4 +229,22 @@ public class ImportSelectorTests {
}
}
@Configuration
@Import(IndirectImportSelector.class)
public static class IndirectConfig {
}
public static class IndirectImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[] { IndirectImport.class.getName()};
}
}
@Sample
public static class IndirectImport {
}
}

Loading…
Cancel
Save