Skip to content

Commit 4b6126c

Browse files
committed
Polishing
1 parent a108e70 commit 4b6126c

File tree

2 files changed

+39
-81
lines changed

2 files changed

+39
-81
lines changed

spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@
8282
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
8383

8484
/**
85+
* Tests for {@link AutowiredAnnotationBeanPostProcessor}.
86+
*
8587
* @author Juergen Hoeller
8688
* @author Mark Fisher
8789
* @author Sam Brannen

spring-context/src/test/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessorTests.java

Lines changed: 37 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import jakarta.annotation.PreDestroy;
2323
import jakarta.annotation.Resource;
2424
import jakarta.ejb.EJB;
25+
import org.junit.jupiter.api.BeforeEach;
2526
import org.junit.jupiter.api.Test;
2627

2728
import org.springframework.beans.BeansException;
@@ -46,15 +47,26 @@
4647
import static org.assertj.core.api.Assertions.assertThat;
4748

4849
/**
50+
* Tests for {@link CommonAnnotationBeanPostProcessor} and
51+
* {@link InitDestroyAnnotationBeanPostProcessor}.
52+
*
4953
* @author Juergen Hoeller
5054
* @author Chris Beams
5155
*/
5256
class CommonAnnotationBeanPostProcessorTests {
5357

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+
5468
@Test
55-
void testPostConstructAndPreDestroy() {
56-
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
57-
bf.addBeanPostProcessor(new CommonAnnotationBeanPostProcessor());
69+
void postConstructAndPreDestroy() {
5870
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(AnnotatedInitDestroyBean.class));
5971

6072
AnnotatedInitDestroyBean bean = (AnnotatedInitDestroyBean) bf.getBean("annotatedBean");
@@ -64,10 +76,9 @@ void testPostConstructAndPreDestroy() {
6476
}
6577

6678
@Test
67-
void testPostConstructAndPreDestroyWithPostProcessor() {
68-
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
79+
void postConstructAndPreDestroyWithPostProcessor() {
6980
bf.addBeanPostProcessor(new InitDestroyBeanPostProcessor());
70-
bf.addBeanPostProcessor(new CommonAnnotationBeanPostProcessor());
81+
bf.addBeanPostProcessor(bpp);
7182
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(AnnotatedInitDestroyBean.class));
7283

7384
AnnotatedInitDestroyBean bean = (AnnotatedInitDestroyBean) bf.getBean("annotatedBean");
@@ -77,7 +88,7 @@ void testPostConstructAndPreDestroyWithPostProcessor() {
7788
}
7889

7990
@Test
80-
void testPostConstructAndPreDestroyWithApplicationContextAndPostProcessor() {
91+
void postConstructAndPreDestroyWithApplicationContextAndPostProcessor() {
8192
GenericApplicationContext ctx = new GenericApplicationContext();
8293
ctx.registerBeanDefinition("bpp1", new RootBeanDefinition(InitDestroyBeanPostProcessor.class));
8394
ctx.registerBeanDefinition("bpp2", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
@@ -91,9 +102,7 @@ void testPostConstructAndPreDestroyWithApplicationContextAndPostProcessor() {
91102
}
92103

93104
@Test
94-
void testPostConstructAndPreDestroyWithLegacyAnnotations() {
95-
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
96-
bf.addBeanPostProcessor(new CommonAnnotationBeanPostProcessor());
105+
void postConstructAndPreDestroyWithLegacyAnnotations() {
97106
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(LegacyAnnotatedInitDestroyBean.class));
98107

99108
LegacyAnnotatedInitDestroyBean bean = (LegacyAnnotatedInitDestroyBean) bf.getBean("annotatedBean");
@@ -103,12 +112,10 @@ void testPostConstructAndPreDestroyWithLegacyAnnotations() {
103112
}
104113

105114
@Test
106-
void testPostConstructAndPreDestroyWithManualConfiguration() {
107-
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
115+
void postConstructAndPreDestroyWithManualConfiguration() {
108116
InitDestroyAnnotationBeanPostProcessor bpp = new InitDestroyAnnotationBeanPostProcessor();
109117
bpp.setInitAnnotationType(PostConstruct.class);
110118
bpp.setDestroyAnnotationType(PreDestroy.class);
111-
bf.addBeanPostProcessor(bpp);
112119
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(AnnotatedInitDestroyBean.class));
113120

114121
AnnotatedInitDestroyBean bean = (AnnotatedInitDestroyBean) bf.getBean("annotatedBean");
@@ -118,9 +125,7 @@ void testPostConstructAndPreDestroyWithManualConfiguration() {
118125
}
119126

120127
@Test
121-
void testPostProcessorWithNullBean() {
122-
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
123-
bf.addBeanPostProcessor(new CommonAnnotationBeanPostProcessor());
128+
void postProcessorWithNullBean() {
124129
RootBeanDefinition rbd = new RootBeanDefinition(NullFactory.class);
125130
rbd.setFactoryMethodName("create");
126131
bf.registerBeanDefinition("bean", rbd);
@@ -130,8 +135,7 @@ void testPostProcessorWithNullBean() {
130135
}
131136

132137
@Test
133-
void testSerialization() throws Exception {
134-
CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
138+
void serialization() throws Exception {
135139
CommonAnnotationBeanPostProcessor bpp2 = SerializationTestUtils.serializeAndDeserialize(bpp);
136140

137141
AnnotatedInitDestroyBean bean = new AnnotatedInitDestroyBean();
@@ -140,7 +144,7 @@ void testSerialization() throws Exception {
140144
}
141145

142146
@Test
143-
void testSerializationWithManualConfiguration() throws Exception {
147+
void serializationWithManualConfiguration() throws Exception {
144148
InitDestroyAnnotationBeanPostProcessor bpp = new InitDestroyAnnotationBeanPostProcessor();
145149
bpp.setInitAnnotationType(PostConstruct.class);
146150
bpp.setDestroyAnnotationType(PreDestroy.class);
@@ -152,11 +156,7 @@ void testSerializationWithManualConfiguration() throws Exception {
152156
}
153157

154158
@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() {
160160
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ResourceInjectionBean.class));
161161
TestBean tb = new TestBean();
162162
bf.registerSingleton("testBean", tb);
@@ -176,11 +176,7 @@ void testResourceInjection() {
176176
}
177177

178178
@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() {
184180
RootBeanDefinition abd = new RootBeanDefinition(ResourceInjectionBean.class);
185181
abd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
186182
bf.registerBeanDefinition("annotatedBean", abd);
@@ -213,11 +209,7 @@ void testResourceInjectionWithPrototypes() {
213209
}
214210

215211
@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() {
221213
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(LegacyResourceInjectionBean.class));
222214
TestBean tb = new TestBean();
223215
bf.registerSingleton("testBean", tb);
@@ -237,9 +229,7 @@ void testResourceInjectionWithLegacyAnnotations() {
237229
}
238230

239231
@Test
240-
void testResourceInjectionWithResolvableDependencyType() {
241-
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
242-
CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
232+
void resourceInjectionWithResolvableDependencyType() {
243233
bpp.setBeanFactory(bf);
244234
bf.addBeanPostProcessor(bpp);
245235
RootBeanDefinition abd = new RootBeanDefinition(ExtendedResourceInjectionBean.class);
@@ -273,11 +263,7 @@ void testResourceInjectionWithResolvableDependencyType() {
273263
}
274264

275265
@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() {
281267
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(DefaultMethodResourceInjectionBean.class));
282268
TestBean tb2 = new TestBean();
283269
bf.registerSingleton("testBean2", tb2);
@@ -293,11 +279,7 @@ void testResourceInjectionWithDefaultMethod() {
293279
}
294280

295281
@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() {
301283
CommonAnnotationBeanPostProcessor bpp2 = new CommonAnnotationBeanPostProcessor();
302284
bpp2.setResourceFactory(bf);
303285
bf.addBeanPostProcessor(bpp2);
@@ -318,9 +300,7 @@ void testResourceInjectionWithTwoProcessors() {
318300
}
319301

320302
@Test
321-
void testResourceInjectionFromJndi() {
322-
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
323-
CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
303+
void resourceInjectionFromJndi() {
324304
SimpleJndiBeanFactory resourceFactory = new SimpleJndiBeanFactory();
325305
ExpectedLookupTemplate jndiTemplate = new ExpectedLookupTemplate();
326306
TestBean tb = new TestBean();
@@ -329,7 +309,6 @@ void testResourceInjectionFromJndi() {
329309
jndiTemplate.addObject("java:comp/env/testBean2", tb2);
330310
resourceFactory.setJndiTemplate(jndiTemplate);
331311
bpp.setResourceFactory(resourceFactory);
332-
bf.addBeanPostProcessor(bpp);
333312
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ResourceInjectionBean.class));
334313

335314
ResourceInjectionBean bean = (ResourceInjectionBean) bf.getBean("annotatedBean");
@@ -343,9 +322,7 @@ void testResourceInjectionFromJndi() {
343322
}
344323

345324
@Test
346-
void testExtendedResourceInjection() {
347-
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
348-
CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
325+
void extendedResourceInjection() {
349326
bpp.setBeanFactory(bf);
350327
bf.addBeanPostProcessor(bpp);
351328
bf.registerResolvableDependency(BeanFactory.class, bf);
@@ -396,9 +373,7 @@ void testExtendedResourceInjection() {
396373
}
397374

398375
@Test
399-
void testExtendedResourceInjectionWithOverriding() {
400-
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
401-
CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
376+
void extendedResourceInjectionWithOverriding() {
402377
bpp.setBeanFactory(bf);
403378
bf.addBeanPostProcessor(bpp);
404379
bf.registerResolvableDependency(BeanFactory.class, bf);
@@ -453,9 +428,7 @@ void testExtendedResourceInjectionWithOverriding() {
453428
}
454429

455430
@Test
456-
void testExtendedEjbInjection() {
457-
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
458-
CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
431+
void extendedEjbInjection() {
459432
bpp.setBeanFactory(bf);
460433
bf.addBeanPostProcessor(bpp);
461434
bf.registerResolvableDependency(BeanFactory.class, bf);
@@ -490,12 +463,7 @@ void testExtendedEjbInjection() {
490463
}
491464

492465
@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() {
499467
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(LazyResourceFieldInjectionBean.class));
500468
bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class));
501469

@@ -508,12 +476,7 @@ void testLazyResolutionWithResourceField() {
508476
}
509477

510478
@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() {
517480
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(LazyResourceMethodInjectionBean.class));
518481
bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class));
519482

@@ -526,12 +489,7 @@ void testLazyResolutionWithResourceMethod() {
526489
}
527490

528491
@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() {
535493
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(LazyResourceCglibInjectionBean.class));
536494
bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class));
537495

@@ -544,10 +502,8 @@ void testLazyResolutionWithCglibProxy() {
544502
}
545503

546504
@Test
547-
void testLazyResolutionWithFallbackTypeMatch() {
548-
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
505+
void lazyResolutionWithFallbackTypeMatch() {
549506
bf.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
550-
CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
551507
bpp.setBeanFactory(bf);
552508
bf.addBeanPostProcessor(bpp);
553509

0 commit comments

Comments
 (0)