From 30478a6a190b3d936f54a7157781cb7b619e064f Mon Sep 17 00:00:00 2001 From: Ville Oikarinen Date: Fri, 23 Aug 2024 12:49:37 +0300 Subject: [PATCH] upgraded pmd --- .../fluentjava/iwant/wsdef/IwantModules.java | 57 +++++++++++++++---- .../iwant/plugin/pmd/CopyPasteReport.java | 2 +- .../iwant/plugin/pmd/PmdReport.java | 8 +-- .../iwant/plugin/pmd/CopyPasteReportTest.java | 2 +- .../iwant/plugin/pmd/PmdReportTest.java | 36 ++++++------ .../plugin/pmd/ruleset-with-naming-only.xml | 2 +- 6 files changed, 71 insertions(+), 36 deletions(-) diff --git a/as-iwant-developer/i-have/wsdef/src/main/java/org/fluentjava/iwant/wsdef/IwantModules.java b/as-iwant-developer/i-have/wsdef/src/main/java/org/fluentjava/iwant/wsdef/IwantModules.java index 7ff1b7ee..646835bb 100644 --- a/as-iwant-developer/i-have/wsdef/src/main/java/org/fluentjava/iwant/wsdef/IwantModules.java +++ b/as-iwant-developer/i-have/wsdef/src/main/java/org/fluentjava/iwant/wsdef/IwantModules.java @@ -69,8 +69,9 @@ private IwantSrcModuleSpex module(String fullName) { private JavaModule antLauncher = binModule("org.apache.ant", "ant-launcher", "1.10.14"); - private JavaModule asm = JavaBinModule.providing(FromRepository - .repo1MavenOrg().group("asm").name("asm").version("3.2").jar()) + private JavaModule asm = JavaBinModule + .providing(FromRepository.repo1MavenOrg().group("org.ow2.asm") + .name("asm").version("9.7").jar()) .end(); private JavaModule commonsIo = JavaBinModule @@ -78,6 +79,9 @@ private IwantSrcModuleSpex module(String fullName) { .name("commons-io").version("2.16.1").jar()) .end(); + private JavaModule commonsLang3 = binModule("org.apache.commons", + "commons-lang3", "3.16.0"); + private JavaModule commonsMath = JavaBinModule .providing(FromRepository.repo1MavenOrg().group("commons-math") .name("commons-math").version("1.2").jar()) @@ -105,14 +109,42 @@ private IwantSrcModuleSpex module(String fullName) { private final JavaModule junit = binModule("junit", "junit", "4.13.2", hamcrestCore); - // TODO document dependency to asm, jaxen - private JavaModule pmd = JavaBinModule.providing(FromRepository - .repo1MavenOrg().group("pmd").name("pmd").version("4.3").jar()) - .end(); + private final JavaBinModule ooxmlNiceXmlMessages = binModule( + "com.github.oowekyala.ooxml", "nice-xml-messages", "3.1"); + + private final JavaBinModule pcollections = binModule("org.pcollections", + "pcollections", "4.0.2"); + + private static final String PMD_VER = "7.4.0"; + private final JavaModule pmdAnt = binModule("net.sourceforge.pmd", + "pmd-ant", PMD_VER); + private final JavaModule pmdCore = binModule("net.sourceforge.pmd", + "pmd-core", PMD_VER); + private final JavaModule pmdJava = binModule("net.sourceforge.pmd", + "pmd-java", PMD_VER); + private final JavaModule pmdLangTest = binModule("net.sourceforge.pmd", + "pmd-lang-test", PMD_VER); + private final JavaModule pmdTest = binModule("net.sourceforge.pmd", + "pmd-test", PMD_VER); + private final JavaModule pmdTestSchema = binModule("net.sourceforge.pmd", + "pmd-test-schema", PMD_VER); + private final List pmdModules = List.of(pmdAnt, pmdCore, + pmdJava, pmdLangTest, pmdTest, pmdTestSchema); private final JavaBinModule jcommander = binModule("com.beust", "jcommander", "1.82"); + private final JavaBinModule saxonHe = binModule("net.sf.saxon", "Saxon-HE", + "12.5"); + + private static final String SLF4J_VER = "2.0.16"; + private final JavaBinModule slf4jApi = binModule("org.slf4j", "slf4j-api", + SLF4J_VER); + private final JavaBinModule slf4jJulToSlf4j = binModule("org.slf4j", + "jul-to-slf4j", SLF4J_VER); + private final JavaBinModule slf4jSimple = binModule("org.slf4j", + "slf4j-simple", SLF4J_VER); + private final JavaBinModule testng = binModule("org.testng", "testng", "6.9.4", jcommander); @@ -142,6 +174,9 @@ private static JavaBinModule nettyModule(String name) { private final JavaBinModule nettyHandlerProxy = nettyModule( "handler-proxy"); + private final JavaBinModule xmlresolver = binModule("org.xmlresolver", + "xmlresolver", "6.0.8"); + private JavaSrcModule iwantApiModel = essentialModule("api-model") .mainDeps().testDeps(junit).end(); @@ -330,11 +365,13 @@ private static IwantSrcModuleSpex withMockrootMainJavas( iwantPluginAnt) .testDeps(junit, iwantApimocks, iwantEmbedded, iwantTestarea).end(); - // TODO don't depend directly on asm, jaxen: pmd depends on them + // TODO don't depend directly on asm, jaxen, ...: pmd depends on them private JavaSrcModule iwantPluginPmd = optionalModule("plugin-pmd") - .testResources("src/test/resources") - .mainDeps(ant, asm, commonsIo, iwantApiCore, iwantApiModel, - iwantApiTarget, iwantEntry, jaxen, pmd) + .testResources("src/test/resources").mainDeps(pmdModules) + .mainDeps(ant, asm, commonsIo, commonsLang3, iwantApiCore, + iwantApiModel, iwantApiTarget, iwantEntry, + ooxmlNiceXmlMessages, pcollections, jaxen, saxonHe, + slf4jApi, slf4jJulToSlf4j, slf4jSimple, xmlresolver) .testDeps(junit, iwantApimocks, iwantTestarea, iwantTestresources) .end(); diff --git a/optional/iwant-plugin-pmd/src/main/java/org/fluentjava/iwant/plugin/pmd/CopyPasteReport.java b/optional/iwant-plugin-pmd/src/main/java/org/fluentjava/iwant/plugin/pmd/CopyPasteReport.java index 4dfe9985..0361ec57 100644 --- a/optional/iwant-plugin-pmd/src/main/java/org/fluentjava/iwant/plugin/pmd/CopyPasteReport.java +++ b/optional/iwant-plugin-pmd/src/main/java/org/fluentjava/iwant/plugin/pmd/CopyPasteReport.java @@ -15,7 +15,7 @@ import org.fluentjava.iwant.entry.Iwant; import org.fluentjava.iwant.entry.Iwant.IwantException; -import net.sourceforge.pmd.cpd.CPDTask; +import net.sourceforge.pmd.ant.CPDTask; public class CopyPasteReport extends TargetBase { diff --git a/optional/iwant-plugin-pmd/src/main/java/org/fluentjava/iwant/plugin/pmd/PmdReport.java b/optional/iwant-plugin-pmd/src/main/java/org/fluentjava/iwant/plugin/pmd/PmdReport.java index ec02ab24..1af8a15e 100644 --- a/optional/iwant-plugin-pmd/src/main/java/org/fluentjava/iwant/plugin/pmd/PmdReport.java +++ b/optional/iwant-plugin-pmd/src/main/java/org/fluentjava/iwant/plugin/pmd/PmdReport.java @@ -87,14 +87,13 @@ public void path(TargetEvaluationContext ctx) throws Exception { PMDTask task = new PMDTask(); task.setProject(new Project()); - task.setShortFilenames(true); RuleSetWrapper rulesetWrapper = new RuleSetWrapper(); rulesetWrapper.addText(rulesetXml.getCanonicalPath()); task.addRuleset(rulesetWrapper); Formatter htmlFormatter = new Formatter(); - htmlFormatter.setType("betterhtml"); + htmlFormatter.setType("html"); File htmlReport = new File(dest, name() + ".html"); htmlFormatter.setToFile(htmlReport); task.addFormatter(htmlFormatter); @@ -150,10 +149,7 @@ private String rulesetXml() { b.append("\n"); b.append(" PMD rules for " + name() + "\n"); b.append("\n"); - b.append(" \n"); - b.append(" \n"); - b.append(" \n"); - b.append(" \n"); + b.append(" \n"); b.append("\n"); b.append("\n"); return b.toString(); diff --git a/optional/iwant-plugin-pmd/src/test/java/org/fluentjava/iwant/plugin/pmd/CopyPasteReportTest.java b/optional/iwant-plugin-pmd/src/test/java/org/fluentjava/iwant/plugin/pmd/CopyPasteReportTest.java index c3e739d1..5b862150 100644 --- a/optional/iwant-plugin-pmd/src/test/java/org/fluentjava/iwant/plugin/pmd/CopyPasteReportTest.java +++ b/optional/iwant-plugin-pmd/src/test/java/org/fluentjava/iwant/plugin/pmd/CopyPasteReportTest.java @@ -101,7 +101,7 @@ public void testReportOfTwoSrcDirectoriesMentionsRedundancyWhenMinimumTokenCount String txtReportContent = txtReportContent(report); assertTrue(txtReportContent - .contains("Found a 9 line (20 tokens) duplication")); + .contains("Found a 9 line (14 tokens) duplication")); } public void testReportOfTwoSrcDirectoriesIsEmptyWhenMinimumTokenCountIsHigh() diff --git a/optional/iwant-plugin-pmd/src/test/java/org/fluentjava/iwant/plugin/pmd/PmdReportTest.java b/optional/iwant-plugin-pmd/src/test/java/org/fluentjava/iwant/plugin/pmd/PmdReportTest.java index 5fbb9a29..363fca6e 100644 --- a/optional/iwant-plugin-pmd/src/test/java/org/fluentjava/iwant/plugin/pmd/PmdReportTest.java +++ b/optional/iwant-plugin-pmd/src/test/java/org/fluentjava/iwant/plugin/pmd/PmdReportTest.java @@ -60,7 +60,7 @@ public void testReportOfZeroSrcDirectoriesProducesReportFiles() report.path(ctx); assertTrue(htmlReportContent(report).length() > 0); - assertTrue(txtReportContent(report).length() > 0); + assertTrue(txtReportContent(report).length() == 0); } public void testReportOfEmptySrcDirectoryProducesReportFiles() @@ -73,7 +73,7 @@ public void testReportOfEmptySrcDirectoryProducesReportFiles() report.path(ctx); assertTrue(htmlReportContent(report).length() > 0); - assertTrue(txtReportContent(report).length() > 0); + assertTrue(txtReportContent(report).length() == 0); } public void testAllReportFormatsOfOneClassesDirWithOneClassWithIssues() @@ -86,28 +86,30 @@ public void testAllReportFormatsOfOneClassesDirWithOneClassWithIssues() report.path(ctx); String htmlReportContent = htmlReportContent(report); - assertTrue(htmlReportContent - .contains("Avoid reassigning parameters such as 'parameter'")); + assertTrue(htmlReportContent.contains( + "The initial value of parameter 'parameter' is never used")); String txtReportContent = txtReportContent(report); assertTrue(txtReportContent.contains( - "org/fluentjava/iwant/plugin/pmd/testfodder/ClassWithPmdIssues.java:5" - + " Avoid reassigning parameters such as 'parameter'")); + "org/fluentjava/iwant/plugin/pmd/testfodder/ClassWithPmdIssues.java:6:" + + " AvoidReassigningParameters: Avoid reassigning parameters such as 'parameter'")); assertTrue(txtReportContent.contains( - "org/fluentjava/iwant/plugin/pmd/testfodder/ClassWithPmdIssues.java:9" - + " Method names should not start with capital letters")); + "org/fluentjava/iwant/plugin/pmd/testfodder/ClassWithPmdIssues.java:9:" + + " MethodNamingConventions: The instance method name 'MethodWithDiscouragedName'" + + " doesn't match '[a-z][a-zA-Z0-9]*")); assertTrue(txtReportContent.contains( - "org/fluentjava/iwant/plugin/pmd/testfodder/ClassWithPmdIssues.java:13" - + " Avoid unused private methods such as 'deadMethod()'.")); + "org/fluentjava/iwant/plugin/pmd/testfodder/ClassWithPmdIssues.java:13:" + + " UnusedPrivateMethod: Avoid unused private methods such as 'deadMethod()")); assertTrue(txtReportContent.contains( - "org/fluentjava/iwant/plugin/pmd/testfodder/ClassWithPmdIssues.java:17" - + " Document empty method")); + "org/fluentjava/iwant/plugin/pmd/testfodder/ClassWithPmdIssues.java:17:" + + " UncommentedEmptyMethodBody: Document empty method body")); String xmlReportContent = xmlReportContent(report); - assertTrue(xmlReportContent.contains("ClassWithPmdIssues.java\">\n" - + "PMD rules with naming only - +