diff --git a/src/main/java/com/tagtraum/perf/gcviewer/GCViewer.java b/src/main/java/com/tagtraum/perf/gcviewer/GCViewer.java index 50307a92..308c8106 100755 --- a/src/main/java/com/tagtraum/perf/gcviewer/GCViewer.java +++ b/src/main/java/com/tagtraum/perf/gcviewer/GCViewer.java @@ -26,6 +26,7 @@ public class GCViewer { private static final int EXIT_OK = 0; private static final int EXIT_EXPORT_FAILED = -1; private static final int EXIT_ARGS_PARSE_FAILED = -2; + private static final int EXIT_TOO_MANY_ARGS = -3; private GCViewerGuiController gcViewerGuiController; private GCViewerArgsParser gcViewerArgsParser; @@ -39,10 +40,13 @@ public GCViewer(GCViewerGuiController gcViewerGuiController, GCViewerArgsParser } public static void main(final String[] args) throws InvocationTargetException, InterruptedException { - new GCViewer().doMain(args); + int exitValue = new GCViewer().doMain(args); + if (exitValue != 0) { + System.exit(exitValue); + } } - public void doMain(String[] args) throws InvocationTargetException, InterruptedException { + public int doMain(String[] args) throws InvocationTargetException, InterruptedException { GCViewerArgsParser argsParser = gcViewerArgsParser; try { argsParser.parseArguments(args); @@ -50,11 +54,12 @@ public void doMain(String[] args) throws InvocationTargetException, InterruptedE catch (GCViewerArgsParserException e) { usage(); LOGGER.log(Level.SEVERE, e.getMessage(), e); - System.exit(EXIT_ARGS_PARSE_FAILED); + return EXIT_ARGS_PARSE_FAILED; } if (argsParser.getArgumentCount() > 3) { usage(); + return EXIT_TOO_MANY_ARGS; } else if (argsParser.getArgumentCount() >= 2) { LOGGER.info("GCViewer command line mode"); @@ -67,15 +72,16 @@ else if (argsParser.getArgumentCount() >= 2) { try { export(gcResource, summaryFilePath, chartFilePath, type); LOGGER.info("export completed successfully"); - System.exit(EXIT_OK); + return EXIT_OK; } catch(Exception e) { LOGGER.log(Level.SEVERE, "Error during report generation", e); - System.exit(EXIT_EXPORT_FAILED); + return EXIT_EXPORT_FAILED; } } else { gcViewerGuiController.startGui(argsParser.getArgumentCount() == 1 ? argsParser.getGcResource() : null); + return EXIT_OK; } } diff --git a/src/test/java/com/tagtraum/perf/gcviewer/GCViewerTest.java b/src/test/java/com/tagtraum/perf/gcviewer/GCViewerTest.java index 3209d8ea..89bf6f02 100644 --- a/src/test/java/com/tagtraum/perf/gcviewer/GCViewerTest.java +++ b/src/test/java/com/tagtraum/perf/gcviewer/GCViewerTest.java @@ -1,57 +1,34 @@ package com.tagtraum.perf.gcviewer; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + +import java.util.Arrays; + import com.tagtraum.perf.gcviewer.ctrl.impl.GCViewerGuiController; import com.tagtraum.perf.gcviewer.model.GCResource; import com.tagtraum.perf.gcviewer.model.GcResourceFile; import com.tagtraum.perf.gcviewer.model.GcResourceSeries; -import org.junit.After; -import org.junit.Before; import org.junit.Test; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.Arrays; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.isEmptyString; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.*; - /** * @author martin.geldmacher */ public class GCViewerTest { - private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); - private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); - private PrintStream oldOut; - private PrintStream oldErr; - - @Before - public void replaceStreams() { - oldOut = System.out; - oldErr = System.err; - System.setOut(new PrintStream(outContent)); - System.setErr(new PrintStream(errContent)); - } - - @After - public void revertToOriginalStreams() { - System.setOut(oldOut); - System.setErr(oldErr); - } - @Test public void singleArgumentOpensGui() throws Exception { GCViewerGuiController controller = mock(GCViewerGuiController.class); GCViewer gcViewer = new GCViewer(controller, new GCViewerArgsParser()); String[] args = {"some_gc.log"}; - gcViewer.doMain(args); + int exitValue = gcViewer.doMain(args); verify(controller).startGui(new GcResourceFile("some_gc.log")); - assertThat(outContent.toString(), isEmptyString()); - assertThat(errContent.toString(), isEmptyString()); + assertThat("exitValue of doMain", exitValue, is(0)); } @Test @@ -60,10 +37,9 @@ public void singleArgumentWithSeriesOpensGui() throws Exception { GCViewer gcViewer = new GCViewer(controller, new GCViewerArgsParser()); String[] args = {"some_gc.log.0;some_gc.log.1;some_gc.log.2"}; - gcViewer.doMain(args); + int exitValue = gcViewer.doMain(args); verify(controller).startGui(new GcResourceSeries(Arrays.asList(new GcResourceFile("some_gc.log.0"), new GcResourceFile("some_gc.log.1"), new GcResourceFile("some_gc.log.2")))); - assertThat(outContent.toString(), isEmptyString()); - assertThat(errContent.toString(), isEmptyString()); + assertThat("result of doMain", exitValue, is(0)); } @Test @@ -72,9 +48,39 @@ public void moreThan3ArgumentsPrintsUsage() throws Exception { GCViewer gcViewer = new GCViewer(controller, new GCViewerArgsParser()); String[] args = {"argument1", "argument2", "argument3", "argument4"}; - gcViewer.doMain(args); + int exitValue = gcViewer.doMain(args); + verify(controller, never()).startGui(any(GCResource.class)); + assertThat("result of doMain", exitValue, is(-3)); + } + + @Test + public void export() throws Exception { + GCViewerGuiController controller = mock(GCViewerGuiController.class); + GCViewer gcViewer = new GCViewer(controller, new GCViewerArgsParser()); + + String[] args = {"target/test-classes/openjdk/SampleSun1_7_0-01_G1_young.txt", "target/export.csv", "target/export.png", "-t", "PLAIN"}; + int exitValue = gcViewer.doMain(args); + verify(controller, never()).startGui(any(GCResource.class)); + assertThat("result of doMain", exitValue, is(0)); + } + + @Test + public void exportFileNotFound() throws Exception { + GCViewerGuiController controller = mock(GCViewerGuiController.class); + GCViewer gcViewer = new GCViewer(controller, new GCViewerArgsParser()); + + String[] args = {"doesNotExist.log", "export.csv", "-t", "PLAIN"}; + int exitValue = gcViewer.doMain(args); verify(controller, never()).startGui(any(GCResource.class)); - assertThat(outContent.toString(), containsString("Welcome to GCViewer with cmdline")); - assertThat(errContent.toString(), isEmptyString()); + assertThat("result of doMain", exitValue, is(-1)); + } + + @Test + public void illegalExportFormat() throws Exception { + GCViewer gcViewer = new GCViewer(); + + String[] args = {"-t", "INVALID"}; + int exitValue = gcViewer.doMain(args); + assertThat("result of doMain", exitValue, is(-2)); } }