Skip to content

Commit

Permalink
upgraded pmd
Browse files Browse the repository at this point in the history
  • Loading branch information
wipu committed Aug 23, 2024
1 parent 8dc6898 commit 30478a6
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,19 @@ 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
.providing(FromRepository.repo1MavenOrg().group("commons-io")
.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())
Expand Down Expand Up @@ -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<JavaModule> 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);

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -150,10 +149,7 @@ private String rulesetXml() {
b.append("\n");
b.append(" <description>PMD rules for " + name() + "</description>\n");
b.append("\n");
b.append(" <rule ref=\"rulesets/basic.xml\" />\n");
b.append(" <rule ref=\"rulesets/design.xml\" />\n");
b.append(" <rule ref=\"rulesets/unusedcode.xml\" />\n");
b.append(" <rule ref=\"rulesets/naming.xml\" />\n");
b.append(" <rule ref=\"rulesets/internal/all-java.xml\" />\n");
b.append("\n");
b.append("</ruleset>\n");
return b.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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"
+ "<violation beginline=\"5\" endline=\"5\""
+ " begincolumn=\"54\" endcolumn=\"62\""
+ " rule=\"AvoidReassigningParameters\""));
assertTrue(xmlReportContent.contains(
"<violation beginline=\"6\" endline=\"6\" begincolumn=\"3\""
+ " endcolumn=\"12\" rule=\"AvoidReassigningParameters\" ruleset=\"Best Practices\""
+ " package=\"net.sf.iwant.plugin.pmd.testfodder\" class=\"ClassWithPmdIssues\""
+ " method=\"methodThatWritesParameter\""));
}

public void testReportOnlyContainsIssuesDefinedInGivenRulesPath()
Expand All @@ -129,7 +131,7 @@ public void testReportOnlyContainsIssuesDefinedInGivenRulesPath()
assertFalse(txtReportContent
.contains("Avoid reassigning parameters such as 'parameter'"));
assertTrue(txtReportContent.contains(
"Method names should not start with capital letters"));
"The instance method name 'MethodWithDiscouragedName'"));
assertFalse(txtReportContent.contains(
"Avoid unused private methods such as 'deadMethod()'."));
assertFalse(txtReportContent.contains("Document empty method"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

<description>PMD rules with naming only</description>

<rule ref="rulesets/naming.xml" />
<rule ref="category/java/codestyle.xml/MethodNamingConventions" />

</ruleset>

0 comments on commit 30478a6

Please sign in to comment.