Skip to content

Commit 2c8dbd9

Browse files
committed
Only require test classes to have a name ending in Tests
Previously, the check would apply to interfaces and annotations as well. This commit tightens up the check so that it only applies to classes. Closes gh-364
1 parent 7037333 commit 2c8dbd9

File tree

6 files changed

+77
-7
lines changed

6 files changed

+77
-7
lines changed

Diff for: spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/check/SpringTestFileNameCheck.java

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2023 the original author or authors.
2+
* Copyright 2017-2024 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.
@@ -18,23 +18,39 @@
1818

1919
import java.io.File;
2020

21+
import com.puppycrawl.tools.checkstyle.JavaParser;
22+
import com.puppycrawl.tools.checkstyle.JavaParser.Options;
2123
import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
2224
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
25+
import com.puppycrawl.tools.checkstyle.api.DetailAST;
2326
import com.puppycrawl.tools.checkstyle.api.FileText;
27+
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
2428

2529
/**
26-
* Checks that test filenames end {@literal Tests.java} and not {@literal Test.java}.
30+
* Checks that test class filenames end {@literal Tests.java} and not {@literal Test.java}.
2731
*
2832
* @author Phillip Webb
33+
* @author Andy Wilkinson
2934
*/
3035
public class SpringTestFileNameCheck extends AbstractFileSetCheck {
3136

3237
@Override
3338
protected void processFiltered(File file, FileText fileText) throws CheckstyleException {
3439
String path = file.getPath().replace('\\', '/');
3540
if (path.contains("src/test/java") && file.getName().endsWith("Test.java")) {
36-
log(1, "testfilename.wrongName");
41+
visitCompilationUnit(JavaParser.parseFileText(fileText, Options.WITHOUT_COMMENTS));
3742
}
3843
}
39-
44+
45+
private void visitCompilationUnit(DetailAST ast) {
46+
DetailAST child = ast.getFirstChild();
47+
while (child != null) {
48+
if (child.getType() == TokenTypes.CLASS_DEF) {
49+
log(1, "testfilename.wrongName");
50+
return;
51+
}
52+
child = child.getNextSibling();
53+
}
54+
}
55+
4056
}

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2023 the original author or authors.
2+
* Copyright 2017-2024 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.
@@ -123,8 +123,10 @@ public static Collection<Parameter> paramaters() throws IOException {
123123
.map(Parameter::new)
124124
.collect(Collectors.toCollection(ArrayList::new));
125125
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")));
126+
Arrays.stream(new File(SOURCES_DIR, "src/test/java").listFiles(SpringChecksTests::sourceFile))
127+
.sorted()
128+
.map(Parameter::new)
129+
.forEach(parameters::add);
128130
return parameters;
129131
}
130132

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 @@
1+
+0 errors
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2017-2024 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 an annotation with a legal name. Only test classes must
19+
* have a name that ends with {@code Tests}.
20+
*
21+
* @author Andy Wilkinson
22+
*/
23+
public @interface AnnotationEndingInTest {
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2017-2024 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 an interface with a legal name. Only test classes must
19+
* have a name that ends with {@code Tests}.
20+
*
21+
* @author Andy Wilkinson
22+
*/
23+
public interface InterfaceEndingInTest {
24+
25+
}

0 commit comments

Comments
 (0)