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 .*;
13
+
14
+ import static org .hamcrest .core .StringContains .containsString ;
15
+ import static org .junit .Assert .*;
16
+
17
+ @ RunWith (Parameterized .class )
18
+ public class JugNoArgsTest {
19
+ @ Parameterized .Parameter
20
+ public String useCase ;
21
+
22
+ private PrintStream oldStrOut ;
23
+ private PrintStream oldStrErr ;
24
+
25
+ private final ByteArrayOutputStream outContent = new ByteArrayOutputStream ();
26
+ private final ByteArrayOutputStream errContent = new ByteArrayOutputStream ();
27
+ private Jug jug_underTest ;
28
+
29
+ @ Before
30
+ public void setup () {
31
+ jug_underTest = new Jug ();
32
+ oldStrOut = System .out ;
33
+ oldStrErr = System .err ;
34
+ PrintStream stubbedStream = new PrintStream (outContent );
35
+ System .setOut (stubbedStream );
36
+ PrintStream stubbedErrStream = new PrintStream (errContent );
37
+ System .setErr (stubbedErrStream );
38
+ }
39
+
40
+ @ After
41
+ public void cleanup () {
42
+ System .setOut (oldStrOut );
43
+ System .setErr (oldStrErr );
44
+ }
45
+
46
+ @ Test
47
+ public void run_givenNoOptions_shouldProduceUUID () {
48
+ // given
49
+
50
+ // when
51
+ jug_underTest .run (new String []{useCase });
52
+
53
+ // then - if it is a UUID then we should be able to parse it back out
54
+ String actualUuid = outContent .toString ();
55
+ assertEquals ('\n' , actualUuid .charAt (actualUuid .length () - 1 ));
56
+
57
+ assertEquals (UUID .class ,
58
+ UUID .fromString (actualUuid .substring (0 , actualUuid .length () - 1 )).getClass ());
59
+ }
60
+
61
+ @ Test
62
+ public void run_givenCount1_shouldProduceUUID () {
63
+ // given
64
+
65
+ // when
66
+ List <String > arguments = new ArrayList <>(Arrays .asList ("-c" , "1" ));
67
+ arguments .add (useCase );
68
+ jug_underTest .run (arguments .toArray ((String []) Array .newInstance (String .class , 0 )));
69
+
70
+ // then - if it is a UUID then we should be able to parse it back out
71
+ String actualUuid = outContent .toString ();
72
+ assertEquals ('\n' , actualUuid .charAt (actualUuid .length () - 1 ));
73
+
74
+ assertEquals (UUID .class ,
75
+ UUID .fromString (actualUuid .substring (0 , actualUuid .length () - 1 )).getClass ());
76
+ }
77
+
78
+ @ Test
79
+ public void run_givenCount2_shouldProduce2UUIDs () {
80
+ // given
81
+
82
+ // when
83
+ List <String > arguments = new ArrayList <>(Arrays .asList ("-c" , "2" ));
84
+ arguments .add (useCase );
85
+ jug_underTest .run (arguments .toArray ((String []) Array .newInstance (String .class , 0 )));
86
+
87
+ // then - if it is a UUID then we should be able to parse it back out
88
+ String [] actualUuids = outContent .toString ().split ("\n " );
89
+ assertEquals (2 , actualUuids .length );
90
+
91
+ for (String actualUuid : actualUuids ) {
92
+ assertEquals (UUID .class ,
93
+ UUID .fromString (actualUuid ).getClass ());
94
+ }
95
+ }
96
+
97
+ @ Test
98
+ public void run_givenEthernet_shouldProduceUUID () {
99
+ // given
100
+
101
+ // when
102
+ List <String > arguments = new ArrayList <>(Arrays .asList ("-e" , ":::::" ));
103
+ arguments .add (useCase );
104
+ jug_underTest .run (arguments .toArray ((String []) Array .newInstance (String .class , 0 )));
105
+
106
+ // then - if it is a UUID then we should be able to parse it back out
107
+ String actualUuid = outContent .toString ();
108
+ assertEquals ('\n' , actualUuid .charAt (actualUuid .length () - 1 ));
109
+
110
+ assertEquals (UUID .class ,
111
+ UUID .fromString (actualUuid .substring (0 , actualUuid .length () - 1 )).getClass ());
112
+ }
113
+
114
+ @ Test
115
+ public void run_givenName_shouldProduceUUID () {
116
+ // given
117
+
118
+ // when
119
+ List <String > arguments = new ArrayList <>(Arrays .asList ("-n" , "hello" ));
120
+ arguments .add (useCase );
121
+ jug_underTest .run (arguments .toArray ((String []) Array .newInstance (String .class , 0 )));
122
+
123
+ // then - if it is a UUID then we should be able to parse it back out
124
+ String actualUuid = outContent .toString ();
125
+ assertEquals ('\n' , actualUuid .charAt (actualUuid .length () - 1 ));
126
+
127
+ assertEquals (UUID .class ,
128
+ UUID .fromString (actualUuid .substring (0 , actualUuid .length () - 1 )).getClass ());
129
+ }
130
+
131
+ @ Test
132
+ public void run_givenDnsNameSpace_shouldProduceUUID () {
133
+ // given
134
+
135
+ // when
136
+ List <String > arguments = new ArrayList <>(Arrays .asList ("-s" , "dns" ));
137
+ arguments .add (useCase );
138
+ jug_underTest .run (arguments .toArray ((String []) Array .newInstance (String .class , 0 )));
139
+
140
+ // then - if it is a UUID then we should be able to parse it back out
141
+ String actualUuid = outContent .toString ();
142
+ assertEquals ('\n' , actualUuid .charAt (actualUuid .length () - 1 ));
143
+
144
+ assertEquals (UUID .class ,
145
+ UUID .fromString (actualUuid .substring (0 , actualUuid .length () - 1 )).getClass ());
146
+ }
147
+
148
+ @ Test
149
+ public void run_givenUrlNameSpace_shouldProduceUUID () {
150
+ // given
151
+
152
+ // when
153
+ List <String > arguments = new ArrayList <>(Arrays .asList ("-s" , "url" ));
154
+ arguments .add (useCase );
155
+ jug_underTest .run (arguments .toArray ((String []) Array .newInstance (String .class , 0 )));
156
+
157
+ // then - if it is a UUID then we should be able to parse it back out
158
+ String actualUuid = outContent .toString ();
159
+ assertEquals ('\n' , actualUuid .charAt (actualUuid .length () - 1 ));
160
+
161
+ assertEquals (UUID .class ,
162
+ UUID .fromString (actualUuid .substring (0 , actualUuid .length () - 1 )).getClass ());
163
+ }
164
+
165
+ @ Test
166
+ public void run_givenPerformance_shouldProducePerformanceInfo () {
167
+ // given
168
+
169
+ // when
170
+ List <String > arguments = Arrays .asList ("-p" , useCase );
171
+ jug_underTest .run (arguments .toArray ((String []) Array .newInstance (String .class , 0 )));
172
+
173
+ // then
174
+ String actualOutput = outContent .toString ();
175
+
176
+ assertThat (actualOutput , containsString ("Performance: took" ));
177
+ }
178
+
179
+ @ Test
180
+ public void run_givenHelp_shouldProduceHelpInfo () {
181
+ // given
182
+
183
+ // when
184
+ List <String > arguments = Arrays .asList ("-h" , useCase );
185
+ jug_underTest .run (arguments .toArray ((String []) Array .newInstance (String .class , 0 )));
186
+
187
+ // then
188
+ String actualOutput = errContent .toString ();
189
+
190
+ assertThat (actualOutput , containsString ("Usage: java" ));
191
+ }
192
+
193
+ @ Test
194
+ public void run_givenVerbose_shouldProduceExtraInfo () {
195
+ // given
196
+
197
+ // when
198
+ List <String > arguments = Arrays .asList ("-v" , useCase );
199
+ jug_underTest .run (arguments .toArray ((String []) Array .newInstance (String .class , 0 )));
200
+
201
+ // then
202
+ String actualOutput = outContent .toString ();
203
+
204
+ assertThat (actualOutput , containsString ("Done." ));
205
+ }
206
+
207
+ @ Parameterized .Parameters (name = "{index} -> type: {0}" )
208
+ public static List <String > useCases () {
209
+ return Arrays .asList (
210
+ "t" ,
211
+ "o" ,
212
+ "r" ,
213
+ "e" ,
214
+ "m"
215
+ );
216
+ }
217
+ }
0 commit comments