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.Iterator;
7   import java.util.List;
8   
9   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  }