From cbbf5a7f2d66debefbe3af8e53e31b947492dc62 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Fri, 14 Apr 2023 08:42:37 -0700 Subject: [PATCH] More javadocs and tests --- .idea/vcs.xml | 6 + .../java/rife/bld/extension/PmdOperation.java | 136 +++++++++++++++++- .../rife/bld/extension/PmdOperationTest.java | 63 ++++++++ 3 files changed, 203 insertions(+), 2 deletions(-) create mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/rife/bld/extension/PmdOperation.java b/src/main/java/rife/bld/extension/PmdOperation.java index 0884e6e..a9499dc 100644 --- a/src/main/java/rife/bld/extension/PmdOperation.java +++ b/src/main/java/rife/bld/extension/PmdOperation.java @@ -33,32 +33,121 @@ import java.util.Objects; import java.util.logging.Level; import java.util.logging.Logger; +/** + * Performs static code analysis with PMD. + * + * @author Erik C. Thauvin + * @since 1.0 + */ public class PmdOperation extends AbstractOperation { - private static final Logger LOGGER = Logger.getLogger(PmdOperation.class.getName()); - private static final String PMD_DIR = "pmd"; + /** + * The default rule set. + */ public static final String RULESET_DEFAULT = "rulesets/java/quickstart.xml"; + private static final Logger LOGGER = Logger.getLogger(PmdOperation.class.getName()); + private static final String PMD_DIR = "pmd"; + + /** + * The cache location. + */ Path cache; + /** + * The debug toggle. + */ boolean debug; + /** + * The encoding. + */ String encoding = "UTF-8"; + /** + * The fail on violation toggle. + */ boolean failOnViolation; + /** + * The path of the ignore file + */ Path ignoreFile; + /** + * The incremental analysis toggle. + */ boolean incrementalAnalysis = true; + /** + * The input paths (source) list. + */ List inputPaths = new ArrayList<>(); + /** + * The input URI. + */ URI inputUri; + /** + * The language version. + */ LanguageVersion languageVersion; + /** + * The path to the report page. + */ Path reportFile; + /** + * The report format. + */ String reportFormat = "text"; + /** + * The rule priority. + */ RulePriority rulePriority = RulePriority.LOW; + /** + * The rule sets list. + */ List 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")))