2020import java .lang .annotation .Retention ;
2121import java .lang .annotation .RetentionPolicy ;
2222import java .lang .annotation .Target ;
23+ import java .util .HashMap ;
24+ import java .util .Map ;
2325
26+ import org .hamcrest .Matcher ;
27+ import org .junit .BeforeClass ;
2428import org .junit .Test ;
2529import org .mockito .InOrder ;
2630import org .springframework .beans .BeansException ;
4145
4246import static org .hamcrest .CoreMatchers .*;
4347import static org .junit .Assert .*;
48+ import static org .mockito .Matchers .*;
4449import static org .mockito .Mockito .*;
4550
4651/**
5055 */
5156public class ImportSelectorTests {
5257
58+ static Map <Class <?>, String > importFrom = new HashMap <Class <?>, String >();
59+
60+ @ BeforeClass
61+ public static void clearImportFrom () {
62+ ImportSelectorTests .importFrom .clear ();
63+ }
64+
5365 @ Test
5466 public void importSelectors () {
5567 DefaultListableBeanFactory beanFactory = spy (new DefaultListableBeanFactory ());
@@ -67,16 +79,25 @@ public void importSelectors() {
6779
6880 @ Test
6981 public void invokeAwareMethodsInImportSelector () {
70-
7182 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (AwareConfig .class );
7283 context .getBean (MessageSource .class );
73-
7484 assertThat (SampleRegistrar .beanFactory , is ((BeanFactory ) context .getBeanFactory ()));
7585 assertThat (SampleRegistrar .classLoader , is (context .getBeanFactory ().getBeanClassLoader ()));
7686 assertThat (SampleRegistrar .resourceLoader , is (notNullValue ()));
7787 assertThat (SampleRegistrar .environment , is ((Environment ) context .getEnvironment ()));
7888 }
7989
90+ @ Test
91+ public void correctMetaDataOnIndirectImports () throws Exception {
92+ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (IndirectConfig .class );
93+ Matcher <String > isFromIndirect = equalTo (IndirectImport .class .getName ());
94+ System .out .println (importFrom );
95+ assertThat (importFrom .get (ImportSelector1 .class ), isFromIndirect );
96+ assertThat (importFrom .get (ImportSelector2 .class ), isFromIndirect );
97+ assertThat (importFrom .get (DeferredImportSelector1 .class ), isFromIndirect );
98+ assertThat (importFrom .get (DeferredImportSelector2 .class ), isFromIndirect );
99+ }
100+
80101 @ Configuration
81102 @ Import (SampleImportSelector .class )
82103 static class AwareConfig {
@@ -133,6 +154,7 @@ public static class ImportSelector1 implements ImportSelector {
133154
134155 @ Override
135156 public String [] selectImports (AnnotationMetadata importingClassMetadata ) {
157+ ImportSelectorTests .importFrom .put (getClass (), importingClassMetadata .getClassName ());
136158 return new String [] { ImportedSelector1 .class .getName () };
137159 }
138160 }
@@ -141,6 +163,7 @@ public static class ImportSelector2 implements ImportSelector {
141163
142164 @ Override
143165 public String [] selectImports (AnnotationMetadata importingClassMetadata ) {
166+ ImportSelectorTests .importFrom .put (getClass (), importingClassMetadata .getClassName ());
144167 return new String [] { ImportedSelector2 .class .getName () };
145168 }
146169 }
@@ -149,6 +172,7 @@ public static class DeferredImportSelector1 implements DeferredImportSelector, O
149172
150173 @ Override
151174 public String [] selectImports (AnnotationMetadata importingClassMetadata ) {
175+ ImportSelectorTests .importFrom .put (getClass (), importingClassMetadata .getClassName ());
152176 return new String [] { DeferredImportedSelector1 .class .getName () };
153177 }
154178
@@ -163,6 +187,7 @@ public static class DeferredImportSelector2 implements DeferredImportSelector {
163187
164188 @ Override
165189 public String [] selectImports (AnnotationMetadata importingClassMetadata ) {
190+ ImportSelectorTests .importFrom .put (getClass (), importingClassMetadata .getClassName ());
166191 return new String [] { DeferredImportedSelector2 .class .getName () };
167192 }
168193
@@ -204,4 +229,22 @@ public String d() {
204229 }
205230 }
206231
232+ @ Configuration
233+ @ Import (IndirectImportSelector .class )
234+ public static class IndirectConfig {
235+
236+ }
237+
238+ public static class IndirectImportSelector implements ImportSelector {
239+ @ Override
240+ public String [] selectImports (AnnotationMetadata importingClassMetadata ) {
241+ return new String [] { IndirectImport .class .getName ()};
242+ }
243+ }
244+
245+ @ Sample
246+ public static class IndirectImport {
247+
248+ }
249+
207250}
0 commit comments