[apex] ApexUnitTestShouldNotUseSeeAllDataTrue: case-insensitive check

The annotation parameter name "seeAllData" is now compared
case-insensitive.

Fixes #5095
This commit is contained in:
Andreas Dangel 2024-07-04 11:48:11 +02:00
parent e6397d5d5c
commit d0928cc185
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3
4 changed files with 69 additions and 1 deletions

View File

@ -15,6 +15,8 @@ This is a {{ site.pmd.release_type }} release.
### 🚀 New and noteworthy
### 🐛 Fixed Issues
* apex-bestpractices
* [#5095](https://github.com/pmd/pmd/issues/5095): \[apex] ApexUnitTestShouldNotUseSeeAllDataTrue false negative due to casing (regression in PMD 7)
### 🚨 API Changes

View File

@ -4,6 +4,8 @@
package net.sourceforge.pmd.lang.apex.ast;
import org.checkerframework.checker.nullness.qual.NonNull;
import com.google.summit.ast.expression.Expression;
import com.google.summit.ast.expression.LiteralExpression;
import com.google.summit.ast.modifier.ElementArgument;
@ -50,4 +52,17 @@ public final class ASTAnnotationParameter extends AbstractApexNode.Single<Elemen
public String getImage() {
return getValue();
}
/**
* Checks whether this annotation parameter has the given name.
* The check is done case-insensitive.
*
* @param name the expected annotation parameter name
* @return {@code true} if this parameter has the expected name.
* @see #SEE_ALL_DATA
* @since 7.4.0
*/
public boolean hasName(@NonNull String name) {
return name.equalsIgnoreCase(getName());
}
}

View File

@ -45,7 +45,7 @@ public class ApexUnitTestShouldNotUseSeeAllDataTrueRule extends AbstractApexUnit
if (modifierNode != null) {
for (ASTAnnotationParameter parameter : modifierNode.descendants(ASTAnnotationParameter.class)) {
if (ASTAnnotationParameter.SEE_ALL_DATA.equals(parameter.getName()) && parameter.getBooleanValue()) {
if (parameter.hasName(ASTAnnotationParameter.SEE_ALL_DATA) && parameter.getBooleanValue()) {
asCtx(data).addViolation(node);
return data;
}

View File

@ -34,4 +34,55 @@ public class Foo {
}
]]></code>
</test-code>
<test-code>
<description>[apex] ApexUnitTestShouldNotUseSeeAllDataTrue false negative due to casing (regression in PMD 7) #5095</description>
<expected-problems>9</expected-problems>
<expected-linenumbers>2,5,10,15,20,25,30,35,40</expected-linenumbers>
<code><![CDATA[
@isTest(SeeAllData = true)
public class SomeTest {
@IsTest(SeeAllData=true)
public static void testMethod1() {
System.assertEquals(1 + 2, 3);
}
@isTest(SeeAllData=true)
public static void testMethod2() {
System.assertEquals(1 + 2, 3);
}
@IsTest(seeAllData=true)
public static void testMethod3() {
System.assertEquals(1 + 2, 3);
}
@isTest(seeAllData=true)
public static void testMethod4() {
System.assertEquals(1 + 2, 3);
}
@IsTest(SeeAllData=True)
public static void testMethod5() {
System.assertEquals(1 + 2, 3);
}
@isTest(SeeAllData=True)
public static void testMethod6() {
System.assertEquals(1 + 2, 3);
}
@IsTest(seeAllData=True)
public static void testMethod7() {
System.assertEquals(1 + 2, 3);
}
@isTest(seeAllData=True)
public static void testMethod8() {
System.assertEquals(1 + 2, 3);
}
}
]]></code>
</test-code>
</test-data>