Skip to content

Commit ae5e3f2

Browse files
committed
Add checkstyle rule to make sure test class names end in "Tests"
Closes gh-222
1 parent 745b0f2 commit ae5e3f2

File tree

9 files changed

+104
-5
lines changed

9 files changed

+104
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2017-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.spring.javaformat.checkstyle.check;
18+
19+
import java.io.File;
20+
21+
import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
22+
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
23+
import com.puppycrawl.tools.checkstyle.api.FileText;
24+
25+
/**
26+
* Checks that test filenames end {@literal Tests.java} and not {@literal Test.java}.
27+
*
28+
* @author Phillip Webb
29+
*/
30+
public class SpringTestFileNameCheck extends AbstractFileSetCheck {
31+
32+
@Override
33+
protected void processFiltered(File file, FileText fileText) throws CheckstyleException {
34+
String path = file.getPath().replace('\\', '/');
35+
if (path.contains("src/test/java") && file.getName().endsWith("Test.java")) {
36+
log(1, "testfilename.wrongName");
37+
}
38+
}
39+
40+
}

Diff for: spring-javaformat/spring-javaformat-checkstyle/src/main/resources/io/spring/javaformat/checkstyle/check/messages.properties

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ methodvisibility.publicMethod=Method ''{0}'' in private class should not be publ
2626
nothis.unexpected=Reference to instance variable ''{0}'' should not use \"this.\".
2727
ternary.equalOperator=Ternary operation should use != when testing.
2828
ternary.missingParen=Ternary operation missing parentheses. Use the form \"(a != b) ? y : n\"
29+
testfilename.wrongName=Test classes should have a name ending with 'Tests.java'.
2930
leadingwhitespace.incorrect=Indentation should be performed with {0} only.
3031
deprecated.missingSince=@Deprecated has no since attribute.
3132
deprecated.emptySince=@Deprecated has an empty since attribute.

Diff for: spring-javaformat/spring-javaformat-checkstyle/src/main/resources/io/spring/javaformat/checkstyle/spring-checkstyle.xml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<property name="headerFile" value="${headerFile}" default="" />
1212
<property name="headerCopyrightPattern" value="${headerCopyrightPattern}" />
1313
</module>
14+
<module name="io.spring.javaformat.checkstyle.check.SpringTestFileNameCheck" />
1415
<module name="com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck" />
1516
<module name="com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck" />
1617

Diff for: spring-javaformat/spring-javaformat-checkstyle/src/test/java/io/spring/javaformat/checkstyle/SpringChecksTests.java

+7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.IOException;
2222
import java.io.InputStream;
2323
import java.nio.file.Files;
24+
import java.nio.file.Path;
2425
import java.util.ArrayList;
2526
import java.util.Arrays;
2627
import java.util.Collection;
@@ -41,6 +42,7 @@
4142
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
4243
import com.puppycrawl.tools.checkstyle.api.Configuration;
4344
import com.puppycrawl.tools.checkstyle.api.RootModule;
45+
import org.junit.jupiter.api.io.TempDir;
4446
import org.junit.jupiter.params.ParameterizedTest;
4547
import org.junit.jupiter.params.provider.MethodSource;
4648
import org.xml.sax.InputSource;
@@ -62,6 +64,9 @@ public class SpringChecksTests {
6264

6365
private static final File DEFAULT_CONFIG = new File(CONFIGS_DIR, "default-checkstyle-configuration.xml");
6466

67+
@TempDir
68+
public Path temp;
69+
6570
@ParameterizedTest
6671
@MethodSource("paramaters")
6772
public void processHasExpectedResults(Parameter parameter) throws Exception {
@@ -118,6 +123,8 @@ public static Collection<Parameter> paramaters() throws IOException {
118123
.map(Parameter::new)
119124
.collect(Collectors.toCollection(ArrayList::new));
120125
parameters.add(new Parameter(new File(SOURCES_DIR, "nopackageinfo/NoPackageInfo.java")));
126+
parameters.add(new Parameter(new File(SOURCES_DIR, "src/test/java/NamedTest.java")));
127+
parameters.add(new Parameter(new File(SOURCES_DIR, "src/test/java/NamedTests.java")));
121128
return parameters;
122129
}
123130

Diff for: spring-javaformat/spring-javaformat-checkstyle/src/test/java/io/spring/javaformat/checkstyle/SpringConfigurationLoaderTests.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public class SpringConfigurationLoaderTests {
4545
@Test
4646
public void loadShouldLoadChecks() {
4747
Collection<FileSetCheck> checks = load(null);
48-
assertThat(checks).hasSize(4);
49-
TreeWalker treeWalker = (TreeWalker) checks.toArray()[3];
48+
assertThat(checks).hasSize(5);
49+
TreeWalker treeWalker = (TreeWalker) checks.toArray()[4];
5050
Set<?> ordinaryChecks = (Set<?>) Extractors.byName("ordinaryChecks").extract(treeWalker);
5151
assertThat(ordinaryChecks).hasSize(60);
5252
}
@@ -56,8 +56,8 @@ public void loadWithExcludeShouldExcludeChecks() {
5656
Set<String> excludes = Collections
5757
.singleton("com.puppycrawl.tools.checkstyle.checks.whitespace.MethodParamPadCheck");
5858
Collection<FileSetCheck> checks = load(excludes);
59-
assertThat(checks).hasSize(4);
60-
TreeWalker treeWalker = (TreeWalker) checks.toArray()[3];
59+
assertThat(checks).hasSize(5);
60+
TreeWalker treeWalker = (TreeWalker) checks.toArray()[4];
6161
Set<?> ordinaryChecks = (Set<?>) Extractors.byName("ordinaryChecks").extract(treeWalker);
6262
assertThat(ordinaryChecks).hasSize(59);
6363
}
@@ -66,7 +66,7 @@ public void loadWithExcludeShouldExcludeChecks() {
6666
public void loadWithExcludeHeaderShouldExcludeChecks() {
6767
Set<String> excludes = Collections.singleton("io.spring.javaformat.checkstyle.check.SpringHeaderCheck");
6868
Object[] checks = load(excludes).stream().toArray();
69-
assertThat(checks).hasSize(3);
69+
assertThat(checks).hasSize(4);
7070
}
7171

7272
private Collection<FileSetCheck> load(Set<String> excludes) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+Test classes should have a name ending with Tests.java
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+0 errors
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2017-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* This is a test with the wrong name.
19+
*
20+
* @author Phillip Webb
21+
*/
22+
public class NamedTest {
23+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2017-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* This is a test with the correct name.
19+
*
20+
* @author Phillip Webb
21+
*/
22+
public class NamedTests {
23+
24+
}

0 commit comments

Comments
 (0)