20
20
import java .util .Collections ;
21
21
import java .util .List ;
22
22
import java .util .Set ;
23
+ import java .util .function .Supplier ;
23
24
import javax .inject .Provider ;
24
25
26
+ import org .junit .Rule ;
25
27
import org .junit .Test ;
28
+ import org .junit .rules .ExpectedException ;
26
29
27
30
import org .springframework .beans .factory .BeanClassLoaderAware ;
28
31
import org .springframework .beans .factory .BeanFactory ;
65
68
*
66
69
* @author Chris Beams
67
70
* @author Juergen Hoeller
71
+ * @author Sam Brannen
68
72
*/
69
73
public class ConfigurationClassProcessingTests {
70
74
@@ -92,40 +96,57 @@ private DefaultListableBeanFactory initBeanFactory(Class<?>... configClasses) {
92
96
}
93
97
94
98
99
+ @ Rule
100
+ public final ExpectedException exception = ExpectedException .none ();
101
+
102
+
103
+ @ Test
104
+ public void customBeanNameIsRespectedWhenConfiguredViaNameAttribute () {
105
+ customBeanNameIsRespected (ConfigWithBeanWithCustomName .class ,
106
+ () -> ConfigWithBeanWithCustomName .testBean , "customName" );
107
+ }
108
+
95
109
@ Test
96
- public void customBeanNameIsRespected () {
110
+ public void customBeanNameIsRespectedWhenConfiguredViaValueAttribute () {
111
+ customBeanNameIsRespected (ConfigWithBeanWithCustomNameConfiguredViaValueAttribute .class ,
112
+ () -> ConfigWithBeanWithCustomNameConfiguredViaValueAttribute .testBean , "enigma" );
113
+ }
114
+
115
+ private void customBeanNameIsRespected (Class <?> testClass , Supplier <TestBean > testBeanSupplier , String beanName ) {
97
116
GenericApplicationContext ac = new GenericApplicationContext ();
98
117
AnnotationConfigUtils .registerAnnotationConfigProcessors (ac );
99
- ac .registerBeanDefinition ("config" , new RootBeanDefinition (ConfigWithBeanWithCustomName . class ));
118
+ ac .registerBeanDefinition ("config" , new RootBeanDefinition (testClass ));
100
119
ac .refresh ();
101
- assertSame (ac .getBean ("customName" ), ConfigWithBeanWithCustomName .testBean );
120
+
121
+ assertSame (testBeanSupplier .get (), ac .getBean (beanName ));
102
122
103
123
// method name should not be registered
104
- try {
105
- ac .getBean ("methodName" );
106
- fail ("bean should not have been registered with 'methodName'" );
107
- }
108
- catch (NoSuchBeanDefinitionException ex ) {
109
- // expected
110
- }
124
+ exception .expect (NoSuchBeanDefinitionException .class );
125
+ ac .getBean ("methodName" );
111
126
}
112
127
113
128
@ Test
114
- public void aliasesAreRespected () {
115
- BeanFactory factory = initBeanFactory (ConfigWithBeanWithAliases .class );
116
- assertSame (factory .getBean ("name1" ), ConfigWithBeanWithAliases .testBean );
117
- String [] aliases = factory .getAliases ("name1" );
118
- for (String alias : aliases )
119
- assertSame (factory .getBean (alias ), ConfigWithBeanWithAliases .testBean );
129
+ public void aliasesAreRespectedWhenConfiguredViaNameAttribute () {
130
+ aliasesAreRespected (ConfigWithBeanWithAliases .class ,
131
+ () -> ConfigWithBeanWithAliases .testBean , "name1" );
132
+ }
133
+
134
+ @ Test
135
+ public void aliasesAreRespectedWhenConfiguredViaValueAttribute () {
136
+ aliasesAreRespected (ConfigWithBeanWithAliasesConfiguredViaValueAttribute .class ,
137
+ () -> ConfigWithBeanWithAliasesConfiguredViaValueAttribute .testBean , "enigma" );
138
+ }
139
+
140
+ private void aliasesAreRespected (Class <?> testClass , Supplier <TestBean > testBeanSupplier , String beanName ) {
141
+ TestBean testBean = testBeanSupplier .get ();
142
+ BeanFactory factory = initBeanFactory (testClass );
143
+
144
+ assertSame (testBean , factory .getBean (beanName ));
145
+ Arrays .stream (factory .getAliases (beanName )).map (factory ::getBean ).forEach (alias -> assertSame (testBean , alias ));
120
146
121
147
// method name should not be registered
122
- try {
123
- factory .getBean ("methodName" );
124
- fail ("bean should not have been registered with 'methodName'" );
125
- }
126
- catch (NoSuchBeanDefinitionException ex ) {
127
- // expected
128
- }
148
+ exception .expect (NoSuchBeanDefinitionException .class );
149
+ factory .getBean ("methodName" );
129
150
}
130
151
131
152
@ Test // SPR-11830
@@ -146,8 +167,9 @@ public void configWithSetWithProviderImplementation() {
146
167
assertSame (ac .getBean ("customName" ), ConfigWithSetWithProviderImplementation .set );
147
168
}
148
169
149
- @ Test ( expected = BeanDefinitionParsingException . class )
170
+ @ Test
150
171
public void testFinalBeanMethod () {
172
+ exception .expect (BeanDefinitionParsingException .class );
151
173
initBeanFactory (ConfigWithFinalBean .class );
152
174
}
153
175
@@ -219,6 +241,7 @@ public void configurationWithAdaptivePrototypes() {
219
241
adaptive = factory .getBean (AdaptiveInjectionPoints .class );
220
242
assertEquals ("adaptiveInjectionPoint1" , adaptive .adaptiveInjectionPoint1 .getName ());
221
243
assertEquals ("setAdaptiveInjectionPoint2" , adaptive .adaptiveInjectionPoint2 .getName ());
244
+ factory .close ();
222
245
}
223
246
224
247
@ Test
@@ -240,15 +263,28 @@ public void configurationWithPostProcessor() {
240
263
241
264
SpousyTestBean listener = factory .getBean ("listenerTestBean" , SpousyTestBean .class );
242
265
assertTrue (listener .refreshed );
266
+ factory .close ();
243
267
}
244
268
245
269
246
270
@ Configuration
247
271
static class ConfigWithBeanWithCustomName {
248
272
249
- static TestBean testBean = new TestBean ();
273
+ static TestBean testBean = new TestBean (ConfigWithBeanWithCustomName . class . getSimpleName () );
250
274
251
- @ Bean (name ="customName" )
275
+ @ Bean (name = "customName" )
276
+ public TestBean methodName () {
277
+ return testBean ;
278
+ }
279
+ }
280
+
281
+
282
+ @ Configuration
283
+ static class ConfigWithBeanWithCustomNameConfiguredViaValueAttribute {
284
+
285
+ static TestBean testBean = new TestBean (ConfigWithBeanWithCustomNameConfiguredViaValueAttribute .class .getSimpleName ());
286
+
287
+ @ Bean ("enigma" )
252
288
public TestBean methodName () {
253
289
return testBean ;
254
290
}
@@ -258,9 +294,21 @@ public TestBean methodName() {
258
294
@ Configuration
259
295
static class ConfigWithBeanWithAliases {
260
296
261
- static TestBean testBean = new TestBean ();
297
+ static TestBean testBean = new TestBean (ConfigWithBeanWithAliases .class .getSimpleName ());
298
+
299
+ @ Bean (name = { "name1" , "alias1" , "alias2" , "alias3" })
300
+ public TestBean methodName () {
301
+ return testBean ;
302
+ }
303
+ }
304
+
305
+
306
+ @ Configuration
307
+ static class ConfigWithBeanWithAliasesConfiguredViaValueAttribute {
308
+
309
+ static TestBean testBean = new TestBean (ConfigWithBeanWithAliasesConfiguredViaValueAttribute .class .getSimpleName ());
262
310
263
- @ Bean (name ={ "name1 " , "alias1" , "alias2" , "alias3" })
311
+ @ Bean ({ "enigma " , "alias1" , "alias2" , "alias3" })
264
312
public TestBean methodName () {
265
313
return testBean ;
266
314
}
@@ -270,9 +318,9 @@ public TestBean methodName() {
270
318
@ Configuration
271
319
static class ConfigWithBeanWithProviderImplementation implements Provider <TestBean > {
272
320
273
- static TestBean testBean = new TestBean ();
321
+ static TestBean testBean = new TestBean (ConfigWithBeanWithProviderImplementation . class . getSimpleName () );
274
322
275
- @ Bean (name = "customName" )
323
+ @ Bean (name = "customName" )
276
324
public TestBean get () {
277
325
return testBean ;
278
326
}
@@ -284,7 +332,7 @@ static class ConfigWithSetWithProviderImplementation implements Provider<Set<Str
284
332
285
333
static Set <String > set = Collections .singleton ("value" );
286
334
287
- @ Bean (name = "customName" )
335
+ @ Bean (name = "customName" )
288
336
public Set <String > get () {
289
337
return set ;
290
338
}
@@ -406,7 +454,7 @@ public int getOrder() {
406
454
};
407
455
}
408
456
409
- //@Bean
457
+ // @Bean
410
458
public BeanFactoryPostProcessor beanFactoryPostProcessor () {
411
459
return new BeanFactoryPostProcessor () {
412
460
@ Override
0 commit comments