From 237b1066760b992c449a23fdcbf28677dd4f0c68 Mon Sep 17 00:00:00 2001 From: Thomas Prouvot Date: Wed, 17 Aug 2022 15:09:54 +0200 Subject: [PATCH 01/18] Add new assert methods for api 56 --- ...ApexUnitTestClassShouldHaveAssertsRule.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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..083ea313560 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,6 +37,24 @@ public class ApexUnitTestClassShouldHaveAssertsRule extends AbstractApexUnitTest ASSERT_METHODS.add("system.assert"); ASSERT_METHODS.add("system.assertequals"); ASSERT_METHODS.add("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.isnnull"); + ASSERT_METHODS.add("system.assert.isnotnull"); + ASSERT_METHODS.add("system.assert.istrue"); + 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.isnnull"); + 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"); From 2e1d913058be17c0407203abd6f8bf5d4e47e427 Mon Sep 17 00:00:00 2001 From: Thomas Prouvot Date: Thu, 18 Aug 2022 10:27:06 +0200 Subject: [PATCH 02/18] Update @tprouvot as a contributor --- .all-contributorsrc | 3 ++- docs/pages/pmd/projectdocs/credits.md | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index 99708783e87..4b24a733c32 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -5917,7 +5917,8 @@ "avatar_url": "https://avatars.githubusercontent.com/u/35368290?v=4", "profile": "https://github.com/tprouvot", "contributions": [ - "bug" + "bug", + "code" ] }, { diff --git a/docs/pages/pmd/projectdocs/credits.md b/docs/pages/pmd/projectdocs/credits.md index 37788cf5f73..eb60d9190c0 100644 --- a/docs/pages/pmd/projectdocs/credits.md +++ b/docs/pages/pmd/projectdocs/credits.md @@ -932,7 +932,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
tobwoerk

🐛 -
tprouvot

🐛 +
tprouvot

🐛 💻
trentchilders

🐛
triandicAnt

🐛
trishul14

🐛 From 2edcfe25cdbab7c56ec0df1ed89fac7a3110d6ac Mon Sep 17 00:00:00 2001 From: Thomas Prouvot Date: Thu, 18 Aug 2022 10:28:19 +0200 Subject: [PATCH 03/18] Update @tprouvot as a contributor --- .all-contributorsrc | 1 - docs/pages/pmd/projectdocs/credits.md | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index 4b24a733c32..5fba86143ba 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -5917,7 +5917,6 @@ "avatar_url": "https://avatars.githubusercontent.com/u/35368290?v=4", "profile": "https://github.com/tprouvot", "contributions": [ - "bug", "code" ] }, diff --git a/docs/pages/pmd/projectdocs/credits.md b/docs/pages/pmd/projectdocs/credits.md index eb60d9190c0..00199aea115 100644 --- a/docs/pages/pmd/projectdocs/credits.md +++ b/docs/pages/pmd/projectdocs/credits.md @@ -932,7 +932,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
tobwoerk

🐛 -
tprouvot

🐛 💻 +
tprouvot

💻
trentchilders

🐛
triandicAnt

🐛
trishul14

