|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| package net.sourceforge.pmd.rules.junit; |
|
5 |
| |
|
6 |
| import net.sourceforge.pmd.AbstractRule; |
|
7 |
| import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration; |
|
8 |
| import net.sourceforge.pmd.ast.ASTMethodDeclarator; |
|
9 |
| |
|
10 |
| import java.util.Iterator; |
|
11 |
| import java.util.List; |
|
12 |
| |
|
13 |
| public class TestClassWithoutTestCases extends AbstractRule { |
|
14 |
| |
|
15 |
6
| public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
|
|
16 |
6
| if (node.isAbstract() || node.isInterface() || node.isNested()) {
|
|
17 |
2
| return data;
|
|
18 |
| } |
|
19 |
| |
|
20 |
4
| String className = node.getImage();
|
|
21 |
4
| if (className.endsWith("Test")) {
|
|
22 |
3
| List m = node.findChildrenOfType(ASTMethodDeclarator.class);
|
|
23 |
3
| boolean testsFound = false;
|
|
24 |
3
| if (m != null) {
|
|
25 |
3
| for (Iterator it = m.iterator(); it.hasNext() && !testsFound;) {
|
|
26 |
2
| ASTMethodDeclarator md = (ASTMethodDeclarator) it.next();
|
|
27 |
2
| if (!isInInnerClassOrInterface(md)
|
|
28 |
| && md.getImage().startsWith("test")) { |
|
29 |
1
| testsFound = true;
|
|
30 |
| } |
|
31 |
| } |
|
32 |
| } |
|
33 |
| |
|
34 |
3
| if (!testsFound) {
|
|
35 |
2
| addViolation(data, node);
|
|
36 |
| } |
|
37 |
| |
|
38 |
| } |
|
39 |
4
| return data;
|
|
40 |
| } |
|
41 |
| |
|
42 |
2
| private boolean isInInnerClassOrInterface(ASTMethodDeclarator md) {
|
|
43 |
2
| ASTClassOrInterfaceDeclaration p = (ASTClassOrInterfaceDeclaration) md.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
|
|
44 |
2
| return p != null && p.isNested();
|
|
45 |
| } |
|
46 |
| } |