20
20
import java .lang .annotation .Retention ;
21
21
import java .lang .annotation .RetentionPolicy ;
22
22
import java .lang .annotation .Target ;
23
+ import java .util .HashMap ;
24
+ import java .util .Map ;
23
25
26
+ import org .hamcrest .Matcher ;
27
+ import org .junit .BeforeClass ;
24
28
import org .junit .Test ;
25
29
import org .mockito .InOrder ;
26
30
import org .springframework .beans .BeansException ;
41
45
42
46
import static org .hamcrest .CoreMatchers .*;
43
47
import static org .junit .Assert .*;
48
+ import static org .mockito .Matchers .*;
44
49
import static org .mockito .Mockito .*;
45
50
46
51
/**
50
55
*/
51
56
public class ImportSelectorTests {
52
57
58
+ static Map <Class <?>, String > importFrom = new HashMap <Class <?>, String >();
59
+
60
+ @ BeforeClass
61
+ public static void clearImportFrom () {
62
+ ImportSelectorTests .importFrom .clear ();
63
+ }
64
+
53
65
@ Test
54
66
public void importSelectors () {
55
67
DefaultListableBeanFactory beanFactory = spy (new DefaultListableBeanFactory ());
@@ -67,16 +79,25 @@ public void importSelectors() {
67
79
68
80
@ Test
69
81
public void invokeAwareMethodsInImportSelector () {
70
-
71
82
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (AwareConfig .class );
72
83
context .getBean (MessageSource .class );
73
-
74
84
assertThat (SampleRegistrar .beanFactory , is ((BeanFactory ) context .getBeanFactory ()));
75
85
assertThat (SampleRegistrar .classLoader , is (context .getBeanFactory ().getBeanClassLoader ()));
76
86
assertThat (SampleRegistrar .resourceLoader , is (notNullValue ()));
77
87
assertThat (SampleRegistrar .environment , is ((Environment ) context .getEnvironment ()));
78
88
}
79
89
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
+
80
101
@ Configuration
81
102
@ Import (SampleImportSelector .class )
82
103
static class AwareConfig {
@@ -133,6 +154,7 @@ public static class ImportSelector1 implements ImportSelector {
133
154
134
155
@ Override
135
156
public String [] selectImports (AnnotationMetadata importingClassMetadata ) {
157
+ ImportSelectorTests .importFrom .put (getClass (), importingClassMetadata .getClassName ());
136
158
return new String [] { ImportedSelector1 .class .getName () };
137
159
}
138
160
}
@@ -141,6 +163,7 @@ public static class ImportSelector2 implements ImportSelector {
141
163
142
164
@ Override
143
165
public String [] selectImports (AnnotationMetadata importingClassMetadata ) {
166
+ ImportSelectorTests .importFrom .put (getClass (), importingClassMetadata .getClassName ());
144
167
return new String [] { ImportedSelector2 .class .getName () };
145
168
}
146
169
}
@@ -149,6 +172,7 @@ public static class DeferredImportSelector1 implements DeferredImportSelector, O
149
172
150
173
@ Override
151
174
public String [] selectImports (AnnotationMetadata importingClassMetadata ) {
175
+ ImportSelectorTests .importFrom .put (getClass (), importingClassMetadata .getClassName ());
152
176
return new String [] { DeferredImportedSelector1 .class .getName () };
153
177
}
154
178
@@ -163,6 +187,7 @@ public static class DeferredImportSelector2 implements DeferredImportSelector {
163
187
164
188
@ Override
165
189
public String [] selectImports (AnnotationMetadata importingClassMetadata ) {
190
+ ImportSelectorTests .importFrom .put (getClass (), importingClassMetadata .getClassName ());
166
191
return new String [] { DeferredImportedSelector2 .class .getName () };
167
192
}
168
193
@@ -204,4 +229,22 @@ public String d() {
204
229
}
205
230
}
206
231
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
+
207
250
}
0 commit comments