Clover coverage report - PMD - 3.9 Coverage timestamp: Tue Dec 19 2006 09:38:44 EST FRAMES NO FRAMES |
file stats: | LOC: | 83 |  | Methods: | 4
| NCLOC: | 70 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
JUnitTestsShouldContainAsserts.java | 80% | 96.8% | 100% | 89.2% |

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