1
1
package com .tagtraum .perf .gcviewer ;
2
2
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
+
3
12
import com .tagtraum .perf .gcviewer .ctrl .impl .GCViewerGuiController ;
4
13
import com .tagtraum .perf .gcviewer .model .GCResource ;
5
14
import com .tagtraum .perf .gcviewer .model .GcResourceFile ;
6
15
import com .tagtraum .perf .gcviewer .model .GcResourceSeries ;
7
- import org .junit .After ;
8
- import org .junit .Before ;
9
16
import org .junit .Test ;
10
17
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
-
21
18
/**
22
19
* @author martin.geldmacher
23
20
*/
24
21
public class GCViewerTest {
25
22
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
-
45
23
@ Test
46
24
public void singleArgumentOpensGui () throws Exception {
47
25
GCViewerGuiController controller = mock (GCViewerGuiController .class );
48
26
GCViewer gcViewer = new GCViewer (controller , new GCViewerArgsParser ());
49
27
50
28
String [] args = {"some_gc.log" };
51
- gcViewer .doMain (args );
29
+ int exitValue = gcViewer .doMain (args );
52
30
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 ));
55
32
}
56
33
57
34
@ Test
@@ -60,10 +37,9 @@ public void singleArgumentWithSeriesOpensGui() throws Exception {
60
37
GCViewer gcViewer = new GCViewer (controller , new GCViewerArgsParser ());
61
38
62
39
String [] args = {"some_gc.log.0;some_gc.log.1;some_gc.log.2" };
63
- gcViewer .doMain (args );
40
+ int exitValue = gcViewer .doMain (args );
64
41
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 ));
67
43
}
68
44
69
45
@ Test
@@ -72,9 +48,39 @@ public void moreThan3ArgumentsPrintsUsage() throws Exception {
72
48
GCViewer gcViewer = new GCViewer (controller , new GCViewerArgsParser ());
73
49
74
50
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 );
76
74
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 ));
79
85
}
80
86
}
0 commit comments