Switched from Lists to Collections

This commit is contained in:
Erik C. Thauvin 2024-07-22 09:41:55 -07:00
parent 00f40e887f
commit 826a967815
Signed by: erik
GPG key ID: 776702A6A2DA330E
14 changed files with 280 additions and 163 deletions

View file

@ -22,11 +22,8 @@ jobs:
distribution: "zulu"
java-version: ${{ matrix.java-version }}
- name: Grant execute permission for bld
run: chmod +x bld
- name: Download the dependencies
- name: Download dependencies
run: ./bld download
- name: Run tests with bld
- name: Run tests
run: ./bld compile test

View file

@ -2,12 +2,12 @@
<library name="bld">
<CLASSES>
<root url="file://$PROJECT_DIR$/lib/bld" />
<root url="jar://$USER_HOME$/.bld/dist/bld-1.9.1.jar!/" />
<root url="jar://$USER_HOME$/.bld/dist/bld-2.0.0-SNAPSHOT.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="file://$PROJECT_DIR$/lib/bld" />
<root url="jar://$USER_HOME$/.bld/dist/bld-1.9.1-sources.jar!/" />
<root url="jar://$USER_HOME$/.bld/dist/bld-2.0.0-SNAPSHOT-sources.jar!/" />
</SOURCES>
<excluded>
<root url="jar://$PROJECT_DIR$/lib/bld/bld-wrapper.jar!/" />

View file

@ -9,7 +9,7 @@
],
"java.configuration.updateBuildConfiguration": "automatic",
"java.project.referencedLibraries": [
"${HOME}/.bld/dist/bld-1.9.1.jar",
"${HOME}/.bld/dist/bld-2.0.0-SNAPSHOT.jar",
"lib/**/*.jar"
],
"java.compile.nullAnalysis.mode": "automatic"

View file

@ -2,12 +2,12 @@
<library name="bld">
<CLASSES>
<root url="file://$PROJECT_DIR$/lib/bld" />
<root url="jar://$USER_HOME$/.bld/dist/bld-1.9.1.jar!/" />
<root url="jar://$USER_HOME$/.bld/dist/bld-2.0.0-SNAPSHOT.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="file://$PROJECT_DIR$/lib/bld" />
<root url="jar://$USER_HOME$/.bld/dist/bld-1.9.1-sources.jar!/" />
<root url="jar://$USER_HOME$/.bld/dist/bld-2.0.0-SNAPSHOT-sources.jar!/" />
</SOURCES>
<excluded>
<root url="jar://$PROJECT_DIR$/lib/bld/bld-wrapper.jar!/" />

View file

@ -1,4 +1,8 @@
<project version="4">
<component name="EntryPointsManager">
<pattern value="com.example.ExamplesBuild" method="checkstyle" />
<pattern value="com.example.ExamplesBuild" />
</component>
<component name="PDMPlugin">
<option name="customRuleSets">
<list>

View file

@ -9,7 +9,7 @@
],
"java.configuration.updateBuildConfiguration": "automatic",
"java.project.referencedLibraries": [
"${HOME}/.bld/dist/bld-1.9.1.jar",
"${HOME}/.bld/dist/bld-2.0.0-SNAPSHOT.jar",
"lib/**/*.jar"
]
}

Binary file not shown.

View file

@ -1,7 +1,7 @@
bld.downloadExtensionJavadoc=false
bld.downloadExtensionSources=true
bld.extensions=com.uwyn.rife2:bld-checkstyle:1.0.1
bld.repositories=MAVEN_LOCAL,MAVEN_CENTRAL,RIFE2_SNAPSHOTS,RIFE2_RELEASES
bld.downloadLocation=
bld.extension-checkstyle=com.uwyn.rife2:bld-checkstyle:1.0.1
bld.repositories=MAVEN_LOCAL,MAVEN_CENTRAL,RIFE2_SNAPSHOTS,RIFE2_RELEASES
bld.sourceDirectories=
bld.version=1.9.1
bld.version=2.0.0-SNAPSHOT

Binary file not shown.

View file

