Clover coverage report - PMD - 3.3
Coverage timestamp: Thu Sep 15 2005 17:59:57 EDT
file stats: LOC: 78   Methods: 4
NCLOC: 65   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
JUnitTestsShouldContainAsserts.java 82.1% 100% 100% 91.7%
coverage coverage
 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 net.sourceforge.pmd.AbstractRule;
 7    import net.sourceforge.pmd.Rule;
 8    import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
 9    import net.sourceforge.pmd.ast.ASTMethodDeclaration;
 10    import net.sourceforge.pmd.ast.ASTName;
 11    import net.sourceforge.pmd.ast.ASTPrimaryExpression;
 12    import net.sourceforge.pmd.ast.ASTPrimaryPrefix;
 13    import net.sourceforge.pmd.ast.ASTResultType;
 14    import net.sourceforge.pmd.ast.ASTStatementExpression;
 15    import net.sourceforge.pmd.ast.Node;
 16   
 17    public class JUnitTestsShouldContainAsserts extends AbstractRule implements Rule {
 18   
 19  15 public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
 20  15 if (node.isInterface()) {
 21  3 return data;
 22    }
 23  12 return super.visit(node, data);
 24    }
 25   
 26  18 public Object visit(ASTMethodDeclaration method, Object data) {
 27  18 if (!method.isPublic() || method.isAbstract() || method.isNative() || method.isStatic()) {
 28  4 return data; // skip various inapplicable method variations
 29    }
 30   
 31  14 if (((ASTResultType) method.jjtGetChild(0)).isVoid() && method.getMethodName().startsWith("test")) {
 32  11 if (!containsAssert(method.getBlock(), false)) {
 33  5 addViolation(data, method);
 34    }
 35    }
 36  14 return data;
 37    }
 38   
 39  85 private boolean containsAssert(Node n, boolean assertFound) {
 40  85 if (!assertFound) {
 41  85 if (n instanceof ASTStatementExpression) {
 42  9 if (isAssertOrFailStatement((ASTStatementExpression)n)) {
 43  6 return true;
 44    }
 45    }
 46  79 if (!assertFound) {
 47  79 for (int i=0;i<n.jjtGetNumChildren() && ! assertFound;i++) {
 48  74 Node c = n.jjtGetChild(i);
 49  74 if (containsAssert(c, assertFound))
 50  22 return true;
 51    }
 52    }
 53    }
 54  57 return false;
 55    }
 56   
 57    /**
 58    * Tells if the expression is an assert statement or not.
 59    */
 60  9 private boolean isAssertOrFailStatement(ASTStatementExpression expression) {
 61  9 if (expression!=null
 62    && expression.jjtGetNumChildren()>0
 63    && expression.jjtGetChild(0) instanceof ASTPrimaryExpression
 64    ) {
 65  9 ASTPrimaryExpression pe = (ASTPrimaryExpression) expression.jjtGetChild(0);
 66  9 if (pe.jjtGetNumChildren()> 0 && pe.jjtGetChild(0) instanceof ASTPrimaryPrefix) {
 67  9 ASTPrimaryPrefix pp = (ASTPrimaryPrefix) pe.jjtGetChild(0);
 68  9 if (pp.jjtGetNumChildren()>0 && pp.jjtGetChild(0) instanceof ASTName) {
 69  9 ASTName n = (ASTName) pp.jjtGetChild(0);
 70  9 if (n.getImage()!=null && (n.getImage().startsWith("assert") || n.getImage().startsWith("fail") )) {
 71  6 return true;
 72    }
 73    }
 74    }
 75    }
 76  3 return false;
 77    }
 78    }