diff --git a/.github/workflows/bld.yml b/.github/workflows/bld.yml index 947f3c4..c0ba763 100644 --- a/.github/workflows/bld.yml +++ b/.github/workflows/bld.yml @@ -1,16 +1,14 @@ name: bld-ci -on: [ push, pull_request, workflow_dispatch ] +on: [push, pull_request, workflow_dispatch] jobs: build-bld-project: + runs-on: ubuntu-latest + strategy: matrix: - java-version: [ 17, 21, 24 ] - kotlin-version: [ 1.9.25, 2.0.21, 2.1.20 ] - os: [ ubuntu-latest, windows-latest, macos-latest ] - - runs-on: ${{ matrix.os }} + java-version: [17, 21, 22] steps: - name: Checkout source repository @@ -24,8 +22,11 @@ jobs: distribution: "zulu" java-version: ${{ matrix.java-version }} - - name: Download dependencies + - name: Grant execute permission for bld + run: chmod +x bld + + - name: Download the dependencies run: ./bld download - - name: Run tests - run: ./bld compile test \ No newline at end of file + - name: Run tests with bld + run: ./bld compile test diff --git a/.github/workflows/pages.yml b/.github/workflows/pages.yml index 508f6a5..bf43624 100644 --- a/.github/workflows/pages.yml +++ b/.github/workflows/pages.yml @@ -47,11 +47,11 @@ jobs: uses: actions/configure-pages@v3 - name: Upload artifact - uses: actions/upload-pages-artifact@v3 + uses: actions/upload-pages-artifact@v1 with: # Upload generated Javadocs repository path: "build/javadoc/" - name: Deploy to GitHub Pages id: deployment - uses: actions/deploy-pages@v4 + uses: actions/deploy-pages@v1 diff --git a/.idea/bld.xml b/.idea/bld.xml deleted file mode 100644 index 6600cee..0000000 --- a/.idea/bld.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/icon.svg b/.idea/icon.svg deleted file mode 100644 index 81220b4..0000000 --- a/.idea/icon.svg +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - diff --git a/.idea/libraries/bld.xml b/.idea/libraries/bld.xml index 153a060..a2969be 100644 --- a/.idea/libraries/bld.xml +++ b/.idea/libraries/bld.xml @@ -2,12 +2,12 @@ - + - + diff --git a/.idea/libraries/compile.xml b/.idea/libraries/compile.xml index 99cc0c0..9bd86aa 100644 --- a/.idea/libraries/compile.xml +++ b/.idea/libraries/compile.xml @@ -7,7 +7,7 @@ - - + + \ No newline at end of file diff --git a/.idea/libraries/runtime.xml b/.idea/libraries/runtime.xml index 56ddbf1..81feb0b 100644 --- a/.idea/libraries/runtime.xml +++ b/.idea/libraries/runtime.xml @@ -7,7 +7,7 @@ - - + + \ No newline at end of file diff --git a/.idea/libraries/test.xml b/.idea/libraries/test.xml index f72f7a3..13b6513 100644 --- a/.idea/libraries/test.xml +++ b/.idea/libraries/test.xml @@ -7,7 +7,7 @@ - - + + \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index ba429d0..d136e4d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -9,7 +9,7 @@ ], "java.configuration.updateBuildConfiguration": "automatic", "java.project.referencedLibraries": [ - "${HOME}/.bld/dist/bld-2.2.1.jar", + "${HOME}/.bld/dist/bld-1.9.1.jar", "lib/**/*.jar" ] } diff --git a/README.md b/README.md index dbf968f..35919d7 100755 --- a/README.md +++ b/README.md @@ -2,22 +2,14 @@ [![License](https://img.shields.io/badge/license-Apache%20License%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![Java](https://img.shields.io/badge/java-17%2B-blue)](https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html) -[![bld](https://img.shields.io/badge/2.2.1-FA9052?label=bld&labelColor=2392FF)](https://rife2.com/bld) +[![bld](https://img.shields.io/badge/1.9.1-FA9052?label=bld&labelColor=2392FF)](https://rife2.com/bld) [![Release](https://flat.badgen.net/maven/v/metadata-url/repo.rife2.com/releases/com/uwyn/rife2/bld-pmd/maven-metadata.xml?color=blue)](https://repo.rife2.com/#/releases/com/uwyn/rife2/bld-pmd) [![Snapshot](https://flat.badgen.net/maven/v/metadata-url/repo.rife2.com/snapshots/com/uwyn/rife2/bld-pmd/maven-metadata.xml?label=snapshot)](https://repo.rife2.com/#/snapshots/com/uwyn/rife2/bld-pmd) [![GitHub CI](https://github.com/rife2/bld-pmd/actions/workflows/bld.yml/badge.svg)](https://github.com/rife2/bld-pmd/actions/workflows/bld.yml) -To install the latest version, add the following to the `lib/bld/bld-wrapper.properties` file: +To install, please refer to the [extensions documentation](https://github.com/rife2/bld/wiki/Extensions). -```properties -bld.extension-pmd=com.uwyn.rife2:bld-pmd -``` - -For more information, please refer to the [extensions](https://github.com/rife2/bld/wiki/Extensions) documentation. - -## Check Source with PMD - -To check all source Codecode using the [Java Quickstart](https://docs.pmd-code.org/latest/pmd_rules_java.html) configuration, add the following to your build file: +To check all source code using the [Java Quickstart](https://docs.pmd-code.org/latest/pmd_rules_java.html) configuration, add the following to your build file: ```java @BuildCommand(summary = "Checks source code with PMD") diff --git a/config/pmd.xml b/config/pmd.xml index 2641880..3d3203c 100644 --- a/config/pmd.xml +++ b/config/pmd.xml @@ -7,9 +7,9 @@ + + - - diff --git a/lib/bld/bld-wrapper.jar b/lib/bld/bld-wrapper.jar index 58e9761..0ba02e9 100644 Binary files a/lib/bld/bld-wrapper.jar and b/lib/bld/bld-wrapper.jar differ diff --git a/lib/bld/bld-wrapper.properties b/lib/bld/bld-wrapper.properties index 4745e94..71ce346 100644 --- a/lib/bld/bld-wrapper.properties +++ b/lib/bld/bld-wrapper.properties @@ -2,4 +2,4 @@ bld.downloadExtensionJavadoc=false bld.downloadExtensionSources=true bld.downloadLocation= bld.repositories=MAVEN_LOCAL,MAVEN_CENTRAL,RIFE2_SNAPSHOTS,RIFE2_RELEASES -bld.version=2.2.1 +bld.version=1.9.1 diff --git a/src/bld/java/rife/bld/extension/PmdOperationBuild.java b/src/bld/java/rife/bld/extension/PmdOperationBuild.java index cbaa6ba..92253f0 100644 --- a/src/bld/java/rife/bld/extension/PmdOperationBuild.java +++ b/src/bld/java/rife/bld/extension/PmdOperationBuild.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2023-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,8 @@ import rife.bld.publish.PublishScm; import java.util.List; -import static rife.bld.dependencies.Repository.*; +import static rife.bld.dependencies.Repository.MAVEN_CENTRAL; +import static rife.bld.dependencies.Repository.RIFE2_RELEASES; import static rife.bld.dependencies.Scope.*; import static rife.bld.operations.JavadocOptions.DocLinkOption.NO_MISSING; @@ -31,25 +32,25 @@ public class PmdOperationBuild extends Project { public PmdOperationBuild() { pkg = "rife.bld.extension"; name = "bld-pmd"; - version = version(1, 2, 3); + version = version(1, 1, 0); javaRelease = 17; downloadSources = true; autoDownloadPurge = true; - - repositories = List.of(MAVEN_LOCAL, MAVEN_CENTRAL, RIFE2_RELEASES, RIFE2_SNAPSHOTS); + repositories = List.of(MAVEN_CENTRAL, RIFE2_RELEASES); - var pmd = version(7, 13, 0); + var pmd = version(7, 2, 0); scope(compile) - .include(dependency("com.uwyn.rife2", "bld", version(2, 2, 1))) + .include(dependency("com.uwyn.rife2", "bld", version(1, 9, 1))) .include(dependency("net.sourceforge.pmd", "pmd-java", pmd)); scope(runtime) - .include(dependency("org.slf4j", "slf4j-simple", version(2, 0, 17))); + .include(dependency("net.sourceforge.pmd", "pmd-java", pmd)) + .include(dependency("org.slf4j", "slf4j-simple", version(2, 0, 13))); scope(test) - .include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 12, 2))) - .include(dependency("org.junit.platform", "junit-platform-console-standalone", version(1, 12, 2))) - .include(dependency("org.assertj", "assertj-core", version(3, 27, 3))); + .include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 10, 2))) + .include(dependency("org.junit.platform", "junit-platform-console-standalone", version(1, 10, 2))) + .include(dependency("org.assertj", "assertj-core", version(3, 26, 0))); javadocOperation() .javadocOptions() @@ -60,26 +61,29 @@ public class PmdOperationBuild extends Project { .link("https://javadoc.io/doc/net.sourceforge.pmd/pmd-core/latest/"); publishOperation() + // .repository(MAVEN_LOCAL) .repository(version.isSnapshot() ? repository("rife2-snapshot") : repository("rife2")) - .repository(repository("github")) .info() .groupId("com.uwyn.rife2") .artifactId("bld-pmd") .description("bld Extension to Perform Static Code Analysis with PMD") .url("https://github.com/rife2/bld-pmd") - .developer(new PublishDeveloper() - .id("ethauvin").name("Erik C. Thauvin") - .email("erik@thauvin.net") - .url("https://erik.thauvin.net/") + .developer( + new PublishDeveloper() + .id("ethauvin").name("Erik C. Thauvin") + .email("erik@thauvin.net") + .url("https://erik.thauvin.net/") ) - .license(new PublishLicense() - .name("The Apache License, Version 2.0") - .url("https://www.apache.org/licenses/LICENSE-2.0.txt") + .license( + new PublishLicense() + .name("The Apache License, Version 2.0") + .url("https://www.apache.org/licenses/LICENSE-2.0.txt") ) - .scm(new PublishScm() - .connection("scm:git:https://github.com/rife2/bld-pmd.git") - .developerConnection("scm:git:git@github.com:rife2/bld-pmd.git") - .url("https://github.com/rife2/bld-pmd") + .scm( + new PublishScm() + .connection("scm:git:https://github.com/rife2/bld-pmd.git") + .developerConnection("scm:git:git@github.com:rife2/bld-pmd.git") + .url("https://github.com/rife2/bld-pmd") ) .signKey(property("sign.key")) .signPassphrase(property("sign.passphrase")); diff --git a/src/main/java/rife/bld/extension/PmdOperation.java b/src/main/java/rife/bld/extension/PmdOperation.java index 25b25c6..7bd5997 100644 --- a/src/main/java/rife/bld/extension/PmdOperation.java +++ b/src/main/java/rife/bld/extension/PmdOperation.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2023-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,7 +22,6 @@ import net.sourceforge.pmd.lang.LanguageVersion; import net.sourceforge.pmd.lang.rule.RulePriority; import rife.bld.BaseProject; import rife.bld.operations.AbstractOperation; -import rife.bld.operations.exceptions.ExitStatusException; import java.io.File; import java.net.URI; @@ -49,10 +48,6 @@ public class PmdOperation extends AbstractOperation { public static final String RULE_SET_DEFAULT = "rulesets/java/quickstart.xml"; private static final Logger LOGGER = Logger.getLogger(PmdOperation.class.getName()); private static final String PMD_DIR = "pmd"; - /** - * The list of paths to exclude. - */ - private final Collection excludes_ = new ArrayList<>(); /** * The input paths (source) list. */ @@ -73,10 +68,6 @@ public class PmdOperation extends AbstractOperation { * The encoding. */ private Charset encoding_ = StandardCharsets.UTF_8; - /** - * The fail on error toggle. - */ - private boolean failOnError_ = true; /** * The fail on violation toggle. */ @@ -105,10 +96,6 @@ public class PmdOperation extends AbstractOperation { * The default language version(s). */ private Collection languageVersions_ = new ArrayList<>(); - /** - * The classpath to prepend. - */ - private String prependClasspath_; /** * The project reference. */ @@ -142,146 +129,6 @@ public class PmdOperation extends AbstractOperation { */ private int threads_ = 1; - /** - * Adds paths to exclude from the analysis. - * - * @param excludes one or more paths to exclude - * @return this operation - * @see #excludes(Path...) - * @since 1.2.0 - */ - public PmdOperation addExcludes(Path... excludes) { - return addExcludes(List.of(excludes)); - } - - /** - * Adds paths to exclude from the analysis. - * - * @param excludes paths to exclude - * @return this operation - * @see #excludes(Collection) - * @since 1.2.0 - */ - public PmdOperation addExcludes(Collection excludes) { - excludes_.addAll(excludes); - return this; - } - - /** - * Adds paths to exclude from the analysis. - * - * @param excludes one or more paths to exclude - * @return this operation - * @see #excludesFiles(Collection) - * @since 1.2.0 - */ - public PmdOperation addExcludesFiles(Collection excludes) { - return addExcludes(excludes.stream().map(File::toPath).toList()); - } - - /** - * Adds paths to exclude from the analysis. - * - * @param excludes one or more paths to exclude - * @return this operation - * @see #excludesFiles(File...) - * @since 1.2.0 - */ - public PmdOperation addExcludesFiles(File... excludes) { - return addExcludesFiles(List.of(excludes)); - } - - /** - * Adds paths to exclude from the analysis. - * - * @param excludes one or more paths to exclude - * @return this operation - * @see #excludesStrings(Collection) - * @since 1.2.0 - */ - public PmdOperation addExcludesStrings(Collection excludes) { - return addExcludes(excludes.stream().map(Paths::get).toList()); - } - - /** - * Adds paths to exclude from the analysis. - * - * @param excludes one or more paths to exclude - * @return this operation - * @see #excludesStrings(String...) - * @since 1.2.0 - */ - public PmdOperation addExcludesStrings(String... excludes) { - return addExcludesStrings(List.of(excludes)); - } - - /** - * Adds paths to source files, or directories containing source files to analyze.\ - * - * @param inputPath one or more paths - * @return this operation - * @see #inputPaths(Path...) - */ - public PmdOperation addInputPaths(Path... inputPath) { - return inputPaths(List.of(inputPath)); - } - - /** - * Adds paths to source files, or directories containing source files to analyze. - * - * @param inputPath one or more paths - * @return this operation - * @see #inputPaths(File...) - */ - public PmdOperation addInputPaths(File... inputPath) { - return addInputPathsFiles(List.of(inputPath)); - } - - /** - * Adds paths to source files, or directories containing source files to analyze. - * - * @param inputPath one or more paths - * @return this operation - * @see #inputPaths(String...) - */ - public PmdOperation addInputPaths(String... inputPath) { - return addInputPathsStrings(List.of(inputPath)); - } - - /** - * Adds paths to source files, or directories containing source files to analyze. - * - * @param inputPath a collection of input paths - * @return this operation - * @see #inputPaths(Collection) - */ - public PmdOperation addInputPaths(Collection inputPath) { - inputPaths_.addAll(inputPath); - return this; - } - - /** - * Adds paths to source files, or directories containing source files to analyze. - * - * @param inputPath a collection of input paths - * @return this operation - * @see #inputPathsFiles(Collection) - */ - public PmdOperation addInputPathsFiles(Collection inputPath) { - return addInputPaths(inputPath.stream().map(File::toPath).toList()); - } - - /** - * Adds paths to source files, or directories containing source files to analyze. - * - * @param inputPath a collection of input paths - * @return this operation - * @see #inputPathsStrings(Collection) - */ - public PmdOperation addInputPathsStrings(Collection inputPath) { - return addInputPaths(inputPath.stream().map(Paths::get).toList()); - } - /** * Adds new rule set paths. *