🐛 From 539838c9064b377df16e2b7dced257790bc6e3c4 Mon Sep 17 00:00:00 2001 From: Thomas Prouvot Date: Thu, 18 Aug 2022 14:40:50 +0200 Subject: [PATCH 04/18] Update previous negative test which is ok now and add new positive test. --- .../xml/ApexUnitTestClassShouldHaveAsserts.xml | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) 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..69d0cba08f9 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); + Assert.assertEquals(someCondition); } public static testMethod void testLocalVerify() { @@ -115,4 +115,18 @@ public class Foo { } ]]> + + + #4096 [apex] api 56.0 ApexAssertionsShouldIncludeMessage and new apex class : Assert + 0 + + From ad21a24b74f96c1e5df0fb7c5075c7f8079c18de Mon Sep 17 00:00:00 2001 From: Thomas Prouvot Date: Thu, 18 Aug 2022 14:48:25 +0200 Subject: [PATCH 05/18] Fix contributors --- .all-contributorsrc | 4 ++-- docs/pages/pmd/projectdocs/credits.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index 5fba86143ba..16274b18391 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -5917,7 +5917,7 @@ "avatar_url": "https://avatars.githubusercontent.com/u/35368290?v=4", "profile": "https://github.com/tprouvot", "contributions": [ - "code" + "bug" ] }, { @@ -6773,4 +6773,4 @@ "contributorsPerLine": 7, "contributorsSortAlphabetically": true, "skipCi": true -} +} \ No newline at end of file diff --git a/docs/pages/pmd/projectdocs/credits.md b/docs/pages/pmd/projectdocs/credits.md index 00199aea115..8a7c21410f6 100644 --- a/docs/pages/pmd/projectdocs/credits.md +++ b/docs/pages/pmd/projectdocs/credits.md @@ -932,7 +932,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
tobwoerk

🐛 -
tprouvot

💻 +
tprouvot

🐛
trentchilders

🐛
triandicAnt

🐛
trishul14

🐛 From a2058c454b551535b243e53553ee7e4370563d3d Mon Sep 17 00:00:00 2001 From: Thomas Prouvot Date: Thu, 18 Aug 2022 16:34:14 +0200 Subject: [PATCH 06/18] move fully qualified variant under comment (PR Comment) --- ...ApexUnitTestClassShouldHaveAssertsRule.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) 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 083ea313560..2e0d717b60d 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,15 +37,6 @@ public class ApexUnitTestClassShouldHaveAssertsRule extends AbstractApexUnitTest ASSERT_METHODS.add("system.assert"); ASSERT_METHODS.add("system.assertequals"); ASSERT_METHODS.add("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.isnnull"); - ASSERT_METHODS.add("system.assert.isnotnull"); - ASSERT_METHODS.add("system.assert.istrue"); ASSERT_METHODS.add("assert.areequal"); ASSERT_METHODS.add("assert.arenotequal"); ASSERT_METHODS.add("assert.fail"); @@ -59,6 +50,15 @@ public class ApexUnitTestClassShouldHaveAssertsRule extends AbstractApexUnitTest 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.isnnull"); + 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 From dc03e52be3c951a0430135fa5c29dca1a57d1a29 Mon Sep 17 00:00:00 2001 From: Thomas Prouvot Date: Thu, 18 Aug 2022 16:34:46 +0200 Subject: [PATCH 07/18] Update method name and number of parameters (PR Comment) --- .../bestpractices/xml/ApexUnitTestClassShouldHaveAsserts.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 69d0cba08f9..b78a6c86f73 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 @@ -102,8 +102,8 @@ public class Foo { Date: Thu, 18 Aug 2022 17:11:22 +0200 Subject: [PATCH 08/18] fix contributors --- .all-contributorsrc | 2 +- docs/pages/pmd/projectdocs/credits.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index 16274b18391..99708783e87 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -6773,4 +6773,4 @@ "contributorsPerLine": 7, "contributorsSortAlphabetically": true, "skipCi": true -} \ No newline at end of file +} diff --git a/docs/pages/pmd/projectdocs/credits.md b/docs/pages/pmd/projectdocs/credits.md index 8a7c21410f6..37788cf5f73 100644 --- a/docs/pages/pmd/projectdocs/credits.md +++ b/docs/pages/pmd/projectdocs/credits.md @@ -932,7 +932,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
tobwoerk

🐛 -
tprouvot

🐛 +
tprouvot

🐛
trentchilders

🐛
triandicAnt

🐛
trishul14

🐛 From e0b9965c27f8663135d4be915b850184c58927e7 Mon Sep 17 00:00:00 2001 From: tprouvot <35368290+tprouvot@users.noreply.github.com> Date: Thu, 25 Aug 2022 10:46:47 +0200 Subject: [PATCH 09/18] Update pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexUnitTestClassShouldHaveAssertsRule.java Co-authored-by: Andreas Dangel --- .../bestpractices/ApexUnitTestClassShouldHaveAssertsRule.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 2e0d717b60d..71f2ea9b997 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 @@ -43,7 +43,7 @@ public class ApexUnitTestClassShouldHaveAssertsRule extends AbstractApexUnitTest ASSERT_METHODS.add("assert.isfalse"); ASSERT_METHODS.add("assert.isinstanceoftype"); ASSERT_METHODS.add("assert.isnotinstanceoftype"); - ASSERT_METHODS.add("assert.isnnull"); + ASSERT_METHODS.add("assert.isnull"); ASSERT_METHODS.add("assert.isnotnull"); ASSERT_METHODS.add("assert.istrue"); // Fully-qualified variants...rare but still valid/possible From d5098ca115de49e450be43415d238c37f0d13987 Mon Sep 17 00:00:00 2001 From: tprouvot <35368290+tprouvot@users.noreply.github.com> Date: Thu, 25 Aug 2022 12:15:30 +0200 Subject: [PATCH 10/18] Update pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/bestpractices/xml/ApexUnitTestClassShouldHaveAsserts.xml Co-authored-by: Andreas Dangel --- .../bestpractices/xml/ApexUnitTestClassShouldHaveAsserts.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 b78a6c86f73..471822ad5a7 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 @@ -117,7 +117,7 @@ public class Foo { - #4096 [apex] api 56.0 ApexAssertionsShouldIncludeMessage and new apex class : Assert + [apex] ApexUnitTestClassShouldHaveAssertsRule: Support new Assert class (Apex v56.0) #4097 0 Date: Thu, 25 Aug 2022 12:17:32 +0200 Subject: [PATCH 11/18] Fix typo --- ...pexUnitTestClassShouldHaveAssertsRule.java | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) 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 2e0d717b60d..d45f93c3971 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 @@ -56,17 +56,20 @@ public class ApexUnitTestClassShouldHaveAssertsRule extends AbstractApexUnitTest ASSERT_METHODS.add("system.assert.isfalse"); ASSERT_METHODS.add("system.assert.isinstanceoftype"); ASSERT_METHODS.add("system.assert.isnotinstanceoftype"); - ASSERT_METHODS.add("system.assert.isnnull"); + 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() { @@ -99,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); @@ -123,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; } From 2e9f25c8ba8ca963660dd604e81fe54fa625e508 Mon Sep 17 00:00:00 2001 From: Thomas Prouvot Date: Thu, 25 Aug 2022 12:17:51 +0200 Subject: [PATCH 12/18] PR review comment --- .../bestpractices/xml/ApexUnitTestClassShouldHaveAsserts.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 b78a6c86f73..f08128a6f7b 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 @@ -102,8 +102,8 @@ public class Foo { Date: Thu, 25 Aug 2022 12:18:15 +0200 Subject: [PATCH 13/18] Add new Assert methods to ApexAssertionsShouldIncludeMessageRule --- ...pexAssertionsShouldIncludeMessageRule.java | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) 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..fa50f753342 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 }); } 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.", From d6194be47b39cb7f41b665ceb08e5b4e25d82e95 Mon Sep 17 00:00:00 2001 From: Thomas Prouvot Date: Thu, 25 Aug 2022 15:31:13 +0200 Subject: [PATCH 14/18] Add tests for new methods --- .../ApexUnitTestClassShouldHaveAsserts.xml | 50 +++++++++++++++++-- 1 file changed, 47 insertions(+), 3 deletions(-) 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 048170d3add..0ce6d81f0fb 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 @@ -122,9 +122,53 @@ public class Foo { From ac94df9e17f6778967cb3e63516e9c0cf7260e80 Mon Sep 17 00:00:00 2001 From: Thomas Prouvot Date: Thu, 25 Aug 2022 15:34:54 +0200 Subject: [PATCH 15/18] Update add violation parameters to macth with new tests --- .../bestpractices/ApexAssertionsShouldIncludeMessageRule.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 fa50f753342..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 @@ -38,7 +38,7 @@ public Object visit(ASTMethodCallExpression node, Object data) { && 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) || ARE_EQUAL.equalsIgnoreCase(methodName) From 065b4b80835c9d27d96516cabbbd3d761709a6ac Mon Sep 17 00:00:00 2001 From: Thomas Prouvot Date: Thu, 25 Aug 2022 15:44:18 +0200 Subject: [PATCH 16/18] Remove param message not mandatory in this test --- .../xml/ApexUnitTestClassShouldHaveAsserts.xml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) 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 0ce6d81f0fb..1e16256b0ac 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 @@ -124,18 +124,18 @@ public class Foo { public class Foo { public static testAreEqual void testSomething() { String sub = 'abcde'.substring(2); - Assert.areEqual('cde', sub, 'Expected characters after first two'); + Assert.areEqual('cde', sub); } public static testAreNotEqual void testSomething() { String sub = 'abcde'.substring(2); - Assert.areNotEqual('xyz', sub, 'Characters not expected after first two'); + Assert.areNotEqual('xyz', sub); } public static testFail void testSomething() { try { SomeClass.methodUnderTest(); - Assert.fail('DmlException Expected'); + Assert.fail(); } catch (DmlException ex) { // Add assertions here about the expected exception } @@ -143,7 +143,7 @@ public class Foo { public static testIsFalse void testSomething() { Boolean containsCode = 'Salesforce'.contains('code'); - Assert.isFalse(containsCode, 'No code'); + Assert.isFalse(containsCode); } public static testIsInstanceOf void testSomething() { @@ -153,22 +153,22 @@ public class Foo { public static testIsNotInstanceOf void testSomething() { Contact con = new Contact(); - Assert.isNotInstanceOfType(con, Account.class, 'Not expected type'); + Assert.isNotInstanceOfType(con, Account.class); } public static testIsNotNull void testSomething() { String myString = 'value'; - Assert.isNotNull(myString, 'myString should not be null'); + Assert.isNotNull(myString); } public static testIsNull void testSomething() { String myString = null; - Assert.isNull(myString, 'String should be null'); + Assert.isNull(myString); } public static testIsTrue void testSomething() { Boolean containsForce = 'Salesforce'.contains('force'); - Assert.isTrue(containsForce, 'Contains force'); + Assert.isTrue(containsForce); } } ]]> From d73cdcc193689fa31f524dabd0378ac0e55b324d Mon Sep 17 00:00:00 2001 From: Thomas Prouvot Date: Thu, 25 Aug 2022 15:44:37 +0200 Subject: [PATCH 17/18] Ad new tests for Assert method class --- .../ApexAssertionsShouldIncludeMessage.xml | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) 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..ac608c7529f 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,62 @@ public class Foo { } ]]> + + + [apex] Support new Assert class (Apex v56.0) #4097 + 0 + + From 29c4e996fb0be080dea93df56bb4f9a438ad3e4e Mon Sep 17 00:00:00 2001 From: Thomas Prouvot Date: Thu, 25 Aug 2022 16:54:11 +0200 Subject: [PATCH 18/18] Increase test coverage --- .../ApexAssertionsShouldIncludeMessage.xml | 77 +++++++++++-------- .../ApexUnitTestClassShouldHaveAsserts.xml | 31 +++++--- 2 files changed, 63 insertions(+), 45 deletions(-) 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 ac608c7529f..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 @@ -50,61 +50,70 @@ 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 1e16256b0ac..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 @@ -122,51 +122,60 @@ public class Foo {