More javadocs and tests
This commit is contained in:
parent
ef8b6df387
commit
cbbf5a7f2d
3 changed files with 203 additions and 2 deletions
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -33,32 +33,121 @@ import java.util.Objects;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs static code analysis with <a href="https://pmd.github.io/">PMD</a>.
|
||||||
|
*
|
||||||
|
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
|
||||||
|
* @since 1.0
|
||||||
|
*/
|
||||||
public class PmdOperation extends AbstractOperation<PmdOperation> {
|
public class PmdOperation extends AbstractOperation<PmdOperation> {
|
||||||
|
/**
|
||||||
|
* The default rule set.
|
||||||
|
*/
|
||||||
|
public static final String RULESET_DEFAULT = "rulesets/java/quickstart.xml";
|
||||||
private static final Logger LOGGER = Logger.getLogger(PmdOperation.class.getName());
|
private static final Logger LOGGER = Logger.getLogger(PmdOperation.class.getName());
|
||||||
private static final String PMD_DIR = "pmd";
|
private static final String PMD_DIR = "pmd";
|
||||||
public static final String RULESET_DEFAULT = "rulesets/java/quickstart.xml";
|
|
||||||
|
/**
|
||||||
|
* The cache location.
|
||||||
|
*/
|
||||||
Path cache;
|
Path cache;
|
||||||
|
/**
|
||||||
|
* The debug toggle.
|
||||||
|
*/
|
||||||
boolean debug;
|
boolean debug;
|
||||||
|
/**
|
||||||
|
* The encoding.
|
||||||
|
*/
|
||||||
String encoding = "UTF-8";
|
String encoding = "UTF-8";
|
||||||
|
/**
|
||||||
|
* The fail on violation toggle.
|
||||||
|
*/
|
||||||
boolean failOnViolation;
|
boolean failOnViolation;
|
||||||
|
/**
|
||||||
|
* The path of the ignore file
|
||||||
|
*/
|
||||||
Path ignoreFile;
|
Path ignoreFile;
|
||||||
|
/**
|
||||||
|
* The incremental analysis toggle.
|
||||||
|
*/
|
||||||
boolean incrementalAnalysis = true;
|
boolean incrementalAnalysis = true;
|
||||||
|
/**
|
||||||
|
* The input paths (source) list.
|
||||||
|
*/
|
||||||
List<Path> inputPaths = new ArrayList<>();
|
List<Path> inputPaths = new ArrayList<>();
|
||||||
|
/**
|
||||||
|
* The input URI.
|
||||||
|
*/
|
||||||
URI inputUri;
|
URI inputUri;
|
||||||
|
/**
|
||||||
|
* The language version.
|
||||||
|
*/
|
||||||
LanguageVersion languageVersion;
|
LanguageVersion languageVersion;
|
||||||
|
/**
|
||||||
|
* The path to the report page.
|
||||||
|
*/
|
||||||
Path reportFile;
|
Path reportFile;
|
||||||
|
/**
|
||||||
|
* The report format.
|
||||||
|
*/
|
||||||
String reportFormat = "text";
|
String reportFormat = "text";
|
||||||
|
/**
|
||||||
|
* The rule priority.
|
||||||
|
*/
|
||||||
RulePriority rulePriority = RulePriority.LOW;
|
RulePriority rulePriority = RulePriority.LOW;
|
||||||
|
/**
|
||||||
|
* The rule sets list.
|
||||||
|
*/
|
||||||
List<String> ruleSets = new ArrayList<>();
|
List<String> ruleSets = new ArrayList<>();
|
||||||
|
/**
|
||||||
|
* The show suppressed flag.
|
||||||
|
*/
|
||||||
boolean showSuppressed;
|
boolean showSuppressed;
|
||||||
|
/**
|
||||||
|
* THe suppressed marker.
|
||||||
|
*/
|
||||||
String suppressedMarker = "NOPMD";
|
String suppressedMarker = "NOPMD";
|
||||||
|
/**
|
||||||
|
* The number of threads.
|
||||||
|
*/
|
||||||
int threads = 1;
|
int threads = 1;
|
||||||
|
/**
|
||||||
|
* The project reference.
|
||||||
|
*/
|
||||||
private Project project;
|
private Project project;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new operation.
|
||||||
|
* <p>
|
||||||
|
* The defaults are:
|
||||||
|
* <ul>
|
||||||
|
* <li>encoding={@code UTF-9}</li>
|
||||||
|
* <li>incrementAnalysis={@code true}</li>
|
||||||
|
* <li>reportFormat={@code text}</li>
|
||||||
|
* <li>rulePriority={@code LOW}</li>
|
||||||
|
* <li>suppressedMarker={@code NOPMD}</li>
|
||||||
|
* </ul>
|
||||||
|
*/
|
||||||
public PmdOperation() {
|
public PmdOperation() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new operation.
|
||||||
|
* <p>
|
||||||
|
* The defaults are:
|
||||||
|
* <ul>
|
||||||
|
* <li>cache={@code build/pmd/pmd-cache}</li>
|
||||||
|
* <li>encoding={@code UTF-9}</li>
|
||||||
|
* <li>incrementAnalysis={@code true}</li>
|
||||||
|
* <li>inputPaths={@code [src/main, src/test]}</li>
|
||||||
|
* <li>reportFile={@code build/pmd/pmd-report-txt}</li>
|
||||||
|
* <li>reportFormat={@code text}</li>
|
||||||
|
* <li>rulePriority={@code LOW}</li>
|
||||||
|
* <li>ruleSets={@code [rulesets/java/quickstart.xml]}</li>
|
||||||
|
* <li>suppressedMarker={@code NOPMD}</li>
|
||||||
|
* </ul>
|
||||||
|
*/
|
||||||
public PmdOperation(Project project) {
|
public PmdOperation(Project project) {
|
||||||
super();
|
super();
|
||||||
this.project = project;
|
this.project = project;
|
||||||
|
@ -80,6 +169,19 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a new rule set path.
|
* Adds a new rule set path.
|
||||||
|
* <p>
|
||||||
|
* The built-in rule set paths are:
|
||||||
|
* <ul>
|
||||||
|
* <li>{@code rulesets/java/quickstart.xml}</li>
|
||||||
|
* <li>{@code category/java/bestpractices.xml}</li>
|
||||||
|
* <li>{@code category/java/codestyle.xml}</li>
|
||||||
|
* <li>{@code category/java/design.xml}</li>
|
||||||
|
* <li>{@code category/java/documentation.xml}</li>
|
||||||
|
* <li>{@code category/java/errorprone.xml}</li>
|
||||||
|
* <li>{@code category/java/multithreading.xml}</li>
|
||||||
|
* <li>{@code category/java/performance.xml}</li>
|
||||||
|
* <li>{@code category/java/security.xml}</li>
|
||||||
|
* </ul>
|
||||||
*
|
*
|
||||||
* @see #ruleSets(String...)
|
* @see #ruleSets(String...)
|
||||||
*/
|
*/
|
||||||
|
@ -161,6 +263,9 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new initialized configuration.
|
||||||
|
*/
|
||||||
public PMDConfiguration initConfiguration(String commandName) {
|
public PMDConfiguration initConfiguration(String commandName) {
|
||||||
PMDConfiguration config = new PMDConfiguration();
|
PMDConfiguration config = new PMDConfiguration();
|
||||||
if (cache == null && project != null && incrementalAnalysis) {
|
if (cache == null && project != null && incrementalAnalysis) {
|
||||||
|
@ -214,6 +319,9 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs the PMD analysis with the given config.
|
||||||
|
*/
|
||||||
public int performPmdAnalysis(String commandName, PMDConfiguration config) throws RuntimeException {
|
public int performPmdAnalysis(String commandName, PMDConfiguration config) throws RuntimeException {
|
||||||
var pmd = PmdAnalysis.create(config);
|
var pmd = PmdAnalysis.create(config);
|
||||||
var report = pmd.performAnalysisAndCollectReport();
|
var report = pmd.performAnalysisAndCollectReport();
|
||||||
|
@ -238,6 +346,17 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
|
||||||
LOGGER.warning(msg);
|
LOGGER.warning(msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
var rules = pmd.getRulesets();
|
||||||
|
if (rules.size() > 0) {
|
||||||
|
int count = 0;
|
||||||
|
for (var rule : rules) {
|
||||||
|
count += rule.getRules().size();
|
||||||
|
}
|
||||||
|
if (LOGGER.isLoggable(Level.INFO)) {
|
||||||
|
LOGGER.info(String.format("[%s] %d rules were checked.", commandName, count));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return numErrors;
|
return numErrors;
|
||||||
}
|
}
|
||||||
|
@ -252,6 +371,19 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the rule set path(s).
|
* Sets the rule set path(s).
|
||||||
|
* <p>
|
||||||
|
* The built-in rule set paths are:
|
||||||
|
* <ul>
|
||||||
|
* <li>{@code rulesets/java/quickstart.xml}</li>
|
||||||
|
* <li>{@code category/java/bestpractices.xml}</li>
|
||||||
|
* <li>{@code category/java/codestyle.xml}</li>
|
||||||
|
* <li>{@code category/java/design.xml}</li>
|
||||||
|
* <li>{@code category/java/documentation.xml}</li>
|
||||||
|
* <li>{@code category/java/errorprone.xml}</li>
|
||||||
|
* <li>{@code category/java/multithreading.xml}</li>
|
||||||
|
* <li>{@code category/java/performance.xml}</li>
|
||||||
|
* <li>{@code category/java/security.xml}</li>
|
||||||
|
* </ul>
|
||||||
*/
|
*/
|
||||||
public PmdOperation ruleSets(String... ruleSet) {
|
public PmdOperation ruleSets(String... ruleSet) {
|
||||||
ruleSets.clear();
|
ruleSets.clear();
|
||||||
|
|
|
@ -56,6 +56,69 @@ public class PmdOperationTest {
|
||||||
.as("no errors").isEqualTo(0);
|
.as("no errors").isEqualTo(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testJavaQuickStart() {
|
||||||
|
var pmd = PMD_OPERATION.ruleSets("rulesets/java/quickstart.xml");
|
||||||
|
assertThat(pmd.performPmdAnalysis("test", pmd.initConfiguration("pmd")))
|
||||||
|
.as("no errors").isEqualTo(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testJavaErrorProne() {
|
||||||
|
var pmd = PMD_OPERATION.ruleSets("category/java/errorprone.xml");
|
||||||
|
assertThat(pmd.performPmdAnalysis("test", pmd.initConfiguration("pmd")))
|
||||||
|
.as("no errors").isGreaterThan(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testJavaCodeStyle() {
|
||||||
|
var pmd = PMD_OPERATION.ruleSets("category/java/codestyle.xml");
|
||||||
|
assertThat(pmd.performPmdAnalysis("test", pmd.initConfiguration("pmd")))
|
||||||
|
.as("no errors").isGreaterThan(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testJavaDesign() {
|
||||||
|
var pmd = PMD_OPERATION.ruleSets("category/java/design.xml");
|
||||||
|
assertThat(pmd.performPmdAnalysis("test", pmd.initConfiguration("pmd")))
|
||||||
|
.as("no errors").isGreaterThan(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testJavaDocumentation() {
|
||||||
|
var pmd = PMD_OPERATION.ruleSets("category/java/documentation.xml");
|
||||||
|
assertThat(pmd.performPmdAnalysis("test", pmd.initConfiguration("pmd")))
|
||||||
|
.as("no errors").isGreaterThan(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testJavaBestPractices() {
|
||||||
|
var pmd = PMD_OPERATION.ruleSets("category/java/bestpractices.xml");
|
||||||
|
assertThat(pmd.performPmdAnalysis("test", pmd.initConfiguration("pmd")))
|
||||||
|
.as("no errors").isEqualTo(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testJavaMultiThreading() {
|
||||||
|
var pmd = PMD_OPERATION.ruleSets("category/java/multithreading");
|
||||||
|
assertThat(pmd.performPmdAnalysis("test", pmd.initConfiguration("pmd")))
|
||||||
|
.as("no errors").isEqualTo(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testJavaPerformance() {
|
||||||
|
var pmd = PMD_OPERATION.ruleSets("category/java/performance.xml");
|
||||||
|
assertThat(pmd.performPmdAnalysis("test", pmd.initConfiguration("pmd")))
|
||||||
|
.as("no errors").isEqualTo(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testJavaSecurity() {
|
||||||
|
var pmd = PMD_OPERATION.ruleSets("category/java/security.xml");
|
||||||
|
assertThat(pmd.performPmdAnalysis("test", pmd.initConfiguration("pmd")))
|
||||||
|
.as("no errors").isEqualTo(0);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testPmdOperation() {
|
void testPmdOperation() {
|
||||||
assertThat(PMD_OPERATION.performPmdAnalysis("test", PMD_OPERATION.initConfiguration("pmd")))
|
assertThat(PMD_OPERATION.performPmdAnalysis("test", PMD_OPERATION.initConfiguration("pmd")))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue