Skip to content

Commit b6237f9

Browse files
authoredDec 25, 2017
Bugfix/#192/fix rewriting unittest (#195)
* #192 rewrite unittest to support parallel execution
1 parent 30873e0 commit b6237f9

File tree

2 files changed

+57
-45
lines changed

2 files changed

+57
-45
lines changed
 

‎src/main/java/com/tagtraum/perf/gcviewer/GCViewer.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class GCViewer {
2626
private static final int EXIT_OK = 0;
2727
private static final int EXIT_EXPORT_FAILED = -1;
2828
private static final int EXIT_ARGS_PARSE_FAILED = -2;
29+
private static final int EXIT_TOO_MANY_ARGS = -3;
2930
private GCViewerGuiController gcViewerGuiController;
3031
private GCViewerArgsParser gcViewerArgsParser;
3132

@@ -39,22 +40,26 @@ public GCViewer(GCViewerGuiController gcViewerGuiController, GCViewerArgsParser
3940
}
4041

4142
public static void main(final String[] args) throws InvocationTargetException, InterruptedException {
42-
new GCViewer().doMain(args);
43+
int exitValue = new GCViewer().doMain(args);
44+
if (exitValue != 0) {
45+
System.exit(exitValue);
46+
}
4347
}
4448

45-
public void doMain(String[] args) throws InvocationTargetException, InterruptedException {
49+
public int doMain(String[] args) throws InvocationTargetException, InterruptedException {
4650
GCViewerArgsParser argsParser = gcViewerArgsParser;
4751
try {
4852
argsParser.parseArguments(args);
4953
}
5054
catch (GCViewerArgsParserException e) {
5155
usage();
5256
LOGGER.log(Level.SEVERE, e.getMessage(), e);
53-
System.exit(EXIT_ARGS_PARSE_FAILED);
57+
return EXIT_ARGS_PARSE_FAILED;
5458
}
5559

5660
if (argsParser.getArgumentCount() > 3) {
5761
usage();
62+
return EXIT_TOO_MANY_ARGS;
5863
}
5964
else if (argsParser.getArgumentCount() >= 2) {
6065
LOGGER.info("GCViewer command line mode");
@@ -67,15 +72,16 @@ else if (argsParser.getArgumentCount() >= 2) {
6772
try {
6873
export(gcResource, summaryFilePath, chartFilePath, type);
6974
LOGGER.info("export completed successfully");
70-
System.exit(EXIT_OK);
75+
return EXIT_OK;
7176
}
7277
catch(Exception e) {
7378
LOGGER.log(Level.SEVERE, "Error during report generation", e);
74-
System.exit(EXIT_EXPORT_FAILED);
79+
return EXIT_EXPORT_FAILED;
7580
}
7681
}
7782
else {
7883
gcViewerGuiController.startGui(argsParser.getArgumentCount() == 1 ? argsParser.getGcResource() : null);
84+
return EXIT_OK;
7985
}
8086
}
8187

Original file line numberDiff line numberDiff line change
@@ -1,57 +1,34 @@
11
package com.tagtraum.perf.gcviewer;
22

3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.is;
5+
import static org.mockito.Matchers.any;
6+
import static org.mockito.Mockito.mock;
7+
import static org.mockito.Mockito.never;
8+
import static org.mockito.Mockito.verify;
9+
10+
import java.util.Arrays;
11+
312
import com.tagtraum.perf.gcviewer.ctrl.impl.GCViewerGuiController;
413
import com.tagtraum.perf.gcviewer.model.GCResource;
514
import com.tagtraum.perf.gcviewer.model.GcResourceFile;
615
import com.tagtraum.perf.gcviewer.model.GcResourceSeries;
7-
import org.junit.After;
8-
import org.junit.Before;
916
import org.junit.Test;
1017

11-
import java.io.ByteArrayOutputStream;
12-
import java.io.PrintStream;
13-
import java.util.Arrays;
14-
15-
import static org.hamcrest.MatcherAssert.assertThat;
16-
import static org.hamcrest.Matchers.containsString;
17-
import static org.hamcrest.Matchers.isEmptyString;
18-
import static org.mockito.Matchers.any;
19-
import static org.mockito.Mockito.*;
20-
2118
/**
2219
* @author martin.geldmacher
2320
*/
2421
public class GCViewerTest {
2522

26-
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
27-
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
28-
private PrintStream oldOut;
29-
private PrintStream oldErr;
30-
31-
@Before
32-
public void replaceStreams() {
33-
oldOut = System.out;
34-
oldErr = System.err;
35-
System.setOut(new PrintStream(outContent));
36-
System.setErr(new PrintStream(errContent));
37-
}
38-
39-
@After
40-
public void revertToOriginalStreams() {
41-
System.setOut(oldOut);
42-
System.setErr(oldErr);
43-
}
44-
4523
@Test
4624
public void singleArgumentOpensGui() throws Exception {
4725
GCViewerGuiController controller = mock(GCViewerGuiController.class);
4826
GCViewer gcViewer = new GCViewer(controller, new GCViewerArgsParser());
4927

5028
String[] args = {"some_gc.log"};
51-
gcViewer.doMain(args);
29+
int exitValue = gcViewer.doMain(args);
5230
verify(controller).startGui(new GcResourceFile("some_gc.log"));
53-
assertThat(outContent.toString(), isEmptyString());
54-
assertThat(errContent.toString(), isEmptyString());
31+
assertThat("exitValue of doMain", exitValue, is(0));
5532
}
5633

5734
@Test
@@ -60,10 +37,9 @@ public void singleArgumentWithSeriesOpensGui() throws Exception {
6037
GCViewer gcViewer = new GCViewer(controller, new GCViewerArgsParser());
6138

6239
String[] args = {"some_gc.log.0;some_gc.log.1;some_gc.log.2"};
63-
gcViewer.doMain(args);
40+
int exitValue = gcViewer.doMain(args);
6441
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"))));
65-
assertThat(outContent.toString(), isEmptyString());
66-
assertThat(errContent.toString(), isEmptyString());
42+
assertThat("result of doMain", exitValue, is(0));
6743
}
6844

6945
@Test
@@ -72,9 +48,39 @@ public void moreThan3ArgumentsPrintsUsage() throws Exception {
7248
GCViewer gcViewer = new GCViewer(controller, new GCViewerArgsParser());
7349

7450
String[] args = {"argument1", "argument2", "argument3", "argument4"};
75-
gcViewer.doMain(args);
51+
int exitValue = gcViewer.doMain(args);
52+
verify(controller, never()).startGui(any(GCResource.class));
53+
assertThat("result of doMain", exitValue, is(-3));
54+
}
55+
56+
@Test
57+
public void export() throws Exception {
58+
GCViewerGuiController controller = mock(GCViewerGuiController.class);
59+
GCViewer gcViewer = new GCViewer(controller, new GCViewerArgsParser());
60+
61+
String[] args = {"target/test-classes/openjdk/SampleSun1_7_0-01_G1_young.txt", "target/export.csv", "target/export.png", "-t", "PLAIN"};
62+
int exitValue = gcViewer.doMain(args);
63+
verify(controller, never()).startGui(any(GCResource.class));
64+
assertThat("result of doMain", exitValue, is(0));
65+
}
66+
67+
@Test
68+
public void exportFileNotFound() throws Exception {
69+
GCViewerGuiController controller = mock(GCViewerGuiController.class);
70+
GCViewer gcViewer = new GCViewer(controller, new GCViewerArgsParser());
71+
72+
String[] args = {"doesNotExist.log", "export.csv", "-t", "PLAIN"};
73+
int exitValue = gcViewer.doMain(args);
7674
verify(controller, never()).startGui(any(GCResource.class));
77-
assertThat(outContent.toString(), containsString("Welcome to GCViewer with cmdline"));
78-
assertThat(errContent.toString(), isEmptyString());
75+
assertThat("result of doMain", exitValue, is(-1));
76+
}
77+
78+
@Test
79+
public void illegalExportFormat() throws Exception {
80+
GCViewer gcViewer = new GCViewer();
81+
82+
String[] args = {"-t", "INVALID"};
83+
int exitValue = gcViewer.doMain(args);
84+
assertThat("result of doMain", exitValue, is(-2));
7985
}
8086
}

0 commit comments

Comments
 (0)