22
22
import jakarta .annotation .PreDestroy ;
23
23
import jakarta .annotation .Resource ;
24
24
import jakarta .ejb .EJB ;
25
+ import org .junit .jupiter .api .BeforeEach ;
25
26
import org .junit .jupiter .api .Test ;
26
27
27
28
import org .springframework .beans .BeansException ;
46
47
import static org .assertj .core .api .Assertions .assertThat ;
47
48
48
49
/**
50
+ * Tests for {@link CommonAnnotationBeanPostProcessor} and
51
+ * {@link InitDestroyAnnotationBeanPostProcessor}.
52
+ *
49
53
* @author Juergen Hoeller
50
54
* @author Chris Beams
51
55
*/
52
56
class CommonAnnotationBeanPostProcessorTests {
53
57
58
+ DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
59
+
60
+ CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor ();
61
+
62
+ @ BeforeEach
63
+ void setup () {
64
+ bpp .setResourceFactory (bf );
65
+ bf .addBeanPostProcessor (bpp );
66
+ }
67
+
54
68
@ Test
55
- void testPostConstructAndPreDestroy () {
56
- DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
57
- bf .addBeanPostProcessor (new CommonAnnotationBeanPostProcessor ());
69
+ void postConstructAndPreDestroy () {
58
70
bf .registerBeanDefinition ("annotatedBean" , new RootBeanDefinition (AnnotatedInitDestroyBean .class ));
59
71
60
72
AnnotatedInitDestroyBean bean = (AnnotatedInitDestroyBean ) bf .getBean ("annotatedBean" );
@@ -64,10 +76,9 @@ void testPostConstructAndPreDestroy() {
64
76
}
65
77
66
78
@ Test
67
- void testPostConstructAndPreDestroyWithPostProcessor () {
68
- DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
79
+ void postConstructAndPreDestroyWithPostProcessor () {
69
80
bf .addBeanPostProcessor (new InitDestroyBeanPostProcessor ());
70
- bf .addBeanPostProcessor (new CommonAnnotationBeanPostProcessor () );
81
+ bf .addBeanPostProcessor (bpp );
71
82
bf .registerBeanDefinition ("annotatedBean" , new RootBeanDefinition (AnnotatedInitDestroyBean .class ));
72
83
73
84
AnnotatedInitDestroyBean bean = (AnnotatedInitDestroyBean ) bf .getBean ("annotatedBean" );
@@ -77,7 +88,7 @@ void testPostConstructAndPreDestroyWithPostProcessor() {
77
88
}
78
89
79
90
@ Test
80
- void testPostConstructAndPreDestroyWithApplicationContextAndPostProcessor () {
91
+ void postConstructAndPreDestroyWithApplicationContextAndPostProcessor () {
81
92
GenericApplicationContext ctx = new GenericApplicationContext ();
82
93
ctx .registerBeanDefinition ("bpp1" , new RootBeanDefinition (InitDestroyBeanPostProcessor .class ));
83
94
ctx .registerBeanDefinition ("bpp2" , new RootBeanDefinition (CommonAnnotationBeanPostProcessor .class ));
@@ -91,9 +102,7 @@ void testPostConstructAndPreDestroyWithApplicationContextAndPostProcessor() {
91
102
}
92
103
93
104
@ Test
94
- void testPostConstructAndPreDestroyWithLegacyAnnotations () {
95
- DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
96
- bf .addBeanPostProcessor (new CommonAnnotationBeanPostProcessor ());
105
+ void postConstructAndPreDestroyWithLegacyAnnotations () {
97
106
bf .registerBeanDefinition ("annotatedBean" , new RootBeanDefinition (LegacyAnnotatedInitDestroyBean .class ));
98
107
99
108
LegacyAnnotatedInitDestroyBean bean = (LegacyAnnotatedInitDestroyBean ) bf .getBean ("annotatedBean" );
@@ -103,12 +112,10 @@ void testPostConstructAndPreDestroyWithLegacyAnnotations() {
103
112
}
104
113
105
114
@ Test
106
- void testPostConstructAndPreDestroyWithManualConfiguration () {
107
- DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
115
+ void postConstructAndPreDestroyWithManualConfiguration () {
108
116
InitDestroyAnnotationBeanPostProcessor bpp = new InitDestroyAnnotationBeanPostProcessor ();
109
117
bpp .setInitAnnotationType (PostConstruct .class );
110
118
bpp .setDestroyAnnotationType (PreDestroy .class );
111
- bf .addBeanPostProcessor (bpp );
112
119
bf .registerBeanDefinition ("annotatedBean" , new RootBeanDefinition (AnnotatedInitDestroyBean .class ));
113
120
114
121
AnnotatedInitDestroyBean bean = (AnnotatedInitDestroyBean ) bf .getBean ("annotatedBean" );
@@ -118,9 +125,7 @@ void testPostConstructAndPreDestroyWithManualConfiguration() {
118
125
}
119
126
120
127
@ Test
121
- void testPostProcessorWithNullBean () {
122
- DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
123
- bf .addBeanPostProcessor (new CommonAnnotationBeanPostProcessor ());
128
+ void postProcessorWithNullBean () {
124
129
RootBeanDefinition rbd = new RootBeanDefinition (NullFactory .class );
125
130
rbd .setFactoryMethodName ("create" );
126
131
bf .registerBeanDefinition ("bean" , rbd );
@@ -130,8 +135,7 @@ void testPostProcessorWithNullBean() {
130
135
}
131
136
132
137
@ Test
133
- void testSerialization () throws Exception {
134
- CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor ();
138
+ void serialization () throws Exception {
135
139
CommonAnnotationBeanPostProcessor bpp2 = SerializationTestUtils .serializeAndDeserialize (bpp );
136
140
137
141
AnnotatedInitDestroyBean bean = new AnnotatedInitDestroyBean ();
@@ -140,7 +144,7 @@ void testSerialization() throws Exception {
140
144
}
141
145
142
146
@ Test
143
- void testSerializationWithManualConfiguration () throws Exception {
147
+ void serializationWithManualConfiguration () throws Exception {
144
148
InitDestroyAnnotationBeanPostProcessor bpp = new InitDestroyAnnotationBeanPostProcessor ();
145
149
bpp .setInitAnnotationType (PostConstruct .class );
146
150
bpp .setDestroyAnnotationType (PreDestroy .class );
@@ -152,11 +156,7 @@ void testSerializationWithManualConfiguration() throws Exception {
152
156
}
153
157
154
158
@ Test
155
- void testResourceInjection () {
156
- DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
157
- CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor ();
158
- bpp .setResourceFactory (bf );
159
- bf .addBeanPostProcessor (bpp );
159
+ void resourceInjection () {
160
160
bf .registerBeanDefinition ("annotatedBean" , new RootBeanDefinition (ResourceInjectionBean .class ));
161
161
TestBean tb = new TestBean ();
162
162
bf .registerSingleton ("testBean" , tb );
@@ -176,11 +176,7 @@ void testResourceInjection() {
176
176
}
177
177
178
178
@ Test
179
- void testResourceInjectionWithPrototypes () {
180
- DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
181
- CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor ();
182
- bpp .setResourceFactory (bf );
183
- bf .addBeanPostProcessor (bpp );
179
+ void resourceInjectionWithPrototypes () {
184
180
RootBeanDefinition abd = new RootBeanDefinition (ResourceInjectionBean .class );
185
181
abd .setScope (BeanDefinition .SCOPE_PROTOTYPE );
186
182
bf .registerBeanDefinition ("annotatedBean" , abd );
@@ -213,11 +209,7 @@ void testResourceInjectionWithPrototypes() {
213
209
}
214
210
215
211
@ Test
216
- void testResourceInjectionWithLegacyAnnotations () {
217
- DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
218
- CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor ();
219
- bpp .setResourceFactory (bf );
220
- bf .addBeanPostProcessor (bpp );
212
+ void resourceInjectionWithLegacyAnnotations () {
221
213
bf .registerBeanDefinition ("annotatedBean" , new RootBeanDefinition (LegacyResourceInjectionBean .class ));
222
214
TestBean tb = new TestBean ();
223
215
bf .registerSingleton ("testBean" , tb );
@@ -237,9 +229,7 @@ void testResourceInjectionWithLegacyAnnotations() {
237
229
}
238
230
239
231
@ Test
240
- void testResourceInjectionWithResolvableDependencyType () {
241
- DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
242
- CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor ();
232
+ void resourceInjectionWithResolvableDependencyType () {
243
233
bpp .setBeanFactory (bf );
244
234
bf .addBeanPostProcessor (bpp );
245
235
RootBeanDefinition abd = new RootBeanDefinition (ExtendedResourceInjectionBean .class );
@@ -273,11 +263,7 @@ void testResourceInjectionWithResolvableDependencyType() {
273
263
}
274
264
275
265
@ Test
276
- void testResourceInjectionWithDefaultMethod () {
277
- DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
278
- CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor ();
279
- bpp .setBeanFactory (bf );
280
- bf .addBeanPostProcessor (bpp );
266
+ void resourceInjectionWithDefaultMethod () {
281
267
bf .registerBeanDefinition ("annotatedBean" , new RootBeanDefinition (DefaultMethodResourceInjectionBean .class ));
282
268
TestBean tb2 = new TestBean ();
283
269
bf .registerSingleton ("testBean2" , tb2 );
@@ -293,11 +279,7 @@ void testResourceInjectionWithDefaultMethod() {
293
279
}
294
280
295
281
@ Test
296
- void testResourceInjectionWithTwoProcessors () {
297
- DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
298
- CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor ();
299
- bpp .setResourceFactory (bf );
300
- bf .addBeanPostProcessor (bpp );
282
+ void resourceInjectionWithTwoProcessors () {
301
283
CommonAnnotationBeanPostProcessor bpp2 = new CommonAnnotationBeanPostProcessor ();
302
284
bpp2 .setResourceFactory (bf );
303
285
bf .addBeanPostProcessor (bpp2 );
@@ -318,9 +300,7 @@ void testResourceInjectionWithTwoProcessors() {
318
300
}
319
301
320
302
@ Test
321
- void testResourceInjectionFromJndi () {
322
- DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
323
- CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor ();
303
+ void resourceInjectionFromJndi () {
324
304
SimpleJndiBeanFactory resourceFactory = new SimpleJndiBeanFactory ();
325
305
ExpectedLookupTemplate jndiTemplate = new ExpectedLookupTemplate ();
326
306
TestBean tb = new TestBean ();
@@ -329,7 +309,6 @@ void testResourceInjectionFromJndi() {
329
309
jndiTemplate .addObject ("java:comp/env/testBean2" , tb2 );
330
310
resourceFactory .setJndiTemplate (jndiTemplate );
331
311
bpp .setResourceFactory (resourceFactory );
332
- bf .addBeanPostProcessor (bpp );
333
312
bf .registerBeanDefinition ("annotatedBean" , new RootBeanDefinition (ResourceInjectionBean .class ));
334
313
335
314
ResourceInjectionBean bean = (ResourceInjectionBean ) bf .getBean ("annotatedBean" );
@@ -343,9 +322,7 @@ void testResourceInjectionFromJndi() {
343
322
}
344
323
345
324
@ Test
346
- void testExtendedResourceInjection () {
347
- DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
348
- CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor ();
325
+ void extendedResourceInjection () {
349
326
bpp .setBeanFactory (bf );
350
327
bf .addBeanPostProcessor (bpp );
351
328
bf .registerResolvableDependency (BeanFactory .class , bf );
@@ -396,9 +373,7 @@ void testExtendedResourceInjection() {
396
373
}
397
374
398
375
@ Test
399
- void testExtendedResourceInjectionWithOverriding () {
400
- DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
401
- CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor ();
376
+ void extendedResourceInjectionWithOverriding () {
402
377
bpp .setBeanFactory (bf );
403
378
bf .addBeanPostProcessor (bpp );
404
379
bf .registerResolvableDependency (BeanFactory .class , bf );
@@ -453,9 +428,7 @@ void testExtendedResourceInjectionWithOverriding() {
453
428
}
454
429
455
430
@ Test
456
- void testExtendedEjbInjection () {
457
- DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
458
- CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor ();
431
+ void extendedEjbInjection () {
459
432
bpp .setBeanFactory (bf );
460
433
bf .addBeanPostProcessor (bpp );
461
434
bf .registerResolvableDependency (BeanFactory .class , bf );
@@ -490,12 +463,7 @@ void testExtendedEjbInjection() {
490
463
}
491
464
492
465
@ Test
493
- void testLazyResolutionWithResourceField () {
494
- DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
495
- CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor ();
496
- bpp .setBeanFactory (bf );
497
- bf .addBeanPostProcessor (bpp );
498
-
466
+ void lazyResolutionWithResourceField () {
499
467
bf .registerBeanDefinition ("annotatedBean" , new RootBeanDefinition (LazyResourceFieldInjectionBean .class ));
500
468
bf .registerBeanDefinition ("testBean" , new RootBeanDefinition (TestBean .class ));
501
469
@@ -508,12 +476,7 @@ void testLazyResolutionWithResourceField() {
508
476
}
509
477
510
478
@ Test
511
- void testLazyResolutionWithResourceMethod () {
512
- DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
513
- CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor ();
514
- bpp .setBeanFactory (bf );
515
- bf .addBeanPostProcessor (bpp );
516
-
479
+ void lazyResolutionWithResourceMethod () {
517
480
bf .registerBeanDefinition ("annotatedBean" , new RootBeanDefinition (LazyResourceMethodInjectionBean .class ));
518
481
bf .registerBeanDefinition ("testBean" , new RootBeanDefinition (TestBean .class ));
519
482
@@ -526,12 +489,7 @@ void testLazyResolutionWithResourceMethod() {
526
489
}
527
490
528
491
@ Test
529
- void testLazyResolutionWithCglibProxy () {
530
- DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
531
- CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor ();
532
- bpp .setBeanFactory (bf );
533
- bf .addBeanPostProcessor (bpp );
534
-
492
+ void lazyResolutionWithCglibProxy () {
535
493
bf .registerBeanDefinition ("annotatedBean" , new RootBeanDefinition (LazyResourceCglibInjectionBean .class ));
536
494
bf .registerBeanDefinition ("testBean" , new RootBeanDefinition (TestBean .class ));
537
495
@@ -544,10 +502,8 @@ void testLazyResolutionWithCglibProxy() {
544
502
}
545
503
546
504
@ Test
547
- void testLazyResolutionWithFallbackTypeMatch () {
548
- DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
505
+ void lazyResolutionWithFallbackTypeMatch () {
549
506
bf .setAutowireCandidateResolver (new ContextAnnotationAutowireCandidateResolver ());
550
- CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor ();
551
507
bpp .setBeanFactory (bf );
552
508
bf .addBeanPostProcessor (bpp );
553
509
0 commit comments