Fixes #1521 [apex] ApexUnitTestClassShouldHaveAsserts: Parsing error on APEX class: expected one element but was: <BlockStatement, BlockStatement>
This commit is contained in:
@@ -14,4 +14,17 @@ public class ASTMethodCallExpression extends AbstractApexNode<MethodCallExpressi
|
||||
public Object jjtAccept(ApexParserVisitor visitor, Object data) {
|
||||
return visitor.visit(this, data);
|
||||
}
|
||||
|
||||
public String getMethodName() {
|
||||
return getNode().getMethodName();
|
||||
}
|
||||
|
||||
public String getFullMethodName() {
|
||||
final String methodName = getMethodName();
|
||||
String typeName = "";
|
||||
if (!getNode().getReferenceExpression().getJadtIdentifiers().isEmpty()) {
|
||||
typeName = getNode().getReferenceExpression().getJadtIdentifiers().get(0).value + ".";
|
||||
}
|
||||
return typeName + methodName;
|
||||
}
|
||||
}
|
@@ -3,9 +3,10 @@
|
||||
*/
|
||||
package net.sourceforge.pmd.lang.apex.rule.apexunit;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
import java.util.Set;
|
||||
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTBlockStatement;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTMethod;
|
||||
@@ -20,11 +21,13 @@ import net.sourceforge.pmd.lang.apex.ast.ApexNode;
|
||||
*/
|
||||
public class ApexUnitTestClassShouldHaveAsserts extends AbstractApexUnitTestRule {
|
||||
|
||||
private static final String SYSTEM = "System";
|
||||
private static final String ASSERT = "assert";
|
||||
private static final String ASSERT_EQUALS = "assertEquals";
|
||||
private static final String ASSERT_NOT_EQUALS = "assertNotEquals";
|
||||
|
||||
private static final Set<String> ASSERT_METHODS = new HashSet<>();
|
||||
static {
|
||||
ASSERT_METHODS.add("System.assert");
|
||||
ASSERT_METHODS.add("System.assertEquals");
|
||||
ASSERT_METHODS.add("System.assertNotEquals");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(ASTMethod node, Object data) {
|
||||
if (!isTestMethodOrClass(node)) {
|
||||
@@ -36,21 +39,18 @@ public class ApexUnitTestClassShouldHaveAsserts extends AbstractApexUnitTestRule
|
||||
|
||||
private Object checkForAssertStatements(ApexNode<?> node, Object data) {
|
||||
final List<ASTBlockStatement> blockStatements = node.findDescendantsOfType(ASTBlockStatement.class);
|
||||
final List<ASTStatement> statements = Iterables.getOnlyElement(blockStatements).findDescendantsOfType(ASTStatement.class);
|
||||
final List<ASTStatement> statements = new ArrayList<>();
|
||||
final List<ASTMethodCallExpression> methodCalls = new ArrayList<>();
|
||||
for (ASTBlockStatement blockStatement : blockStatements) {
|
||||
statements.addAll(blockStatement.findDescendantsOfType(ASTStatement.class));
|
||||
methodCalls.addAll(blockStatement.findDescendantsOfType(ASTMethodCallExpression.class));
|
||||
}
|
||||
boolean isAssertFound = false;
|
||||
|
||||
for (final ASTStatement statement : statements) {
|
||||
final List<ASTMethodCallExpression> methodCalls = statement.findDescendantsOfType(ASTMethodCallExpression.class);
|
||||
|
||||
for (final ASTMethodCallExpression methodCallExpression : methodCalls) {
|
||||
final String methodName = methodCallExpression.getNode().getMethod().getName();
|
||||
|
||||
if (methodCallExpression.getNode().getDefiningType().getApexName().equalsIgnoreCase(SYSTEM)
|
||||
&& (methodName.equalsIgnoreCase(ASSERT)
|
||||
|| methodName.equalsIgnoreCase(ASSERT_EQUALS)
|
||||
|| methodName.equalsIgnoreCase(ASSERT_NOT_EQUALS))) {
|
||||
isAssertFound = true;
|
||||
}
|
||||
|
||||
for (final ASTMethodCallExpression methodCallExpression : methodCalls) {
|
||||
if (ASSERT_METHODS.contains(methodCallExpression.getFullMethodName())) {
|
||||
isAssertFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -16,4 +16,44 @@ public class Foo {
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>#1521 [apex] ApexUnitTestClassShouldHaveAsserts: Parsing error on APEX class: expected one element but was: <BlockStatement, BlockStatement></description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
@isTest
|
||||
private class C2_Assignment_Report_Job_Test {
|
||||
|
||||
public static testMethod void testCreateAssignmentsForDifferentRoles() {
|
||||
User u = C2_Test_Utils.getWFUser('testWfc', true);
|
||||
System.runAs(u) {
|
||||
C2_Assignment_Report_Data_Aggregate_Job job = new C2_Assignment_Report_Data_Aggregate_Job();
|
||||
String query = job.start(null).getQuery();
|
||||
C2_Receiver_Unit__c ru = new C2_Receiver_Unit__c(Name='RU1');
|
||||
insert ru;
|
||||
List<C2_Case__c> cases = new List<C2_Case__c>();
|
||||
cases.add(new C2_Case__c(Name='TT0000000', Queue__c = 'PA-CSA', Status__c=C2_Case_Util.STATUS_NEW, PA_CSA__c=u.Id, DSA__c=u.Id, QR_CSA__c=u.Id, Receiver_Unit__c=ru.Id));
|
||||
cases.add(new C2_Case__c(Name='TT0000001', Queue__c = 'PA-CSA', Status__c=C2_Case_Util.STATUS_NEW, PA_CSA__c=u.Id, DSA__c=u.Id, QR_CSA__c=u.Id, Receiver_Unit__c=ru.Id));
|
||||
cases.add(new C2_Case__c(Name='TT0000002', Queue__c = 'PA-CSA', Status__c=C2_Case_Util.STATUS_NEW, PA_CSA__c=u.Id, DSA__c=u.Id, QR_CSA__c=u.Id, Receiver_Unit__c=ru.Id));
|
||||
cases.add(new C2_Case__c(Name='TT0000011', Queue__c = 'DSA', Status__c=C2_Case_Util.STATUS_PACSA_COMPLETE, PA_CSA__c=u.Id, DSA__c=u.Id, QR_CSA__c=u.Id, Receiver_Unit__c=ru.Id));
|
||||
cases.add(new C2_Case__c(Name='TT0000012', Queue__c = 'DSA', Status__c=C2_Case_Util.STATUS_PACSA_COMPLETE, PA_CSA__c=u.Id, DSA__c=u.Id, QR_CSA__c=u.Id, Receiver_Unit__c=ru.Id));
|
||||
cases.add(new C2_Case__c(Name='TT0000021', Queue__c = 'QR-CSA', Status__c=C2_Case_Util.STATUS_DSA_COMPLETE, PA_CSA__c=u.Id, DSA__c=u.Id, QR_CSA__c=u.Id, Receiver_Unit__c=ru.Id));
|
||||
insert cases;
|
||||
List<C2_Case__c> scope = Database.query(query);
|
||||
Test.startTest();
|
||||
job.execute(null, scope);
|
||||
Test.stopTest();
|
||||
List<C2_Case_Assign_Report_Data__c> caseEventEntries =
|
||||
[select Num_Cases_Assign__c, Num_Cases_Not_Completed__c, Num_Cases_Reassigned__c, Num_Cases_Completed__c, User__c, Role__c
|
||||
from C2_Case_Assign_Report_Data__c];
|
||||
System.assertEquals(3, caseEventEntries.size());
|
||||
Map<String, Integer> userIdWithRole2ReportData = getCaseEventEntriesMap(caseEventEntries);
|
||||
System.assertEquals(3, userIdWithRole2ReportData.get(createUserRoleKey(u.Id, 'PA-CSA')));
|
||||
System.assertEquals(2, userIdWithRole2ReportData.get(createUserRoleKey(u.Id, 'DSA')));
|
||||
System.assertEquals(1, userIdWithRole2ReportData.get(createUserRoleKey(u.Id, 'QR-CSA')));
|
||||
}
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
</test-data>
|
@@ -27,6 +27,8 @@
|
||||
|
||||
**Bugfixes:**
|
||||
|
||||
* apex-apexunit
|
||||
* [#1521](https://sourceforge.net/p/pmd/bugs/1521/): \[apex] ApexUnitTestClassShouldHaveAsserts: Parsing error on APEX class: expected one element but was: <BlockStatement, BlockStatement>
|
||||
* Java
|
||||
* [#1530](https://sourceforge.net/p/pmd/bugs/1530/): \[java] Parser exception on Java code
|
||||
* [#1490](https://sourceforge.net/p/pmd/bugs/1490/): \[java] PMD Error while processing - NullPointerException
|
||||
|
Reference in New Issue
Block a user