From b202472ca06829d7cda136be5c9fad2790dbf8a1 Mon Sep 17 00:00:00 2001 From: William Brockhus Date: Wed, 12 May 2021 19:45:35 +1000 Subject: [PATCH] fix: check for deprecated testmethod Instead of checking methods in a test class that have system.assert, which gives false positives, check for the presence of the testmethod modifier. --- ...tMethodShouldHaveIsTestAnnotationRule.java | 42 +++++++++---------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexUnitTestMethodShouldHaveIsTestAnnotationRule.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexUnitTestMethodShouldHaveIsTestAnnotationRule.java index 66185f9a3d..3b09f58aec 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexUnitTestMethodShouldHaveIsTestAnnotationRule.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexUnitTestMethodShouldHaveIsTestAnnotationRule.java @@ -4,6 +4,8 @@ package net.sourceforge.pmd.lang.apex.rule.bestpractices; +import static apex.jorje.semantic.symbol.type.ModifierTypeInfos.TEST_METHOD; + import java.util.HashSet; import java.util.List; import java.util.Locale; @@ -14,35 +16,29 @@ import net.sourceforge.pmd.lang.apex.ast.ASTMethodCallExpression; import net.sourceforge.pmd.lang.apex.rule.AbstractApexUnitTestRule; public class ApexUnitTestMethodShouldHaveIsTestAnnotationRule extends AbstractApexUnitTestRule { - private static final Set ASSERT_METHODS = new HashSet<>(); - - static { - ASSERT_METHODS.add("system.assert"); - ASSERT_METHODS.add("system.assertequals"); - ASSERT_METHODS.add("system.assertnotequals"); - } @Override public Object visit(final ASTMethod node, final Object data) { - // test methods should have @isTest annotation. + // test methods should have @isTest annotation not testMethod if (isTestMethodOrClass(node)) { - return data; - } - return checkForAssertStatements(node, data); - } - - private Object checkForAssertStatements(final ASTMethod testMethod, final Object data) { - List methodCallList = testMethod.findDescendantsOfType(ASTMethodCallExpression.class); - String assertMethodName; - for (ASTMethodCallExpression assertMethodCall : methodCallList) { - assertMethodName = assertMethodCall.getFullMethodName().toLowerCase(Locale.ROOT); - if (ASSERT_METHODS.contains(assertMethodName)) { - addViolationWithMessage(data, testMethod, - "''{0}'' method should have @IsTest annotation.", - new Object[] { testMethod.getImage() }); - return data; + if (hasDeprecatedTestMethodAnnotation(node)) { + return addViolation(node, data); } } return data; } + + private boolean hasDeprecatedTestMethodAnnotation(final ASTMethod method) { + return method.getNode().getModifiers().has(TEST_METHOD); + } + + private Object addViolation(final ASTMethod testMethod, final Object data) { + addViolationWithMessage( + data, + testMethod, + "''{0}'' method should have @IsTest annotation.", + new Object[] { testMethod.getImage() } + ); + return data; + } }