01: /**
02: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03: */package net.sourceforge.pmd.rules.junit;
04:
05: import java.util.Iterator;
06: import java.util.List;
07:
08: import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
09: import net.sourceforge.pmd.ast.ASTMethodDeclaration;
10:
11: public class TestClassWithoutTestCases extends AbstractJUnitRule {
12:
13: public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
14: if (node.isAbstract() || node.isInterface() || node.isNested()) {
15: return data;
16: }
17:
18: List<ASTMethodDeclaration> m = node
19: .findChildrenOfType(ASTMethodDeclaration.class);
20: boolean testsFound = false;
21:
22: if (m != null) {
23: for (Iterator<ASTMethodDeclaration> it = m.iterator(); it
24: .hasNext()
25: && !testsFound;) {
26: ASTMethodDeclaration md = it.next();
27: if (!isInInnerClassOrInterface(md)
28: && isJUnitMethod(md, data))
29: testsFound = true;
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
42: .getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
43: return p != null && p.isNested();
44: }
45: }
|