From 7bf399d70868807363c4d58a63d34dd92282de63 Mon Sep 17 00:00:00 2001 From: chewiebug Date: Thu, 7 Sep 2017 12:47:52 +0200 Subject: [PATCH 1/3] #192 rewrite unittest to support parallel execution --- .../com/tagtraum/perf/gcviewer/GCViewer.java | 16 ++++--- .../tagtraum/perf/gcviewer/GCViewerTest.java | 45 +++++-------------- 2 files changed, 21 insertions(+), 40 deletions(-) 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..2b883748 100644 --- a/src/test/java/com/tagtraum/perf/gcviewer/GCViewerTest.java +++ b/src/test/java/com/tagtraum/perf/gcviewer/GCViewerTest.java @@ -4,54 +4,31 @@ 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.hamcrest.Matchers.is; import static org.mockito.Matchers.any; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; /** * @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,8 @@ 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(outContent.toString(), containsString("Welcome to GCViewer with cmdline")); - assertThat(errContent.toString(), isEmptyString()); + assertThat("result of doMain", exitValue, is(-3)); } } From 70de53ec39693c969feac01e4f3f93c645554d84 Mon Sep 17 00:00:00 2001 From: chewiebug Date: Mon, 25 Dec 2017 15:44:33 +0100 Subject: [PATCH 2/3] #192 improve coverage of new code --- .../tagtraum/perf/gcviewer/GCViewerTest.java | 47 +++++++++++++++---- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/src/test/java/com/tagtraum/perf/gcviewer/GCViewerTest.java b/src/test/java/com/tagtraum/perf/gcviewer/GCViewerTest.java index 2b883748..410cbfa4 100644 --- a/src/test/java/com/tagtraum/perf/gcviewer/GCViewerTest.java +++ b/src/test/java/com/tagtraum/perf/gcviewer/GCViewerTest.java @@ -1,13 +1,5 @@ package com.tagtraum.perf.gcviewer; -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.Test; - -import java.util.Arrays; - import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.mockito.Matchers.any; @@ -15,6 +7,14 @@ 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.Test; + /** * @author martin.geldmacher */ @@ -52,4 +52,35 @@ public void moreThan3ArgumentsPrintsUsage() throws Exception { 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/SampleShenandoahAggressiveHeuristics.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("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)); + } } From 2834bf467383a5142bba546dcf3f1f41a8fdb8ca Mon Sep 17 00:00:00 2001 From: chewiebug Date: Mon, 25 Dec 2017 16:02:13 +0100 Subject: [PATCH 3/3] #192 change input file for unittest --- src/test/java/com/tagtraum/perf/gcviewer/GCViewerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/tagtraum/perf/gcviewer/GCViewerTest.java b/src/test/java/com/tagtraum/perf/gcviewer/GCViewerTest.java index 410cbfa4..89bf6f02 100644 --- a/src/test/java/com/tagtraum/perf/gcviewer/GCViewerTest.java +++ b/src/test/java/com/tagtraum/perf/gcviewer/GCViewerTest.java @@ -58,7 +58,7 @@ public void export() throws Exception { GCViewerGuiController controller = mock(GCViewerGuiController.class); GCViewer gcViewer = new GCViewer(controller, new GCViewerArgsParser()); - String[] args = {"target/test-classes/openjdk/SampleShenandoahAggressiveHeuristics.txt", "target/export.csv", "target/export.png", "-t", "PLAIN"}; + 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));