View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.java.rule.junit;
5   
6   import java.util.List;
7   
8   import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
9   import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
10  
11  public class TestClassWithoutTestCasesRule extends AbstractJUnitRule {
12  
13      @Override
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.findDescendantsOfType(ASTMethodDeclaration.class);
20          boolean testsFound = false;
21  
22          if (m != null) {
23              for (ASTMethodDeclaration md : m) {
24                  if (!isInInnerClassOrInterface(md) && isJUnitMethod(md, data)) {
25                      testsFound = true;
26                  }
27              }
28          }
29  
30          if (!testsFound) {
31              addViolation(data, node);
32          }
33  
34          return data;
35      }
36  
37      private boolean isInInnerClassOrInterface(ASTMethodDeclaration md) {
38          ASTClassOrInterfaceDeclaration p = md.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
39          return p != null && p.isNested();
40      }
41  }