1
+ package com .googlecode .scheme2ddl ;
2
+
3
+ import org .springframework .test .util .ReflectionTestUtils ;
4
+ import org .testng .annotations .AfterMethod ;
5
+ import org .testng .annotations .BeforeMethod ;
6
+ import org .testng .annotations .DataProvider ;
7
+ import org .testng .annotations .Test ;
8
+
9
+ import java .io .ByteArrayOutputStream ;
10
+ import java .io .PrintStream ;
11
+
12
+ import static org .hamcrest .MatcherAssert .assertThat ;
13
+ import static org .hamcrest .core .StringContains .containsString ;
14
+ import static org .testng .Assert .*;
15
+
16
+ /**
17
+ * Created by Anton Reshetnikov on 06 Dec 2016.
18
+ */
19
+
20
+ public class MainCLITest {
21
+
22
+ private final ByteArrayOutputStream outContent = new ByteArrayOutputStream ();
23
+ private final ByteArrayOutputStream errContent = new ByteArrayOutputStream ();
24
+
25
+ private final PrintStream outOriginal = System .out ;
26
+ private final PrintStream errorOriginal = System .err ;
27
+
28
+ @ BeforeMethod
29
+ public void setUp () throws Exception {
30
+ ReflectionTestUtils .setField (Main .class , "justPrintUsage" , false );
31
+ ReflectionTestUtils .setField (Main .class , "justPrintVersion" , false );
32
+ ReflectionTestUtils .setField (Main .class , "justTestConnection" , false );
33
+ ReflectionTestUtils .setField (Main .class , "dbUrl" , null );
34
+ }
35
+
36
+ @ BeforeMethod
37
+ public void setUpStreams () {
38
+ System .setOut (new PrintStream (outContent ));
39
+ System .setErr (new PrintStream (errContent ));
40
+ }
41
+
42
+ @ AfterMethod
43
+ public void cleanUpStreams () {
44
+ System .setOut (outOriginal );
45
+ System .setErr (errorOriginal );
46
+ outContent .reset ();
47
+ errContent .reset ();
48
+ }
49
+
50
+
51
+ @ DataProvider
52
+ public static Object [][] testPrintUsageOptionsParams () {
53
+ return new Object [][]{
54
+ new String [][]{{"-h" }},
55
+ new String [][]{{"--help" }},
56
+ new String [][]{{"-help" }},
57
+ new String [][]{{"-h" }},
58
+ new String [][]{{"-url" , "1" , "-tc" , "-h" }},
59
+ new String [][]{{"-tc" , "--help" }},
60
+ };
61
+ }
62
+
63
+
64
+ @ Test (dataProvider = "testPrintUsageOptionsParams" )
65
+ public void testPrintUsageOptions (String [] args ) throws Exception {
66
+ Main .main (args );
67
+ assertThat (outContent .toString (), containsString ("java -jar scheme2ddl.jar" ));
68
+ assertThat (outContent .toString (), containsString ("example: scott/tiger@localhost:1521:ORCL" ));
69
+ }
70
+
71
+ @ Test
72
+ public void testPrintVersionOption () throws Exception {
73
+ String [] args = {"-version" };
74
+ Main .main (args );
75
+ assertThat (outContent .toString (), containsString ("scheme2ddl version " ));
76
+ }
77
+
78
+ @ Test (expectedExceptions = Exception .class , expectedExceptionsMessageRegExp = "Unknown argument: .*" )
79
+ public void testUnknownArgument () throws Exception {
80
+ String [] args = {"-xYx" };
81
+ Main .main (args );
82
+ }
83
+
84
+
85
+ }
0 commit comments