diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 2835228f20..c5e1fe39d1 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -70,6 +70,7 @@ The CLI itself remains compatible, if you run PMD via command-line, no action is {% jdoc core::PMDConfiguration#setRuleSets(java.util.List) %}, {% jdoc core::PMDConfiguration#addRuleSet(java.lang.String) %}, and {% jdoc core::PMDConfiguration#getRuleSetPaths() %}. +* Several members of {% jdoc test::cli.BaseCLITest %} have been deprecated with replacements. #### Experimental APIs diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/cli/PMDCommandLineInterface.java b/pmd-core/src/main/java/net/sourceforge/pmd/cli/PMDCommandLineInterface.java index 08b7641f5e..78152be9e5 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/cli/PMDCommandLineInterface.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/cli/PMDCommandLineInterface.java @@ -7,6 +7,7 @@ package net.sourceforge.pmd.cli; import java.util.Properties; import net.sourceforge.pmd.PMD; +import net.sourceforge.pmd.PMD.StatusCode; import net.sourceforge.pmd.PMDVersion; import net.sourceforge.pmd.annotation.InternalApi; import net.sourceforge.pmd.lang.LanguageRegistry; @@ -26,13 +27,39 @@ import com.beust.jcommander.ParameterException; @InternalApi public final class PMDCommandLineInterface { + @Deprecated public static final String PROG_NAME = "pmd"; + /** + * @deprecated This is used for testing, but support for it will be removed in PMD 7. + * Use {@link PMD#runPmd(String...)} or an overload to avoid exiting the VM. In PMD 7, + * {@link PMD#main(String[])} will call {@link System#exit(int)} always. + */ + @Deprecated public static final String NO_EXIT_AFTER_RUN = "net.sourceforge.pmd.cli.noExit"; + + /** + * @deprecated This is used for testing, but support for it will be removed in PMD 7. + * Use {@link PMD#runPmd(String...)} or an overload to avoid exiting the VM. In PMD 7, + * {@link PMD#main(String[])} will call {@link System#exit(int)} always. + */ + @Deprecated public static final String STATUS_CODE_PROPERTY = "net.sourceforge.pmd.cli.status"; + /** + * @deprecated Use {@link StatusCode#OK} + */ + @Deprecated public static final int NO_ERRORS_STATUS = 0; + /** + * @deprecated Use {@link StatusCode#ERROR} + */ + @Deprecated public static final int ERROR_STATUS = 1; + /** + * @deprecated Use {@link StatusCode#VIOLATIONS_FOUND} + */ + @Deprecated public static final int VIOLATIONS_FOUND = 4; private PMDCommandLineInterface() { } @@ -124,7 +151,10 @@ public final class PMDCommandLineInterface { * For testing purpose only... * * @param args + * + * @deprecated Use {@link PMD#runPmd(String...)} */ + @Deprecated public static void main(String[] args) { System.out.println(PMDCommandLineInterface.buildUsageText()); } @@ -159,13 +189,14 @@ public final class PMDCommandLineInterface { } /** - * @deprecated Use {@link PMD#main(String[])} + * @deprecated Use {@link PMD#runPmd(String...)} */ @Deprecated public static void run(String[] args) { setStatusCodeOrExit(PMD.run(args)); } + @Deprecated public static void setStatusCodeOrExit(int status) { if (isExitAfterRunSet()) { System.exit(status); diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/cli/CLITest.java b/pmd-java/src/test/java/net/sourceforge/pmd/cli/CLITest.java index 601542675a..3ddaa56643 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/cli/CLITest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/cli/CLITest.java @@ -9,6 +9,8 @@ import static org.hamcrest.Matchers.containsString; import org.junit.Test; +import net.sourceforge.pmd.PMD.StatusCode; + /** * @author Romain Pelisse <belaran@gmail.com> * @@ -51,21 +53,21 @@ public class CLITest extends BaseCLITest { @Test public void exitStatusWithViolations() { String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/errorprone.xml", }; - String log = runTest(4, args); + String log = runTest(StatusCode.VIOLATIONS_FOUND, 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 log = runTest(0, args); + String log = runTest(StatusCode.OK, 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 log = runTest(0, args); + String log = runTest(StatusCode.OK, args); assertThat(log, containsString("Avoid empty if")); } @@ -75,7 +77,7 @@ public class CLITest extends BaseCLITest { @Test public void testWrongRuleset() { String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/designn.xml", }; - String log = runTest(1, args); + String log = runTest(StatusCode.ERROR, args); assertThat(log, containsString("Can't find resource 'category/java/designn.xml' for rule 'null'." + " Make sure the resource is a valid file")); } @@ -86,7 +88,7 @@ public class CLITest extends BaseCLITest { @Test public void testWrongRulesetWithRulename() { String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/designn.xml/UseCollectionIsEmpty", }; - String log = runTest(1, args); + String log = runTest(StatusCode.ERROR, args); assertThat(log, containsString("Can't find resource 'category/java/designn.xml' for rule " + "'UseCollectionIsEmpty'.")); } @@ -97,7 +99,7 @@ public class CLITest extends BaseCLITest { @Test public void testWrongRulename() { String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml/ThisRuleDoesNotExist", }; - String log = runTest(1, args); + String log = runTest(StatusCode.OK, args); assertThat(log, containsString("No rules found. Maybe you misspelled a rule name?" + " (category/java/design.xml/ThisRuleDoesNotExist)")); } diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/cli/BaseCLITest.java b/pmd-test/src/main/java/net/sourceforge/pmd/cli/BaseCLITest.java index b18808fd02..54e9739159 100644 --- a/pmd-test/src/main/java/net/sourceforge/pmd/cli/BaseCLITest.java +++ b/pmd-test/src/main/java/net/sourceforge/pmd/cli/BaseCLITest.java @@ -25,6 +25,7 @@ import org.junit.BeforeClass; import net.sourceforge.pmd.PMD; import net.sourceforge.pmd.PMD.StatusCode; +import net.sourceforge.pmd.internal.util.AssertionUtil; /** * @author Romain Pelisse <belaran@gmail.com> @@ -79,11 +80,19 @@ public abstract class BaseCLITest { } } + /** + * @deprecated Use {@link #runTest(String...)}, note that + * it returns the log while this returns the name of a file containing the log. + */ @Deprecated protected String runTest(String[] args, String testname) { - return runTest(args); + return runTest(args, testname, 0); } + /** + * @deprecated Use {@link #runTest(StatusCode, String...)}, note that + * it returns the log while this returns the name of a file containing the log. + */ @Deprecated protected String runTest(String[] args, String testname, int expectedExitCode) { String filename = TEST_OUPUT_DIRECTORY + testname + ".txt"; @@ -100,27 +109,49 @@ public abstract class BaseCLITest { * Returns the log output. */ protected String runTest(String... args) { - return runTest(0, args); + return runTest(StatusCode.OK, args); } /** * Returns the log output. + * + * @deprecated Use {@link #runTest(StatusCode, String...)} */ + @Deprecated protected String runTest(int expectedExitCode, String... args) { + switch (expectedExitCode) { + case 0: + return runTest(StatusCode.OK, args); + case 1: + return runTest(StatusCode.ERROR, args); + case 4: + return runTest(StatusCode.VIOLATIONS_FOUND, args); + default: + throw AssertionUtil.shouldNotReachHere("unknown status code " + expectedExitCode); + } + } + + protected String runTest(StatusCode 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()); + assertEquals(expectedExitCode, statusCode); return console.toString(); } + /** + * @deprecated Use {@link #runTest(StatusCode, String...)} + */ @Deprecated protected void runPMDWith(String[] args) { PMD.main(args); } + /** + * @deprecated Use {@link #runTest(StatusCode, String...)} instead of checking the return code manually + */ @Deprecated protected void checkStatusCode(int expectedExitCode) { int statusCode = getStatusCode(); @@ -129,6 +160,10 @@ public abstract class BaseCLITest { } } + /** + * @deprecated Use {@link #runTest(StatusCode, String...)} instead + * of checking the return code manually + */ @Deprecated protected int getStatusCode() { return Integer.parseInt(System.getProperty(PMDCommandLineInterface.STATUS_CODE_PROPERTY)); diff --git a/pmd-xml/src/test/java/net/sourceforge/pmd/lang/xml/XmlCliTest.java b/pmd-xml/src/test/java/net/sourceforge/pmd/lang/xml/XmlCliTest.java index 2c9af24104..14e717b237 100644 --- a/pmd-xml/src/test/java/net/sourceforge/pmd/lang/xml/XmlCliTest.java +++ b/pmd-xml/src/test/java/net/sourceforge/pmd/lang/xml/XmlCliTest.java @@ -14,6 +14,7 @@ import org.apache.commons.lang3.StringUtils; import org.junit.Assert; import org.junit.Test; +import net.sourceforge.pmd.PMD.StatusCode; import net.sourceforge.pmd.cli.BaseCLITest; public class XmlCliTest extends BaseCLITest { @@ -36,19 +37,19 @@ public class XmlCliTest extends BaseCLITest { @Test public void analyzeSingleXmlWithoutForceLanguage() { - String log = runTest(0, createArgs("/src/file1.ext")); + String log = runTest(StatusCode.OK, createArgs("/src/file1.ext")); assertRuleMessage(0, log); } @Test public void analyzeSingleXmlWithForceLanguage() { - String log = runTest(4, createArgs("/src/file1.ext", "-force-language", "xml")); + String log = runTest(StatusCode.VIOLATIONS_FOUND, createArgs("/src/file1.ext", "-force-language", "xml")); assertRuleMessage(1, log); } @Test public void analyzeDirectoryWithForceLanguage() { - String log = runTest(4, createArgs("/src/", "-force-language", "xml")); + String log = runTest(StatusCode.VIOLATIONS_FOUND, createArgs("/src/", "-force-language", "xml")); assertRuleMessage(3, log); }