|
1 | 1 | /*
|
2 |
| - * Copyright 2017-2023 the original author or authors. |
| 2 | + * Copyright 2017-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
18 | 18 |
|
19 | 19 | import java.io.File;
|
20 | 20 |
|
| 21 | +import com.puppycrawl.tools.checkstyle.JavaParser; |
| 22 | +import com.puppycrawl.tools.checkstyle.JavaParser.Options; |
21 | 23 | import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
|
22 | 24 | import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
|
| 25 | +import com.puppycrawl.tools.checkstyle.api.DetailAST; |
23 | 26 | import com.puppycrawl.tools.checkstyle.api.FileText;
|
| 27 | +import com.puppycrawl.tools.checkstyle.api.TokenTypes; |
24 | 28 |
|
25 | 29 | /**
|
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}. |
27 | 31 | *
|
28 | 32 | * @author Phillip Webb
|
| 33 | + * @author Andy Wilkinson |
29 | 34 | */
|
30 | 35 | public class SpringTestFileNameCheck extends AbstractFileSetCheck {
|
31 | 36 |
|
32 | 37 | @Override
|
33 | 38 | protected void processFiltered(File file, FileText fileText) throws CheckstyleException {
|
34 | 39 | String path = file.getPath().replace('\\', '/');
|
35 | 40 | if (path.contains("src/test/java") && file.getName().endsWith("Test.java")) {
|
36 |
| - log(1, "testfilename.wrongName"); |
| 41 | + visitCompilationUnit(JavaParser.parseFileText(fileText, Options.WITHOUT_COMMENTS)); |
37 | 42 | }
|
38 | 43 | }
|
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 | + |
40 | 56 | }
|
0 commit comments