1
+ package com .fasterxml .uuid ;
2
+
3
+ import org .junit .After ;
4
+ import org .junit .Before ;
5
+ import org .junit .Test ;
6
+ import org .junit .runner .RunWith ;
7
+ import org .junit .runners .Parameterized ;
8
+
9
+ import java .io .ByteArrayOutputStream ;
10
+ import java .io .PrintStream ;
11
+ import java .lang .reflect .Array ;
12
+ import java .util .ArrayList ;
13
+ import java .util .Arrays ;
14
+ import java .util .List ;
15
+ import java .util .UUID ;
16
+
17
+ import static org .hamcrest .core .StringContains .containsString ;
18
+ import static org .junit .Assert .assertEquals ;
19
+ import static org .junit .Assert .assertThat ;
20
+
21
+ @ RunWith (Parameterized .class )
22
+ public class JugNamedTest {
23
+ @ Parameterized .Parameter
24
+ public UseCase useCase ;
25
+
26
+ private PrintStream oldStrOut ;
27
+ private PrintStream oldStrErr ;
28
+
29
+ private final ByteArrayOutputStream outContent = new ByteArrayOutputStream ();
30
+ private final ByteArrayOutputStream errContent = new ByteArrayOutputStream ();
31
+ private Jug jug_underTest ;
32
+
33
+ @ Before
34
+ public void setup () {
35
+ jug_underTest = new Jug ();oldStrOut = System .out ;
36
+ oldStrErr = System .err ;
37
+ PrintStream stubbedStream = new PrintStream (outContent );
38
+ System .setOut (stubbedStream );
39
+ PrintStream stubbedErrStream = new PrintStream (errContent );
40
+ System .setErr (stubbedErrStream );
41
+ }
42
+
43
+ @ After
44
+ public void cleanup () {
45
+ System .setOut (oldStrOut );
46
+ System .setErr (oldStrErr );
47
+ }
48
+
49
+ @ Test
50
+ public void run_shouldProduceUUID () {
51
+ // given
52
+
53
+ // when
54
+ List <String > arguments = useCase .getArgs ();
55
+ jug_underTest .run (arguments .toArray ((String []) Array .newInstance (String .class , 0 )));
56
+
57
+ // then - if it is a UUID then we should be able to parse it back out
58
+ String actualUuid = outContent .toString ();
59
+ assertEquals ('\n' , actualUuid .charAt (actualUuid .length () - 1 ));
60
+
61
+ assertEquals (UUID .class ,
62
+ UUID .fromString (actualUuid .substring (0 , actualUuid .length () - 1 )).getClass ());
63
+ }
64
+
65
+ @ Test
66
+ public void run_givenCount3_shouldProduceUUID () {
67
+ // given
68
+
69
+ // when
70
+ List <String > arguments = useCase .getArgs ();
71
+ arguments .add (0 , "-c" );
72
+ arguments .add (1 , "3" );
73
+ jug_underTest .run (arguments .toArray ((String []) Array .newInstance (String .class , 0 )));
74
+
75
+ // then - if it is a UUID then we should be able to parse it back out
76
+ String [] actualUuids = outContent .toString ().split ("\n " );
77
+ for (String actualUuid : actualUuids ) {
78
+ assertEquals (UUID .class ,
79
+ UUID .fromString (actualUuid ).getClass ());
80
+ }
81
+ }
82
+
83
+ @ Test
84
+ public void run_givenPerformance_shouldProducePerformanceInfo () {
85
+ // given
86
+
87
+ // when
88
+ List <String > arguments = useCase .getArgs ();
89
+ arguments .add (0 , "-p" );
90
+ jug_underTest .run (arguments .toArray ((String []) Array .newInstance (String .class , 0 )));
91
+
92
+ // then
93
+ String actualOutput = outContent .toString ();
94
+
95
+ assertThat (actualOutput , containsString ("Performance: took" ));
96
+ }
97
+ @ Test
98
+ public void run_givenHelp_shouldProduceHelpInfo () {
99
+ // given
100
+
101
+ // when
102
+ List <String > arguments = useCase .getArgs ();
103
+ arguments .add (0 , "-h" );
104
+ jug_underTest .run (arguments .toArray ((String []) Array .newInstance (String .class , 0 )));
105
+
106
+ // then
107
+ String actualOutput = errContent .toString ();
108
+
109
+ assertThat (actualOutput , containsString ("Usage: java" ));
110
+ }
111
+
112
+ @ Test
113
+ public void run_givenVerbose_shouldProduceExtraInfo () {
114
+ // given
115
+
116
+ // when
117
+ List <String > arguments = useCase .getArgs ();
118
+ arguments .add (0 , "-v" );
119
+ jug_underTest .run (arguments .toArray ((String []) Array .newInstance (String .class , 0 )));
120
+
121
+ // then
122
+ String actualOutput = outContent .toString ();
123
+
124
+ assertThat (actualOutput , containsString ("Done." ));
125
+ }
126
+
127
+ @ Test
128
+ public void run_givenVerboseAndPerformance_shouldProduceExtraInfo () {
129
+ // given
130
+
131
+ // when
132
+ List <String > arguments = useCase .getArgs ();
133
+ arguments .add (0 , "-v" );
134
+ arguments .add (1 , "-p" );
135
+ jug_underTest .run (arguments .toArray ((String []) Array .newInstance (String .class , 0 )));
136
+
137
+ // then
138
+ String actualOutput = outContent .toString ();
139
+
140
+ assertThat (actualOutput , containsString ("Done." ));
141
+ assertThat (actualOutput , containsString ("Performance: took" ));
142
+ }
143
+
144
+ @ Parameterized .Parameters (name = "{index} -> {0}" )
145
+ public static List <UseCase > useCases () {
146
+ return Arrays .asList (
147
+ new UseCase ("n" , "-n" , "world" , "-s" , "url" ),
148
+ new UseCase ("n" , "-n" , "world" , "-s" , "dns" )
149
+ );
150
+ }
151
+
152
+ private static class UseCase {
153
+ private final String type ;
154
+ private String [] options = new String []{};
155
+
156
+ public UseCase (String type , String ...options ) {
157
+ this .type = type ;
158
+ if (options != null ) {
159
+ this .options = options ;
160
+ }
161
+ }
162
+
163
+ public List <String > getArgs () {
164
+ List <String > arguments = new ArrayList <>(Arrays .asList (options ));
165
+ arguments .add (type );
166
+ return arguments ;
167
+ }
168
+
169
+ @ Override
170
+ public String toString () {
171
+ if (options .length == 0 ) {
172
+ return String .format ("type: %s, options: no options" , type );
173
+ } else {
174
+ return String .format ("type: %s, options: %s" , type , String .join (", " , options ));
175
+ }
176
+ }
177
+ }
178
+ }
0 commit comments