ruleSets = new ArrayList<>();
+ /**
+ * The show suppressed flag.
+ */
boolean showSuppressed;
+ /**
+ * THe suppressed marker.
+ */
String suppressedMarker = "NOPMD";
+ /**
+ * The number of threads.
+ */
int threads = 1;
+ /**
+ * The project reference.
+ */
private Project project;
+ /**
+ * Creates a new operation.
+ *
+ * The defaults are:
+ *
+ * - encoding={@code UTF-9}
+ * - incrementAnalysis={@code true}
+ * - reportFormat={@code text}
+ * - rulePriority={@code LOW}
+ * - suppressedMarker={@code NOPMD}
+ *
+ */
public PmdOperation() {
super();
}
+ /**
+ * Creates a new operation.
+ *
+ * The defaults are:
+ *
+ * - cache={@code build/pmd/pmd-cache}
+ * - encoding={@code UTF-9}
+ * - incrementAnalysis={@code true}
+ * - inputPaths={@code [src/main, src/test]}
+ * - reportFile={@code build/pmd/pmd-report-txt}
+ * - reportFormat={@code text}
+ * - rulePriority={@code LOW}
+ * - ruleSets={@code [rulesets/java/quickstart.xml]}
+ * - suppressedMarker={@code NOPMD}
+ *
+ */
public PmdOperation(Project project) {
super();
this.project = project;
@@ -80,6 +169,19 @@ public class PmdOperation extends AbstractOperation {
/**
* Adds a new rule set path.
+ *
+ * The built-in rule set paths are:
+ *
+ * - {@code rulesets/java/quickstart.xml}
+ * - {@code category/java/bestpractices.xml}
+ * - {@code category/java/codestyle.xml}
+ * - {@code category/java/design.xml}
+ * - {@code category/java/documentation.xml}
+ * - {@code category/java/errorprone.xml}
+ * - {@code category/java/multithreading.xml}
+ * - {@code category/java/performance.xml}
+ * - {@code category/java/security.xml}
+ *
*
* @see #ruleSets(String...)
*/
@@ -161,6 +263,9 @@ public class PmdOperation extends AbstractOperation {
return this;
}
+ /**
+ * Creates a new initialized configuration.
+ */
public PMDConfiguration initConfiguration(String commandName) {
PMDConfiguration config = new PMDConfiguration();
if (cache == null && project != null && incrementalAnalysis) {
@@ -214,6 +319,9 @@ public class PmdOperation extends AbstractOperation {
return this;
}
+ /**
+ * Performs the PMD analysis with the given config.
+ */
public int performPmdAnalysis(String commandName, PMDConfiguration config) throws RuntimeException {
var pmd = PmdAnalysis.create(config);
var report = pmd.performAnalysisAndCollectReport();
@@ -238,6 +346,17 @@ public class PmdOperation extends AbstractOperation {
LOGGER.warning(msg);
}
}
+ } else {
+ var rules = pmd.getRulesets();
+ if (rules.size() > 0) {
+ int count = 0;
+ for (var rule : rules) {
+ count += rule.getRules().size();
+ }
+ if (LOGGER.isLoggable(Level.INFO)) {
+ LOGGER.info(String.format("[%s] %d rules were checked.", commandName, count));
+ }
+ }
}
return numErrors;
}
@@ -252,6 +371,19 @@ public class PmdOperation extends AbstractOperation {
/**
* Sets the rule set path(s).
+ *
+ * The built-in rule set paths are:
+ *
+ * - {@code rulesets/java/quickstart.xml}
+ * - {@code category/java/bestpractices.xml}
+ * - {@code category/java/codestyle.xml}
+ * - {@code category/java/design.xml}
+ * - {@code category/java/documentation.xml}
+ * - {@code category/java/errorprone.xml}
+ * - {@code category/java/multithreading.xml}
+ * - {@code category/java/performance.xml}
+ * - {@code category/java/security.xml}
+ *
*/
public PmdOperation ruleSets(String... ruleSet) {
ruleSets.clear();
diff --git a/src/test/java/rife/bld/extension/PmdOperationTest.java b/src/test/java/rife/bld/extension/PmdOperationTest.java
index 2f0cb82..62f504e 100644
--- a/src/test/java/rife/bld/extension/PmdOperationTest.java
+++ b/src/test/java/rife/bld/extension/PmdOperationTest.java
@@ -56,6 +56,69 @@ public class PmdOperationTest {
.as("no errors").isEqualTo(0);
}
+ @Test
+ void testJavaQuickStart() {
+ var pmd = PMD_OPERATION.ruleSets("rulesets/java/quickstart.xml");
+ assertThat(pmd.performPmdAnalysis("test", pmd.initConfiguration("pmd")))
+ .as("no errors").isEqualTo(0);
+ }
+
+ @Test
+ void testJavaErrorProne() {
+ var pmd = PMD_OPERATION.ruleSets("category/java/errorprone.xml");
+ assertThat(pmd.performPmdAnalysis("test", pmd.initConfiguration("pmd")))
+ .as("no errors").isGreaterThan(0);
+ }
+
+ @Test
+ void testJavaCodeStyle() {
+ var pmd = PMD_OPERATION.ruleSets("category/java/codestyle.xml");
+ assertThat(pmd.performPmdAnalysis("test", pmd.initConfiguration("pmd")))
+ .as("no errors").isGreaterThan(0);
+ }
+
+ @Test
+ void testJavaDesign() {
+ var pmd = PMD_OPERATION.ruleSets("category/java/design.xml");
+ assertThat(pmd.performPmdAnalysis("test", pmd.initConfiguration("pmd")))
+ .as("no errors").isGreaterThan(0);
+ }
+
+ @Test
+ void testJavaDocumentation() {
+ var pmd = PMD_OPERATION.ruleSets("category/java/documentation.xml");
+ assertThat(pmd.performPmdAnalysis("test", pmd.initConfiguration("pmd")))
+ .as("no errors").isGreaterThan(0);
+ }
+
+ @Test
+ void testJavaBestPractices() {
+ var pmd = PMD_OPERATION.ruleSets("category/java/bestpractices.xml");
+ assertThat(pmd.performPmdAnalysis("test", pmd.initConfiguration("pmd")))
+ .as("no errors").isEqualTo(0);
+ }
+
+ @Test
+ void testJavaMultiThreading() {
+ var pmd = PMD_OPERATION.ruleSets("category/java/multithreading");
+ assertThat(pmd.performPmdAnalysis("test", pmd.initConfiguration("pmd")))
+ .as("no errors").isEqualTo(0);
+ }
+
+ @Test
+ void testJavaPerformance() {
+ var pmd = PMD_OPERATION.ruleSets("category/java/performance.xml");
+ assertThat(pmd.performPmdAnalysis("test", pmd.initConfiguration("pmd")))
+ .as("no errors").isEqualTo(0);
+ }
+
+ @Test
+ void testJavaSecurity() {
+ var pmd = PMD_OPERATION.ruleSets("category/java/security.xml");
+ assertThat(pmd.performPmdAnalysis("test", pmd.initConfiguration("pmd")))
+ .as("no errors").isEqualTo(0);
+ }
+
@Test
void testPmdOperation() {
assertThat(PMD_OPERATION.performPmdAnalysis("test", PMD_OPERATION.initConfiguration("pmd")))