Skip to content

Commit 093323b

Browse files
committed
Introduce Checkstyle rule to prohibit class names ending with "Test"
1 parent c18d1ce commit 093323b

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

spring-context/src/test/java/example/scannable/_package.java renamed to spring-context/src/test/java/example/scannable/PackageMarker.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,10 +16,10 @@
1616

1717
package example.scannable;
1818

19-
2019
/**
21-
* Marker class for example.scannable package.
20+
* Marker class for {@code example.scannable} package.
2221
*
2322
* @see org.springframework.context.annotation.ComponentScan#basePackageClasses()
2423
*/
25-
public class _package { }
24+
public class PackageMarker {
25+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public class ComponentScanAnnotationIntegrationTests {
7878
@Test
7979
public void controlScan() {
8080
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
81-
ctx.scan(example.scannable._package.class.getPackage().getName());
81+
ctx.scan(example.scannable.PackageMarker.class.getPackage().getName());
8282
ctx.refresh();
8383
assertThat(ctx.containsBean("fooServiceImpl")).as(
8484
"control scan for example.scannable package failed to register FooServiceImpl bean").isTrue();
@@ -326,7 +326,7 @@ public boolean match(MetadataReader metadataReader, MetadataReaderFactory metada
326326

327327

328328
@Configuration
329-
@ComponentScan(basePackageClasses = example.scannable._package.class)
329+
@ComponentScan(basePackageClasses = example.scannable.PackageMarker.class)
330330
class ComponentScanAnnotatedConfig {
331331

332332
@Bean
@@ -456,7 +456,7 @@ class ComponentScanWithMultipleAnnotationIncludeFilters2 {}
456456
@ComponentScan(
457457
value = "example.scannable",
458458
basePackages = "example.scannable",
459-
basePackageClasses = example.scannable._package.class)
459+
basePackageClasses = example.scannable.PackageMarker.class)
460460
class ComponentScanWithBasePackagesAndValueAlias {}
461461

462462

src/checkstyle/checkstyle.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@
5151
<module name="com.puppycrawl.tools.checkstyle.checks.design.InnerTypeLastCheck" />
5252
<module name="com.puppycrawl.tools.checkstyle.checks.design.OneTopLevelClassCheck" />
5353

54+
<!-- Type Names -->
55+
<module name="TypeName">
56+
<property name="format" value="^[A-Z][a-zA-Z0-9_]*(?&lt;!Test)$" />
57+
<property name="tokens" value="CLASS_DEF" />
58+
<message key="name.invalidPattern"
59+
value="Class name ''{0}'' must not end with ''Test'' (checked pattern ''{1}'')." />
60+
</module>
61+
5462
<!-- Coding -->
5563
<module name="com.puppycrawl.tools.checkstyle.checks.coding.CovariantEqualsCheck" />
5664
<module name="com.puppycrawl.tools.checkstyle.checks.coding.EmptyStatementCheck" />

0 commit comments

Comments
 (0)