@ -1,9 +1,9 @@
bld.downloadExtensionJavadoc=false
bld.downloadExtensionSources=true
bld.extension-pmd=com.uwyn.rife2:bld-pmd:0.9.9
bld.extension-jacoco=com.uwyn.rife2:bld-jacoco-report:0.9.5
bld.extension-exec=com.uwyn.rife2:bld-exec:1.0.0
bld.repositories=MAVEN_CENTRAL,MAVEN_LOCAL,RIFE2_SNAPSHOTS,RIFE2_RELEASES
bld.downloadLocation=
bld.extension-exec=com.uwyn.rife2:bld-exec:1.0.1
bld.extension-jacoco=com.uwyn.rife2:bld-jacoco-report:0.9.6
bld.extension-pmd=com.uwyn.rife2:bld-pmd:1.1.2
bld.repositories=MAVEN_CENTRAL,MAVEN_LOCAL,RIFE2_SNAPSHOTS,RIFE2_RELEASES
bld.sourceDirectories=
bld.version=1.9.1
bld.version=2.0.0-SNAPSHOT

View file

@ -25,8 +25,7 @@ import rife.bld.publish.PublishScm;
import java.io.IOException;
import java.util.List;
import static rife.bld.dependencies.Repository.MAVEN_CENTRAL;
import static rife.bld.dependencies.Repository.RIFE2_RELEASES;
import static rife.bld.dependencies.Repository.*;
import static rife.bld.dependencies.Scope.compile;
import static rife.bld.dependencies.Scope.test;
import static rife.bld.operations.JavadocOptions.DocLinkOption.NO_MISSING;
@ -35,20 +34,20 @@ public class CheckstyleOperationBuild extends Project {
public CheckstyleOperationBuild() {
pkg = "rife.bld.extension";
name = "CheckstyleOperation";
version = version(1, 0, 1);
version = version(1, 0, 2, "SNAPSHOT");
javaRelease = 17;
downloadSources = true;
autoDownloadPurge = true;
repositories = List.of(MAVEN_CENTRAL, RIFE2_RELEASES);
repositories = List.of(MAVEN_LOCAL, MAVEN_CENTRAL, RIFE2_RELEASES, RIFE2_SNAPSHOTS);
scope(compile)
.include(dependency("com.uwyn.rife2", "bld", version(1, 9, 1)));
.include(dependency("com.uwyn.rife2", "bld", version(2, 0, 0, "SNAPSHOT")));
scope(test)
.include(dependency("com.puppycrawl.tools", "checkstyle", version(10, 17, 0)))
.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)));
.include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 10, 3)))
.include(dependency("org.junit.platform", "junit-platform-console-standalone", version(1, 10, 3)))
.include(dependency("org.assertj", "assertj-core", version(3, 26, 3)));
javadocOperation()
.javadocOptions()
.author()
@ -90,14 +89,14 @@ public class CheckstyleOperationBuild extends Project {
}
@BuildCommand(summary = "Generates JaCoCo Reports")
public void jacoco() throws IOException {
public void jacoco() throws Exception {
new JacocoReportOperation()
.fromProject(this)
.execute();
}
@BuildCommand(summary = "Runs PMD analysis")
public void pmd() {
public void pmd() throws Exception {
new PmdOperation()
.fromProject(this)
.failOnViolation(true)

View file

@ -17,11 +17,15 @@
package rife.bld.extension;
import rife.bld.BaseProject;
import rife.bld.extension.checkstyle.OutputFormat;
import rife.bld.operations.AbstractProcessOperation;
import rife.bld.operations.exceptions.ExitStatusException;
import java.nio.file.Path;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
@ -32,16 +36,11 @@ import java.util.logging.Logger;
*/
public class CheckstyleOperation extends AbstractProcessOperation<CheckstyleOperation> {
private static final Logger LOGGER = Logger.getLogger(CheckstyleOperation.class.getName());
protected final List<String> exclude = new ArrayList<>();
protected final List<String> excludeRegex = new ArrayList<>();
/**
* The command line options.
*/
protected final Map<String, String> options = new ConcurrentHashMap<>();
/**
* The source files(s) or folder(s).
*/
protected final Set<String> sourceDirs = new TreeSet<>();
private final Collection<String> excludeRegex_ = new ArrayList<>();
private final Collection<File> exclude_ = new ArrayList<>();
private final Map<String, String> options_ = new ConcurrentHashMap<>();
private final Set<File> sourceDir_ = new TreeSet<>();
private BaseProject project_;
/**
@ -52,7 +51,7 @@ public class CheckstyleOperation extends AbstractProcessOperation<CheckstyleOper
*/
public CheckstyleOperation branchMatchingXpath(String xPathQuery) {
if (isNotBlank(xPathQuery)) {
options.put("-b", xPathQuery);
options_.put("-b", xPathQuery);
}
return this;
}
@ -67,11 +66,23 @@ public class CheckstyleOperation extends AbstractProcessOperation<CheckstyleOper
*/
public CheckstyleOperation configurationFile(String file) {
if (isNotBlank(file)) {
options.put("-c", file);
options_.put("-c", file);
}
return this;
}
/**
* Specifies the location of the file that defines the configuration modules. The location can either be a
* filesystem location, or a name passed to the {@link ClassLoader#getResource(String) ClassLoader.getResource() }
* method. A configuration file is required.
*
* @param file the file
* @return the checkstyle operation
*/
public CheckstyleOperation configurationFile(File file) {
return configurationFile(file.getAbsolutePath());
}
/**
* Prints all debug logging of CheckStyle utility.
*
@ -80,9 +91,9 @@ public class CheckstyleOperation extends AbstractProcessOperation<CheckstyleOper
*/
public CheckstyleOperation debug(boolean isDebug) {
if (isDebug) {
options.put("-d", "");
options_.put("-d", "");
} else {
options.remove("-d");
options_.remove("-d");
}
return this;
}
@ -96,11 +107,20 @@ public class CheckstyleOperation extends AbstractProcessOperation<CheckstyleOper
* @see #sourceDir(Collection)
*/
public CheckstyleOperation exclude(String... path) {
for (var p : path) {
if (isNotBlank(p)) {
exclude.add(p);
}
}
exclude_.addAll(Arrays.stream(path).map(File::new).toList());
return this;
}
/**
* Directory/file to exclude from CheckStyle. The path can be the full, absolute path, or relative to the current
* path. Multiple excludes are allowed.
*
* @param path one or more paths
* @return the checkstyle operation
* @see #sourceDir(Collection)
*/
public CheckstyleOperation exclude(File... path) {
exclude_.addAll(List.of(path));
return this;
}
@ -112,12 +132,8 @@ public class CheckstyleOperation extends AbstractProcessOperation<CheckstyleOper
* @return the checkstyle operation
* @see #exclude(String...)
*/
public CheckstyleOperation exclude(Collection<String> paths) {
for (var p : paths) {
if (isNotBlank(p)) {
exclude.add(p);
}
}
public CheckstyleOperation exclude(Collection<File> paths) {
exclude_.addAll(paths);
return this;
}
@ -129,12 +145,7 @@ public class CheckstyleOperation extends AbstractProcessOperation<CheckstyleOper
* @see #excludeRegex(Collection)
*/
public CheckstyleOperation excludeRegex(String... regex) {
for (var r : regex) {
if (isNotBlank(r)) {
excludeRegex.add(r);
}
}
excludeRegex_.addAll(List.of(regex));
return this;
}
@ -146,61 +157,72 @@ public class CheckstyleOperation extends AbstractProcessOperation<CheckstyleOper
* @see #excludeRegex(String...)
*/
public CheckstyleOperation excludeRegex(Collection<String> regex) {
for (var r : regex) {
if (isNotBlank(r)) {
excludeRegex.add(r);
}
}
excludeRegex_.addAll(regex);
return this;
}
@Override
public void execute() throws IOException, InterruptedException, ExitStatusException {
if (project_ == null) {
if (LOGGER.isLoggable(Level.SEVERE) && !silent()) {
LOGGER.severe("A project must be specified.");
}
throw new ExitStatusException(ExitStatusException.EXIT_FAILURE);
} else {
super.execute();
}
}
/**
* Part of the {@link #execute} operation, constructs the command list
* to use for building the process.
*/
@Override
protected List<String> executeConstructProcessCommandList() {
if (project_ == null) {
LOGGER.severe("A project must be specified.");
} else if (sourceDirs.isEmpty()) {
sourceDirs.add(project_.srcMainJavaDirectory().getPath());
sourceDirs.add(project_.srcTestJavaDirectory().getPath());
}
final List<String> args = new ArrayList<>();
args.add(javaTool());
args.add("-cp");
args.add(String.format("%s:%s:%s:%s", Path.of(project_.libTestDirectory().getPath(), "*"),
Path.of(project_.libCompileDirectory().getPath(), "*"), project_.buildMainDirectory(),
project_.buildTestDirectory()));
args.add("com.puppycrawl.tools.checkstyle.Main");
options.forEach((k, v) -> {
args.add(k);
if (!v.isEmpty()) {
args.add(v);
if (project_ != null) {
if (sourceDir_.isEmpty()) {
sourceDir_.add(project_.srcMainJavaDirectory());
sourceDir_.add(project_.srcTestJavaDirectory());
}
});
args.add(javaTool());
if (!exclude.isEmpty()) {
for (var e : exclude) {
if (isNotBlank(e)) {
args.add("-e " + e);
args.add("-cp");
args.add(String.format("%s:%s:%s:%s", new File(project_.libTestDirectory(), "*"),
new File(project_.libCompileDirectory(), "*"), project_.buildMainDirectory(),
project_.buildTestDirectory()));
args.add("com.puppycrawl.tools.checkstyle.Main");
options_.forEach((k, v) -> {
args.add(k);
if (!v.isEmpty()) {
args.add(v);
}
});
if (!exclude_.isEmpty()) {
for (var e : exclude_) {
if (e.exists()) {
args.add("-e " + e.getAbsolutePath());
}
}
}
}
if (!excludeRegex.isEmpty()) {
for (var e : excludeRegex) {
if (isNotBlank(e)) {
args.add("-x " + e);
if (!excludeRegex_.isEmpty()) {
for (var e : excludeRegex_) {
if (isNotBlank(e)) {
args.add("-x " + e);
}
}
}
}
args.addAll(sourceDirs);
args.addAll(sourceDir_.stream().map(File::getAbsolutePath).toList());
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, String.join(" ", args));
}
}
return args;
}
@ -222,25 +244,25 @@ public class CheckstyleOperation extends AbstractProcessOperation<CheckstyleOper
*/
public CheckstyleOperation executeIgnoredModules(boolean isAllowIgnoreModules) {
if (isAllowIgnoreModules) {
options.put("-E", "");
options_.put("-E", "");
} else {
options.remove("-E");
options_.remove("-E");
}
return this;
}
/**
* Specifies the output format. Valid values: {@link CheckstyleFormatOption#XML},
* {@link CheckstyleFormatOption#SARIF}, {@link CheckstyleFormatOption#PLAIN} for the XML, sarif and default logger
* Specifies the output format. Valid values: {@link OutputFormat#XML},
* {@link OutputFormat#SARIF}, {@link OutputFormat#PLAIN} for the XML, sarif and default logger
* respectively.
* <p>
* Defaults to {@link CheckstyleFormatOption#PLAIN}.
* Defaults to {@link OutputFormat#PLAIN}.
*
* @param format the output format
* @return the checkstyle operation
*/
public CheckstyleOperation format(CheckstyleFormatOption format) {
options.put("-f", format.label.toLowerCase());
public CheckstyleOperation format(OutputFormat format) {
options_.put("-f", format.label.toLowerCase());
return this;
}
@ -255,9 +277,9 @@ public class CheckstyleOperation extends AbstractProcessOperation<CheckstyleOper
*/
public CheckstyleOperation generateXpathSuppression(boolean xPathSuppression) {
if (xPathSuppression) {
options.put("-g", "");
options_.put("-g", "");
} else {
options.remove("-g");
options_.remove("-g");
}
return this;
}
@ -279,13 +301,22 @@ public class CheckstyleOperation extends AbstractProcessOperation<CheckstyleOper
*/
public CheckstyleOperation javadocTree(boolean isTree) {
if (isTree) {
options.put("-j", "");
options_.put("-j", "");
} else {
options.remove("-j");
options_.remove("-j");
}
return this;
}
/**
* Returns the command line options.
*
* @return the command line options
*/
public Map<String, String> options() {
return options_;
}
/**
* Sets the output file.
* <p>
@ -296,11 +327,23 @@ public class CheckstyleOperation extends AbstractProcessOperation<CheckstyleOper
*/
public CheckstyleOperation outputPath(String file) {
if (isNotBlank(file)) {
options.put("-o", file);
options_.put("-o", file);
}
return this;
}
/**
* Sets the output file.
* <p>
* Defaults to stdout.
*
* @param file the output file
* @return the checkstyle operation
*/
public CheckstyleOperation outputPath(File file) {
return outputPath(file.getAbsolutePath());
}
/**
* Sets the property files to load.
*
@ -309,11 +352,21 @@ public class CheckstyleOperation extends AbstractProcessOperation<CheckstyleOper
*/
public CheckstyleOperation propertiesFile(String file) {
if (isNotBlank(file)) {
options.put("-p", file);
options_.put("-p", file);
}
return this;
}
/**
* Sets the property files to load.
*
* @param file the file
* @return the checkstyle operation
*/
public CheckstyleOperation propertiesFile(File file) {
return propertiesFile(file.getAbsolutePath());
}
/**
* Specified the file(s) or folder(s) containing the source files to check.
*
@ -322,7 +375,19 @@ public class CheckstyleOperation extends AbstractProcessOperation<CheckstyleOper
* @see #sourceDir(Collection)
*/
public CheckstyleOperation sourceDir(String... dir) {
sourceDirs.addAll(Arrays.stream(dir).filter(this::isNotBlank).toList());
sourceDir_.addAll(Arrays.stream(dir).map(File::new).toList());
return this;
}
/**
* Specified the file(s) or folder(s) containing the source files to check.
*
* @param dir one or more directories
* @return the checkstyle operation
* @see #sourceDir(Collection)
*/
public CheckstyleOperation sourceDir(File... dir) {
sourceDir_.addAll(List.of(dir));
return this;
}
@ -333,11 +398,20 @@ public class CheckstyleOperation extends AbstractProcessOperation<CheckstyleOper
* @return the checkstyle operation
* @see #sourceDir(String...)
*/
public CheckstyleOperation sourceDir(Collection<String> dirs) {
sourceDirs.addAll(dirs.stream().filter(this::isNotBlank).toList());
public CheckstyleOperation sourceDir(Collection<File> dirs) {
sourceDir_.addAll(dirs);
return this;
}
/**
* Returns the file(s) or folders(s) containing the sources files to check
*
* @return the files or directories
*/
public Set<File> sourceDir() {
return sourceDir_;
}
/**
* Prints xpath suppressions at the file's line and column position. Argument is the line and column number
* (separated by a {@code :} ) in the file that the suppression should be generated for. The option cannot be
@ -352,7 +426,7 @@ public class CheckstyleOperation extends AbstractProcessOperation<CheckstyleOper
*/
public CheckstyleOperation suppressionLineColumnNumber(String lineColumnNumber) {
if (isNotBlank(lineColumnNumber)) {
options.put("-s", lineColumnNumber);
options_.put("-s", lineColumnNumber);
}
return this;
}
@ -367,7 +441,7 @@ public class CheckstyleOperation extends AbstractProcessOperation<CheckstyleOper
* @return the checkstyle operation
*/
public CheckstyleOperation tabWith(int length) {
options.put("-w", String.valueOf(length));
options_.put("-w", String.valueOf(length));
return this;
}
@ -380,9 +454,9 @@ public class CheckstyleOperation extends AbstractProcessOperation<CheckstyleOper
*/
public CheckstyleOperation tree(boolean isTree) {
if (isTree) {
options.put("-t", "");
options_.put("-t", "");
} else {
options.remove("-t");
options_.remove("-t");
}
return this;
}
@ -396,9 +470,9 @@ public class CheckstyleOperation extends AbstractProcessOperation<CheckstyleOper
*/
public CheckstyleOperation treeWithComments(boolean isTree) {
if (isTree) {
options.put("-T", "");
options_.put("-T", "");
} else {
options.remove("-T");
options_.remove("-T");
}
return this;
}
@ -412,9 +486,9 @@ public class CheckstyleOperation extends AbstractProcessOperation<CheckstyleOper
*/
public CheckstyleOperation treeWithJavadoc(boolean isTree) {
if (isTree) {
options.put("-J", "");
options_.put("-J", "");
} else {
options.remove("-J");
options_.remove("-J");
}
return this;
}

View file

@ -1,5 +1,5 @@
/*
* Copyright 2023-2004 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.
@ -14,19 +14,19 @@
* limitations under the License.
*/
package rife.bld.extension;
package rife.bld.extension.checkstyle;
/**
* The Checkstyle output format for XML, sarif and default (plaib) logger.
*/
public enum CheckstyleFormatOption {
public enum OutputFormat {
XML("xml"),
SARIF("sarif"),
PLAIN("plain");
public final String label;
CheckstyleFormatOption(String label) {
OutputFormat(String label) {
this.label = label;
}
}

View file

@ -16,10 +16,12 @@
package rife.bld.extension;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import rife.bld.BaseProject;
import rife.bld.Project;
import rife.bld.WebProject;
import rife.bld.extension.checkstyle.OutputFormat;
import rife.bld.operations.exceptions.ExitStatusException;
import java.io.File;
@ -27,21 +29,38 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
class CheckstyleOperationTest {
public static final String SRC_MAIN_JAVA = "src/main/java";
public static final String SRC_TEST_JAVA = "src/test/java";
private static final String ADD = "add";
private static final String BAR = "bar";
private static final String FOO = "foo";
private static final String REMOVE = "remove";
@BeforeAll
static void beforeAll() {
var level = Level.ALL;
var logger = Logger.getLogger("rife.bld.extension");
var consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(level);
logger.addHandler(consoleHandler);
logger.setLevel(level);
logger.setUseParentHandlers(false);
}
@Test
void branchMatchingXpath() {
var op = new CheckstyleOperation().fromProject(new Project()).branchMatchingXpath(FOO);
assertThat(op.options.get("-b")).isEqualTo(FOO);
assertThat(op.options().get("-b")).isEqualTo(FOO);
}
@Test
@ -53,16 +72,16 @@ class CheckstyleOperationTest {
var params = new CheckstyleOperation()
.fromProject(new Project())
.branchMatchingXpath("xpath")
.configurationFile("config")
.debug(true)
.exclude("path")
.configurationFile(new File("config"))
.exclude(SRC_MAIN_JAVA)
.excludeRegex("regex")
.executeIgnoredModules(true)
.format(CheckstyleFormatOption.XML)
.format(OutputFormat.XML)
.generateXpathSuppression(true)
.javadocTree(true)
.outputPath("optionPath")
.propertiesFile("properties")
.outputPath(new File("optionPath"))
.propertiesFile(new File("properties"))
.suppressionLineColumnNumber("12")
.tabWith(1)
.tree(true)
@ -85,25 +104,33 @@ class CheckstyleOperationTest {
@Test
void configurationFile() {
var op = new CheckstyleOperation().fromProject(new Project()).configurationFile(FOO);
assertThat(op.options.get("-c")).isEqualTo(FOO);
assertThat(op.options().get("-c")).isEqualTo(FOO);
}
@Test
void debug() {
var op = new CheckstyleOperation().fromProject(new Project()).debug(true);
assertThat(op.options.containsKey("-d")).as(ADD).isTrue();
assertThat(op.options().containsKey("-d")).as(ADD).isTrue();
op = op.debug(false);
assertThat(op.options.containsKey("-d")).as(REMOVE).isFalse();
assertThat(op.options().containsKey("-d")).as(REMOVE).isFalse();
}
@Test
void exclude() {
var op = new CheckstyleOperation().fromProject(new Project()).exclude(FOO, BAR);
var foo = new File(SRC_MAIN_JAVA);
var bar = new File(SRC_TEST_JAVA);
var op = new CheckstyleOperation().fromProject(new Project()).exclude(SRC_MAIN_JAVA, SRC_TEST_JAVA);
var e = "-e ";
assertThat(op.executeConstructProcessCommandList()).contains(e + FOO, e + BAR);
assertThat(op.executeConstructProcessCommandList()).as("strings")
.contains(e + foo.getAbsolutePath()).contains(e + bar.getAbsolutePath());
op = new CheckstyleOperation().fromProject(new Project()).exclude(List.of(FOO, BAR));
assertThat(op.executeConstructProcessCommandList()).as("as list").contains(e + FOO, e + BAR);
op = new CheckstyleOperation().fromProject(new Project()).exclude(foo, bar);
assertThat(op.executeConstructProcessCommandList()).as("files")
.contains(e + foo.getAbsolutePath()).contains(e + bar.getAbsolutePath());
op = new CheckstyleOperation().fromProject(new Project()).exclude(List.of(foo, bar));
assertThat(op.executeConstructProcessCommandList()).as("list")
.contains(e + foo.getAbsolutePath()).contains(e + bar.getAbsolutePath());
}
@Test
@ -122,13 +149,19 @@ class CheckstyleOperationTest {
tmpFile.deleteOnExit();
var op = new CheckstyleOperation()
.fromProject(new WebProject())
.sourceDir("src/main/java", "src/test/java")
.sourceDir(SRC_MAIN_JAVA, SRC_TEST_JAVA)
.configurationFile("src/test/resources/google_checks.xml")
.outputPath(tmpFile.getAbsolutePath());
op.execute();
assertThat(tmpFile).exists();
}
@Test
void executeNoProject() {
var op = new CheckstyleOperation();
assertThatCode(op::execute).isInstanceOf(ExitStatusException.class);
}
@Test
void executeConstructProcessCommandList() {
var op = new CheckstyleOperation().fromProject(new BaseProject())
@ -137,7 +170,7 @@ class CheckstyleOperationTest {
.propertiesFile("config/checkstyle.properties")
.debug(true)
.executeIgnoredModules(true)
.sourceDir("src/main/java", "src/test/java");
.sourceDir(SRC_MAIN_JAVA, SRC_TEST_JAVA);
assertThat(String.join(" ", op.executeConstructProcessCommandList()))
.startsWith("java -cp ")
.endsWith(
@ -146,15 +179,16 @@ class CheckstyleOperationTest {
"-b xpath " +
"-c config/checkstyle.xml " +
"-d -E " +
"src/main/java src/test/java");
new File(SRC_MAIN_JAVA).getAbsolutePath() + " " +
new File(SRC_TEST_JAVA).getAbsolutePath());
}
@Test
void executeIgnoredModules() {
var op = new CheckstyleOperation().fromProject(new Project()).executeIgnoredModules(true);
assertThat(op.options.containsKey("-E")).as(ADD).isTrue();
assertThat(op.options().containsKey("-E")).as(ADD).isTrue();
op = op.executeIgnoredModules(false);
assertThat(op.options.containsKey("-E")).as(REMOVE).isFalse();
assertThat(op.options().containsKey("-E")).as(REMOVE).isFalse();
}
@Test
@ -163,7 +197,7 @@ class CheckstyleOperationTest {
tmpFile.deleteOnExit();
var op = new CheckstyleOperation()
.fromProject(new WebProject())
.sourceDir(List.of("src/main/java", "src/test/java"))
.sourceDir(SRC_MAIN_JAVA, SRC_TEST_JAVA)
.configurationFile("src/test/resources/sun_checks.xml")
.outputPath(tmpFile.getAbsolutePath());
assertThatCode(op::execute).isInstanceOf(ExitStatusException.class);
@ -172,79 +206,88 @@ class CheckstyleOperationTest {
@Test
void format() {
var op = new CheckstyleOperation().fromProject(new Project()).format(CheckstyleFormatOption.XML);
assertThat(op.options.get("-f")).isEqualTo("xml");
var op = new CheckstyleOperation().fromProject(new Project()).format(OutputFormat.XML);
assertThat(op.options().get("-f")).isEqualTo("xml");
}
@Test
void generateXpathSuppression() {
var op = new CheckstyleOperation().fromProject(new Project()).generateXpathSuppression(true);
assertThat(op.options.containsKey("-g")).as(ADD).isTrue();
assertThat(op.options().containsKey("-g")).as(ADD).isTrue();
op = op.generateXpathSuppression(false);
assertThat(op.options.containsKey("-g")).as(REMOVE).isFalse();
assertThat(op.options().containsKey("-g")).as(REMOVE).isFalse();
}
@Test
void javadocTree() {
var op = new CheckstyleOperation().fromProject(new Project()).javadocTree(true);
assertThat(op.options.containsKey("-j")).as(ADD).isTrue();
assertThat(op.options().containsKey("-j")).as(ADD).isTrue();
op = op.javadocTree(false);
assertThat(op.options.containsKey("-j")).as(REMOVE).isFalse();
assertThat(op.options().containsKey("-j")).as(REMOVE).isFalse();
}
@Test
void outputPath() {
var op = new CheckstyleOperation().fromProject(new Project()).outputPath(FOO);
assertThat(op.options.get("-o")).isEqualTo(FOO);
assertThat(op.options().get("-o")).isEqualTo(FOO);
}
@Test
void propertiesFile() {
var op = new CheckstyleOperation().fromProject(new Project()).propertiesFile(FOO);
assertThat(op.options.get("-p")).isEqualTo(FOO);
assertThat(op.options().get("-p")).isEqualTo(FOO);
}
@Test
void sourceDir() {
var foo = new File(FOO);
var bar = new File(BAR);
var op = new CheckstyleOperation().fromProject(new Project()).sourceDir(FOO);
assertThat(op.sourceDirs).contains(FOO);
op = op.sourceDir(FOO, BAR);
assertThat(op.sourceDirs).as("foo, bar").hasSize(2).contains(FOO).contains(BAR);
assertThat(op.sourceDir()).contains(foo);
op = op.sourceDir(foo, bar);
assertThat(op.sourceDir()).as("foo, bar").hasSize(2)
.contains(foo).contains(bar);
op = op.sourceDir(List.of(foo, bar));
assertThat(op.sourceDir()).as("List.of(foo, bar)").hasSize(2)
.contains(foo).contains(bar);
}
@Test
void suppressionLineColumnNumber() {
var op = new CheckstyleOperation().fromProject(new Project()).suppressionLineColumnNumber(FOO + ':' + BAR);
assertThat(op.options.get("-s")).isEqualTo(FOO + ':' + BAR);
assertThat(op.options().get("-s")).isEqualTo(FOO + ':' + BAR);
}
@Test
void tabWith() {
var op = new CheckstyleOperation().fromProject(new Project()).tabWith(9);
assertThat(op.options.get("-w")).isEqualTo("9");
assertThat(op.options().get("-w")).isEqualTo("9");
}
@Test
void tree() {
var op = new CheckstyleOperation().fromProject(new Project()).tree(true);
assertThat(op.options.containsKey("-t")).as(ADD).isTrue();
assertThat(op.options().containsKey("-t")).as(ADD).isTrue();
op = op.tree(false);
assertThat(op.options.containsKey("-t")).as(REMOVE).isFalse();
assertThat(op.options().containsKey("-t")).as(REMOVE).isFalse();
}
@Test
void treeWithComments() {
var op = new CheckstyleOperation().fromProject(new Project()).treeWithComments(true);
assertThat(op.options.containsKey("-T")).as(ADD).isTrue();
assertThat(op.options().containsKey("-T")).as(ADD).isTrue();
op = op.treeWithComments(false);
assertThat(op.options.containsKey("-T")).as(REMOVE).isFalse();
assertThat(op.options().containsKey("-T")).as(REMOVE).isFalse();
}
@Test
void treeWithJavadoc() {
var op = new CheckstyleOperation().fromProject(new Project()).treeWithJavadoc(true);
assertThat(op.options.containsKey("-J")).as(ADD).isTrue();
assertThat(op.options().containsKey("-J")).as(ADD).isTrue();
op = op.treeWithJavadoc(false);
assertThat(op.options.containsKey("-J")).as(REMOVE).isFalse();
assertThat(op.options().containsKey("-J")).as(REMOVE).isFalse();
}
}