View Javadoc

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