Cleaned up one method

This commit is contained in:
Clément Fournier
2017-03-23 18:34:39 +01:00
committed by Juan Martín Sotuyo Dodero
parent f644aa5744
commit 38d3f4f67f

View File

@ -1,98 +1,94 @@
/** /**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/ */
package net.sourceforge.pmd.lang.java.rule.junit;
package net.sourceforge.pmd.lang.java.rule.junit;
import java.util.List;
import java.util.List;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTMemberValuePair; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTMemberValuePair;
import net.sourceforge.pmd.lang.java.ast.ASTName; import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTNormalAnnotation; import net.sourceforge.pmd.lang.java.ast.ASTName;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression; import net.sourceforge.pmd.lang.java.ast.ASTNormalAnnotation;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix; import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
import net.sourceforge.pmd.lang.java.ast.ASTStatementExpression; import net.sourceforge.pmd.lang.java.ast.ASTStatementExpression;
public class JUnitTestsShouldIncludeAssertRule extends AbstractJUnitRule { public class JUnitTestsShouldIncludeAssertRule extends AbstractJUnitRule {
@Override @Override
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) { public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
if (node.isInterface()) { if (node.isInterface()) {
return data; return data;
} }
return super.visit(node, data); return super.visit(node, data);
} }
@Override @Override
public Object visit(ASTMethodDeclaration method, Object data) { public Object visit(ASTMethodDeclaration method, Object data) {
if (isJUnitMethod(method, data)) { if (isJUnitMethod(method, data)) {
if (!containsAssert(method.getBlock(), false) && !containsExpect(method.jjtGetParent())) { if (!containsAssert(method.getBlock(), false) && !containsExpect(method.jjtGetParent())) {
addViolation(data, method); addViolation(data, method);
} }
} }
return data; return data;
} }
private boolean containsAssert(Node n, boolean assertFound) { private boolean containsAssert(Node n, boolean assertFound) {
if (!assertFound) { if (!assertFound) {
if (n instanceof ASTStatementExpression) { if (n instanceof ASTStatementExpression) {
if (isAssertOrFailStatement((ASTStatementExpression) n)) { if (isAssertOrFailStatement((ASTStatementExpression) n)) {
return true; return true;
} }
} }
if (!assertFound) { if (!assertFound) {
for (int i = 0; i < n.jjtGetNumChildren() && !assertFound; i++) { for (int i = 0; i < n.jjtGetNumChildren() && !assertFound; i++) {
Node c = n.jjtGetChild(i); Node c = n.jjtGetChild(i);
if (containsAssert(c, assertFound)) { if (containsAssert(c, assertFound)) {
return true; return true;
} }
} }
} }
} }
return false; return false;
} }
/** /**
* Tells if the node contains a Test annotation with an expected exception. * Tells if the node contains a Test annotation with an expected exception.
*/ */
private boolean containsExpect(Node methodParent) { private boolean containsExpect(Node methodParent) {
List<ASTNormalAnnotation> annotations = methodParent.findDescendantsOfType(ASTNormalAnnotation.class); List<ASTNormalAnnotation> annotations = methodParent.findDescendantsOfType(ASTNormalAnnotation.class);
for (ASTNormalAnnotation annotation : annotations) { for (ASTNormalAnnotation annotation : annotations) {
ASTName name = annotation.getFirstChildOfType(ASTName.class); ASTName name = annotation.getFirstChildOfType(ASTName.class);
if (name != null if (name != null && ("Test".equals(name.getImage())
&& ("Test".equals(name.getImage()) || name.getType() != null && name.getType().equals(JUNIT4_CLASS))) { || name.getType() != null && name.getType().equals(JUNIT4_CLASS))) {
List<ASTMemberValuePair> memberValues = annotation.findDescendantsOfType(ASTMemberValuePair.class); List<ASTMemberValuePair> memberValues = annotation.findDescendantsOfType(ASTMemberValuePair.class);
for (ASTMemberValuePair pair : memberValues) { for (ASTMemberValuePair pair : memberValues) {
if ("expected".equals(pair.getImage())) { if ("expected".equals(pair.getImage())) {
return true; return true;
} }
} }
} }
} }
return false; return false;
} }
/** /**
* Tells if the expression is an assert statement or not. * Tells if the expression is an assert statement or not.
*/ */
private boolean isAssertOrFailStatement(ASTStatementExpression expression) { private boolean isAssertOrFailStatement(ASTStatementExpression expression) {
if (expression != null && expression.jjtGetNumChildren() > 0 if (expression != null) {
&& expression.jjtGetChild(0) instanceof ASTPrimaryExpression) { ASTPrimaryExpression pe = expression.getFirstChildOfType(ASTPrimaryExpression.class);
ASTPrimaryExpression pe = (ASTPrimaryExpression) expression.jjtGetChild(0); if (pe != null) {
if (pe.jjtGetNumChildren() > 0 && pe.jjtGetChild(0) instanceof ASTPrimaryPrefix) { String img = pe.jjtGetChild(0).jjtGetChild(0).getImage();
ASTPrimaryPrefix pp = (ASTPrimaryPrefix) pe.jjtGetChild(0); if (img != null && (img.startsWith("assert") || img.startsWith("fail")
if (pp.jjtGetNumChildren() > 0 && pp.jjtGetChild(0) instanceof ASTName) { || img.startsWith("Assert.assert") || img.startsWith("Assert.fail"))) {
String img = ((ASTName) pp.jjtGetChild(0)).getImage(); return true;
if (img != null }
&& (img.startsWith("assert") || img.startsWith("fail") || img.startsWith("Assert.assert") || img }
.startsWith("Assert.fail"))) { }
return true; return false;
} }
} }
}
}
return false;
}
}