Fix apex rules

This commit is contained in:
Clément Fournier
2020-06-27 17:57:21 +02:00
parent 15dbd03061
commit 50cd4668a8
10 changed files with 70 additions and 17 deletions

View File

@ -10,6 +10,7 @@ Rules which enforce generally accepted best practices.
</description>
<rule name="ApexAssertionsShouldIncludeMessage"
language="apex"
since="6.13.0"
message="Apex test assert statement should make use of the message parameter."
class="net.sourceforge.pmd.lang.apex.rule.bestpractices.ApexAssertionsShouldIncludeMessageRule"
@ -37,6 +38,7 @@ public class Foo {
</rule>
<rule name="ApexUnitTestClassShouldHaveAsserts"
language="apex"
since="5.5.1"
message="Apex unit tests should System.assert() or assertEquals() or assertNotEquals()"
class="net.sourceforge.pmd.lang.apex.rule.bestpractices.ApexUnitTestClassShouldHaveAssertsRule"
@ -62,6 +64,7 @@ public class Foo {
</rule>
<rule name="ApexUnitTestMethodShouldHaveIsTestAnnotation"
language="apex"
since="6.13.0"
message="Apex test methods should have @isTest annotation."
class="net.sourceforge.pmd.lang.apex.rule.bestpractices.ApexUnitTestMethodShouldHaveIsTestAnnotationRule"
@ -94,6 +97,7 @@ private class ATest {
</rule>
<rule name="ApexUnitTestShouldNotUseSeeAllDataTrue"
language="apex"
since="5.5.1"
message="Apex unit tests should not use @isTest(seeAllData = true)"
class="net.sourceforge.pmd.lang.apex.rule.bestpractices.ApexUnitTestShouldNotUseSeeAllDataTrueRule"
@ -118,6 +122,7 @@ public class Foo {
</rule>
<rule name="AvoidGlobalModifier"
language="apex"
since="5.5.0"
message="Avoid using global modifier"
class="net.sourceforge.pmd.lang.apex.rule.bestpractices.AvoidGlobalModifierRule"
@ -139,6 +144,7 @@ global class Unchangeable {
</rule>
<rule name="AvoidLogicInTrigger"
language="apex"
since="5.5.0"
message="Avoid logic in triggers"
class="net.sourceforge.pmd.lang.apex.rule.bestpractices.AvoidLogicInTriggerRule"

View File

@ -10,6 +10,7 @@ Rules which enforce a specific coding style.
</description>
<rule name="ClassNamingConventions"
language="apex"
since="5.5.0"
message="The {0} name ''{1}'' doesn''t match ''{2}''"
class="net.sourceforge.pmd.lang.apex.rule.codestyle.ClassNamingConventionsRule"
@ -129,6 +130,7 @@ class Foo {
</rule>
<rule name="FieldNamingConventions"
language="apex"
since="6.15.0"
message="The {0} name ''{1}'' doesn''t match ''{2}''"
class="net.sourceforge.pmd.lang.apex.rule.codestyle.FieldNamingConventionsRule"
@ -189,6 +191,7 @@ for (int i = 0; i < 42; i++) { // preferred approach
</rule>
<rule name="FormalParameterNamingConventions"
language="apex"
since="6.15.0"
message="The {0} name ''{1}'' doesn''t match ''{2}''"
class="net.sourceforge.pmd.lang.apex.rule.codestyle.FormalParameterNamingConventionsRule"
@ -214,6 +217,7 @@ public class Foo {
</rule>
<rule name="LocalVariableNamingConventions"
language="apex"
since="6.15.0"
message="The {0} name ''{1}'' doesn''t match ''{2}''"
class="net.sourceforge.pmd.lang.apex.rule.codestyle.LocalVariableNamingConventionsRule"
@ -241,6 +245,7 @@ public class Foo {
</rule>
<rule name="MethodNamingConventions"
language="apex"
since="5.5.0"
message="The {0} name ''{1}'' doesn''t match ''{2}''"
class="net.sourceforge.pmd.lang.apex.rule.codestyle.MethodNamingConventionsRule"
@ -265,6 +270,7 @@ public class Foo {
</rule>
<rule name="OneDeclarationPerLine"
language="apex"
since="6.7.0"
message="Use one statement for each line, it enhances code readability."
class="net.sourceforge.pmd.lang.apex.rule.ApexXPathRule"
@ -307,6 +313,7 @@ Integer b;
</rule>
<rule name="PropertyNamingConventions"
language="apex"
since="6.15.0"
message="The {0} name ''{1}'' doesn''t match ''{2}''"
class="net.sourceforge.pmd.lang.apex.rule.codestyle.PropertyNamingConventionsRule"
@ -332,6 +339,7 @@ public class Foo {
</rule>
<rule name="VariableNamingConventions"
language="apex"
since="5.5.0"
deprecated="true"
message="{0} variable {1} should begin with {2}"

View File

@ -9,7 +9,8 @@
Rules that help you discover design issues.
</description>
<rule name="AvoidDeeplyNestedIfStmts"
<rule language="apex"
name="AvoidDeeplyNestedIfStmts"
since="5.5.0"
message="Deeply nested if..then statements are hard to read"
class="net.sourceforge.pmd.lang.apex.rule.design.AvoidDeeplyNestedIfStmtsRule"
@ -35,7 +36,8 @@ public class Foo {
</example>
</rule>
<rule name="CyclomaticComplexity"
<rule language="apex"
name="CyclomaticComplexity"
message="The {0} ''{1}'' has a{2} cyclomatic complexity of {3}."
since="6.0.0"
class="net.sourceforge.pmd.lang.apex.rule.design.CyclomaticComplexityRule"
@ -86,7 +88,8 @@ public class Complicated {
</example>
</rule>
<rule name="CognitiveComplexity"
<rule language="apex"
name="CognitiveComplexity"
message="The {0} ''{1}'' has a{2} cognitive complexity of {3}."
since="6.22.0"
class="net.sourceforge.pmd.lang.apex.rule.design.CognitiveComplexityRule"
@ -148,7 +151,8 @@ public class Foo {
</example>
</rule>
<rule name="ExcessiveClassLength"
<rule language="apex"
name="ExcessiveClassLength"
since="5.5.0"
message="Avoid really long classes."
class="net.sourceforge.pmd.lang.apex.rule.design.ExcessiveClassLengthRule"
@ -179,7 +183,8 @@ public class Foo {
</example>
</rule>
<rule name="ExcessiveParameterList"
<rule language="apex"
name="ExcessiveParameterList"
since="5.5.0"
message="Avoid long parameter lists."
class="net.sourceforge.pmd.lang.apex.rule.design.ExcessiveParameterListRule"
@ -203,7 +208,8 @@ public void addPerson(Date birthdate, BodyMeasurements measurements, int ssn) {
</example>
</rule>
<rule name="ExcessivePublicCount"
<rule language="apex"
name="ExcessivePublicCount"
since="5.5.0"
message="This class has a bunch of public methods and attributes"
class="net.sourceforge.pmd.lang.apex.rule.design.ExcessivePublicCountRule"
@ -232,7 +238,8 @@ public class Foo {
</example>
</rule>
<rule name="NcssConstructorCount"
<rule language="apex"
name="NcssConstructorCount"
since="5.5.0"
message="The constructor has an NCSS line count of {0}"
class="net.sourceforge.pmd.lang.apex.rule.design.NcssConstructorCountRule"
@ -260,7 +267,8 @@ public class Foo extends Bar {
</example>
</rule>
<rule name="NcssMethodCount"
<rule language="apex"
name="NcssMethodCount"
since="5.5.0"
message="The method ''{0}()'' has an NCSS line count of {1}"
class="net.sourceforge.pmd.lang.apex.rule.design.NcssMethodCountRule"
@ -287,7 +295,8 @@ public class Foo extends Bar {
</example>
</rule>
<rule name="NcssTypeCount"
<rule language="apex"
name="NcssTypeCount"
since="5.5.0"
message="The type has an NCSS line count of {0}"
class="net.sourceforge.pmd.lang.apex.rule.design.NcssTypeCountRule"
@ -316,7 +325,8 @@ public class Foo extends Bar {
</example>
</rule>
<rule name="StdCyclomaticComplexity"
<rule language="apex"
name="StdCyclomaticComplexity"
since="5.5.0"
message="The {0} ''{1}'' has a Standard Cyclomatic Complexity of {2}."
class="net.sourceforge.pmd.lang.apex.rule.design.StdCyclomaticComplexityRule"
@ -370,7 +380,8 @@ public class Foo {
</example>
</rule>
<rule name="TooManyFields"
<rule language="apex"
name="TooManyFields"
since="5.5.0"
message="Too many fields"
class="net.sourceforge.pmd.lang.apex.rule.design.TooManyFieldsRule"

View File

@ -10,6 +10,7 @@ Rules that are related to code documentation.
</description>
<rule name="ApexDoc"
language="apex"
since="6.8.0"
message="ApexDoc comment is missing or incorrect"
class="net.sourceforge.pmd.lang.apex.rule.documentation.ApexDocRule"

View File

@ -10,6 +10,7 @@ Rules to detect constructs that are either broken, extremely confusing or prone
</description>
<rule name="ApexCSRF"
language="apex"
since="5.5.3"
message="Avoid making DML operations in Apex class constructor or initializers"
class="net.sourceforge.pmd.lang.apex.rule.errorprone.ApexCSRFRule"
@ -50,6 +51,7 @@ public class Foo {
</rule>
<rule name="AvoidDirectAccessTriggerMap"
language="apex"
since="6.0.0"
message="Avoid directly accessing Trigger.old and Trigger.new"
class="net.sourceforge.pmd.lang.apex.rule.ApexXPathRule"
@ -82,6 +84,7 @@ trigger AccountTrigger on Account (before insert, before update) {
</rule>
<rule name="AvoidHardcodingId"
language="apex"
since="6.0.0"
message="Hardcoding Id's is bound to break when changing environments."
class="net.sourceforge.pmd.lang.apex.rule.errorprone.AvoidHardcodingIdRule"
@ -293,6 +296,7 @@ public void bar(Integer a, Integer b) {
</rule>
<rule name="MethodWithSameNameAsEnclosingClass"
language="apex"
since="5.5.0"
message="Classes should not have non-constructor methods with the same name as the class"
class="net.sourceforge.pmd.lang.apex.rule.errorprone.MethodWithSameNameAsEnclosingClassRule"
@ -314,6 +318,7 @@ public class MyClass {
</rule>
<rule name="AvoidNonExistentAnnotations"
language="apex"
since="6.5.0"
message="Use of non existent annotations will lead to broken Apex code which will not compile in the future."
class="net.sourceforge.pmd.lang.apex.rule.errorprone.AvoidNonExistentAnnotationsRule"
@ -336,6 +341,7 @@ public class MyClass {
</rule>
<rule name="TestMethodsMustBeInTestClasses"
language="apex"
since="6.22.0"
message="Test methods must be in test classes"
class="net.sourceforge.pmd.lang.apex.rule.ApexXPathRule"

View File

@ -10,6 +10,7 @@ Rules that flag suboptimal code.
</description>
<rule name="AvoidDmlStatementsInLoops"
language="apex"
since="5.5.0"
message="Avoid DML statements inside loops"
class="net.sourceforge.pmd.lang.apex.rule.performance.AvoidDmlStatementsInLoopsRule"
@ -34,6 +35,7 @@ public class Something {
</rule>
<rule name="AvoidSoqlInLoops"
language="apex"
since="5.5.0"
message="Avoid Soql queries inside loops"
class="net.sourceforge.pmd.lang.apex.rule.performance.AvoidSoqlInLoopsRule"
@ -56,6 +58,7 @@ public class Something {
</rule>
<rule name="AvoidSoslInLoops"
language="apex"
since="6.0.0"
message="Avoid Sosl queries inside loops"
class="net.sourceforge.pmd.lang.apex.rule.performance.AvoidSoslInLoopsRule"

View File

@ -10,6 +10,7 @@ Rules that flag potential security flaws.
</description>
<rule name="ApexBadCrypto"
language="apex"
since="5.5.3"
message="Apex classes should use random IV/key"
class="net.sourceforge.pmd.lang.apex.rule.security.ApexBadCryptoRule"
@ -32,6 +33,7 @@ public without sharing class Foo {
</rule>
<rule name="ApexCRUDViolation"
language="apex"
since="5.5.3"
message="Validate CRUD permission before SOQL/DML operation"
class="net.sourceforge.pmd.lang.apex.rule.security.ApexCRUDViolationRule"
@ -65,6 +67,7 @@ public class Foo {
<rule name="ApexCSRF" ref="category/apex/errorprone.xml/ApexCSRF" deprecated="true"/>
<rule name="ApexDangerousMethods"
language="apex"
since="5.5.3"
message="Calling potentially dangerous method"
class="net.sourceforge.pmd.lang.apex.rule.security.ApexDangerousMethodsRule"
@ -92,6 +95,7 @@ public class Foo {
</rule>
<rule name="ApexInsecureEndpoint"
language="apex"
since="5.5.3"
message="Apex callouts should use encrypted communication channels"
class="net.sourceforge.pmd.lang.apex.rule.security.ApexInsecureEndpointRule"
@ -114,6 +118,7 @@ public without sharing class Foo {
</rule>
<rule name="ApexOpenRedirect"
language="apex"
since="5.5.3"
message="Apex classes should safely redirect to a known location"
class="net.sourceforge.pmd.lang.apex.rule.security.ApexOpenRedirectRule"
@ -136,6 +141,7 @@ public without sharing class Foo {
</rule>
<rule name="ApexSharingViolations"
language="apex"
since="5.5.3"
message="Apex classes should declare a sharing model if DML or SOQL/SOSL is used"
class="net.sourceforge.pmd.lang.apex.rule.security.ApexSharingViolationsRule"
@ -155,6 +161,7 @@ public without sharing class Foo {
</rule>
<rule name="ApexSOQLInjection"
language="apex"
since="5.5.3"
message="Avoid untrusted/unescaped variables in DML query"
class="net.sourceforge.pmd.lang.apex.rule.security.ApexSOQLInjectionRule"
@ -175,6 +182,7 @@ public class Foo {
</rule>
<rule name="ApexSuggestUsingNamedCred"
language="apex"
since="5.5.3"
message="Suggest named credentials for authentication"
class="net.sourceforge.pmd.lang.apex.rule.security.ApexSuggestUsingNamedCredRule"
@ -209,6 +217,7 @@ public class Foo {
</rule>
<rule name="ApexXSSFromEscapeFalse"
language="apex"
since="5.5.3"
message="Apex classes should escape Strings in error messages"
class="net.sourceforge.pmd.lang.apex.rule.security.ApexXSSFromEscapeFalseRule"
@ -229,6 +238,7 @@ public without sharing class Foo {
</rule>
<rule name="ApexXSSFromURLParam"
language="apex"
since="5.5.3"
message="Apex classes should escape/sanitize Strings obtained from URL parameters"
class="net.sourceforge.pmd.lang.apex.rule.security.ApexXSSFromURLParamRule"

View File

@ -1209,17 +1209,17 @@ public class RuleSetFactoryTest {
// listed here is finally removed.
private static final String DEPRECATED_RULE_NAME = "MockRule3";
private static final String REFERENCE_TO_DEPRECATED_RULE = "<?xml version=\"1.0\"?>\n"
private static final String REFERENCE_TO_DEPRECATED_RULE = "<?xml version=\"1.0\"?>\n"
+ "<ruleset name=\"test\">\n"
+ "<description>testdesc</description>\n"
+ "<rule " + "ref=\"" + DEPRECATED_RULE_RULESET_NAME + "/" + DEPRECATED_RULE_NAME + "\" />\n" +
"</ruleset>";
+ "<rule " + "ref=\"" + DEPRECATED_RULE_RULESET_NAME + "/" + DEPRECATED_RULE_NAME + "\" />\n"
+ "</ruleset>";
private static final String REFERENCE_TO_RULESET_WITH_DEPRECATED_RULE = "<?xml version=\"1.0\"?>\n"
+ "<ruleset name=\"test\">\n"
+ "<description>testdesc</description>\n"
+ "<rule " + "ref=\"" + DEPRECATED_RULE_RULESET_NAME + "\" />\n" +
"</ruleset>";
+ "<rule " + "ref=\"" + DEPRECATED_RULE_RULESET_NAME + "\" />\n"
+ "</ruleset>";
private static final String DFA = "<?xml version=\"1.0\"?>\n"
+ "<ruleset name=\"test\">\n"

View File

@ -44,7 +44,11 @@
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>

View File

@ -33,6 +33,7 @@ import javax.xml.parsers.SAXParserFactory;
import org.apache.commons.io.FilenameUtils;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.contrib.java.lang.system.SystemErrRule;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
@ -50,6 +51,9 @@ import net.sourceforge.pmd.util.ResourceLoader;
* subclassed for each language.
*/
public abstract class AbstractRuleSetFactoryTest {
@org.junit.Rule
public final SystemErrRule systemErrRule = new SystemErrRule().enableLog().muteForSuccessfulTests();
private static SAXParserFactory saxParserFactory;
private static ValidateDefaultHandler validateDefaultHandler;
private static SAXParser saxParser;