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,6 +1,7 @@
/** /**
* 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;
@ -12,7 +13,6 @@ import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTName; import net.sourceforge.pmd.lang.java.ast.ASTName;
import net.sourceforge.pmd.lang.java.ast.ASTNormalAnnotation; import net.sourceforge.pmd.lang.java.ast.ASTNormalAnnotation;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression; import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
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 {
@ -61,8 +61,8 @@ public class JUnitTestsShouldIncludeAssertRule extends AbstractJUnitRule {
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())) {
@ -78,21 +78,17 @@ public class JUnitTestsShouldIncludeAssertRule extends AbstractJUnitRule {
* 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();
if (img != null
&& (img.startsWith("assert") || img.startsWith("fail") || img.startsWith("Assert.assert") || img
.startsWith("Assert.fail"))) {
return true; return true;
} }
} }
} }
}
return false; return false;
} }
} }