Merge branch 'tiobe/fix-error-handling' of https://github.com/tiobe/pmd into tiobe-tiobe/fix-error-handling

This commit is contained in:
Andreas Dangel
2015-03-21 18:16:16 +01:00
5 changed files with 46 additions and 6 deletions

View File

@ -121,5 +121,11 @@
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.8.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -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();

View File

@ -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));

View File

@ -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;
}

View File

@ -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");
}
}