Skip to content

Commit

Permalink
Bugfix/chewiebug#192/fix rewriting unittest (chewiebug#195)
Browse files Browse the repository at this point in the history
* chewiebug#192 rewrite unittest to support parallel execution
  • Loading branch information
chewiebug authored Dec 25, 2017
1 parent 30873e0 commit 482b1a1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 45 deletions.
16 changes: 11 additions & 5 deletions src/main/java/com/tagtraum/perf/gcviewer/GCViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -39,22 +40,26 @@ 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);
}
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");
Expand All @@ -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;
}
}

Expand Down
86 changes: 46 additions & 40 deletions src/test/java/com/tagtraum/perf/gcviewer/GCViewerTest.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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));
}
}

0 comments on commit 482b1a1

Please sign in to comment.