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