Skip to content

Commit 1a05ba3

Browse files
committed
Polishing
1 parent 3bda2b7 commit 1a05ba3

File tree

2 files changed

+87
-124
lines changed

2 files changed

+87
-124
lines changed

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

Lines changed: 57 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -74,74 +74,59 @@
7474
* @since 3.1
7575
*/
7676
@SuppressWarnings("resource")
77-
public class ComponentScanAnnotationIntegrationTests {
77+
class ComponentScanAnnotationIntegrationTests {
7878

7979
@Test
80-
public void controlScan() {
80+
void controlScan() {
8181
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
8282
ctx.scan(example.scannable.PackageMarker.class.getPackage().getName());
8383
ctx.refresh();
84-
assertThat(ctx.containsBean("fooServiceImpl")).as(
85-
"control scan for example.scannable package failed to register FooServiceImpl bean").isTrue();
84+
assertContextContainsBean(ctx, "fooServiceImpl");
8685
}
8786

8887
@Test
89-
public void viaContextRegistration() {
90-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
91-
ctx.register(ComponentScanAnnotatedConfig.class);
92-
ctx.refresh();
88+
void viaContextRegistration() {
89+
AnnotationConfigApplicationContext ctx =
90+
new AnnotationConfigApplicationContext(ComponentScanAnnotatedConfig.class);
9391
ctx.getBean(ComponentScanAnnotatedConfig.class);
9492
ctx.getBean(TestBean.class);
95-
assertThat(ctx.containsBeanDefinition("componentScanAnnotatedConfig")).as("config class bean not found")
96-
.isTrue();
93+
assertThat(ctx.containsBeanDefinition("componentScanAnnotatedConfig")).as("config class bean not found").isTrue();
9794
assertThat(ctx.containsBean("fooServiceImpl")).as("@ComponentScan annotated @Configuration class registered directly against " +
98-
"AnnotationConfigApplicationContext did not trigger component scanning as expected")
99-
.isTrue();
95+
"AnnotationConfigApplicationContext did not trigger component scanning as expected").isTrue();
10096
}
10197

10298
@Test
103-
public void viaContextRegistration_WithValueAttribute() {
104-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
105-
ctx.register(ComponentScanAnnotatedConfig_WithValueAttribute.class);
106-
ctx.refresh();
99+
void viaContextRegistration_WithValueAttribute() {
100+
AnnotationConfigApplicationContext ctx =
101+
new AnnotationConfigApplicationContext(ComponentScanAnnotatedConfig_WithValueAttribute.class);
107102
ctx.getBean(ComponentScanAnnotatedConfig_WithValueAttribute.class);
108103
ctx.getBean(TestBean.class);
109-
assertThat(ctx.containsBeanDefinition("componentScanAnnotatedConfig_WithValueAttribute")).as("config class bean not found")
110-
.isTrue();
104+
assertThat(ctx.containsBeanDefinition("componentScanAnnotatedConfig_WithValueAttribute")).as("config class bean not found").isTrue();
111105
assertThat(ctx.containsBean("fooServiceImpl")).as("@ComponentScan annotated @Configuration class registered directly against " +
112-
"AnnotationConfigApplicationContext did not trigger component scanning as expected")
113-
.isTrue();
106+
"AnnotationConfigApplicationContext did not trigger component scanning as expected").isTrue();
114107
}
115108

116109
@Test
117-
public void viaContextRegistration_FromPackageOfConfigClass() {
118-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
119-
ctx.register(ComponentScanAnnotatedConfigWithImplicitBasePackage.class);
120-
ctx.refresh();
110+
void viaContextRegistration_FromPackageOfConfigClass() {
111+
AnnotationConfigApplicationContext ctx =
112+
new AnnotationConfigApplicationContext(ComponentScanAnnotatedConfigWithImplicitBasePackage.class);
121113
ctx.getBean(ComponentScanAnnotatedConfigWithImplicitBasePackage.class);
122-
assertThat(ctx.containsBeanDefinition("componentScanAnnotatedConfigWithImplicitBasePackage")).as("config class bean not found")
123-
.isTrue();
114+
assertThat(ctx.containsBeanDefinition("componentScanAnnotatedConfigWithImplicitBasePackage")).as("config class bean not found").isTrue();
124115
assertThat(ctx.containsBean("scannedComponent")).as("@ComponentScan annotated @Configuration class registered directly against " +
125-
"AnnotationConfigApplicationContext did not trigger component scanning as expected")
126-
.isTrue();
127-
assertThat(ctx.getBean(ConfigurableComponent.class).isFlag()).as("@Bean method overrides scanned class")
128-
.isTrue();
116+
"AnnotationConfigApplicationContext did not trigger component scanning as expected").isTrue();
117+
assertThat(ctx.getBean(ConfigurableComponent.class).isFlag()).as("@Bean method overrides scanned class").isTrue();
129118
}
130119

131120
@Test
132-
public void viaContextRegistration_WithComposedAnnotation() {
133-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
134-
ctx.register(ComposedAnnotationConfig.class);
135-
ctx.refresh();
121+
void viaContextRegistration_WithComposedAnnotation() {
122+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ComposedAnnotationConfig.class);
136123
ctx.getBean(ComposedAnnotationConfig.class);
137124
ctx.getBean(SimpleComponent.class);
138125
ctx.getBean(ClassWithNestedComponents.NestedComponent.class);
139126
ctx.getBean(ClassWithNestedComponents.OtherNestedComponent.class);
140-
assertThat(ctx.containsBeanDefinition("componentScanAnnotationIntegrationTests.ComposedAnnotationConfig")).as("config class bean not found")
141-
.isTrue();
127+
assertThat(ctx.containsBeanDefinition("componentScanAnnotationIntegrationTests.ComposedAnnotationConfig")).as("config class bean not found").isTrue();
142128
assertThat(ctx.containsBean("simpleComponent")).as("@ComponentScan annotated @Configuration class registered directly against " +
143-
"AnnotationConfigApplicationContext did not trigger component scanning as expected")
144-
.isTrue();
129+
"AnnotationConfigApplicationContext did not trigger component scanning as expected").isTrue();
145130
}
146131

147132
@Test
@@ -154,7 +139,7 @@ void multipleComposedComponentScanAnnotations() { // gh-30941
154139
}
155140

156141
@Test
157-
public void viaBeanRegistration() {
142+
void viaBeanRegistration() {
158143
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
159144
bf.registerBeanDefinition("componentScanAnnotatedConfig",
160145
genericBeanDefinition(ComponentScanAnnotatedConfig.class).getBeanDefinition());
@@ -164,54 +149,50 @@ public void viaBeanRegistration() {
164149
ctx.refresh();
165150
ctx.getBean(ComponentScanAnnotatedConfig.class);
166151
ctx.getBean(TestBean.class);
167-
assertThat(ctx.containsBeanDefinition("componentScanAnnotatedConfig")).as("config class bean not found")
168-
.isTrue();
152+
assertThat(ctx.containsBeanDefinition("componentScanAnnotatedConfig")).as("config class bean not found").isTrue();
169153
assertThat(ctx.containsBean("fooServiceImpl")).as("@ComponentScan annotated @Configuration class registered as bean " +
170-
"definition did not trigger component scanning as expected")
171-
.isTrue();
154+
"definition did not trigger component scanning as expected").isTrue();
172155
}
173156

174157
@Test
175-
public void withCustomBeanNameGenerator() {
176-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
177-
ctx.register(ComponentScanWithBeanNameGenerator.class);
178-
ctx.refresh();
179-
assertThat(ctx.containsBean("custom_fooServiceImpl")).isTrue();
180-
assertThat(ctx.containsBean("fooServiceImpl")).isFalse();
158+
void withCustomBeanNameGenerator() {
159+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanWithBeanNameGenerator.class);
160+
assertContextContainsBean(ctx, "custom_fooServiceImpl");
161+
assertContextDoesNotContainBean(ctx, "fooServiceImpl");
181162
}
182163

183164
@Test
184-
public void withScopeResolver() {
165+
void withScopeResolver() {
185166
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanWithScopeResolver.class);
186167
// custom scope annotation makes the bean prototype scoped. subsequent calls
187168
// to getBean should return distinct instances.
188169
assertThat(ctx.getBean(CustomScopeAnnotationBean.class)).isNotSameAs(ctx.getBean(CustomScopeAnnotationBean.class));
189-
assertThat(ctx.containsBean("scannedComponent")).isFalse();
170+
assertContextDoesNotContainBean(ctx, "scannedComponent");
190171
}
191172

192173
@Test
193-
public void multiComponentScan() {
174+
void multiComponentScan() {
194175
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MultiComponentScan.class);
195176
assertThat(ctx.getBean(CustomScopeAnnotationBean.class)).isNotSameAs(ctx.getBean(CustomScopeAnnotationBean.class));
196-
assertThat(ctx.containsBean("scannedComponent")).isTrue();
177+
assertContextContainsBean(ctx, "scannedComponent");
197178
}
198179

199180
@Test
200-
public void withCustomTypeFilter() {
181+
void withCustomTypeFilter() {
201182
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanWithCustomTypeFilter.class);
202183
assertThat(ctx.getDefaultListableBeanFactory().containsSingleton("componentScanParserTests.KustomAnnotationAutowiredBean")).isFalse();
203184
KustomAnnotationAutowiredBean testBean = ctx.getBean("componentScanParserTests.KustomAnnotationAutowiredBean", KustomAnnotationAutowiredBean.class);
204185
assertThat(testBean.getDependency()).isNotNull();
205186
}
206187

207188
@Test
208-
public void withAwareTypeFilter() {
189+
void withAwareTypeFilter() {
209190
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanWithAwareTypeFilter.class);
210191
assertThat(ctx.getEnvironment().matchesProfiles("the-filter-ran")).isTrue();
211192
}
212193

213194
@Test
214-
public void withScopedProxy() throws IOException, ClassNotFoundException {
195+
void withScopedProxy() throws Exception {
215196
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
216197
ctx.register(ComponentScanWithScopedProxy.class);
217198
ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope());
@@ -228,7 +209,7 @@ public void withScopedProxy() throws IOException, ClassNotFoundException {
228209
}
229210

230211
@Test
231-
public void withScopedProxyThroughRegex() throws IOException, ClassNotFoundException {
212+
void withScopedProxyThroughRegex() throws IOException, ClassNotFoundException {
232213
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
233214
ctx.register(ComponentScanWithScopedProxyThroughRegex.class);
234215
ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope());
@@ -240,7 +221,7 @@ public void withScopedProxyThroughRegex() throws IOException, ClassNotFoundExcep
240221
}
241222

242223
@Test
243-
public void withScopedProxyThroughAspectJPattern() throws IOException, ClassNotFoundException {
224+
void withScopedProxyThroughAspectJPattern() throws IOException, ClassNotFoundException {
244225
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
245226
ctx.register(ComponentScanWithScopedProxyThroughAspectJPattern.class);
246227
ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope());
@@ -252,41 +233,41 @@ public void withScopedProxyThroughAspectJPattern() throws IOException, ClassNotF
252233
}
253234

254235
@Test
255-
public void withMultipleAnnotationIncludeFilters1() throws IOException, ClassNotFoundException {
256-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
257-
ctx.register(ComponentScanWithMultipleAnnotationIncludeFilters1.class);
258-
ctx.refresh();
236+
void withMultipleAnnotationIncludeFilters1() throws IOException, ClassNotFoundException {
237+
AnnotationConfigApplicationContext ctx =
238+
new AnnotationConfigApplicationContext(ComponentScanWithMultipleAnnotationIncludeFilters1.class);
259239
ctx.getBean(DefaultNamedComponent.class); // @CustomStereotype-annotated
260240
ctx.getBean(MessageBean.class); // @CustomComponent-annotated
261241
}
262242

263243
@Test
264-
public void withMultipleAnnotationIncludeFilters2() throws IOException, ClassNotFoundException {
265-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
266-
ctx.register(ComponentScanWithMultipleAnnotationIncludeFilters2.class);
267-
ctx.refresh();
244+
void withMultipleAnnotationIncludeFilters2() throws IOException, ClassNotFoundException {
245+
AnnotationConfigApplicationContext ctx =
246+
new AnnotationConfigApplicationContext(ComponentScanWithMultipleAnnotationIncludeFilters2.class);
268247
ctx.getBean(DefaultNamedComponent.class); // @CustomStereotype-annotated
269248
ctx.getBean(MessageBean.class); // @CustomComponent-annotated
270249
}
271250

272251
@Test
273-
public void withBasePackagesAndValueAlias() {
274-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
275-
ctx.register(ComponentScanWithBasePackagesAndValueAlias.class);
276-
ctx.refresh();
277-
assertThat(ctx.containsBean("fooServiceImpl")).isTrue();
252+
void withBasePackagesAndValueAlias() {
253+
AnnotationConfigApplicationContext ctx =
254+
new AnnotationConfigApplicationContext(ComponentScanWithBasePackagesAndValueAlias.class);
255+
assertContextContainsBean(ctx, "fooServiceImpl");
278256
}
279257

280258
private static void assertContextContainsBean(ApplicationContext ctx, String beanName) {
281-
assertThat(ctx.containsBean(beanName)).as("context contains bean " + beanName).isTrue();
259+
assertThat(ctx.containsBean(beanName)).as("context should contain bean " + beanName).isTrue();
260+
}
261+
private static void assertContextDoesNotContainBean(ApplicationContext ctx, String beanName) {
262+
assertThat(ctx.containsBean(beanName)).as("context should not contain bean " + beanName).isFalse();
282263
}
283264

284265

285266
@Configuration
286267
@ComponentScan
287268
@Retention(RetentionPolicy.RUNTIME)
288269
@Target(ElementType.TYPE)
289-
public @interface ComposedConfiguration {
270+
@interface ComposedConfiguration {
290271

291272
@AliasFor(annotation = ComponentScan.class)
292273
String[] basePackages() default {};
@@ -296,22 +277,22 @@ private static void assertContextContainsBean(ApplicationContext ctx, String bea
296277
@ComponentScan
297278
@Retention(RetentionPolicy.RUNTIME)
298279
@Target(ElementType.TYPE)
299-
public @interface ComposedConfiguration2 {
280+
@interface ComposedConfiguration2 {
300281

301282
@AliasFor(annotation = ComponentScan.class)
302283
String[] basePackages() default {};
303284
}
304285

305286
@ComposedConfiguration(basePackages = "org.springframework.context.annotation.componentscan.simple")
306-
public static class ComposedAnnotationConfig {
287+
static class ComposedAnnotationConfig {
307288
}
308289

309290
@ComposedConfiguration(basePackages = "org.springframework.context.annotation.componentscan.simple")
310291
@ComposedConfiguration2(basePackages = "example.scannable.sub")
311292
static class MultipleComposedAnnotationsConfig {
312293
}
313294

314-
public static class AwareTypeFilter implements TypeFilter, EnvironmentAware,
295+
static class AwareTypeFilter implements TypeFilter, EnvironmentAware,
315296
ResourceLoaderAware, BeanClassLoaderAware, BeanFactoryAware {
316297

317298
private BeanFactory beanFactory;

0 commit comments

Comments
 (0)