Rework tests to use the PmdAnalysisResults record
This commit is contained in:
parent
630f3fa8d2
commit
be90889b8d
7 changed files with 547 additions and 447 deletions
1
.github/workflows/bld.yml
vendored
1
.github/workflows/bld.yml
vendored
|
@ -7,7 +7,6 @@ jobs:
|
|||
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 }}
|
||||
|
|
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
|
@ -25,7 +25,7 @@
|
|||
</option>
|
||||
<option name="skipTestSources" value="false" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/build" />
|
||||
</component>
|
||||
</project>
|
|
@ -20,6 +20,7 @@ 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.BeforeAll;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
@ -38,6 +39,8 @@ import java.nio.file.Path;
|
|||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.logging.ConsoleHandler;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
|
@ -49,25 +52,36 @@ import static org.assertj.core.api.Assertions.assertThatCode;
|
|||
* @since 1.0
|
||||
*/
|
||||
class PmdOperationTests {
|
||||
static final String ANALYSIS_FAILURE = "analysis should fail";
|
||||
static final String ANALYSIS_SUCCESS = "analysis should succeed";
|
||||
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";
|
||||
static final String COMMAND_NAME = "pmd";
|
||||
static final String DESIGN_XML = "category/java/design.xml";
|
||||
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";
|
||||
private static final String ANALYSIS_FAILURE = "analysis should fail";
|
||||
private static final String ANALYSIS_SUCCESS = "analysis should succeed";
|
||||
private static final String BAR = "bar";
|
||||
private static final String CATEGORY_FOO = "category/foo.xml";
|
||||
private static final Path CODE_STYLE_SAMPLE = Path.of("src/test/resources/java/CodeStyle.java");
|
||||
private static final String CODE_STYLE_XML = "category/java/codestyle.xml";
|
||||
private static final String COMMAND_NAME = "pmd";
|
||||
private static final String DESIGN_XML = "category/java/design.xml";
|
||||
private static final String DOCUMENTATION_XML = "category/java/documentation.xml";
|
||||
private static final Path ERROR_PRONE_SAMPLE = Path.of("src/test/resources/java/ErrorProne.java");
|
||||
private static final String ERROR_PRONE_XML = "category/java/errorprone.xml";
|
||||
private static final File FILE_BAR = new File(BAR);
|
||||
private static final String FOO = "foo";
|
||||
private static final File FILE_FOO = new File(FOO);
|
||||
private static final Path PATH_BAR = Path.of(BAR);
|
||||
private static final Path PATH_FOO = Path.of(FOO);
|
||||
private static final String PERFORMANCE_XML = "category/java/performance.xml";
|
||||
private static final String SECURITY_XML = "category/java/security.xml";
|
||||
private static final String TEST = "test";
|
||||
|
||||
@BeforeAll
|
||||
public static void beforeAll() {
|
||||
var consoleHandler = new ConsoleHandler();
|
||||
var logger = PmdOperation.LOGGER;
|
||||
|
||||
consoleHandler.setLevel(Level.ALL);
|
||||
logger.addHandler(consoleHandler);
|
||||
logger.setLevel(Level.ALL);
|
||||
logger.setUseParentHandlers(false);
|
||||
}
|
||||
|
||||
PmdOperation newPmdOperation() {
|
||||
return new PmdOperation()
|
||||
|
@ -87,18 +101,29 @@ class PmdOperationTests {
|
|||
.ruleSets(CODE_STYLE_XML)
|
||||
.inputPaths(List.of(CODE_STYLE_SAMPLE))
|
||||
.cache(cache);
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)).rulesChecked())
|
||||
.isGreaterThan(0);
|
||||
var f = cache.toFile();
|
||||
assertThat(f.exists()).as("cache should exist").isTrue();
|
||||
assertThat(f.delete()).as("cache should be deleted").isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void collectFilesRecursively() {
|
||||
var pmd = newPmdOperation().ruleSets(ERROR_PRONE_XML).collectFilesRecursively(false);
|
||||
|
||||
var config = pmd.initConfiguration(COMMAND_NAME);
|
||||
assertThat(config.collectFilesRecursively()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void encoding() {
|
||||
var pmd = newPmdOperation().ruleSets(CATEGORY_FOO).encoding("UTF-16");
|
||||
|
||||
PMDConfiguration config = pmd.initConfiguration(COMMAND_NAME);
|
||||
assertThat(config.getSourceEncoding()).as("encoding should be UTF-16").isEqualTo(StandardCharsets.UTF_16);
|
||||
assertThat(config.getSourceEncoding()).as("encoding should be UTF-16")
|
||||
.isEqualTo(StandardCharsets.UTF_16);
|
||||
|
||||
pmd = pmd.encoding(StandardCharsets.ISO_8859_1);
|
||||
config = pmd.initConfiguration(COMMAND_NAME);
|
||||
|
@ -106,30 +131,10 @@ class PmdOperationTests {
|
|||
.isEqualTo(StandardCharsets.ISO_8859_1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void ignoreFile() throws ExitStatusException {
|
||||
var pmd = newPmdOperation()
|
||||
.ruleSets(ERROR_PRONE_XML, CODE_STYLE_XML)
|
||||
.ignoreFile(Path.of("src/test/resources/ignore.txt"));
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
.as("%s for error prone and code style", ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
|
||||
pmd.inputPaths().clear();
|
||||
pmd.inputPaths(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE);
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
.as("%s for error prone and code style samples", ANALYSIS_SUCCESS).isEqualTo(0);
|
||||
|
||||
pmd.ruleSets().clear();
|
||||
pmd.inputPaths().clear();
|
||||
assertThat(pmd.inputPaths(ERROR_PRONE_SAMPLE)
|
||||
.ignoreFile(new File("src/test/resources/ignore-single.txt"))
|
||||
.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
.as("%s for error prone sample", ANALYSIS_SUCCESS).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void incrementalAnalysis() {
|
||||
var pmd = newPmdOperation().ruleSets(CATEGORY_FOO).incrementalAnalysis(true);
|
||||
|
||||
var config = pmd.initConfiguration(COMMAND_NAME);
|
||||
assertThat(config.isIgnoreIncrementalAnalysis()).isFalse();
|
||||
}
|
||||
|
@ -147,14 +152,14 @@ class PmdOperationTests {
|
|||
|
||||
assertThat(pmd.languageVersions()).as("should contain default language version")
|
||||
.contains(language.getDefaultVersion());
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)).processingErrors())
|
||||
.as("should have processing errors").isGreaterThan(0);
|
||||
|
||||
assertThat(pmd.defaultLanguageVersions(language.getVersion("17"), language.getVersion("21"))
|
||||
.languageVersions(language.getVersions())
|
||||
.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
.as("%s with language versions 17 & 21", ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
|
||||
.excludesStrings("src/test/resources/txt", "src/test/resources/xml")
|
||||
.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)).configurationErrors())
|
||||
.as("should have no processing errors").isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -166,7 +171,7 @@ class PmdOperationTests {
|
|||
@Test
|
||||
void priority() throws ExitStatusException {
|
||||
var pmd = newPmdOperation().inputPaths(CODE_STYLE_SAMPLE).minimumPriority(RulePriority.HIGH);
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)).rulesChecked())
|
||||
.as(ANALYSIS_SUCCESS).isEqualTo(0);
|
||||
}
|
||||
|
||||
|
@ -174,9 +179,12 @@ class PmdOperationTests {
|
|||
void relativizeRoots() {
|
||||
var baz = Path.of("baz/foz");
|
||||
|
||||
var pmd = newPmdOperation().ruleSets(List.of(CATEGORY_FOO)).relativizeRoots(PATH_FOO).
|
||||
relativizeRoots(PATH_BAR.toFile()).relativizeRoots(baz.toString())
|
||||
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 config = pmd.initConfiguration(COMMAND_NAME);
|
||||
assertThat(config.getRelativizeRoots()).as("multiple roots").isEqualTo(pmd.relativizeRoots())
|
||||
.containsExactly(PATH_FOO, PATH_BAR, baz, PATH_FOO, PATH_BAR, baz);
|
||||
|
@ -222,7 +230,6 @@ class PmdOperationTests {
|
|||
var config = pmd.initConfiguration(COMMAND_NAME);
|
||||
assertThat(config.getUri()).isEqualTo(uri);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Exclusion Tests")
|
||||
|
@ -291,7 +298,8 @@ class PmdOperationTests {
|
|||
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));
|
||||
assertThat(config.getExcludes())
|
||||
.containsExactly(exclude.stream().map(File::toPath).toArray(Path[]::new));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -309,7 +317,416 @@ class PmdOperationTests {
|
|||
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));
|
||||
assertThat(config.getExcludes())
|
||||
.containsExactly(exclude.stream().map(Paths::get).toArray(Path[]::new));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Failure Tests")
|
||||
class FailureTests {
|
||||
@Test
|
||||
void failOnError() {
|
||||
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 failOnValidation() {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Ignore File Tests")
|
||||
class IgnoreFileTests {
|
||||
@Test
|
||||
void ignoreFile() throws ExitStatusException {
|
||||
var pmd = newPmdOperation()
|
||||
.ruleSets(ERROR_PRONE_XML, CODE_STYLE_XML)
|
||||
.inputPaths(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE)
|
||||
.ignoreFile(Path.of("src/test/resources/txt/ignore.txt"));
|
||||
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)).violations())
|
||||
.as("%s for error prone and code style", ANALYSIS_SUCCESS).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void ignoreSingleFile() throws ExitStatusException {
|
||||
var pmd = newPmdOperation()
|
||||
.ruleSets(ERROR_PRONE_XML)
|
||||
.inputPaths(ERROR_PRONE_SAMPLE)
|
||||
.ignoreFile(new File("src/test/resources/txt/ignore-single.txt"));
|
||||
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)).violations())
|
||||
.as("%s for error prone sample", ANALYSIS_SUCCESS).isEqualTo(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Input Paths Tests")
|
||||
class InputPathsTests {
|
||||
@Test
|
||||
void fileArray() throws ExitStatusException {
|
||||
var pmd = newPmdOperation()
|
||||
.ruleSets(PmdOperation.RULE_SET_DEFAULT, CODE_STYLE_XML)
|
||||
.inputPaths(ERROR_PRONE_SAMPLE.toFile(), CODE_STYLE_SAMPLE.toFile());
|
||||
|
||||
assertThat(pmd.inputPaths()).containsExactly(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE);
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)).violations())
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void mainOperation() throws ExitStatusException {
|
||||
var pmd = newPmdOperation()
|
||||
.inputPaths(new File("src/main"))
|
||||
.performPmdAnalysis(TEST, newPmdOperation().initConfiguration(COMMAND_NAME))
|
||||
.violations();
|
||||
|
||||
assertThat(pmd).as(ANALYSIS_SUCCESS).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void pathArray() throws ExitStatusException {
|
||||
var pmd = newPmdOperation()
|
||||
.ruleSets(PmdOperation.RULE_SET_DEFAULT, CODE_STYLE_XML)
|
||||
.inputPaths(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE);
|
||||
|
||||
assertThat(pmd.inputPaths()).containsExactly(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE);
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)).violations())
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void pathList() throws ExitStatusException {
|
||||
var pmd = newPmdOperation()
|
||||
.ruleSets(PmdOperation.RULE_SET_DEFAULT, CODE_STYLE_XML)
|
||||
.inputPathsFiles(List.of(ERROR_PRONE_SAMPLE.toFile(), CODE_STYLE_SAMPLE.toFile()));
|
||||
|
||||
assertThat(pmd.inputPaths()).containsExactly(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE);
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)).violations())
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stringArray() throws ExitStatusException {
|
||||
var pmd = newPmdOperation()
|
||||
.ruleSets(PmdOperation.RULE_SET_DEFAULT, CODE_STYLE_XML)
|
||||
.inputPaths(ERROR_PRONE_SAMPLE.toString(), CODE_STYLE_SAMPLE.toString());
|
||||
|
||||
assertThat(pmd.inputPaths()).containsExactly(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE);
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)).violations())
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stringList() throws ExitStatusException {
|
||||
var pmd = newPmdOperation()
|
||||
.ruleSets(PmdOperation.RULE_SET_DEFAULT, CODE_STYLE_XML)
|
||||
.inputPathsStrings(List.of(ERROR_PRONE_SAMPLE.toString(), CODE_STYLE_SAMPLE.toString()));
|
||||
|
||||
assertThat(pmd.inputPaths()).containsExactly(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE);
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)).violations())
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void xml() throws ExitStatusException {
|
||||
var pmd = newPmdOperation().addInputPaths("src/test/resources/xml/pmd.xml")
|
||||
.ruleSets("src/test/resources/xml/basic.xml");
|
||||
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)).configurationErrors())
|
||||
.isEqualTo(0);
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Add Input Paths Tests")
|
||||
class AddInputPathsTests {
|
||||
final BaseProject project = new BaseProject();
|
||||
|
||||
@Test
|
||||
void addInputPathsAsFileArray() {
|
||||
var pmd = new PmdOperation().fromProject(project);
|
||||
pmd.inputPaths().clear();
|
||||
pmd = pmd.addInputPaths(project.srcMainDirectory(), project.srcTestDirectory());
|
||||
|
||||
assertThat(pmd.inputPaths()).as("File...").containsExactly(project.srcMainDirectory().toPath(),
|
||||
project.srcTestDirectory().toPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
void addInputPathsAsFileList() {
|
||||
var pmd = new PmdOperation().fromProject(project);
|
||||
pmd.inputPaths().clear();
|
||||
pmd = pmd.addInputPathsFiles(List.of(project.srcMainDirectory(), project.srcTestDirectory()));
|
||||
|
||||
assertThat(pmd.inputPaths()).as("List(File...)")
|
||||
.containsExactly(project.srcMainDirectory().toPath(), project.srcTestDirectory().toPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
void addInputPathsAsPathArray() {
|
||||
var pmd = new PmdOperation().fromProject(project);
|
||||
pmd.inputPaths().clear();
|
||||
pmd = pmd.addInputPaths(project.srcMainDirectory().toPath(), project.srcTestDirectory().toPath());
|
||||
|
||||
assertThat(pmd.inputPaths()).as("Path...")
|
||||
.containsExactly(project.srcMainDirectory().toPath(), project.srcTestDirectory().toPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
void addInputPathsAsPathList() {
|
||||
var pmd = new PmdOperation().fromProject(project);
|
||||
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());
|
||||
}
|
||||
|
||||
@Test
|
||||
void addInputPathsAsStringArray() {
|
||||
var pmd = new PmdOperation().fromProject(project);
|
||||
pmd.inputPaths().clear();
|
||||
pmd = pmd.addInputPaths(project.srcMainDirectory().getAbsolutePath(),
|
||||
project.srcTestDirectory().getAbsolutePath());
|
||||
|
||||
assertThat(pmd.inputPaths()).as("String...")
|
||||
.containsExactly(project.srcMainDirectory().toPath(), project.srcTestDirectory().toPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
void addInputPathsAsStringList() {
|
||||
var pmd = new PmdOperation().fromProject(project);
|
||||
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());
|
||||
}
|
||||
|
||||
@Test
|
||||
void addInputPathsWithDefaults() {
|
||||
var pmd = new PmdOperation().fromProject(project);
|
||||
assertThat(pmd.inputPaths()).as("default input paths")
|
||||
.containsExactly(project.srcMainDirectory().toPath(), project.srcTestDirectory().toPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Reporting Tests")
|
||||
class ReportingTests {
|
||||
@Test
|
||||
void reportFile() throws FileNotFoundException, ExitStatusException {
|
||||
var report = new File("build", "pmd-report-file");
|
||||
report.deleteOnExit();
|
||||
var pmd = newPmdOperation()
|
||||
.ruleSets(List.of(ERROR_PRONE_XML, DESIGN_XML))
|
||||
.reportFile(report);
|
||||
|
||||
pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME));
|
||||
assertThat(report).as("report should exist").exists();
|
||||
|
||||
try (var writer = new PrintWriter(report)) {
|
||||
writer.write("");
|
||||
}
|
||||
assertThat(report).as("report should not be empty").isEmpty();
|
||||
|
||||
pmd.reportFile(report.getAbsolutePath()).performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME));
|
||||
assertThat(report).as("report should not be empty").isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void reportFormat() throws IOException, ExitStatusException {
|
||||
var pmd = newPmdOperation()
|
||||
.ruleSets(ERROR_PRONE_XML)
|
||||
.reportFormat("xml")
|
||||
.inputPaths(ERROR_PRONE_SAMPLE);
|
||||
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)).violations())
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
try (var softly = new AutoCloseableSoftAssertions()) {
|
||||
try (var br = Files.newBufferedReader(pmd.reportFile())) {
|
||||
softly.assertThat(br.readLine()).as("XML report for %s", pmd.reportFile().toString())
|
||||
.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void reportProperties() throws ExitStatusException {
|
||||
var pmd = newPmdOperation().ruleSets(CODE_STYLE_XML, ERROR_PRONE_XML)
|
||||
.includeLineNumber(true)
|
||||
.reportProperties(new Properties());
|
||||
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)).processingErrors())
|
||||
.isEqualTo(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("RuleSets Tests")
|
||||
class RuleSetsTests {
|
||||
@Test
|
||||
void javaBestPractices() throws ExitStatusException {
|
||||
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)).violations())
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaCodeStyle() throws ExitStatusException {
|
||||
var pmd = newPmdOperation().ruleSets(CODE_STYLE_XML).inputPaths(CODE_STYLE_SAMPLE);
|
||||
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)).violations())
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaCodeStyleAndErrorProne() throws ExitStatusException {
|
||||
var pmd = newPmdOperation().ruleSets(CODE_STYLE_XML).inputPaths(CODE_STYLE_SAMPLE);
|
||||
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)).violations())
|
||||
.as("analysis with code style").isGreaterThan(0);
|
||||
|
||||
pmd = pmd.ruleSets(ERROR_PRONE_XML).inputPaths(ERROR_PRONE_SAMPLE);
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)).violations())
|
||||
.as("analysis with code style and error prone").isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaDesign() throws ExitStatusException {
|
||||
var pmd = newPmdOperation()
|
||||
.ruleSets(DESIGN_XML)
|
||||
.inputPaths("src/test/resources/java/Design.java")
|
||||
.cache(new File("build/pmd/design-cache"));
|
||||
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)).violations())
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaDocumentation() throws ExitStatusException {
|
||||
var pmd = newPmdOperation()
|
||||
.ruleSets(DOCUMENTATION_XML)
|
||||
.inputPaths(Path.of("src/test/resources/java/Documentation.java"));
|
||||
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)).violations())
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaErrorProne() throws ExitStatusException {
|
||||
var pmd = newPmdOperation().ruleSets(ERROR_PRONE_XML).inputPaths(ERROR_PRONE_SAMPLE);
|
||||
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)).violations())
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaMultiThreading() throws ExitStatusException {
|
||||
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)).violations())
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaPerformance() throws ExitStatusException {
|
||||
var pmd = newPmdOperation()
|
||||
.ruleSets(PERFORMANCE_XML)
|
||||
.inputPaths(Path.of("src/test/resources/java/Performance.java"));
|
||||
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)).violations())
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaQuickStart() throws ExitStatusException {
|
||||
var pmd = newPmdOperation()
|
||||
.ruleSets(PmdOperation.RULE_SET_DEFAULT)
|
||||
.inputPaths(new File("src/test/resources/java/"));
|
||||
|
||||
assertThat(pmd.ruleSets()).containsExactly(PmdOperation.RULE_SET_DEFAULT);
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)).violations())
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaSecurity() throws ExitStatusException {
|
||||
var pmd = newPmdOperation()
|
||||
.ruleSets(SECURITY_XML)
|
||||
.inputPaths(Path.of("src/test/resources/java/Security.java"));
|
||||
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)).violations())
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void ruleSetsConfigFile() throws ExitStatusException {
|
||||
var pmd = newPmdOperation()
|
||||
.ruleSets("src/test/resources/xml/pmd.xml")
|
||||
.ignoreFile("src/test/resources/txt/ignore-all.txt");
|
||||
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)).hasNoErrors()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void ruleSetsEmpty() throws ExitStatusException {
|
||||
var pmd = newPmdOperation().ruleSets("").failOnError(false);
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)).errors())
|
||||
.isEqualTo(1);
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Add RuleSets Tests")
|
||||
class AddRuleSetsTests {
|
||||
@Test
|
||||
void addRuleSetsWithDefault() {
|
||||
var pmd = new PmdOperation().fromProject(new BaseProject());
|
||||
|
||||
assertThat(pmd.ruleSets()).containsExactly(PmdOperation.RULE_SET_DEFAULT);
|
||||
}
|
||||
|
||||
@Test
|
||||
void addRuleSetsWithErrorProne() {
|
||||
var pmd = new PmdOperation().fromProject(new BaseProject());
|
||||
|
||||
pmd.addRuleSet(ERROR_PRONE_XML);
|
||||
|
||||
assertThat(pmd.ruleSets()).containsExactly(PmdOperation.RULE_SET_DEFAULT, ERROR_PRONE_XML);
|
||||
}
|
||||
|
||||
@Test
|
||||
void addRuleSetsWithList() {
|
||||
var pmd = new PmdOperation().fromProject(new BaseProject());
|
||||
pmd.ruleSets().clear();
|
||||
pmd.addRuleSet(List.of(PmdOperation.RULE_SET_DEFAULT, ERROR_PRONE_XML));
|
||||
|
||||
assertThat(pmd.ruleSets()).as("rulesets as collection")
|
||||
.containsExactly(PmdOperation.RULE_SET_DEFAULT, ERROR_PRONE_XML);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -329,7 +746,7 @@ class PmdOperationTests {
|
|||
.ruleSets("config/pmd.xml");
|
||||
|
||||
assertThat(pmd.ruleSets()).containsExactly("config/pmd.xml");
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)).violations())
|
||||
.as(ANALYSIS_SUCCESS).isEqualTo(0);
|
||||
}
|
||||
|
||||
|
@ -339,320 +756,4 @@ class PmdOperationTests {
|
|||
assertThatCode(pmd::execute).isInstanceOf(ExitStatusException.class);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Failure Tests")
|
||||
class FailureTests {
|
||||
@Test
|
||||
void failOnError() {
|
||||
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 failOnValidation() {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Input Paths Tests")
|
||||
class InputPathsTests {
|
||||
@Test
|
||||
void addInputPaths() throws ExitStatusException {
|
||||
var project = new BaseProject();
|
||||
var pmd = new PmdOperation().fromProject(project);
|
||||
|
||||
assertThat(pmd.inputPaths()).as("default input paths")
|
||||
.containsExactly(project.srcMainDirectory().toPath(), project.srcTestDirectory().toPath());
|
||||
|
||||
var err = pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME));
|
||||
|
||||
pmd.inputPaths().clear();
|
||||
pmd = pmd.addInputPaths(project.srcMainDirectory(), project.srcTestDirectory());
|
||||
|
||||
assertThat(pmd.inputPaths()).as("File...").containsExactly(project.srcMainDirectory().toPath(),
|
||||
project.srcTestDirectory().toPath());
|
||||
|
||||
pmd.inputPaths().clear();
|
||||
pmd = pmd.addInputPathsFiles(List.of(project.srcMainDirectory(), project.srcTestDirectory()));
|
||||
|
||||
assertThat(pmd.inputPaths()).as("List(File...)")
|
||||
.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)))
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0).isEqualTo(err);
|
||||
}
|
||||
|
||||
@Test
|
||||
void fileArray() throws ExitStatusException {
|
||||
var pmd = newPmdOperation()
|
||||
.ruleSets(PmdOperation.RULE_SET_DEFAULT, CODE_STYLE_XML)
|
||||
.inputPaths(ERROR_PRONE_SAMPLE.toFile(), CODE_STYLE_SAMPLE.toFile());
|
||||
assertThat(pmd.inputPaths()).containsExactly(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE);
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void mainOperation() throws ExitStatusException {
|
||||
var pmd = newPmdOperation().inputPaths(new File("src/main"))
|
||||
.performPmdAnalysis(TEST, newPmdOperation().initConfiguration(COMMAND_NAME));
|
||||
assertThat(pmd).as(ANALYSIS_SUCCESS).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void pathArray() throws ExitStatusException {
|
||||
var pmd = newPmdOperation()
|
||||
.ruleSets(PmdOperation.RULE_SET_DEFAULT, CODE_STYLE_XML)
|
||||
.inputPaths(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE);
|
||||
assertThat(pmd.inputPaths()).containsExactly(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE);
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void pathList() throws ExitStatusException {
|
||||
var pmd = newPmdOperation()
|
||||
.ruleSets(PmdOperation.RULE_SET_DEFAULT, CODE_STYLE_XML)
|
||||
.inputPathsFiles(List.of(ERROR_PRONE_SAMPLE.toFile(), CODE_STYLE_SAMPLE.toFile()));
|
||||
assertThat(pmd.inputPaths()).containsExactly(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE);
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stringArray() throws ExitStatusException {
|
||||
var pmd = newPmdOperation()
|
||||
.ruleSets(PmdOperation.RULE_SET_DEFAULT, CODE_STYLE_XML)
|
||||
.inputPaths(ERROR_PRONE_SAMPLE.toString(), CODE_STYLE_SAMPLE.toString());
|
||||
assertThat(pmd.inputPaths()).containsExactly(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE);
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stringList() throws ExitStatusException {
|
||||
var pmd = newPmdOperation()
|
||||
.ruleSets(PmdOperation.RULE_SET_DEFAULT, CODE_STYLE_XML)
|
||||
.inputPathsStrings(List.of(ERROR_PRONE_SAMPLE.toString(), CODE_STYLE_SAMPLE.toString()));
|
||||
assertThat(pmd.inputPaths()).containsExactly(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE);
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void xml() throws ExitStatusException {
|
||||
var pmd = newPmdOperation().addInputPaths("src/test/resources/pmd.xml")
|
||||
.ruleSets("src/test/resources/xml/basic.xml");
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Reporting Tests")
|
||||
class ReportingTests {
|
||||
@Test
|
||||
void reportFile() throws FileNotFoundException, ExitStatusException {
|
||||
var report = new File("build", "pmd-report-file");
|
||||
report.deleteOnExit();
|
||||
var pmd = newPmdOperation().ruleSets(List.of(ERROR_PRONE_XML, DESIGN_XML)).reportFile(report);
|
||||
pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME));
|
||||
assertThat(report).as("report should exist").exists();
|
||||
|
||||
try (var writer = new PrintWriter(report)) {
|
||||
writer.write("");
|
||||
}
|
||||
assertThat(report).as("report should not be empty").isEmpty();
|
||||
|
||||
pmd.reportFile(report.getAbsolutePath()).performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME));
|
||||
assertThat(report).as("report should not be empty").isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void reportFormat() throws IOException, ExitStatusException {
|
||||
var pmd = newPmdOperation().ruleSets(ERROR_PRONE_XML).reportFormat("xml").inputPaths(ERROR_PRONE_SAMPLE);
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
try (var softly = new AutoCloseableSoftAssertions()) {
|
||||
try (var br = Files.newBufferedReader(pmd.reportFile())) {
|
||||
softly.assertThat(br.readLine()).as("XML report for %s", pmd.reportFile().toString())
|
||||
.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void reportProperties() throws ExitStatusException {
|
||||
var pmd = newPmdOperation().ruleSets(CODE_STYLE_XML, ERROR_PRONE_XML)
|
||||
.includeLineNumber(true)
|
||||
.reportProperties(new Properties());
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("RuleSets Tests")
|
||||
class RuleSetsTests {
|
||||
@Test
|
||||
void addRuleSets() throws ExitStatusException {
|
||||
var pmd = new PmdOperation().fromProject(new BaseProject());
|
||||
|
||||
assertThat(pmd.ruleSets()).containsExactly(PmdOperation.RULE_SET_DEFAULT);
|
||||
|
||||
pmd.addRuleSet(ERROR_PRONE_XML);
|
||||
|
||||
assertThat(pmd.ruleSets()).containsExactly(PmdOperation.RULE_SET_DEFAULT, ERROR_PRONE_XML);
|
||||
|
||||
var err = pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME));
|
||||
|
||||
pmd.ruleSets().clear();
|
||||
|
||||
pmd.addRuleSet(List.of(PmdOperation.RULE_SET_DEFAULT, ERROR_PRONE_XML));
|
||||
|
||||
assertThat(pmd.ruleSets()).as("rulesets as collection")
|
||||
.containsExactly(PmdOperation.RULE_SET_DEFAULT, ERROR_PRONE_XML);
|
||||
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0).isEqualTo(err);
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaBestPractices() throws ExitStatusException {
|
||||
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)))
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaCodeStyle() throws ExitStatusException {
|
||||
var pmd = newPmdOperation().ruleSets(CODE_STYLE_XML).inputPaths(CODE_STYLE_SAMPLE);
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaCodeStyleAndErrorProne() throws ExitStatusException {
|
||||
var pmd = newPmdOperation().ruleSets(CODE_STYLE_XML).inputPaths(CODE_STYLE_SAMPLE);
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
.as("analysis with code style").isGreaterThan(0);
|
||||
|
||||
pmd = pmd.ruleSets(ERROR_PRONE_XML).inputPaths(ERROR_PRONE_SAMPLE);
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
.as("analysis with code style and error prone").isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaDesign() throws ExitStatusException {
|
||||
var pmd = newPmdOperation()
|
||||
.ruleSets(DESIGN_XML)
|
||||
.inputPaths("src/test/resources/java/Design.java")
|
||||
.cache(new File("build/pmd/design-cache"));
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaDocumentation() throws ExitStatusException {
|
||||
var pmd = newPmdOperation()
|
||||
.ruleSets(DOCUMENTATION_XML)
|
||||
.inputPaths(Path.of("src/test/resources/java/Documentation.java"));
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaErrorProne() throws ExitStatusException {
|
||||
var pmd = newPmdOperation().ruleSets(ERROR_PRONE_XML).inputPaths(ERROR_PRONE_SAMPLE);
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaMultiThreading() throws ExitStatusException {
|
||||
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)))
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaPerformance() throws ExitStatusException {
|
||||
var pmd = newPmdOperation()
|
||||
.ruleSets(PERFORMANCE_XML)
|
||||
.inputPaths(Path.of("src/test/resources/java/Performance.java"));
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaQuickStart() throws ExitStatusException {
|
||||
var pmd = newPmdOperation().ruleSets(PmdOperation.RULE_SET_DEFAULT)
|
||||
.inputPaths(new File("src/test/resources/java/"));
|
||||
assertThat(pmd.ruleSets()).containsExactly(PmdOperation.RULE_SET_DEFAULT);
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaSecurity() throws ExitStatusException {
|
||||
var pmd = newPmdOperation().ruleSets(SECURITY_XML)
|
||||
.inputPaths(Path.of("src/test/resources/java/Security.java"));
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
.as(ANALYSIS_FAILURE).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void ruleSetsConfigFile() throws ExitStatusException {
|
||||
var pmd = newPmdOperation().ruleSets("src/test/resources/pmd.xml")
|
||||
.ignoreFile("src/test/resources/ignore-all.txt");
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
.as(ANALYSIS_SUCCESS).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void ruleSetsEmpty() throws ExitStatusException {
|
||||
var pmd = newPmdOperation().ruleSets("").failOnError(false);
|
||||
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
|
||||
.as(ANALYSIS_SUCCESS).isEqualTo(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue