27
27
import org .springframework .beans .BeanWrapper ;
28
28
import org .springframework .beans .factory .FactoryBean ;
29
29
import org .springframework .beans .factory .NoUniqueBeanDefinitionException ;
30
- import org .springframework .beans .factory .annotation .Qualifier ;
31
30
import org .springframework .beans .factory .config .BeanDefinition ;
32
31
import org .springframework .beans .factory .config .BeanFactoryPostProcessor ;
33
32
import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
@@ -72,7 +71,7 @@ void beanNameWithFactoryBeanPrefixIsRejected() {
72
71
73
72
@ Test
74
73
void replaceBeanByNameWithMatchingBeanDefinition () {
75
- AnnotationConfigApplicationContext context = createContext (CaseByName .class );
74
+ AnnotationConfigApplicationContext context = createContext (ByNameTestCase .class );
76
75
context .registerBean ("descriptionBean" , String .class , () -> "Original" );
77
76
context .refresh ();
78
77
@@ -81,7 +80,7 @@ void replaceBeanByNameWithMatchingBeanDefinition() {
81
80
82
81
@ Test
83
82
void replaceBeanByNameWithoutMatchingBeanDefinitionFails () {
84
- AnnotationConfigApplicationContext context = createContext (CaseByName .class );
83
+ AnnotationConfigApplicationContext context = createContext (ByNameTestCase .class );
85
84
86
85
assertThatIllegalStateException ()
87
86
.isThrownBy (context ::refresh )
@@ -91,7 +90,7 @@ void replaceBeanByNameWithoutMatchingBeanDefinitionFails() {
91
90
92
91
@ Test
93
92
void replaceBeanByNameWithMatchingBeanDefinitionAndWrongTypeFails () {
94
- AnnotationConfigApplicationContext context = createContext (CaseByName .class );
93
+ AnnotationConfigApplicationContext context = createContext (ByNameTestCase .class );
95
94
context .registerBean ("descriptionBean" , Integer .class , () -> -1 );
96
95
97
96
assertThatIllegalStateException ()
@@ -117,7 +116,7 @@ void replaceBeanByNameCanOverrideBeanProducedByFactoryBeanWithResolvableTypeObje
117
116
}
118
117
119
118
private AnnotationConfigApplicationContext prepareContextWithFactoryBean (Object objectTypeAttribute ) {
120
- AnnotationConfigApplicationContext context = createContext (CaseOverrideBeanProducedByFactoryBean .class );
119
+ AnnotationConfigApplicationContext context = createContext (OverrideBeanProducedByFactoryBeanTestCase .class );
121
120
// Register a TestFactoryBean that will not be overridden
122
121
context .registerBean ("testFactoryBean" , TestFactoryBean .class , TestFactoryBean ::new );
123
122
// Register another TestFactoryBean that will be overridden
@@ -129,7 +128,7 @@ private AnnotationConfigApplicationContext prepareContextWithFactoryBean(Object
129
128
130
129
@ Test
131
130
void replaceBeanByTypeWithSingleMatchingBean () {
132
- AnnotationConfigApplicationContext context = createContext (CaseByType .class );
131
+ AnnotationConfigApplicationContext context = createContext (ByTypeTestCase .class );
133
132
context .registerBean ("someInteger" , Integer .class , () -> 1 );
134
133
context .refresh ();
135
134
@@ -138,30 +137,30 @@ void replaceBeanByTypeWithSingleMatchingBean() {
138
137
139
138
@ Test
140
139
void replaceBeanByTypeWithoutMatchingBeanFails () {
141
- AnnotationConfigApplicationContext context = createContext (CaseByType .class );
140
+ AnnotationConfigApplicationContext context = createContext (ByTypeTestCase .class );
142
141
143
142
assertThatIllegalStateException ()
144
143
.isThrownBy (context ::refresh )
145
144
.withMessage ("Unable to override bean: no beans of type java.lang.Integer " +
146
- "(as required by annotated field 'CaseByType .counter')" );
145
+ "(as required by annotated field 'ByTypeTestCase .counter')" );
147
146
}
148
147
149
148
@ Test
150
- void replaceBeanByTypeWithMultipleMatchesAndNoQualifierFails () {
151
- AnnotationConfigApplicationContext context = createContext (CaseByType .class );
149
+ void replaceBeanByTypeWithMultipleCandidatesAndNoQualifierFails () {
150
+ AnnotationConfigApplicationContext context = createContext (ByTypeTestCase .class );
152
151
context .registerBean ("someInteger" , Integer .class , () -> 1 );
153
152
context .registerBean ("anotherInteger" , Integer .class , () -> 2 );
154
153
155
154
assertThatIllegalStateException ()
156
155
.isThrownBy (context ::refresh )
157
156
.withMessage ("Unable to select a bean to override: found 2 beans " +
158
- "of type java.lang.Integer (as required by annotated field 'CaseByType .counter'): " +
157
+ "of type java.lang.Integer (as required by annotated field 'ByTypeTestCase .counter'): " +
159
158
"[someInteger, anotherInteger]" );
160
159
}
161
160
162
161
@ Test
163
- void replaceBeanByTypeWithMultipleMatchesAndFieldNameAsFallbackQualifierMatches () {
164
- AnnotationConfigApplicationContext context = createContext (CaseByType .class );
162
+ void replaceBeanByTypeWithMultipleCandidatesAndFieldNameAsFallbackQualifier () {
163
+ AnnotationConfigApplicationContext context = createContext (ByTypeTestCase .class );
165
164
context .registerBean ("counter" , Integer .class , () -> 1 );
166
165
context .registerBean ("someInteger" , Integer .class , () -> 2 );
167
166
context .refresh ();
@@ -206,7 +205,7 @@ void replaceBeanByTypeWithMultipleCandidatesAndMultiplePrimaryBeansFails() {
206
205
207
206
@ Test
208
207
void createOrReplaceBeanByNameWithMatchingBeanDefinition () {
209
- AnnotationConfigApplicationContext context = createContext (CaseByNameWithReplaceOrCreateStrategy .class );
208
+ AnnotationConfigApplicationContext context = createContext (ByNameWithReplaceOrCreateStrategyTestCase .class );
210
209
context .registerBean ("descriptionBean" , String .class , () -> "Original" );
211
210
context .refresh ();
212
211
@@ -215,15 +214,15 @@ void createOrReplaceBeanByNameWithMatchingBeanDefinition() {
215
214
216
215
@ Test
217
216
void createOrReplaceBeanByNameWithoutMatchingDefinitionCreatesBeanDefinition () {
218
- AnnotationConfigApplicationContext context = createContext (CaseByNameWithReplaceOrCreateStrategy .class );
217
+ AnnotationConfigApplicationContext context = createContext (ByNameWithReplaceOrCreateStrategyTestCase .class );
219
218
context .refresh ();
220
219
221
220
assertThat (context .getBean ("descriptionBean" )).isEqualTo ("overridden" );
222
221
}
223
222
224
223
@ Test
225
224
void createOrReplaceBeanByTypeWithMatchingBean () {
226
- AnnotationConfigApplicationContext context = createContext (CaseByTypeWithReplaceOrCreateStrategy .class );
225
+ AnnotationConfigApplicationContext context = createContext (ByTypeWithReplaceOrCreateStrategyTestCase .class );
227
226
context .registerBean ("someBean" , String .class , () -> "Original" );
228
227
context .refresh ();
229
228
@@ -232,7 +231,7 @@ void createOrReplaceBeanByTypeWithMatchingBean() {
232
231
233
232
@ Test
234
233
void createOrReplaceBeanByTypeWithoutMatchingDefinitionCreatesBeanDefinition () {
235
- AnnotationConfigApplicationContext context = createContext (CaseByTypeWithReplaceOrCreateStrategy .class );
234
+ AnnotationConfigApplicationContext context = createContext (ByTypeWithReplaceOrCreateStrategyTestCase .class );
236
235
context .refresh ();
237
236
238
237
String generatedBeanName = "java.lang.String#0" ;
@@ -242,7 +241,7 @@ void createOrReplaceBeanByTypeWithoutMatchingDefinitionCreatesBeanDefinition() {
242
241
243
242
@ Test
244
243
void postProcessorShouldNotTriggerEarlyInitialization () {
245
- AnnotationConfigApplicationContext context = createContext (CaseByTypeWithReplaceOrCreateStrategy .class );
244
+ AnnotationConfigApplicationContext context = createContext (ByTypeWithReplaceOrCreateStrategyTestCase .class );
246
245
247
246
context .register (FactoryBeanRegisteringPostProcessor .class );
248
247
context .register (EarlyBeanInitializationDetector .class );
@@ -252,7 +251,7 @@ void postProcessorShouldNotTriggerEarlyInitialization() {
252
251
253
252
@ Test
254
253
void replaceBeanByNameWithMatchingBeanDefinitionWithExplicitSingletonScope () {
255
- AnnotationConfigApplicationContext context = createContext (CaseByName .class );
254
+ AnnotationConfigApplicationContext context = createContext (ByNameTestCase .class );
256
255
RootBeanDefinition definition = new RootBeanDefinition (String .class , () -> "ORIGINAL" );
257
256
definition .setScope (BeanDefinition .SCOPE_SINGLETON );
258
257
context .registerBeanDefinition ("descriptionBean" , definition );
@@ -265,7 +264,7 @@ void replaceBeanByNameWithMatchingBeanDefinitionWithExplicitSingletonScope() {
265
264
@ Test
266
265
void replaceBeanByNameWithMatchingBeanDefinitionForClassBasedSingletonFactoryBean () {
267
266
String beanName = "descriptionBean" ;
268
- AnnotationConfigApplicationContext context = createContext (CaseByName .class );
267
+ AnnotationConfigApplicationContext context = createContext (ByNameTestCase .class );
269
268
RootBeanDefinition factoryBeanDefinition = new RootBeanDefinition (SingletonStringFactoryBean .class );
270
269
context .registerBeanDefinition (beanName , factoryBeanDefinition );
271
270
@@ -277,7 +276,7 @@ void replaceBeanByNameWithMatchingBeanDefinitionForClassBasedSingletonFactoryBea
277
276
@ Test // gh-33800
278
277
void replaceBeanByNameWithMatchingBeanDefinitionForClassBasedNonSingletonFactoryBean () {
279
278
String beanName = "descriptionBean" ;
280
- AnnotationConfigApplicationContext context = createContext (CaseByName .class );
279
+ AnnotationConfigApplicationContext context = createContext (ByNameTestCase .class );
281
280
RootBeanDefinition factoryBeanDefinition = new RootBeanDefinition (NonSingletonStringFactoryBean .class );
282
281
context .registerBeanDefinition (beanName , factoryBeanDefinition );
283
282
@@ -314,7 +313,7 @@ void replaceBeanByNameWithMatchingBeanDefinitionForInterfaceBasedNonSingletonFac
314
313
void replaceBeanByNameWithMatchingBeanDefinitionWithPrototypeScopeFails () {
315
314
String beanName = "descriptionBean" ;
316
315
317
- AnnotationConfigApplicationContext context = createContext (CaseByName .class );
316
+ AnnotationConfigApplicationContext context = createContext (ByNameTestCase .class );
318
317
RootBeanDefinition definition = new RootBeanDefinition (String .class , () -> "ORIGINAL" );
319
318
definition .setScope (BeanDefinition .SCOPE_PROTOTYPE );
320
319
context .registerBeanDefinition (beanName , definition );
@@ -329,7 +328,7 @@ void replaceBeanByNameWithMatchingBeanDefinitionWithCustomScopeFails() {
329
328
String beanName = "descriptionBean" ;
330
329
String scope = "customScope" ;
331
330
332
- AnnotationConfigApplicationContext context = createContext (CaseByName .class );
331
+ AnnotationConfigApplicationContext context = createContext (ByNameTestCase .class );
333
332
ConfigurableListableBeanFactory beanFactory = context .getBeanFactory ();
334
333
beanFactory .registerScope (scope , new SimpleThreadScope ());
335
334
RootBeanDefinition definition = new RootBeanDefinition (String .class , () -> "ORIGINAL" );
@@ -356,7 +355,7 @@ void replaceBeanByNameWithMatchingBeanDefinitionForPrototypeScopedFactoryBeanFai
356
355
357
356
@ Test
358
357
void replaceBeanByNameWithMatchingBeanDefinitionRetainsPrimaryAndFallbackFlags () {
359
- AnnotationConfigApplicationContext context = createContext (CaseByName .class );
358
+ AnnotationConfigApplicationContext context = createContext (ByNameTestCase .class );
360
359
RootBeanDefinition definition = new RootBeanDefinition (String .class , () -> "ORIGINAL" );
361
360
definition .setPrimary (true );
362
361
definition .setFallback (true );
@@ -373,7 +372,7 @@ void replaceBeanByNameWithMatchingBeanDefinitionRetainsPrimaryAndFallbackFlags()
373
372
374
373
@ Test
375
374
void qualifiedElementIsSetToBeanOverrideFieldForNonexistentBeanDefinition () {
376
- AnnotationConfigApplicationContext context = createContext (CaseByNameWithQualifier .class );
375
+ AnnotationConfigApplicationContext context = createContext (TestBeanByNameTestCase .class );
377
376
378
377
assertThatNoException ().isThrownBy (context ::refresh );
379
378
assertThat (context .getBeanDefinition ("descriptionBean" ))
@@ -384,12 +383,12 @@ void qualifiedElementIsSetToBeanOverrideFieldForNonexistentBeanDefinition() {
384
383
private void qualifiedElementIsField (RootBeanDefinition def ) {
385
384
assertThat (def .getQualifiedElement ()).isInstanceOfSatisfying (Field .class ,
386
385
field -> {
387
- assertThat (field .getDeclaringClass ()).isEqualTo (CaseByNameWithQualifier .class );
386
+ assertThat (field .getDeclaringClass ()).isEqualTo (TestBeanByNameTestCase .class );
388
387
assertThat (field .getName ()).as ("annotated field name" ).isEqualTo ("description" );
389
388
});
390
389
}
391
390
392
- private AnnotationConfigApplicationContext createContext (Class <?> testClass ) {
391
+ private static AnnotationConfigApplicationContext createContext (Class <?> testClass ) {
393
392
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ();
394
393
Set <BeanOverrideHandler > handlers = new LinkedHashSet <>(BeanOverrideHandler .forTestClass (testClass ));
395
394
new BeanOverrideContextCustomizer (handlers ).customizeContext (context , mock (MergedContextConfiguration .class ));
@@ -409,35 +408,35 @@ static class FactoryBeanPrefixTestCase {
409
408
410
409
}
411
410
412
- static class CaseByName {
411
+ static class ByNameTestCase {
413
412
414
413
@ DummyBean (beanName = "descriptionBean" )
415
414
private String description ;
416
415
417
416
}
418
417
419
- static class CaseByType {
418
+ static class ByTypeTestCase {
420
419
421
420
@ DummyBean
422
421
private Integer counter ;
423
422
424
423
}
425
424
426
- static class CaseByNameWithReplaceOrCreateStrategy {
425
+ static class ByNameWithReplaceOrCreateStrategyTestCase {
427
426
428
427
@ DummyBean (beanName = "descriptionBean" , strategy = BeanOverrideStrategy .REPLACE_OR_CREATE )
429
428
private String description ;
430
429
431
430
}
432
431
433
- static class CaseByTypeWithReplaceOrCreateStrategy {
432
+ static class ByTypeWithReplaceOrCreateStrategyTestCase {
434
433
435
434
@ DummyBean (strategy = BeanOverrideStrategy .REPLACE_OR_CREATE )
436
435
private String description ;
437
436
438
437
}
439
438
440
- static class CaseByNameAndByTypeWithReplaceOrCreateStrategy {
439
+ static class ByNameAndByTypeWithReplaceOrCreateStrategyTestCase {
441
440
442
441
@ DummyBean (beanName = "descriptionBean" , strategy = BeanOverrideStrategy .REPLACE_OR_CREATE )
443
442
private String description ;
@@ -447,18 +446,17 @@ static class CaseByNameAndByTypeWithReplaceOrCreateStrategy {
447
446
448
447
}
449
448
450
- static class CaseOverrideBeanProducedByFactoryBean {
449
+ static class OverrideBeanProducedByFactoryBeanTestCase {
451
450
452
451
@ DummyBean (beanName = "beanToBeOverridden" )
453
452
CharSequence description ;
454
453
455
454
}
456
455
457
- static class CaseByNameWithQualifier {
456
+ static class TestBeanByNameTestCase {
458
457
459
- @ Qualifier ("preferThis" )
460
458
@ TestBean (name = "descriptionBean" )
461
- private String description ;
459
+ String description ;
462
460
463
461
static String descriptionBean () {
464
462
return "overridden" ;
0 commit comments