forked from phoedos/pmd
Fix java cli tests
This commit is contained in:
@ -235,19 +235,14 @@ public class PMD {
|
||||
if (pmd.getRulesets().isEmpty()) {
|
||||
return PMDCommandLineInterface.NO_ERRORS_STATUS;
|
||||
}
|
||||
|
||||
Report report = pmd.performAnalysis();
|
||||
return report.getViolations().size();
|
||||
} catch (Exception e) {
|
||||
String message = e.getMessage();
|
||||
if (message != null) {
|
||||
LOG.severe(message);
|
||||
} else {
|
||||
LOG.log(Level.SEVERE, "Exception during processing", e);
|
||||
try {
|
||||
Report report = pmd.performAnalysis();
|
||||
return report.getViolations().size();
|
||||
} catch (Exception e) {
|
||||
pmd.getLog().errorEx("Exception during processing", e);
|
||||
pmd.getLog().info(PMDCommandLineInterface.buildUsageText());
|
||||
return PMDCommandLineInterface.NO_ERRORS_STATUS;
|
||||
}
|
||||
LOG.log(Level.FINE, "Exception during processing", e);
|
||||
LOG.info(PMDCommandLineInterface.buildUsageText());
|
||||
return PMDCommandLineInterface.NO_ERRORS_STATUS;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -9,6 +9,7 @@ import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import net.sourceforge.pmd.annotation.InternalApi;
|
||||
@ -50,12 +51,11 @@ import net.sourceforge.pmd.util.log.SimplePmdLogger;
|
||||
*/
|
||||
public final class PmdAnalysisBuilder implements AutoCloseable {
|
||||
|
||||
private final PmdLogger fileCollectionLogger = new SimplePmdLogger(Logger.getLogger("pmd-file-collection"));
|
||||
private final PmdLogger renderingLogger = new SimplePmdLogger(Logger.getLogger("pmd-rendering"));
|
||||
private final FileCollector collector;
|
||||
private final List<Renderer> renderers = new ArrayList<>();
|
||||
private final List<RuleSet> ruleSets = new ArrayList<>();
|
||||
private final PMDConfiguration configuration;
|
||||
private final SimplePmdLogger logger = new SimplePmdLogger(Logger.getLogger("net.sourceforge.pmd"));
|
||||
|
||||
/**
|
||||
* Constructs a new instance. The files paths (input files, filelist,
|
||||
@ -67,8 +67,10 @@ public final class PmdAnalysisBuilder implements AutoCloseable {
|
||||
this.configuration = config;
|
||||
this.collector = FileCollector.newCollector(
|
||||
config.getLanguageVersionDiscoverer(),
|
||||
fileCollectionLogger
|
||||
logger
|
||||
);
|
||||
final Level logLevel = configuration.isDebug() ? Level.FINER : Level.INFO;
|
||||
this.logger.getBackend().setLevel(logLevel);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -180,7 +182,7 @@ public final class PmdAnalysisBuilder implements AutoCloseable {
|
||||
try {
|
||||
renderer.start();
|
||||
} catch (IOException e) {
|
||||
renderingLogger.errorEx("Error while starting renderer " + renderer.getName(), e);
|
||||
logger.errorEx("Error while starting renderer " + renderer.getName(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -193,7 +195,7 @@ public final class PmdAnalysisBuilder implements AutoCloseable {
|
||||
renderer.end();
|
||||
renderer.flush();
|
||||
} catch (IOException e) {
|
||||
renderingLogger.errorEx("Error while finishing renderer " + renderer.getName(), e);
|
||||
logger.errorEx("Error while finishing renderer " + renderer.getName(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -204,10 +206,14 @@ public final class PmdAnalysisBuilder implements AutoCloseable {
|
||||
: new MonoThreadProcessor(configuration);
|
||||
}
|
||||
|
||||
public PmdLogger getLog() {
|
||||
return logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
collector.close();
|
||||
|
||||
/*
|
||||
* Make sure it's our own classloader before attempting to close it....
|
||||
* Maven + Jacoco provide us with a cloaseable classloader that if closed
|
||||
|
@ -69,15 +69,15 @@ public final class FileCollectionUtil {
|
||||
collectFiles(collector, configuration.getInputPaths());
|
||||
}
|
||||
|
||||
if (null != configuration.getInputUri()) {
|
||||
if (configuration.getInputUri() != null) {
|
||||
collectDB(collector, configuration.getInputUri());
|
||||
}
|
||||
|
||||
if (null != configuration.getInputFilePath()) {
|
||||
if (configuration.getInputFilePath() != null) {
|
||||
collectFileList(collector, configuration.getInputFilePath());
|
||||
}
|
||||
|
||||
if (null != configuration.getIgnoreFilePath()) {
|
||||
if (configuration.getIgnoreFilePath() != null) {
|
||||
// todo disable trace logs for this secondary collector
|
||||
try (FileCollector excludeCollector = FileCollector.newCollector(configuration.getLanguageVersionDiscoverer(), collector.getLog())) {
|
||||
collectFileList(excludeCollector, configuration.getIgnoreFilePath());
|
||||
|
@ -89,7 +89,7 @@ public class RuleBuilder {
|
||||
Language lang = LanguageRegistry.findLanguageByTerseName(languageName);
|
||||
if (lang == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"Unknown Language '" + languageName + "' for rule" + name + ", supported Languages are "
|
||||
"Unknown Language '" + languageName + "' for rule " + name + ", supported Languages are "
|
||||
+ LanguageRegistry.commaSeparatedTerseNamesForLanguage(LanguageRegistry.findWithRuleSupport()));
|
||||
}
|
||||
language = lang;
|
||||
|
@ -27,7 +27,6 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import net.sourceforge.pmd.PmdAnalysisBuilder;
|
||||
import net.sourceforge.pmd.annotation.Experimental;
|
||||
@ -48,7 +47,6 @@ import net.sourceforge.pmd.util.log.PmdLogger;
|
||||
@SuppressWarnings("PMD.CloseResource")
|
||||
public final class FileCollector implements AutoCloseable {
|
||||
|
||||
private static final Logger DEFAULT_LOG = Logger.getLogger(FileCollector.class.getName());
|
||||
private final List<TextFile> allFilesToProcess = new ArrayList<>();
|
||||
private final List<Closeable> resourcesToClose = new ArrayList<>();
|
||||
private Charset charset = StandardCharsets.UTF_8;
|
||||
@ -134,7 +132,7 @@ public final class FileCollector implements AutoCloseable {
|
||||
}
|
||||
LanguageVersion languageVersion = discoverLanguage(file.toString());
|
||||
if (languageVersion != null) {
|
||||
allFilesToProcess.add(new NioTextFile(file, charset, languageVersion, getDisplayName(file)));
|
||||
addFileImpl(new NioTextFile(file, charset, languageVersion, getDisplayName(file)));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@ -197,7 +195,7 @@ public final class FileCollector implements AutoCloseable {
|
||||
}
|
||||
|
||||
private void addFileImpl(TextFile textFile) {
|
||||
log.trace("Adding file {0}", textFile.getPathId());
|
||||
log.trace("Adding file {0} (lang: {1}) ", textFile.getPathId(), textFile.getLanguageVersion().getTerseName());
|
||||
allFilesToProcess.add(textFile);
|
||||
}
|
||||
|
||||
|
@ -11,6 +11,8 @@ package net.sourceforge.pmd.util.log;
|
||||
*/
|
||||
public interface PmdLogger {
|
||||
|
||||
void info(String message, Object... formatArgs);
|
||||
|
||||
void trace(String message, Object... formatArgs);
|
||||
|
||||
void debug(String message, Object... formatArgs);
|
||||
|
@ -22,6 +22,10 @@ public class SimplePmdLogger implements PmdLogger {
|
||||
this.backend = backend;
|
||||
}
|
||||
|
||||
public Logger getBackend() {
|
||||
return backend;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void trace(String message, Object... formatArgs) {
|
||||
if (backend.isLoggable(Level.FINER)) {
|
||||
@ -36,6 +40,13 @@ public class SimplePmdLogger implements PmdLogger {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void info(String message, Object... formatArgs) {
|
||||
if (backend.isLoggable(Level.INFO)) {
|
||||
backend.info(MessageFormat.format(message, formatArgs));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warning(String message, Object... formatArgs) {
|
||||
if (backend.isLoggable(Level.WARNING)) {
|
||||
|
@ -4,16 +4,15 @@
|
||||
|
||||
package net.sourceforge.pmd.cli;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.sourceforge.pmd.util.FileUtil;
|
||||
|
||||
/**
|
||||
* @author Romain Pelisse <belaran@gmail.com>
|
||||
*
|
||||
@ -21,62 +20,58 @@ import net.sourceforge.pmd.util.FileUtil;
|
||||
public class CLITest extends BaseCLITest {
|
||||
@Test
|
||||
public void minimalArgs() {
|
||||
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/bestpractices.xml,category/java/design.xml", };
|
||||
runTest(args, "minimalArgs");
|
||||
runTest("-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/bestpractices.xml,category/java/design.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void minimumPriority() {
|
||||
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml", "-min", "1", };
|
||||
runTest(args, "minimumPriority");
|
||||
runTest(args);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void usingDebug() {
|
||||
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml", "-debug", };
|
||||
runTest(args, "minimalArgsWithDebug");
|
||||
runTest("-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml", "-debug");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void usingDebugLongOption() {
|
||||
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml", "--debug", };
|
||||
runTest(args, "minimalArgsWithDebug");
|
||||
runTest("-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml", "--debug");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void changeJavaVersion() {
|
||||
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml", "-version", "1.5", "-language",
|
||||
"java", "-debug", };
|
||||
String resultFilename = runTest(args, "chgJavaVersion");
|
||||
assertTrue("Invalid Java version",
|
||||
FileUtil.findPatternInFile(new File(resultFilename), "Using Java version: Java 1.5"));
|
||||
"java", "--debug", };
|
||||
String log = runTest(args);
|
||||
Matcher matcher = Pattern.compile("Adding file .*\\.java \\(lang: java 1\\.5\\)").matcher(log);
|
||||
assertTrue(matcher.find());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exitStatusNoViolations() {
|
||||
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml", };
|
||||
runTest(args, "exitStatusNoViolations");
|
||||
runTest("-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exitStatusWithViolations() {
|
||||
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/errorprone.xml", };
|
||||
String resultFilename = runTest(args, "exitStatusWithViolations", 4);
|
||||
assertTrue(FileUtil.findPatternInFile(new File(resultFilename), "Avoid empty if"));
|
||||
String log = runTest(4, args);
|
||||
assertThat(log, containsString("Avoid empty if"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exitStatusWithViolationsAndWithoutFailOnViolations() {
|
||||
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/errorprone.xml", "-failOnViolation", "false", };
|
||||
String resultFilename = runTest(args, "exitStatusWithViolationsAndWithoutFailOnViolations", 0);
|
||||
assertTrue(FileUtil.findPatternInFile(new File(resultFilename), "Avoid empty if"));
|
||||
String log = runTest(0, args);
|
||||
assertThat(log, containsString("Avoid empty if"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exitStatusWithViolationsAndWithoutFailOnViolationsLongOption() {
|
||||
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/errorprone.xml", "--fail-on-violation", "false", };
|
||||
String resultFilename = runTest(args, "exitStatusWithViolationsAndWithoutFailOnViolations", 0);
|
||||
assertTrue(FileUtil.findPatternInFile(new File(resultFilename), "Avoid empty if"));
|
||||
String log = runTest(0, args);
|
||||
assertThat(log, containsString("Avoid empty if"));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -85,12 +80,9 @@ public class CLITest extends BaseCLITest {
|
||||
@Test
|
||||
public void testWrongRuleset() {
|
||||
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/designn.xml", };
|
||||
String filename = TEST_OUPUT_DIRECTORY + "testWrongRuleset.txt";
|
||||
createTestOutputFile(filename);
|
||||
runPMDWith(args);
|
||||
Assert.assertEquals(1, getStatusCode());
|
||||
assertTrue(FileUtil.findPatternInFile(new File(filename),
|
||||
"Can't find resource 'category/java/designn.xml' for rule 'null'." + " Make sure the resource is a valid file"));
|
||||
String log = runTest(1, args);
|
||||
assertThat(log, containsString("Can't find resource 'category/java/designn.xml' for rule 'null'."
|
||||
+ " Make sure the resource is a valid file"));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -99,12 +91,9 @@ public class CLITest extends BaseCLITest {
|
||||
@Test
|
||||
public void testWrongRulesetWithRulename() {
|
||||
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/designn.xml/UseCollectionIsEmpty", };
|
||||
String filename = TEST_OUPUT_DIRECTORY + "testWrongRuleset.txt";
|
||||
createTestOutputFile(filename);
|
||||
runPMDWith(args);
|
||||
Assert.assertEquals(1, getStatusCode());
|
||||
assertTrue(FileUtil.findPatternInFile(new File(filename),
|
||||
"Can't find resource 'category/java/designn.xml' for rule " + "'UseCollectionIsEmpty'."));
|
||||
String log = runTest(1, args);
|
||||
assertThat(log, containsString("Can't find resource 'category/java/designn.xml' for rule "
|
||||
+ "'UseCollectionIsEmpty'."));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -113,11 +102,8 @@ public class CLITest extends BaseCLITest {
|
||||
@Test
|
||||
public void testWrongRulename() {
|
||||
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml/ThisRuleDoesNotExist", };
|
||||
String filename = TEST_OUPUT_DIRECTORY + "testWrongRuleset.txt";
|
||||
createTestOutputFile(filename);
|
||||
runPMDWith(args);
|
||||
Assert.assertEquals(1, getStatusCode());
|
||||
assertTrue(FileUtil.findPatternInFile(new File(filename), Pattern
|
||||
.quote("No rules found. Maybe you misspelled a rule name?" + " (category/java/design.xml/ThisRuleDoesNotExist)")));
|
||||
String log = runTest(1, args);
|
||||
assertThat(log, containsString("No rules found. Maybe you misspelled a rule name?"
|
||||
+ " (category/java/design.xml/ThisRuleDoesNotExist)"));
|
||||
}
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ public class CLITest extends BaseCLITest {
|
||||
public void useEcmaScript() {
|
||||
String[] args = { "-d", SOURCE_FOLDER, "-f", "xml", "-R", "ecmascript-basic", "-version", "3", "-l",
|
||||
"ecmascript", "-debug", };
|
||||
String resultFilename = runTest(args, "useEcmaScript");
|
||||
String resultFilename = runTest(args);
|
||||
assertTrue("Invalid JavaScript version",
|
||||
FileUtil.findPatternInFile(new File(resultFilename), "Using Ecmascript version: Ecmascript 3"));
|
||||
}
|
||||
|
@ -4,9 +4,11 @@
|
||||
|
||||
package net.sourceforge.pmd.cli;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
@ -18,6 +20,7 @@ import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
|
||||
import net.sourceforge.pmd.PMD;
|
||||
import net.sourceforge.pmd.PMD.StatusCode;
|
||||
|
||||
/**
|
||||
* @author Romain Pelisse <belaran@gmail.com>
|
||||
@ -72,25 +75,44 @@ public abstract class BaseCLITest {
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
protected String runTest(String[] args, String testname) {
|
||||
return runTest(args, testname, 0);
|
||||
return runTest(args);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
protected String runTest(String[] args, String testname, int expectedExitCode) {
|
||||
String filename = TEST_OUPUT_DIRECTORY + testname + ".txt";
|
||||
long start = System.currentTimeMillis();
|
||||
createTestOutputFile(filename);
|
||||
System.out.println("Start running test " + testname);
|
||||
runPMDWith(args);
|
||||
checkStatusCode(expectedExitCode);
|
||||
StatusCode statusCode = PMD.runPmd(args);
|
||||
assertEquals(expectedExitCode, statusCode.toInt());
|
||||
System.out.println("Test finished successfully after " + (System.currentTimeMillis() - start) + "ms.");
|
||||
return filename;
|
||||
}
|
||||
|
||||
protected String runTest(String... args) {
|
||||
return runTest(0, args);
|
||||
}
|
||||
|
||||
protected String runTest(int expectedExitCode, String... args) {
|
||||
ByteArrayOutputStream console = new ByteArrayOutputStream();
|
||||
PrintStream out = new PrintStream(console);
|
||||
System.setOut(out);
|
||||
System.setErr(out);
|
||||
|
||||
StatusCode statusCode = PMD.runPmd(args);
|
||||
assertEquals(expectedExitCode, statusCode.toInt());
|
||||
return console.toString();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
protected void runPMDWith(String[] args) {
|
||||
PMD.main(args);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
protected void checkStatusCode(int expectedExitCode) {
|
||||
int statusCode = getStatusCode();
|
||||
if (statusCode != expectedExitCode) {
|
||||
@ -98,6 +120,7 @@ public abstract class BaseCLITest {
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
protected int getStatusCode() {
|
||||
return Integer.parseInt(System.getProperty(PMDCommandLineInterface.STATUS_CODE_PROPERTY));
|
||||
}
|
||||
|
@ -37,21 +37,21 @@ public class XmlCliTest extends BaseCLITest {
|
||||
|
||||
@Test
|
||||
public void analyzeSingleXmlWithoutForceLanguage() {
|
||||
String resultFilename = runTest(createArgs("/src/file1.ext"), "analyzeSingleXmlWithoutForceLanguage", 0);
|
||||
String resultFilename = runTest(createArgs("/src/file1.ext"), 0);
|
||||
assertRuleMessage(0, resultFilename);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void analyzeSingleXmlWithForceLanguage() {
|
||||
String resultFilename = runTest(createArgs("/src/file1.ext", "-force-language", "xml"),
|
||||
"analyzeSingleXmlWithForceLanguage", 4);
|
||||
4);
|
||||
assertRuleMessage(1, resultFilename);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void analyzeDirectoryWithForceLanguage() {
|
||||
String resultFilename = runTest(createArgs("/src/", "-force-language", "xml"),
|
||||
"analyzeDirectoryWithForceLanguage", 4);
|
||||
4);
|
||||
assertRuleMessage(3, resultFilename);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user