diff --git a/.gitignore b/.gitignore index a4b24e4553..a4967e344f 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ node_modules # rule docs are generated docs/pages/pmd/rules +.history/* \ No newline at end of file diff --git a/pmd-apex-6.49.0-SNAPSHOT.jar b/pmd-apex-6.49.0-SNAPSHOT.jar deleted file mode 100644 index 82c0e96d43..0000000000 Binary files a/pmd-apex-6.49.0-SNAPSHOT.jar and /dev/null differ diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexUnitTestClassShouldHaveRunAsRule.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexUnitTestClassShouldHaveRunAsRule.java new file mode 100644 index 0000000000..4a403c50ee --- /dev/null +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexUnitTestClassShouldHaveRunAsRule.java @@ -0,0 +1,45 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.apex.rule.bestpractices; + +import java.util.ArrayList; +import java.util.List; + +import net.sourceforge.pmd.lang.apex.ast.ASTBlockStatement; +import net.sourceforge.pmd.lang.apex.ast.ASTMethod; +import net.sourceforge.pmd.lang.apex.ast.ASTRunAsBlockStatement; +import net.sourceforge.pmd.lang.apex.ast.ApexNode; +import net.sourceforge.pmd.lang.apex.rule.AbstractApexUnitTestRule; + +/** + * Apex unit tests should have System.runAs methods in them + * + * @author t.prouvot + */ +public class ApexUnitTestClassShouldHaveRunAsRule extends AbstractApexUnitTestRule { + + @Override + public Object visit(ASTMethod node, Object data) { + if (!isTestMethodOrClass(node)) { + return data; + } + + return checkForRunAsStatements(node, data); + } + + private Object checkForRunAsStatements(ApexNode node, Object data) { + final List blockStatements = node.findDescendantsOfType(ASTBlockStatement.class); + final List runAsStatements = new ArrayList<>(); + + for (ASTBlockStatement blockStatement : blockStatements) { + runAsStatements.addAll(blockStatement.findDescendantsOfType(ASTRunAsBlockStatement.class)); + } + + if (!!runAsStatements.isEmpty()) { + addViolation(data, node); + } + return data; + } +} diff --git a/pmd-apex/src/main/resources/category/apex/bestpractices.xml b/pmd-apex/src/main/resources/category/apex/bestpractices.xml index 84afa3c915..c54bb17323 100644 --- a/pmd-apex/src/main/resources/category/apex/bestpractices.xml +++ b/pmd-apex/src/main/resources/category/apex/bestpractices.xml @@ -64,6 +64,44 @@ public class Foo { + + +Apex unit tests should include at least one runAs method. This makes the tests more robust, and independent from the +user running it. + + 3 + + + + + + diff --git a/pmd-apex/src/main/resources/rulesets/apex/quickstart.xml b/pmd-apex/src/main/resources/rulesets/apex/quickstart.xml index 5fd3386416..2543193a41 100644 --- a/pmd-apex/src/main/resources/rulesets/apex/quickstart.xml +++ b/pmd-apex/src/main/resources/rulesets/apex/quickstart.xml @@ -250,6 +250,9 @@ + + 3 + diff --git a/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexUnitTestClassShouldHaveRunAsTest.java b/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexUnitTestClassShouldHaveRunAsTest.java new file mode 100644 index 0000000000..1c20e4ad7c --- /dev/null +++ b/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexUnitTestClassShouldHaveRunAsTest.java @@ -0,0 +1,11 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.apex.rule.bestpractices; + +import net.sourceforge.pmd.testframework.PmdRuleTst; + +public class ApexUnitTestClassShouldHaveRunAsTest extends PmdRuleTst { + // no additional unit tests +} diff --git a/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/bestpractices/xml/ApexUnitTestClassShouldHaveRunAs.xml b/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/bestpractices/xml/ApexUnitTestClassShouldHaveRunAs.xml new file mode 100644 index 0000000000..4eefe99873 --- /dev/null +++ b/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/bestpractices/xml/ApexUnitTestClassShouldHaveRunAs.xml @@ -0,0 +1,99 @@ + + + + + Problematic apex unit test - no runAs calls + 1 + + + + + + + ApexUnitTestClassShouldHaveRunAs assumes APEX is case sensitive + 0 + + + + + + + ApexUnitTestClassShouldHaveRunAs normal test case + 0 + + + + + + + ApexUnitTestClassShouldHaveRunAs: Verify use of custom class, negative test + 2 + 3,7 + + + + + \ No newline at end of file diff --git a/pmd-core/src/main/resources/rulesets/releases/6510.xml b/pmd-core/src/main/resources/rulesets/releases/6510.xml new file mode 100644 index 0000000000..34f6df9363 --- /dev/null +++ b/pmd-core/src/main/resources/rulesets/releases/6510.xml @@ -0,0 +1,14 @@ + + + + +This ruleset contains links to rules that are new in PMD v6.51.0 + + + + + +