diff --git a/pmd-core/pom.xml b/pmd-core/pom.xml index d8b6643221..611f09d4ae 100644 --- a/pmd-core/pom.xml +++ b/pmd-core/pom.xml @@ -121,5 +121,11 @@ mockito-all test + + com.github.stefanbirkner + system-rules + 1.8.0 + test + diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/PMD.java b/pmd-core/src/main/java/net/sourceforge/pmd/PMD.java index 042e442d12..28bb5d2985 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/PMD.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/PMD.java @@ -439,7 +439,7 @@ public class PMD { } catch (Exception e) { System.out.println(PMDCommandLineInterface.buildUsageText()); System.out.println(); - System.out.println(e.getMessage()); + System.err.println(e.getMessage()); status = PMDCommandLineInterface.ERROR_STATUS; } finally { logHandlerManager.close(); 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 8f0f66538f..8ecec514cd 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 @@ -41,7 +41,7 @@ public class PMDCommandLineInterface { } catch (ParameterException e) { jcommander.usage(); System.out.println(buildUsageText(jcommander)); - System.out.println(e.getMessage()); + System.err.println(e.getMessage()); setStatusCodeOrExit(ERROR_STATUS); } return arguments; @@ -174,9 +174,13 @@ public class PMDCommandLineInterface { } } - private static boolean isExitAfterRunSet() { - return (System.getenv(NO_EXIT_AFTER_RUN) == null ? false : true); - } + private static boolean isExitAfterRunSet() { + String noExit = System.getenv(NO_EXIT_AFTER_RUN); + if (noExit == null) { + noExit = System.getProperty(NO_EXIT_AFTER_RUN); + } + return (noExit == null ? true : false); + } private static void setStatusCode(int statusCode) { System.setProperty(STATUS_CODE_PROPERTY, Integer.toString(statusCode)); diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDCommandLineInterface.java b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDCommandLineInterface.java index 4e7c5f70a8..49fc2ad07f 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDCommandLineInterface.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDCommandLineInterface.java @@ -64,7 +64,7 @@ public class CPDCommandLineInterface { } catch (ParameterException e) { jcommander.usage(); System.out.println(buildUsageText()); - System.out.println(" " + e.getMessage()); + System.err.println(" " + e.getMessage()); setStatusCodeOrExit(1); return; } diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/cli/PMDCommandLineInterfaceTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/cli/PMDCommandLineInterfaceTest.java index 7d51439525..8979b9faad 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/cli/PMDCommandLineInterfaceTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/cli/PMDCommandLineInterfaceTest.java @@ -4,12 +4,27 @@ package net.sourceforge.pmd.cli; import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.contrib.java.lang.system.ExpectedSystemExit; +import org.junit.contrib.java.lang.system.RestoreSystemProperties; /** * Unit test for {@link PMDCommandLineInterface} */ public class PMDCommandLineInterfaceTest { + @Rule + public final ExpectedSystemExit exit = ExpectedSystemExit.none(); + + @Rule //Restores system properties after test + public final RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties(); + + @Before + public void clearSystemProperties () { + System.clearProperty(PMDCommandLineInterface.NO_EXIT_AFTER_RUN); + System.clearProperty(PMDCommandLineInterface.STATUS_CODE_PROPERTY); + } @Test public void testProperties() { @@ -20,4 +35,19 @@ public class PMDCommandLineInterfaceTest { Assert.assertEquals("output_folder", params.getProperties().getProperty("outputDir")); } + + @Test + public void testSetStatusCodeOrExit_DoExit() { + exit.expectSystemExitWithStatus(0); + + PMDCommandLineInterface.setStatusCodeOrExit(0); + } + + @Test + public void testSetStatusCodeOrExit_SetStatus() { + System.setProperty(PMDCommandLineInterface.NO_EXIT_AFTER_RUN, "1"); + + PMDCommandLineInterface.setStatusCodeOrExit(0); + Assert.assertEquals(System.getProperty(PMDCommandLineInterface.STATUS_CODE_PROPERTY), "0"); + } }