|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2013 the original author or authors. |
|
|
|
* Copyright 2002-2014 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -20,9 +20,14 @@ 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; |
|
|
|
import java.lang.annotation.Target; |
|
|
|
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.junit.Test; |
|
|
|
import org.mockito.InOrder; |
|
|
|
import org.mockito.InOrder; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.BeansException; |
|
|
|
import org.springframework.beans.BeansException; |
|
|
|
import org.springframework.beans.factory.BeanClassLoaderAware; |
|
|
|
import org.springframework.beans.factory.BeanClassLoaderAware; |
|
|
|
import org.springframework.beans.factory.BeanFactory; |
|
|
|
import org.springframework.beans.factory.BeanFactory; |
|
|
|
@ -41,7 +46,8 @@ import org.springframework.core.type.AnnotationMetadata; |
|
|
|
|
|
|
|
|
|
|
|
import static org.hamcrest.CoreMatchers.*; |
|
|
|
import static org.hamcrest.CoreMatchers.*; |
|
|
|
import static org.junit.Assert.*; |
|
|
|
import static org.junit.Assert.*; |
|
|
|
import static org.mockito.Matchers.*; |
|
|
|
import static org.mockito.Matchers.anyObject; |
|
|
|
|
|
|
|
import static org.mockito.Matchers.eq; |
|
|
|
import static org.mockito.Mockito.*; |
|
|
|
import static org.mockito.Mockito.*; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -51,11 +57,19 @@ import static org.mockito.Mockito.*; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class ImportSelectorTests { |
|
|
|
public class ImportSelectorTests { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static Map<Class<?>, String> importFrom = new HashMap<Class<?>, String>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@BeforeClass |
|
|
|
|
|
|
|
public static void clearImportFrom() { |
|
|
|
|
|
|
|
ImportSelectorTests.importFrom.clear(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void importSelectors() { |
|
|
|
public void importSelectors() { |
|
|
|
DefaultListableBeanFactory beanFactory = spy(new DefaultListableBeanFactory()); |
|
|
|
DefaultListableBeanFactory beanFactory = spy(new DefaultListableBeanFactory()); |
|
|
|
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( |
|
|
|
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(beanFactory); |
|
|
|
beanFactory); |
|
|
|
|
|
|
|
context.register(Config.class); |
|
|
|
context.register(Config.class); |
|
|
|
context.refresh(); |
|
|
|
context.refresh(); |
|
|
|
context.getBean(Config.class); |
|
|
|
context.getBean(Config.class); |
|
|
|
@ -68,22 +82,31 @@ public class ImportSelectorTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void invokeAwareMethodsInImportSelector() { |
|
|
|
public void invokeAwareMethodsInImportSelector() { |
|
|
|
|
|
|
|
|
|
|
|
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AwareConfig.class); |
|
|
|
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AwareConfig.class); |
|
|
|
context.getBean(MessageSource.class); |
|
|
|
context.getBean(MessageSource.class); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(SampleRegistrar.beanFactory, is((BeanFactory) context.getBeanFactory())); |
|
|
|
assertThat(SampleRegistrar.beanFactory, is((BeanFactory) context.getBeanFactory())); |
|
|
|
assertThat(SampleRegistrar.classLoader, is(context.getBeanFactory().getBeanClassLoader())); |
|
|
|
assertThat(SampleRegistrar.classLoader, is(context.getBeanFactory().getBeanClassLoader())); |
|
|
|
assertThat(SampleRegistrar.resourceLoader, is(notNullValue())); |
|
|
|
assertThat(SampleRegistrar.resourceLoader, is(notNullValue())); |
|
|
|
assertThat(SampleRegistrar.environment, is((Environment) context.getEnvironment())); |
|
|
|
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()); |
|
|
|
|
|
|
|
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 |
|
|
|
@Configuration |
|
|
|
@Import(SampleImportSelector.class) |
|
|
|
@Import(SampleImportSelector.class) |
|
|
|
static class AwareConfig { |
|
|
|
static class AwareConfig { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static class SampleImportSelector implements ImportSelector, BeanClassLoaderAware, ResourceLoaderAware, |
|
|
|
static class SampleImportSelector implements ImportSelector, BeanClassLoaderAware, ResourceLoaderAware, |
|
|
|
BeanFactoryAware, EnvironmentAware { |
|
|
|
BeanFactoryAware, EnvironmentAware { |
|
|
|
|
|
|
|
|
|
|
|
@ -118,38 +141,45 @@ public class ImportSelectorTests { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Sample |
|
|
|
@Sample |
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
static class Config { |
|
|
|
static class Config { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Target(ElementType.TYPE) |
|
|
|
@Target(ElementType.TYPE) |
|
|
|
@Retention(RetentionPolicy.RUNTIME) |
|
|
|
@Retention(RetentionPolicy.RUNTIME) |
|
|
|
@Import({ DeferredImportSelector1.class, DeferredImportSelector2.class, |
|
|
|
@Import({DeferredImportSelector1.class, DeferredImportSelector2.class, ImportSelector1.class, ImportSelector2.class}) |
|
|
|
ImportSelector1.class, ImportSelector2.class }) |
|
|
|
|
|
|
|
public static @interface Sample { |
|
|
|
public static @interface Sample { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static class ImportSelector1 implements ImportSelector { |
|
|
|
public static class ImportSelector1 implements ImportSelector { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public String[] selectImports(AnnotationMetadata importingClassMetadata) { |
|
|
|
public String[] selectImports(AnnotationMetadata importingClassMetadata) { |
|
|
|
|
|
|
|
ImportSelectorTests.importFrom.put(getClass(), importingClassMetadata.getClassName()); |
|
|
|
return new String[] { ImportedSelector1.class.getName() }; |
|
|
|
return new String[] { ImportedSelector1.class.getName() }; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static class ImportSelector2 implements ImportSelector { |
|
|
|
public static class ImportSelector2 implements ImportSelector { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public String[] selectImports(AnnotationMetadata importingClassMetadata) { |
|
|
|
public String[] selectImports(AnnotationMetadata importingClassMetadata) { |
|
|
|
|
|
|
|
ImportSelectorTests.importFrom.put(getClass(), importingClassMetadata.getClassName()); |
|
|
|
return new String[] { ImportedSelector2.class.getName() }; |
|
|
|
return new String[] { ImportedSelector2.class.getName() }; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static class DeferredImportSelector1 implements DeferredImportSelector, Ordered { |
|
|
|
public static class DeferredImportSelector1 implements DeferredImportSelector, Ordered { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public String[] selectImports(AnnotationMetadata importingClassMetadata) { |
|
|
|
public String[] selectImports(AnnotationMetadata importingClassMetadata) { |
|
|
|
|
|
|
|
ImportSelectorTests.importFrom.put(getClass(), importingClassMetadata.getClassName()); |
|
|
|
return new String[] { DeferredImportedSelector1.class.getName() }; |
|
|
|
return new String[] { DeferredImportedSelector1.class.getName() }; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -159,16 +189,18 @@ public class ImportSelectorTests { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Order(Ordered.HIGHEST_PRECEDENCE) |
|
|
|
@Order(Ordered.HIGHEST_PRECEDENCE) |
|
|
|
public static class DeferredImportSelector2 implements DeferredImportSelector { |
|
|
|
public static class DeferredImportSelector2 implements DeferredImportSelector { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public String[] selectImports(AnnotationMetadata importingClassMetadata) { |
|
|
|
public String[] selectImports(AnnotationMetadata importingClassMetadata) { |
|
|
|
|
|
|
|
ImportSelectorTests.importFrom.put(getClass(), importingClassMetadata.getClassName()); |
|
|
|
return new String[] { DeferredImportedSelector2.class.getName() }; |
|
|
|
return new String[] { DeferredImportedSelector2.class.getName() }; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
public static class ImportedSelector1 { |
|
|
|
public static class ImportedSelector1 { |
|
|
|
|
|
|
|
|
|
|
|
@ -178,6 +210,7 @@ public class ImportSelectorTests { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
public static class ImportedSelector2 { |
|
|
|
public static class ImportedSelector2 { |
|
|
|
|
|
|
|
|
|
|
|
@ -187,6 +220,7 @@ public class ImportSelectorTests { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
public static class DeferredImportedSelector1 { |
|
|
|
public static class DeferredImportedSelector1 { |
|
|
|
|
|
|
|
|
|
|
|
@ -196,6 +230,7 @@ public class ImportSelectorTests { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
public static class DeferredImportedSelector2 { |
|
|
|
public static class DeferredImportedSelector2 { |
|
|
|
|
|
|
|
|
|
|
|
@ -205,4 +240,24 @@ 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 { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|