01 /**
02 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03 */
04 package net.sourceforge.pmd.lang.java.rule.junit;
05
06 import java.util.Iterator;
07 import java.util.List;
08
09 import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
10 import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
11
12 public class TestClassWithoutTestCasesRule extends AbstractJUnitRule {
13
14 @Override
15 public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
16 if (node.isAbstract() || node.isInterface() || node.isNested()) {
17 return data;
18 }
19
20 List<ASTMethodDeclaration> m = node.findDescendantsOfType(ASTMethodDeclaration.class);
21 boolean testsFound = false;
22
23 if (m != null) {
24 for (Iterator<ASTMethodDeclaration> it = m.iterator(); it.hasNext() && !testsFound;) {
25 ASTMethodDeclaration md = it.next();
26 if (!isInInnerClassOrInterface(md)
27 && isJUnitMethod(md, data)) {
28 testsFound = true;
29 }
30 }
31 }
32
33 if (!testsFound) {
34 addViolation(data, node);
35 }
36
37 return data;
38 }
39
40 private boolean isInInnerClassOrInterface(ASTMethodDeclaration md) {
41 ASTClassOrInterfaceDeclaration p = md.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
42 return p != null && p.isNested();
43 }
44 }
|