[apex] Fix rule test for UnusedMethod

Refs #4453
This commit is contained in:
Andreas Dangel committed 2023-06-29 11:52:02 +02:00
1 parent 670eea4f3b
commit 256ae42830
4 files changed
+27 -6

No files matched your search

@@ -11,6 +11,9 @@ public class UnusedMethodRule extends AbstractApexRule {
@Override
public Object visit(ASTMethod node, Object data) {
if (node.isSynthetic()) {
return data;
}
// Check if any 'Unused' Issues align with this method
node.getRoot().getGlobalIssues().stream()
@@ -11,6 +11,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import net.sourceforge.pmd.PMDConfiguration;
import net.sourceforge.pmd.PmdAnalysis;
@@ -25,22 +26,33 @@ import net.sourceforge.pmd.lang.apex.ApexLanguageModule;
import net.sourceforge.pmd.lang.apex.ApexLanguageProperties;
import net.sourceforge.pmd.reporting.GlobalAnalysisListener;
import com.nawforce.pkgforce.path.PathFactory;
import com.nawforce.pkgforce.path.PathLike;
import com.nawforce.runtime.platform.Environment;
import scala.Option;
class UnusedMethodTest {
@TempDir
private Path tempDir;
@Test
void findUnusedMethodsWithSfdxProject() throws Exception {
Path testProjectDir = Paths.get("src/test/resources/net/sourceforge/pmd/lang/apex/rule/design/UnusedMethod/project1");
Report report = runRule(testProjectDir);
assertEquals(1, report.getViolations().size());
assertViolation(report.getViolations().get(0), "Foo.cls", 6);
assertViolation(report.getViolations().get(0), "Foo.cls", 10); // line 10 is method unusedMethod()
}
private void assertViolation(RuleViolation violation, String fileName, int lineNumber) {
assertEquals("Foo.cls", violation.getFileId().getFileName());
assertEquals(6, violation.getBeginLine()); // line 6 is method unusedMethod()
assertEquals(fileName, violation.getFileId().getFileName());
assertEquals(lineNumber, violation.getBeginLine());
}
private Report runRule(Path testProjectDir) throws IOException {
Option<PathLike> pathLikeOption = Option.apply(PathFactory.apply(tempDir.toString()));
Option<Option<PathLike>> cachDirOption = Option.apply(pathLikeOption);
Environment.setCacheDirOverride(cachDirOption);
Language apexLanguage = ApexLanguageModule.getInstance();
LanguageVersion languageVersion = apexLanguage.getDefaultVersion();
PMDConfiguration configuration = new PMDConfiguration();
@@ -4,5 +4,6 @@
"path": "src",
"default": true
}
]
],
"namespace": "foo_ns"
}
@@ -3,10 +3,15 @@
*/
public class Foo {
// this method is needed, otherwise the whole class is considered as unused, as it would contain
// then only one unused method.
private void other() {}
public void unusedMethod() {
// ääää // at least 4 umlauts are required in order to
// reproduce the ArrayIndexOutOfBoundsException
Integer a;
// reproduce the ArrayIndexOutOfBoundsException (#4453)
// up to 7.0.0-rc3 with export PMD_JAVA_OPTS="-Dfile.encoding=ISO-8859-15" (e.g. platform encoding is not UTF-8)
other();
// note: there should be no additional characters
// after this method, only the two closing brackets
}