@@ -303,7 +150,8 @@ public class PmdOperation extends AbstractOperation { * @see #ruleSets(String...) */ public PmdOperation addRuleSet(String... ruleSet) { - return addRuleSet(List.of(ruleSet)); + ruleSets_.addAll(List.of(ruleSet)); + return this; } /** @@ -322,9 +170,9 @@ public class PmdOperation extends AbstractOperation { *

  • {@code category/java/security.xml}
  • * * - * @param ruleSet a collection of rule set paths + * @param ruleSet one or more rule set * @return this operation - * @see #ruleSets(Collection) + * @see #ruleSets(Collection */ public PmdOperation addRuleSet(Collection ruleSet) { ruleSets_.addAll(ruleSet); @@ -339,20 +187,6 @@ public class PmdOperation extends AbstractOperation { return this; } - /** - * Sets the location of the cache file for incremental analysis. - */ - public PmdOperation cache(File cache) { - return cache(cache.toPath()); - } - - /** - * Sets the location of the cache file for incremental analysis. - */ - public PmdOperation cache(String cache) { - return cache(Path.of(cache)); - } - /** * Sets the default language version to be used for all input files. * @@ -360,13 +194,14 @@ public class PmdOperation extends AbstractOperation { * @return this operation */ public PmdOperation defaultLanguageVersions(LanguageVersion... languageVersion) { - return languageVersions(List.of(languageVersion)); + languageVersions_.addAll(List.of(languageVersion)); + return this; } /** * Sets the default language version to be used for all input files. * - * @param languageVersion a collection language versions + * @param languageVersion the language versions * @return this operation */ public PmdOperation defaultLanguageVersions(Collection languageVersion) { @@ -380,7 +215,8 @@ public class PmdOperation extends AbstractOperation { *

    The valid values are the standard character sets of {@link java.nio.charset.Charset Charset}.

    */ public PmdOperation encoding(String encoding) { - return encoding(Charset.forName(encoding)); + encoding_ = Charset.forName(encoding); + return this; } /** @@ -394,100 +230,13 @@ public class PmdOperation extends AbstractOperation { return this; } - /** - * Sets paths to exclude from the analysis. - * - * @param excludes one or more paths to exclude - * @return this operation - * @see #addExcludes(Path...) - */ - public PmdOperation excludes(Path... excludes) { - excludes(List.of(excludes)); - return this; - } - - /** - * Sets paths to exclude from the analysis. - * - * @param excludes paths to exclude - * @return this operation - * @see #addExcludes(Collection) - */ - public PmdOperation excludes(Collection excludes) { - excludes_.clear(); - excludes_.addAll(excludes); - return this; - } - - /** - * Returns the paths to exclude from the analysis. - * - * @return the exclude paths - */ - public Collection excludes() { - return excludes_; - } - - /** - * Sets paths to exclude from the analysis. - * - * @param excludes one or more paths to exclude - * @return this operation - * @see #excludesFiles(Collection) - * @since 1.2.0 - */ - public PmdOperation excludesFiles(Collection excludes) { - excludes(excludes.stream().map(File::toPath).toList()); - return this; - } - - /** - * Sets paths to exclude from the analysis. - * - * @param excludes one or more paths to exclude - * @return this operation - * @see #excludesFiles(File...) - * @since 1.2.0 - */ - public PmdOperation excludesFiles(File... excludes) { - return excludesFiles(List.of(excludes)); - } - - /** - * Sets paths to exclude from the analysis. - * - * @param excludes one or more paths to exclude - * @return this operation - * @see #excludesStrings(Collection) - * @since 1.2.0 - */ - public PmdOperation excludesStrings(Collection excludes) { - excludes(excludes.stream().map(Paths::get).toList()); - return this; - } - - /** - * Sets paths to exclude from the analysis. - * - * @param excludes one or more paths to exclude - * @return this operation - * @see #excludesStrings(String...) - * @since 1.2.0 - */ - public PmdOperation excludesStrings(String... excludes) { - return excludesStrings(List.of(excludes)); - } - /** * Performs the PMD code analysis operation. */ @Override - public void execute() throws Exception { + public void execute() { if (project_ == null) { - if (LOGGER.isLoggable(Level.SEVERE) && !silent()) { - LOGGER.log(Level.SEVERE, "A project is required to run this operation."); - } - throw new ExitStatusException(ExitStatusException.EXIT_FAILURE); + throw new IllegalArgumentException("ERROR: project required."); } var commandName = project_.getCurrentCommandName(); @@ -495,28 +244,7 @@ public class PmdOperation extends AbstractOperation { } /** - * Sets whether the build will exit on recoverable errors. - *

    - * Default is: {@code true} - *

    - * Note: If only violations are found, see {@link #failOnViolation(boolean) failOnViolation} - * - * @param failOnError whether to exit and fail the build if recoverable errors occurred - * @return this operation - * @see #failOnViolation(boolean) - */ - public PmdOperation failOnError(boolean failOnError) { - failOnError_ = failOnError; - return this; - } - - /** - * Sets whether the build will continue on violations. - *

    - * Note: If additionally recoverable errors occurred, see {@link #failOnError(boolean) failOnError} - * - * @param failOnViolation whether to exit and fail the build if violations are found - * @return this operation + * Sets whether the build will continue on warnings. */ public PmdOperation failOnViolation(boolean failOnViolation) { failOnViolation_ = failOnViolation; @@ -580,7 +308,8 @@ public class PmdOperation extends AbstractOperation { * @return this operation */ public PmdOperation ignoreFile(File ignoreFile) { - return ignoreFile(ignoreFile.toPath()); + ignoreFile_ = ignoreFile.toPath(); + return this; } /** @@ -590,7 +319,8 @@ public class PmdOperation extends AbstractOperation { * @return this operation */ public PmdOperation ignoreFile(String ignoreFile) { - return ignoreFile(Path.of(ignoreFile)); + ignoreFile_ = Paths.get(ignoreFile); + return this; } /** @@ -620,17 +350,8 @@ public class PmdOperation extends AbstractOperation { * @return this operation */ public PMDConfiguration initConfiguration(String commandName) { - var config = new PMDConfiguration(); + PMDConfiguration config = new PMDConfiguration(); - // addRelativizeRoots - config.addRelativizeRoots(relativizeRoots_.stream().toList()); - - // prependAuxClasspath - if (prependClasspath_ != null) { - config.prependAuxClasspath(prependClasspath_); - } - - // setAnalysisCacheLocation if (cache_ == null && project_ != null && incrementalAnalysis_) { config.setAnalysisCacheLocation( Paths.get(project_.buildDirectory().getPath(), PMD_DIR, PMD_DIR + "-cache").toFile().getAbsolutePath()); @@ -638,50 +359,37 @@ public class PmdOperation extends AbstractOperation { config.setAnalysisCacheLocation(cache_.toFile().getAbsolutePath()); } - // setDefaultLanguageVersions + config.setFailOnViolation(failOnViolation_); + if (languageVersions_ != null) { config.setDefaultLanguageVersions(languageVersions_.stream().toList()); } - // setExcludes - if (!excludes_.isEmpty()) { - config.setExcludes(excludes_.stream().toList()); - } - - // setFailOnError - config.setFailOnError(failOnError_); - // setFailOnViolation - config.setFailOnViolation(failOnViolation_); - - // setForceLanguageVersion if (forcedLanguageVersion_ != null) { config.setForceLanguageVersion(forcedLanguageVersion_); } - // setIgnoreFilePath if (ignoreFile_ != null) { config.setIgnoreFilePath(ignoreFile_); } - // setIgnoreIncrementalAnalysis config.setIgnoreIncrementalAnalysis(!incrementalAnalysis_); - // setInputPathList if (inputPaths_.isEmpty()) { throw new IllegalArgumentException(commandName + ": InputPaths required."); } else { config.setInputPathList(inputPaths_.stream().toList()); } + if (reportProperties_ != null) { + config.setReportProperties(reportProperties_); + } - // setInputUri if (inputUri_ != null) { config.setInputUri(inputUri_); } - // setMinimumPriority config.setMinimumPriority(rulePriority_); - // setReportFile if (project_ != null) { config.setReportFile(Objects.requireNonNullElseGet(reportFile_, () -> Paths.get(project_.buildDirectory().getPath(), @@ -690,25 +398,12 @@ public class PmdOperation extends AbstractOperation { config.setReportFile(reportFile_); } - // setReportFormat + config.addRelativizeRoots(relativizeRoots_.stream().toList()); config.setReportFormat(reportFormat_); - - // setReportProperties - if (reportProperties_ != null) { - config.setReportProperties(reportProperties_); - } - - // setRuleSets config.setRuleSets(ruleSets_.stream().toList()); - - // setShowSuppressedViolations config.setShowSuppressedViolations(showSuppressed_); - // setSourceEncoding config.setSourceEncoding(encoding_); - // setSuppressMarker config.setSuppressMarker(suppressedMarker_); - - // setThreads config.setThreads(threads_); return config; @@ -719,10 +414,12 @@ public class PmdOperation extends AbstractOperation { * * @param inputPath one or more paths * @return this operation - * @see #addInputPaths(Path...) + * @see #addInputPaths(Path...) */ public PmdOperation inputPaths(Path... inputPath) { - return inputPaths(List.of(inputPath)); + inputPaths_.clear(); + inputPaths_.addAll(List.of(inputPath)); + return this; } /** @@ -732,39 +429,88 @@ public class PmdOperation extends AbstractOperation { * * @param inputPath one or more paths * @return this operation - * @see #addInputPaths(File...) + * @see #addInputPaths(File...) */ public PmdOperation inputPaths(File... inputPath) { - return inputPathsFiles(List.of(inputPath)); + inputPaths_.clear(); + inputPaths_.addAll(Arrays.stream(inputPath).map(File::toPath).toList()); + return this; } /** * Sets paths to source files, or directories containing source files to analyze. *

    * Previous entries are disregarded. - * * @param inputPath one or more paths * @return this operation - * @see #addInputPaths(String...) + * @see #addInputPaths(String...) */ public PmdOperation inputPaths(String... inputPath) { - return inputPathsStrings(List.of(inputPath)); + inputPaths_.clear(); + inputPaths_.addAll(Arrays.stream(inputPath).map(Paths::get).toList()); + return this; } /** * Sets paths to source files, or directories containing source files to analyze. *

    * Previous entries are disregarded. - * - * @param inputPath a collection of input paths + * @param inputPath the input paths * @return this operation - * @see #addInputPaths(Collection) + * @see #addInputPaths(Collection) */ public PmdOperation inputPaths(Collection inputPath) { inputPaths_.clear(); inputPaths_.addAll(inputPath); return this; } + /** + * Adds paths to source files, or directories containing source files to analyze.\ + * + * @param inputPath one or more paths + * @return this operation + * @see #inputPaths(Path...) + */ + public PmdOperation addInputPaths(Path... inputPath) { + inputPaths_.addAll(List.of(inputPath)); + return this; + } + + /** + * Adds paths to source files, or directories containing source files to analyze. + * + * @param inputPath one or more paths + * @return this operation + * @see #inputPaths(File...) + */ + public PmdOperation addInputPaths(File... inputPath) { + inputPaths_.addAll(Arrays.stream(inputPath).map(File::toPath).toList()); + return this; + } + + /** + * Adds paths to source files, or directories containing source files to analyze. + * + * @param inputPath one or more paths + * @return this operation + * @see #addInputPaths(String...) + */ + public PmdOperation addInputPaths(String... inputPath) { + inputPaths_.addAll(Arrays.stream(inputPath).map(Paths::get).toList()); + return this; + } + + /** + * Adds paths to source files, or directories containing source files to analyze. + * + * @param inputPath the input paths + * @return this operation + * @see #inputPaths(Collection) + */ + public PmdOperation addInputPaths(Collection inputPath) { + inputPaths_.addAll(inputPath); + return this; + } /** * Returns paths to source files, or directories containing source files to analyze. @@ -775,32 +521,6 @@ public class PmdOperation extends AbstractOperation { return inputPaths_; } - /** - * Sets paths to source files, or directories containing source files to analyze. - *

    - * Previous entries are disregarded. - * - * @param inputPath a collection of input paths - * @return this operation - * @see #addInputPathsFiles(Collection) - */ - public PmdOperation inputPathsFiles(Collection inputPath) { - return inputPaths(inputPath.stream().map(File::toPath).toList()); - } - - /** - * Sets paths to source files, or directories containing source files to analyze. - *

    - * Previous entries are disregarded. - * - * @param inputPath a collection of input paths - * @return this operation - * @see #addInputPathsStrings(Collection) - */ - public PmdOperation inputPathsStrings(Collection inputPath) { - return inputPaths(inputPath.stream().map(Paths::get).toList()); - } - /** * Sets the default language versions. * @@ -808,13 +528,14 @@ public class PmdOperation extends AbstractOperation { * @return this operation */ public PmdOperation languageVersions(LanguageVersion... languageVersion) { - return languageVersions(List.of(languageVersion)); + languageVersions_.addAll(List.of(languageVersion)); + return this; } /** * Sets the default language versions. * - * @param languageVersions a collection language versions + * @param languageVersions the language versions * @return this operation */ public PmdOperation languageVersions(Collection languageVersions) { @@ -846,22 +567,21 @@ public class PmdOperation extends AbstractOperation { * * @param commandName the command name * @param config the configuration - * @return the number of violations - * @throws ExitStatusException if an error occurs + * @return the number of errors + * @throws RuntimeException if an error occurs */ @SuppressWarnings({"PMD.CloseResource", "PMD.AvoidInstantiatingObjectsInLoops"}) - public int performPmdAnalysis(String commandName, PMDConfiguration config) throws ExitStatusException { + public int performPmdAnalysis(String commandName, PMDConfiguration config) throws RuntimeException { var pmd = PmdAnalysis.create(config); var report = pmd.performAnalysisAndCollectReport(); - if (LOGGER.isLoggable(Level.INFO) && !silent()) { + if (LOGGER.isLoggable(Level.INFO)) { LOGGER.log(Level.INFO, "[{0}] inputPaths{1}", new Object[]{commandName, inputPaths_}); LOGGER.log(Level.INFO, "[{0}] ruleSets{1}", new Object[]{commandName, ruleSets_}); } - - var numViolations = report.getViolations().size(); - if (numViolations > 0) { + var numErrors = report.getViolations().size(); + if (numErrors > 0) { for (var v : report.getViolations()) { - if (LOGGER.isLoggable(Level.WARNING) && !silent()) { + if (LOGGER.isLoggable(Level.WARNING)) { final String msg; if (includeLineNumber_) { msg = "[{0}] {1}:{2}\n\t{3} ({4})\n\t\t--> {5}"; @@ -881,18 +601,15 @@ public class PmdOperation extends AbstractOperation { } var violations = String.format( - "[%s] %d rule violations were found. See the report at: %s", commandName, numViolations, + "[%s] %d rule violations were found. See the report at: %s", commandName, numErrors, config.getReportFilePath().toUri()); if (config.isFailOnViolation()) { - if (LOGGER.isLoggable(Level.SEVERE) && !silent()) { - LOGGER.log(Level.SEVERE, violations); + throw new RuntimeException(violations); // NOPMD + } else { + if (LOGGER.isLoggable(Level.WARNING)) { + LOGGER.warning(violations); } - throw new ExitStatusException(ExitStatusException.EXIT_FAILURE); - } else if (LOGGER.isLoggable(Level.WARNING) && !silent()) { - LOGGER.warning(violations); } - } else if (pmd.getReporter().numErrors() > 0 && failOnError_) { - throw new ExitStatusException(ExitStatusException.EXIT_FAILURE); } else { var rules = pmd.getRulesets(); if (!rules.isEmpty()) { @@ -900,78 +617,52 @@ public class PmdOperation extends AbstractOperation { for (var rule : rules) { count += rule.getRules().size(); } - if (LOGGER.isLoggable(Level.INFO) && !silent()) { + if (LOGGER.isLoggable(Level.INFO)) { LOGGER.info(String.format("[%s] %d rules were checked.", commandName, count)); } } } - return numViolations; + return numErrors; } /** - * Prepend the specified classpath like string to the current ClassLoader of the configuration. If no ClassLoader - * is currently configured, the ClassLoader used to load the PMDConfiguration class will be used as the parent - * ClassLoader of the created ClassLoader. - *

    - * If the classpath String looks like a URL to a file (i.e. starts with {@code file://}) the file will be read with - * each line representing an entry on the classpath. + * Adds several paths to shorten paths that are output in the report. * - * @param classpath one or more classpath - * @return this operation + * @param relativeRoot one or more relative root paths + * @return this operations */ - public PmdOperation prependAuxClasspath(String... classpath) { - prependClasspath_ = String.join(File.pathSeparator, classpath); + public PmdOperation relativizeRoots(Path... relativeRoot) { + relativizeRoots_.addAll(List.of(relativeRoot)); return this; } - /** - * Returns the prepended classpath. - * - * @return the classpath - */ - public String prependAuxClasspath() { - return prependClasspath_; - } - /** * Adds several paths to shorten paths that are output in the report. * * @param relativeRoot one or more relative root paths - * @return this operation - * @see #relativizeRoots(Collection) - */ - public PmdOperation relativizeRoots(Path... relativeRoot) { - return relativizeRoots(List.of(relativeRoot)); - } - - /** - * Adds several paths to shorten paths that are output in the report. - * - * @param relativeRoot one or more relative root paths - * @return this operation - * @see #relativizeRootsFiles(Collection) + * @return this operations */ public PmdOperation relativizeRoots(File... relativeRoot) { - return relativizeRootsFiles(List.of(relativeRoot)); + relativizeRoots_.addAll(Arrays.stream(relativeRoot).map(File::toPath).toList()); + return this; } /** * Adds several paths to shorten paths that are output in the report. * * @param relativeRoot one or more relative root paths - * @return this operation - * @see #relativizeRootsStrings(Collection) + * @return this operations */ public PmdOperation relativizeRoots(String... relativeRoot) { - return relativizeRootsStrings(List.of(relativeRoot)); + relativizeRoots_.addAll(Arrays.stream(relativeRoot).map(Paths::get).toList()); + return this; } /** * Adds several paths to shorten paths that are output in the report. * - * @param relativeRoot a collection of relative root paths - * @return this operation - * @see #relativizeRoots(Path...) + * @param relativeRoot the relative root paths + * @return this operations */ public PmdOperation relativizeRoots(Collection relativeRoot) { relativizeRoots_.addAll(relativeRoot); @@ -987,28 +678,6 @@ public class PmdOperation extends AbstractOperation { return relativizeRoots_; } - /** - * Adds several paths to shorten paths that are output in the report. - * - * @param relativeRoot a collection of relative root paths - * @return this operation - * @see #relativizeRoots(File...) - */ - public PmdOperation relativizeRootsFiles(Collection relativeRoot) { - return relativizeRoots(relativeRoot.stream().map(File::toPath).toList()); - } - - /** - * Adds several paths to shorten paths that are output in the report. - * - * @param relativeRoot a collection of relative root paths - * @return this operation - * @see #relativizeRoots(String...) - */ - public PmdOperation relativizeRootsStrings(Collection relativeRoot) { - return relativizeRoots(relativeRoot.stream().map(Paths::get).toList()); - } - /** * Sets the path to the report page. * @@ -1027,7 +696,8 @@ public class PmdOperation extends AbstractOperation { * @return this operation */ public PmdOperation reportFile(File reportFile) { - return reportFile(reportFile.toPath()); + reportFile_ = reportFile.toPath(); + return this; } /** @@ -1037,7 +707,8 @@ public class PmdOperation extends AbstractOperation { * @return this operation */ public PmdOperation reportFile(String reportFile) { - return reportFile(Paths.get(reportFile)); + reportFile_ = Paths.get(reportFile); + return this; } /** @@ -1092,7 +763,9 @@ public class PmdOperation extends AbstractOperation { * @see #addRuleSet(String...) */ public PmdOperation ruleSets(String... ruleSet) { - return ruleSets(List.of(ruleSet)); + ruleSets_.clear(); + ruleSets_.addAll(List.of(ruleSet)); + return this; } /** @@ -1111,7 +784,7 @@ public class PmdOperation extends AbstractOperation { *

  • {@code category/java/security.xml}
  • * * - * @param ruleSet a collection of rule set paths + * @param ruleSet one or more rule set * @return this operation * @see #addRuleSet(Collection) */ diff --git a/src/test/java/rife/bld/extension/PmdOperationTest.java b/src/test/java/rife/bld/extension/PmdOperationTest.java index 77321d6..6cb5dce 100644 --- a/src/test/java/rife/bld/extension/PmdOperationTest.java +++ b/src/test/java/rife/bld/extension/PmdOperationTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2023-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,10 +19,8 @@ package rife.bld.extension; import net.sourceforge.pmd.PMDConfiguration; import net.sourceforge.pmd.lang.LanguageRegistry; import net.sourceforge.pmd.lang.rule.RulePriority; -import org.assertj.core.api.AutoCloseableSoftAssertions; import org.junit.jupiter.api.Test; import rife.bld.BaseProject; -import rife.bld.operations.exceptions.ExitStatusException; import java.io.File; import java.io.FileNotFoundException; @@ -47,7 +45,6 @@ import static org.assertj.core.api.Assertions.assertThatCode; * @since 1.0 */ class PmdOperationTest { - static final String BAR = "bar"; static final String CATEGORY_FOO = "category/foo.xml"; static final Path CODE_STYLE_SAMPLE = Path.of("src/test/resources/java/CodeStyle.java"); static final String CODE_STYLE_XML = "category/java/codestyle.xml"; @@ -56,11 +53,6 @@ class PmdOperationTest { static final String DOCUMENTATION_XML = "category/java/documentation.xml"; static final Path ERROR_PRONE_SAMPLE = Path.of("src/test/resources/java/ErrorProne.java"); static final String ERROR_PRONE_XML = "category/java/errorprone.xml"; - static final File FILE_BAR = new File(BAR); - static final String FOO = "foo"; - static final File FILE_FOO = new File(FOO); - static final Path PATH_BAR = Path.of(BAR); - static final Path PATH_FOO = Path.of(FOO); static final String PERFORMANCE_XML = "category/java/performance.xml"; static final String SECURITY_XML = "category/java/security.xml"; static final String TEST = "test"; @@ -68,46 +60,13 @@ class PmdOperationTest { PmdOperation newPmdOperation() { return new PmdOperation() .inputPaths(Path.of("src/main"), Path.of("src/test")) - .cache("build/" + COMMAND_NAME + "/pmd-cache") + .cache(Paths.get("build", COMMAND_NAME, "pmd-cache")) .failOnViolation(false) .reportFile(Paths.get("build", COMMAND_NAME, "pmd-test-report.txt")); } @Test - void testAddExcludes() { - var pmd = newPmdOperation().ruleSets(CATEGORY_FOO).addExcludes(PATH_FOO); - var config = pmd.initConfiguration(COMMAND_NAME); - assertThat(config.getExcludes()).containsExactly(PATH_FOO); - - pmd = pmd.addExcludes(PATH_BAR); - config = pmd.initConfiguration(COMMAND_NAME); - assertThat(config.getExcludes()).containsExactly(PATH_FOO, PATH_BAR); - } - - @Test - void testAddExcludesFiles() { - var pmd = newPmdOperation().ruleSets(CATEGORY_FOO).addExcludesFiles(FILE_FOO); - var config = pmd.initConfiguration(COMMAND_NAME); - assertThat(config.getExcludes()).containsExactly(FILE_FOO.toPath()); - - pmd = pmd.addExcludesFiles(FILE_BAR); - config = pmd.initConfiguration(COMMAND_NAME); - assertThat(config.getExcludes()).containsExactly(FILE_FOO.toPath(), FILE_BAR.toPath()); - } - - @Test - void testAddExcludesStrings() { - var pmd = newPmdOperation().ruleSets(CATEGORY_FOO).addExcludesStrings(FOO); - var config = pmd.initConfiguration(COMMAND_NAME); - assertThat(config.getExcludes()).containsExactly(PATH_FOO); - - pmd = pmd.addExcludesStrings(BAR); - config = pmd.initConfiguration(COMMAND_NAME); - assertThat(config.getExcludes()).containsExactly(PATH_FOO, PATH_BAR); - } - - @Test - void testAddInputPaths() throws ExitStatusException { + void testAddInputPath() { var project = new BaseProject(); var pmd = new PmdOperation().fromProject(project); @@ -117,49 +76,30 @@ class PmdOperationTest { var err = pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)); pmd.inputPaths().clear(); - pmd = pmd.addInputPaths(project.srcMainDirectory(), project.srcTestDirectory()); + pmd.addInputPaths(project.srcMainDirectory()); - assertThat(pmd.inputPaths()).as("File...").containsExactly(project.srcMainDirectory().toPath(), + assertThat(pmd.inputPaths()).as("main").containsExactly(project.srcMainDirectory().toPath()); + + pmd.inputPaths().clear(); + pmd.addInputPaths(project.srcMainDirectory().toPath(), project.srcTestDirectory().toPath()); + + assertThat(pmd.inputPaths()).as("toPath(main, test)").containsExactly(project.srcMainDirectory().toPath(), project.srcTestDirectory().toPath()); pmd.inputPaths().clear(); - pmd = pmd.addInputPathsFiles(List.of(project.srcMainDirectory(), project.srcTestDirectory())); + pmd.addInputPaths(List.of(project.srcMainDirectory().toPath(), project.srcTestDirectory().toPath())); - assertThat(pmd.inputPaths()).as("List(File...)") - .containsExactly(project.srcMainDirectory().toPath(), project.srcTestDirectory().toPath()); + assertThat(pmd.inputPaths()).as("List(main, test)").containsExactly( + project.srcMainDirectory().toPath(), + project.srcTestDirectory().toPath()); - pmd.inputPaths().clear(); - pmd = pmd.addInputPaths(project.srcMainDirectory().getAbsolutePath(), - project.srcTestDirectory().getAbsolutePath()); - - assertThat(pmd.inputPaths()).as("String...") - .containsExactly(project.srcMainDirectory().toPath(), project.srcTestDirectory().toPath()); - - pmd.inputPaths().clear(); - pmd = pmd.addInputPathsStrings( - List.of(project.srcMainDirectory().getAbsolutePath(), project.srcTestDirectory().getAbsolutePath())); - - assertThat(pmd.inputPaths()).as("List(String...)") - .containsExactly(project.srcMainDirectory().toPath(), project.srcTestDirectory().toPath()); - - pmd.inputPaths().clear(); - pmd = pmd.addInputPaths(project.srcMainDirectory().toPath(), project.srcTestDirectory().toPath()); - - assertThat(pmd.inputPaths()).as("Path...") - .containsExactly(project.srcMainDirectory().toPath(), project.srcTestDirectory().toPath()); - - pmd.inputPaths().clear(); - pmd = pmd.addInputPaths(List.of(project.srcMainDirectory().toPath(), project.srcTestDirectory().toPath())); - - assertThat(pmd.inputPaths()).as("List(Path)") - .containsExactly(project.srcMainDirectory().toPath(), project.srcTestDirectory().toPath()); assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))) .isGreaterThan(0).isEqualTo(err); } @Test - void testAddRuleSets() throws ExitStatusException { + void testAddRuleSets() { var pmd = new PmdOperation().fromProject(new BaseProject()); assertThat(pmd.ruleSets()).containsExactly(PmdOperation.RULE_SET_DEFAULT); @@ -182,12 +122,9 @@ class PmdOperationTest { } @Test - void testCache() throws ExitStatusException { + void testCache() { var cache = Path.of("build/pmd/temp-cache"); - var pmd = newPmdOperation() - .ruleSets(CODE_STYLE_XML) - .inputPaths(List.of(CODE_STYLE_SAMPLE)) - .cache(cache); + var pmd = newPmdOperation().ruleSets(CODE_STYLE_XML).inputPaths(List.of(CODE_STYLE_SAMPLE)).cache(cache); assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isGreaterThan(0); var f = cache.toFile(); assertThat(f.exists()).as("file exits").isTrue(); @@ -206,60 +143,7 @@ class PmdOperationTest { } @Test - void testExcludes() { - var foz = Path.of("foz/baz"); - var baz = Path.of("baz/foz"); - - var pmd = newPmdOperation().ruleSets(CATEGORY_FOO).excludes(PATH_FOO, PATH_BAR); - var config = pmd.initConfiguration(COMMAND_NAME); - assertThat(pmd.excludes()).containsExactly(List.of(PATH_FOO, PATH_BAR).toArray(new Path[0])); - assertThat(config.getExcludes()).containsExactly(List.of(PATH_FOO, PATH_BAR).toArray(new Path[0])); - - var excludes = List.of(List.of(PATH_FOO, PATH_BAR), List.of(foz, baz)); - for (var exclude : excludes) { - pmd = newPmdOperation().ruleSets(CATEGORY_FOO).excludes(exclude); - config = pmd.initConfiguration(COMMAND_NAME); - assertThat(config.getExcludes()).containsExactly(exclude.toArray(new Path[0])); - } - } - - @Test - void testExcludesFiles() { - var foz = new File("foz"); - var baz = new File("baz"); - - var pmd = newPmdOperation().ruleSets(CATEGORY_FOO).excludesFiles(FILE_FOO, FILE_BAR); - var config = pmd.initConfiguration(COMMAND_NAME); - assertThat(config.getExcludes()).containsExactly(FILE_FOO.toPath(), FILE_BAR.toPath()); - - var excludes = List.of(List.of(FILE_FOO, FILE_BAR), List.of(foz, baz)); - for (var exclude : excludes) { - pmd = newPmdOperation().ruleSets(CATEGORY_FOO).excludesFiles(exclude); - config = pmd.initConfiguration(COMMAND_NAME); - assertThat(config.getExcludes()).containsExactly(exclude.stream().map(File::toPath).toArray(Path[]::new)); - } - } - - @Test - void testExcludesStrings() { - var foz = "foz"; - var baz = "baz"; - - var pmd = newPmdOperation().ruleSets(CATEGORY_FOO).excludesStrings(FOO, BAR); - var config = pmd.initConfiguration(COMMAND_NAME); - assertThat(pmd.excludes()).containsExactly(PATH_FOO, PATH_BAR); - assertThat(config.getExcludes()).containsExactly(PATH_FOO, PATH_BAR); - - var excludes = List.of(List.of(FOO, BAR), List.of(foz, baz)); - for (var exclude : excludes) { - pmd = newPmdOperation().ruleSets(CATEGORY_FOO).excludesStrings(exclude); - config = pmd.initConfiguration(COMMAND_NAME); - assertThat(config.getExcludes()).containsExactly(exclude.stream().map(Paths::get).toArray(Path[]::new)); - } - } - - @Test - void testExecute() throws ExitStatusException { + void testExecute() { var pmd = new PmdOperation().fromProject(new BaseProject()); assertThat(pmd.inputPaths()).containsExactly(Paths.get("src/main").toAbsolutePath(), @@ -273,34 +157,16 @@ class PmdOperationTest { assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isEqualTo(0); } - @Test - void testExecuteNoProject() { - var pmd = new PmdOperation(); - assertThatCode(pmd::execute).isInstanceOf(ExitStatusException.class); - } - - @Test - void testFailOnError() { - var pmd = newPmdOperation().ruleSets("src/test/resources/xml/old.xml") - .inputPaths(Path.of("src/test/resources/java/Documentation.java")); - assertThatCode(() -> pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))) - .isInstanceOf(ExitStatusException.class); - assertThatCode(() -> pmd.failOnError(false).performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))) - .doesNotThrowAnyException(); - } - @Test void testFailOnValidation() { var pmd = newPmdOperation().ruleSets(DOCUMENTATION_XML) .inputPaths(Path.of("src/test/resources/java/Documentation.java")); assertThatCode(() -> pmd.failOnViolation(true).performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))) - .isInstanceOf(ExitStatusException.class); - assertThatCode(() -> pmd.failOnViolation(false).performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))) - .doesNotThrowAnyException(); + .isInstanceOf(RuntimeException.class).hasMessageContaining('[' + TEST + ']'); } @Test - void testIgnoreFile() throws ExitStatusException { + void testIgnoreFile() { var pmd = newPmdOperation() .ruleSets(ERROR_PRONE_XML, CODE_STYLE_XML) .ignoreFile(Path.of("src/test/resources/ignore.txt")); @@ -325,73 +191,48 @@ class PmdOperationTest { } @Test - void testInputPaths() throws ExitStatusException { + void testInputPaths() { var pmd = newPmdOperation() .ruleSets(PmdOperation.RULE_SET_DEFAULT, CODE_STYLE_XML) .inputPaths(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE); - assertThat(pmd.inputPaths()).as("Path....").containsExactly(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE); - assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isGreaterThan(0); - - pmd = newPmdOperation() - .ruleSets(PmdOperation.RULE_SET_DEFAULT, CODE_STYLE_XML) - .inputPaths(ERROR_PRONE_SAMPLE.toFile(), CODE_STYLE_SAMPLE.toFile()); - assertThat(pmd.inputPaths()).as("File...").containsExactly(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE); - assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isGreaterThan(0); - - pmd = newPmdOperation() - .ruleSets(PmdOperation.RULE_SET_DEFAULT, CODE_STYLE_XML) - .inputPaths(ERROR_PRONE_SAMPLE.toString(), CODE_STYLE_SAMPLE.toString()); - assertThat(pmd.inputPaths()).as("String...").containsExactly(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE); - assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isGreaterThan(0); - - pmd = newPmdOperation() - .ruleSets(PmdOperation.RULE_SET_DEFAULT, CODE_STYLE_XML) - .inputPathsFiles(List.of(ERROR_PRONE_SAMPLE.toFile(), CODE_STYLE_SAMPLE.toFile())); - assertThat(pmd.inputPaths()).as("List(Path...)").containsExactly(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE); - assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isGreaterThan(0); - - pmd = newPmdOperation() - .ruleSets(PmdOperation.RULE_SET_DEFAULT, CODE_STYLE_XML) - .inputPathsStrings(List.of(ERROR_PRONE_SAMPLE.toString(), CODE_STYLE_SAMPLE.toString())); - assertThat(pmd.inputPaths()).as("List(String...)").containsExactly(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE); + assertThat(pmd.inputPaths()).contains(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE); assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isGreaterThan(0); } @Test - void testJavaBestPractices() throws ExitStatusException { + void testJavaBestPractices() { var pmd = newPmdOperation().ruleSets("category/java/bestpractices.xml") .inputPaths(Path.of("src/test/resources/java/BestPractices.java")); assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isGreaterThan(0); } @Test - void testJavaCodeStyle() throws ExitStatusException { + void testJavaCodeStyle() { var pmd = newPmdOperation().ruleSets(CODE_STYLE_XML).inputPaths(CODE_STYLE_SAMPLE); assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isGreaterThan(0); } @Test - void testJavaCodeStyleAndErrorProne() throws ExitStatusException { + void testJavaCodeStyleAndErrorProne() { var pmd = newPmdOperation().ruleSets(CODE_STYLE_XML).inputPaths(CODE_STYLE_SAMPLE); assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))) .as("code style").isGreaterThan(0); - pmd = pmd.ruleSets(ERROR_PRONE_XML).inputPaths(ERROR_PRONE_SAMPLE); assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))) .as("code style + error prone").isGreaterThan(0); } @Test - void testJavaDesign() throws ExitStatusException { + void testJavaDesign() { var pmd = newPmdOperation() .ruleSets(DESIGN_XML) .inputPaths("src/test/resources/java/Design.java") - .cache(new File("build/pmd/design-cache")); + .cache(Path.of("build/pmd/design-cache")); assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isGreaterThan(0); } @Test - void testJavaDocumentation() throws ExitStatusException { + void testJavaDocumentation() { var pmd = newPmdOperation() .ruleSets(DOCUMENTATION_XML) .inputPaths(Path.of("src/test/resources/java/Documentation.java")); @@ -399,20 +240,20 @@ class PmdOperationTest { } @Test - void testJavaErrorProne() throws ExitStatusException { + void testJavaErrorProne() { var pmd = newPmdOperation().ruleSets(ERROR_PRONE_XML).inputPaths(ERROR_PRONE_SAMPLE); assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isGreaterThan(0); } @Test - void testJavaMultiThreading() throws ExitStatusException { + void testJavaMultiThreading() { var pmd = newPmdOperation().ruleSets("category/java/multithreading.xml") .inputPaths(Path.of("src/test/resources/java/MultiThreading.java")); assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isGreaterThan(0); } @Test - void testJavaPerformance() throws ExitStatusException { + void testJavaPerformance() { var pmd = newPmdOperation() .ruleSets(PERFORMANCE_XML) .inputPaths(Path.of("src/test/resources/java/Performance.java")); @@ -420,7 +261,7 @@ class PmdOperationTest { } @Test - void testJavaQuickStart() throws ExitStatusException { + void testJavaQuickStart() { var pmd = newPmdOperation().ruleSets(PmdOperation.RULE_SET_DEFAULT) .inputPaths(new File("src/test/resources/java/")); assertThat(pmd.ruleSets()).containsExactly(PmdOperation.RULE_SET_DEFAULT); @@ -428,21 +269,21 @@ class PmdOperationTest { } @Test - void testJavaSecurity() throws ExitStatusException { + void testJavaSecurity() { var pmd = newPmdOperation().ruleSets(SECURITY_XML) .inputPaths(Path.of("src/test/resources/java/Security.java")); assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isGreaterThan(0); } @Test - void testLanguageVersions() throws ExitStatusException { + void testLanguageVersions() { var language = LanguageRegistry.PMD.getLanguageById("java"); assertThat(language).isNotNull(); var pmd = newPmdOperation() .forceLanguageVersion(language.getLatestVersion()) .defaultLanguageVersions(language.getVersions()) - .languageVersions(language.getDefaultVersion()) + .languageVersions(language.getVersion("22")) .ruleSets(PmdOperation.RULE_SET_DEFAULT); assertThat(pmd.languageVersions()).contains(language.getDefaultVersion()); assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isGreaterThan(0); @@ -454,50 +295,33 @@ class PmdOperationTest { } @Test - void testMainOperation() throws ExitStatusException { + void testMainOperation() { var pmd = newPmdOperation().inputPaths(new File("src/main")) .performPmdAnalysis(TEST, newPmdOperation().initConfiguration(COMMAND_NAME)); assertThat(pmd).isEqualTo(0); } @Test - void testPrependAuxClasspath() { - var pmd = newPmdOperation().ruleSets(CATEGORY_FOO).prependAuxClasspath(FOO, BAR); - assertThat(pmd.prependAuxClasspath()).isEqualTo(FOO + File.pathSeparator + BAR); - } - - @Test - void testPriority() throws ExitStatusException { + void testPriority() { var pmd = newPmdOperation().inputPaths(CODE_STYLE_SAMPLE).minimumPriority(RulePriority.HIGH); assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isEqualTo(0); } @Test void testRelativizeRoots() { + var foo = Path.of("foo/bar"); + var bar = Path.of("bar/foo"); var baz = Path.of("baz/foz"); - var pmd = newPmdOperation().ruleSets(List.of(CATEGORY_FOO)).relativizeRoots(PATH_FOO). - relativizeRoots(PATH_BAR.toFile()).relativizeRoots(baz.toString()) - .relativizeRoots(List.of(PATH_FOO, PATH_BAR, baz)); + var pmd = newPmdOperation().ruleSets(List.of(CATEGORY_FOO)).relativizeRoots(foo).relativizeRoots(bar.toFile()) + .relativizeRoots(baz.toString()).relativizeRoots(List.of(foo, bar, baz)); var config = pmd.initConfiguration(COMMAND_NAME); - assertThat(config.getRelativizeRoots()).isEqualTo(pmd.relativizeRoots()) - .containsExactly(PATH_FOO, PATH_BAR, baz, PATH_FOO, PATH_BAR, baz); - - pmd = newPmdOperation().ruleSets(List.of(CATEGORY_FOO)) - .relativizeRootsFiles(List.of(PATH_FOO.toFile(), PATH_BAR.toFile(), baz.toFile())); - config = pmd.initConfiguration(COMMAND_NAME); - assertThat(config.getRelativizeRoots()).as("List(File...)").isEqualTo(pmd.relativizeRoots()) - .containsExactly(PATH_FOO, PATH_BAR, baz); - - pmd = newPmdOperation().ruleSets(List.of(CATEGORY_FOO)) - .relativizeRootsStrings(List.of(PATH_FOO.toString(), PATH_BAR.toString(), baz.toString())); - config = pmd.initConfiguration(COMMAND_NAME); - assertThat(config.getRelativizeRoots()).as("List(String....)").isEqualTo(pmd.relativizeRoots()) - .containsExactly(PATH_FOO, PATH_BAR, baz); + assertThat(config.getRelativizeRoots()).isEqualTo(pmd.relativizeRoots()); + assertThat(config.getRelativizeRoots()).containsExactly(foo, bar, baz, foo, bar, baz); } @Test - void testReportFile() throws FileNotFoundException, ExitStatusException { + void testReportFile() throws FileNotFoundException { var report = new File("build", "pmd-report-file"); report.deleteOnExit(); var pmd = newPmdOperation().ruleSets(List.of(ERROR_PRONE_XML, DESIGN_XML)).reportFile(report); @@ -514,19 +338,16 @@ class PmdOperationTest { } @Test - void testReportFormat() throws IOException, ExitStatusException { + void testReportFormat() throws IOException { var pmd = newPmdOperation().ruleSets(ERROR_PRONE_XML).reportFormat("xml").inputPaths(ERROR_PRONE_SAMPLE); assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isGreaterThan(0); - try (var softly = new AutoCloseableSoftAssertions()) { - try (var br = Files.newBufferedReader(pmd.reportFile())) { - softly.assertThat(br.readLine()).as("xml report") - .startsWith(""); - } + try (var br = Files.newBufferedReader(pmd.reportFile())) { + assertThat(br.readLine()).as("xml report").startsWith(""); } } @Test - void testReportProperties() throws ExitStatusException { + void testReportProperties() { var pmd = newPmdOperation().ruleSets(CODE_STYLE_XML, ERROR_PRONE_XML) .includeLineNumber(true) .reportProperties(new Properties()); @@ -534,15 +355,15 @@ class PmdOperationTest { } @Test - void testRuleSetsConfigFile() throws ExitStatusException { + void testRuleSetsConfigFile() { var pmd = newPmdOperation().ruleSets("src/test/resources/pmd.xml") .ignoreFile("src/test/resources/ignore-all.txt"); assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isEqualTo(0); } @Test - void testRuleSetsEmpty() throws ExitStatusException { - var pmd = newPmdOperation().ruleSets("").failOnError(false); + void testRuleSetsEmpty() { + var pmd = newPmdOperation().ruleSets(""); assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isEqualTo(0); } @@ -576,7 +397,7 @@ class PmdOperationTest { } @Test - void testXml() throws ExitStatusException { + void testXml() { var pmd = newPmdOperation().addInputPaths("src/test/resources/pmd.xml") .ruleSets("src/test/resources/xml/basic.xml"); assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isGreaterThan(0); diff --git a/src/test/resources/pmd.xml b/src/test/resources/pmd.xml index 18cc49b..c4c6617 100644 --- a/src/test/resources/pmd.xml +++ b/src/test/resources/pmd.xml @@ -1,7 +1,7 @@ - Erik's Ruleset diff --git a/src/test/resources/xml/basic.xml b/src/test/resources/xml/basic.xml index 4043fba..c261dc7 100644 --- a/src/test/resources/xml/basic.xml +++ b/src/test/resources/xml/basic.xml @@ -1,8 +1,8 @@ - The Basic ruleset contains a collection of good practices which should be followed. diff --git a/src/test/resources/xml/old.xml b/src/test/resources/xml/old.xml deleted file mode 100644 index fa71516..0000000 --- a/src/test/resources/xml/old.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - Deprecated (old) rule - - \ No newline at end of file