View Javadoc
1 package net.sourceforge.pmd.rules.junit; 2 3 import net.sourceforge.pmd.AbstractRule; 4 import net.sourceforge.pmd.Rule; 5 import net.sourceforge.pmd.RuleContext; 6 import net.sourceforge.pmd.ast.ASTArguments; 7 import net.sourceforge.pmd.ast.ASTName; 8 import net.sourceforge.pmd.ast.ASTPrimaryExpression; 9 import net.sourceforge.pmd.ast.ASTPrimaryPrefix; 10 11 import java.util.ArrayList; 12 import java.util.Iterator; 13 import java.util.List; 14 15 public class JUnitAssertionsShouldIncludeMessageRule extends AbstractRule implements Rule { 16 17 private static class AssertionCall { 18 public int args; 19 public String name; 20 21 public AssertionCall(int args, String name) { 22 this.args = args; 23 this.name = name; 24 } 25 } 26 27 private List checks = new ArrayList(); 28 29 public JUnitAssertionsShouldIncludeMessageRule() { 30 checks.add(new AssertionCall(2, "assertEquals")); 31 checks.add(new AssertionCall(1, "assertTrue")); 32 checks.add(new AssertionCall(1, "assertNull")); 33 checks.add(new AssertionCall(2, "assertSame")); 34 checks.add(new AssertionCall(1, "assertNotNull")); 35 } 36 37 public Object visit(ASTArguments node, Object data) { 38 for (Iterator i = checks.iterator(); i.hasNext();) { 39 AssertionCall call = (AssertionCall) i.next(); 40 check((RuleContext) data, node, call.args, call.name); 41 } 42 return super.visit(node, data); 43 } 44 45 private void check(RuleContext ctx, ASTArguments node, int args, String targetMethodName) { 46 if (node.getArgumentCount() == args && node.jjtGetParent().jjtGetParent() instanceof ASTPrimaryExpression) { 47 ASTPrimaryExpression primary = (ASTPrimaryExpression) node.jjtGetParent().jjtGetParent(); 48 if (primary.jjtGetChild(0) instanceof ASTPrimaryPrefix && primary.jjtGetChild(0).jjtGetNumChildren() > 0 && primary.jjtGetChild(0).jjtGetChild(0) instanceof ASTName) { 49 ASTName name = (ASTName) primary.jjtGetChild(0).jjtGetChild(0); 50 if (name.getImage().equals(targetMethodName)) { 51 ctx.getReport().addRuleViolation(createRuleViolation(ctx, name.getBeginLine())); 52 } 53 } 54 } 55 } 56 }

This page was automatically generated by Maven