diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexAssertionsShouldIncludeMessageRule.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexAssertionsShouldIncludeMessageRule.java index 679935d617c..29192061216 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexAssertionsShouldIncludeMessageRule.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexAssertionsShouldIncludeMessageRule.java @@ -12,17 +12,39 @@ public class ApexAssertionsShouldIncludeMessageRule extends AbstractApexUnitTest private static final String ASSERT = "System.assert"; private static final String ASSERT_EQUALS = "System.assertEquals"; private static final String ASSERT_NOT_EQUALS = "System.assertNotEquals"; + private static final String ARE_EQUAL = "Assert.areEqual"; + private static final String ARE_NOT_EQUAL = "Assert.areNotEqual"; + private static final String IS_FALSE = "Assert.isFalse"; + private static final String FAIL = "Assert.fail"; + private static final String IS_INSTANCE_OF_TYPE = "Assert.isInstanceOfType"; + private static final String IS_NOT_INSTANCE_OF_TYPE = "Assert.isNotInstanceOfType"; + private static final String IS_NOT_NULL = "Assert.isNotNull"; + private static final String IS_NULL = "Assert.isNull"; + private static final String IS_TRUE = "Assert.isTrue"; @Override public Object visit(ASTMethodCallExpression node, Object data) { String methodName = node.getFullMethodName(); - if (ASSERT.equalsIgnoreCase(methodName) && node.getNumChildren() == 2) { + if (FAIL.equalsIgnoreCase(methodName) && node.getNumChildren() == 1) { + addViolationWithMessage(data, node, + "''{0}'' should have 1 parameters.", + new Object[] { FAIL }); + } else if ((ASSERT.equalsIgnoreCase(methodName) + || IS_FALSE.equalsIgnoreCase(methodName) + || IS_NOT_NULL.equalsIgnoreCase(methodName) + || IS_NULL.equalsIgnoreCase(methodName) + || IS_TRUE.equalsIgnoreCase(methodName)) + && node.getNumChildren() == 2) { addViolationWithMessage(data, node, "''{0}'' should have 2 parameters.", - new Object[] { ASSERT }); + new Object[] { methodName }); } else if ((ASSERT_EQUALS.equalsIgnoreCase(methodName) - || ASSERT_NOT_EQUALS.equalsIgnoreCase(methodName)) + || ASSERT_NOT_EQUALS.equalsIgnoreCase(methodName) + || ARE_EQUAL.equalsIgnoreCase(methodName) + || ARE_NOT_EQUAL.equalsIgnoreCase(methodName) + || IS_INSTANCE_OF_TYPE.equalsIgnoreCase(methodName) + || IS_NOT_INSTANCE_OF_TYPE.equalsIgnoreCase(methodName)) && node.getNumChildren() == 3) { addViolationWithMessage(data, node, "''{0}'' should have 3 parameters.", diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexUnitTestClassShouldHaveAssertsRule.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexUnitTestClassShouldHaveAssertsRule.java index 084a25daf63..a2f655d7324 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexUnitTestClassShouldHaveAssertsRule.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexUnitTestClassShouldHaveAssertsRule.java @@ -37,18 +37,39 @@ public class ApexUnitTestClassShouldHaveAssertsRule extends AbstractApexUnitTest ASSERT_METHODS.add("system.assert"); ASSERT_METHODS.add("system.assertequals"); ASSERT_METHODS.add("system.assertnotequals"); + ASSERT_METHODS.add("assert.areequal"); + ASSERT_METHODS.add("assert.arenotequal"); + ASSERT_METHODS.add("assert.fail"); + ASSERT_METHODS.add("assert.isfalse"); + ASSERT_METHODS.add("assert.isinstanceoftype"); + ASSERT_METHODS.add("assert.isnotinstanceoftype"); + ASSERT_METHODS.add("assert.isnull"); + ASSERT_METHODS.add("assert.isnotnull"); + ASSERT_METHODS.add("assert.istrue"); // Fully-qualified variants...rare but still valid/possible ASSERT_METHODS.add("system.system.assert"); ASSERT_METHODS.add("system.system.assertequals"); ASSERT_METHODS.add("system.system.assertnotequals"); + ASSERT_METHODS.add("system.assert.areequal"); + ASSERT_METHODS.add("system.assert.arenotequal"); + ASSERT_METHODS.add("system.assert.fail"); + ASSERT_METHODS.add("system.assert.isfalse"); + ASSERT_METHODS.add("system.assert.isinstanceoftype"); + ASSERT_METHODS.add("system.assert.isnotinstanceoftype"); + ASSERT_METHODS.add("system.assert.isnull"); + ASSERT_METHODS.add("system.assert.isnotnull"); + ASSERT_METHODS.add("system.assert.istrue"); } - // Using a string property instead of a regex property to ensure that the compiled pattern can be case-insensitive - private static final PropertyDescriptor ADDITIONAL_ASSERT_METHOD_PATTERN_DESCRIPTOR = - stringProperty("additionalAssertMethodPattern") - .desc("A regular expression for one or more custom test assertion method patterns.").defaultValue("").build(); + // Using a string property instead of a regex property to ensure that the + // compiled pattern can be case-insensitive + private static final PropertyDescriptor ADDITIONAL_ASSERT_METHOD_PATTERN_DESCRIPTOR = stringProperty( + "additionalAssertMethodPattern") + .desc("A regular expression for one or more custom test assertion method patterns.").defaultValue("") + .build(); - // A simple compiled pattern cache to ensure that we only ever try to compile the configured pattern once for a given run + // A simple compiled pattern cache to ensure that we only ever try to compile + // the configured pattern once for a given run private Optional compiledAdditionalAssertMethodPattern = null; public ApexUnitTestClassShouldHaveAssertsRule() { @@ -81,7 +102,8 @@ private Object checkForAssertStatements(ApexNode node, Object data) { } } - // If we didn't find assert method invocations the simple way and we have a configured pattern, try it + // If we didn't find assert method invocations the simple way and we have a + // configured pattern, try it if (!isAssertFound) { final String additionalAssertMethodPattern = getProperty(ADDITIONAL_ASSERT_METHOD_PATTERN_DESCRIPTOR); final Pattern compiledPattern = getCompiledAdditionalAssertMethodPattern(additionalAssertMethodPattern); @@ -105,12 +127,15 @@ private Object checkForAssertStatements(ApexNode node, Object data) { private Pattern getCompiledAdditionalAssertMethodPattern(String additionalAssertMethodPattern) { if (StringUtils.isNotBlank(additionalAssertMethodPattern)) { - // Check for presence first since we will cache a null value for patterns that don't compile + // Check for presence first since we will cache a null value for patterns that + // don't compile if (compiledAdditionalAssertMethodPattern == null) { try { - compiledAdditionalAssertMethodPattern = Optional.of(Pattern.compile(additionalAssertMethodPattern, Pattern.CASE_INSENSITIVE)); + compiledAdditionalAssertMethodPattern = Optional + .of(Pattern.compile(additionalAssertMethodPattern, Pattern.CASE_INSENSITIVE)); } catch (IllegalArgumentException e) { - // Cache a null compiled pattern so that we won't try to compile this one again during the run + // Cache a null compiled pattern so that we won't try to compile this one again + // during the run compiledAdditionalAssertMethodPattern = Optional.ofNullable(null); throw e; } diff --git a/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/bestpractices/xml/ApexAssertionsShouldIncludeMessage.xml b/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/bestpractices/xml/ApexAssertionsShouldIncludeMessage.xml index 71790b39db4..f05e9124dab 100644 --- a/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/bestpractices/xml/ApexAssertionsShouldIncludeMessage.xml +++ b/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/bestpractices/xml/ApexAssertionsShouldIncludeMessage.xml @@ -49,4 +49,71 @@ public class Foo { } ]]> + + + [apex] Support new Assert class (Apex v56.0) #4097 + 0 + + diff --git a/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/bestpractices/xml/ApexUnitTestClassShouldHaveAsserts.xml b/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/bestpractices/xml/ApexUnitTestClassShouldHaveAsserts.xml index 8e9520f15d4..28ba29690b3 100644 --- a/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/bestpractices/xml/ApexUnitTestClassShouldHaveAsserts.xml +++ b/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/bestpractices/xml/ApexUnitTestClassShouldHaveAsserts.xml @@ -103,7 +103,7 @@ public class Foo { @isTest public class Foo { public static testMethod void testAssertIsTrue() { - Assert.isTrue(someCondition); + MyAssert.isTrue(someCondition); } public static testMethod void testLocalVerify() { @@ -115,4 +115,71 @@ public class Foo { } ]]> + + + [apex] ApexUnitTestClassShouldHaveAssertsRule: Support new Assert class (Apex v56.0) #4097 + 0 + +