2
2
3
3
import static java .util .Arrays .asList ;
4
4
import static org .hamcrest .MatcherAssert .assertThat ;
5
+ import static org .hamcrest .Matchers .emptyString ;
5
6
import static org .hamcrest .Matchers .instanceOf ;
6
7
import static org .hamcrest .Matchers .is ;
7
8
import static org .hamcrest .Matchers .lessThan ;
9
+ import static org .hamcrest .Matchers .nullValue ;
8
10
import static org .hamcrest .Matchers .theInstance ;
9
11
import static org .junit .Assert .assertThrows ;
12
+ import static org .junit .Assert .assertTrue ;
10
13
import static org .junit .Assert .fail ;
11
- import static org .mockito .Mockito .mock ;
12
- import static org .mockito .Mockito .verify ;
13
- import static org .mockito .Mockito .verifyNoInteractions ;
14
- import static org .mockito .Mockito .when ;
15
14
16
15
import hudson .model .TaskListener ;
17
16
import hudson .plugins .git .GitException ;
17
+ import hudson .util .StreamTaskListener ;
18
+ import java .io .ByteArrayOutputStream ;
18
19
import java .io .PrintStream ;
20
+ import java .nio .charset .StandardCharsets ;
19
21
import java .util .ArrayList ;
20
22
import java .util .Collections ;
21
23
import java .util .List ;
@@ -32,15 +34,17 @@ public class GitCommandsExecutorTest {
32
34
33
35
private final int threads ;
34
36
private final TaskListener listener ;
37
+ private final ByteArrayOutputStream logStream ;
35
38
36
39
public GitCommandsExecutorTest (int threads ) {
37
40
this .threads = threads ;
38
- this .listener = mockTaskListener ();
41
+ this .logStream = new ByteArrayOutputStream ();
42
+ this .listener = new StreamTaskListener (new PrintStream (logStream ), StandardCharsets .UTF_8 );
39
43
}
40
44
41
45
@ After
42
46
public void verifyCorrectExecutorServiceShutdown () {
43
- verifyNoInteractions ( listener );
47
+ loggedNoOutput ( );
44
48
}
45
49
46
50
@ Parameters (name = "threads={0}" )
@@ -58,7 +62,7 @@ public void allCommandsSucceed() throws Exception {
58
62
new GitCommandsExecutor (threads , listener ).invokeAll (commands );
59
63
60
64
for (Callable <String > command : commands ) {
61
- verify (command ). call ( );
65
+ wasCalled (command );
62
66
}
63
67
}
64
68
@@ -69,14 +73,11 @@ public void allCommandsFail() throws Exception {
69
73
commands .add (erroneousCommand (new RuntimeException ("some error" )));
70
74
}
71
75
72
- try {
76
+ Exception e = assertThrows ( GitException . class , () -> {
73
77
new GitCommandsExecutor (threads , listener ).invokeAll (commands );
74
- fail ("Expected an exception but none was thrown" );
75
- } catch (Exception e ) {
76
- assertThat (e , instanceOf (GitException .class ));
77
- assertThat (e .getMessage (), is ("java.lang.RuntimeException: some error" ));
78
- assertThat (e .getCause (), instanceOf (RuntimeException .class ));
79
- }
78
+ });
79
+ assertThat (e .getMessage (), is ("java.lang.RuntimeException: some error" ));
80
+ assertThat (e .getCause (), instanceOf (RuntimeException .class ));
80
81
}
81
82
82
83
@ Test
@@ -88,21 +89,16 @@ public void firstCommandFails() throws Exception {
88
89
successfulCommand ("some value" , commandExecutionTime ));
89
90
90
91
long executionStartMillis = System .currentTimeMillis ();
91
- try {
92
+ Exception e = assertThrows ( GitException . class , () -> {
92
93
new GitCommandsExecutor (threads , listener ).invokeAll (commands );
93
- fail ("Expected an exception but none was thrown" );
94
- } catch (Exception e ) {
95
- assertThat (e , instanceOf (GitException .class ));
96
- assertThat (e .getMessage (), is ("java.lang.RuntimeException: some error" ));
97
- assertThat (e .getCause (), instanceOf (RuntimeException .class ));
98
- }
94
+ });
95
+ assertThat (e .getMessage (), is ("java.lang.RuntimeException: some error" ));
96
+ assertThat (e .getCause (), instanceOf (RuntimeException .class ));
99
97
long executionStopMillis = System .currentTimeMillis ();
100
98
101
99
for (Callable <String > command : commands ) {
102
100
if (commands .indexOf (command ) < threads ) {
103
- verify (command ).call ();
104
- } else {
105
- verifyNoInteractions (command );
101
+ wasCalled (command );
106
102
}
107
103
}
108
104
assertThat (executionStopMillis - executionStartMillis , is (lessThan (commandExecutionTime )));
@@ -115,17 +111,14 @@ public void lastCommandFails() throws Exception {
115
111
successfulCommand ("some value" ),
116
112
erroneousCommand (new RuntimeException ("some error" )));
117
113
118
- try {
114
+ Exception e = assertThrows ( GitException . class , () -> {
119
115
new GitCommandsExecutor (threads , listener ).invokeAll (commands );
120
- fail ("Expected an exception but none was thrown" );
121
- } catch (Exception e ) {
122
- assertThat (e , instanceOf (GitException .class ));
123
- assertThat (e .getMessage (), is ("java.lang.RuntimeException: some error" ));
124
- assertThat (e .getCause (), instanceOf (RuntimeException .class ));
125
- }
116
+ });
117
+ assertThat (e .getMessage (), is ("java.lang.RuntimeException: some error" ));
118
+ assertThat (e .getCause (), instanceOf (RuntimeException .class ));
126
119
127
120
for (Callable <String > command : commands ) {
128
- verify (command ). call ( );
121
+ wasCalled (command );
129
122
}
130
123
}
131
124
@@ -139,7 +132,7 @@ public void moreCommandsThanThreads() throws Exception {
139
132
new GitCommandsExecutor (threads , listener ).invokeAll (commands );
140
133
141
134
for (Callable <String > command : commands ) {
142
- verify (command ). call ( );
135
+ wasCalled (command );
143
136
}
144
137
}
145
138
@@ -153,7 +146,7 @@ public void lessCommandsThanThreads() throws Exception {
153
146
new GitCommandsExecutor (threads , listener ).invokeAll (commands );
154
147
155
148
for (Callable <String > command : commands ) {
156
- verify (command ). call ( );
149
+ wasCalled (command );
157
150
}
158
151
}
159
152
@@ -185,9 +178,9 @@ public void callerThreadWasInterrupted() throws Exception {
185
178
186
179
for (Callable <String > command : commands ) {
187
180
if (commands .indexOf (command ) < threads ) {
188
- verify (command ). call ( );
181
+ wasCalled (command );
189
182
} else {
190
- verifyNoInteractions ( command );
183
+ loggedNoOutput ( );
191
184
}
192
185
}
193
186
assertThat (callerStopMillis - callerStartMillis , is (lessThan (commandExecutionTime )));
@@ -198,63 +191,113 @@ public void callerThreadWasInterrupted() throws Exception {
198
191
public void commandWasInterrupted () throws Exception {
199
192
Exception commandException = new InterruptedException ("some interrupt" );
200
193
List <Callable <String >> commands = Collections .singletonList (erroneousCommand (commandException ));
201
- assertThrows (InterruptedException .class , () -> new GitCommandsExecutor (threads , listener ).invokeAll (commands ));
194
+ Exception e = assertThrows (
195
+ InterruptedException .class , () -> new GitCommandsExecutor (threads , listener ).invokeAll (commands ));
196
+ assertThat (e .getMessage (), is (nullValue ()));
197
+ if (threads == 1 ) {
198
+ assertThat (e .getCause (), is (nullValue ()));
199
+ } else {
200
+ assertThat (e .getCause (), is (theInstance (commandException )));
201
+ }
202
202
}
203
203
204
204
@ Test
205
205
public void commandHadGitProblem () throws Exception {
206
206
Exception commandException = new GitException ("some error" );
207
207
List <Callable <String >> commands = Collections .singletonList (erroneousCommand (commandException ));
208
208
209
- try {
209
+ Exception e = assertThrows ( GitException . class , () -> {
210
210
new GitCommandsExecutor (threads , listener ).invokeAll (commands );
211
- fail ("Expected an exception but none was thrown" );
212
- } catch (Exception e ) {
213
- assertThat (e , instanceOf (GitException .class ));
214
- assertThat (e .getMessage (), is ("hudson.plugins.git.GitException: some error" ));
215
- assertThat (e .getCause (), is (theInstance (commandException )));
216
- }
211
+ });
212
+ assertThat (e .getMessage (), is ("hudson.plugins.git.GitException: some error" ));
213
+ assertThat (e .getCause (), is (theInstance (commandException )));
217
214
}
218
215
219
216
@ Test
220
217
public void commandHadUnknownProblem () throws Exception {
221
218
Exception commandException = new RuntimeException ("some error" );
222
219
List <Callable <String >> commands = Collections .singletonList (erroneousCommand (commandException ));
223
220
224
- try {
221
+ Exception e = assertThrows ( GitException . class , () -> {
225
222
new GitCommandsExecutor (threads , listener ).invokeAll (commands );
226
- fail ("Expected an exception but none was thrown" );
227
- } catch (Exception e ) {
228
- assertThat (e , instanceOf (GitException .class ));
229
- assertThat (e .getMessage (), is ("java.lang.RuntimeException: some error" ));
230
- assertThat (e .getCause (), is (theInstance (commandException )));
231
- }
232
- }
233
-
234
- private TaskListener mockTaskListener () {
235
- TaskListener listener = mock (TaskListener .class );
236
- when (listener .getLogger ()).thenReturn (mock (PrintStream .class ));
237
- return listener ;
223
+ });
224
+ assertThat (e .getMessage (), is ("java.lang.RuntimeException: some error" ));
225
+ assertThat (e .getCause (), is (theInstance (commandException )));
238
226
}
239
227
240
228
private Callable <String > successfulCommand (String value ) throws Exception {
241
- Callable <String > command = mock (Callable .class );
242
- when (command .call ()).thenReturn (value );
243
- return command ;
229
+ return new SuccessfulCommand (value );
244
230
}
245
231
246
232
private Callable <String > successfulCommand (String value , long commandExecutionTime ) throws Exception {
247
- Callable <String > command = mock (Callable .class );
248
- when (command .call ()).then (invocation -> {
249
- Thread .sleep (commandExecutionTime );
233
+ return new SuccessfulCommand (value , commandExecutionTime );
234
+ }
235
+
236
+ private void loggedNoOutput () {
237
+ String loggedOutput = logStream .toString ();
238
+ assertThat (loggedOutput , is (emptyString ()));
239
+ }
240
+
241
+ private void wasCalled (Callable <String > command ) {
242
+ if (command instanceof SuccessfulCommand succeeds ) {
243
+ assertTrue (succeeds .wasCalled ());
244
+ } else if (command instanceof ErroneousCommand erroneous ) {
245
+ assertTrue (erroneous .wasCalled ());
246
+ } else {
247
+ fail ("Unexpected command type " + command );
248
+ }
249
+ }
250
+
251
+ private class SuccessfulCommand implements Callable {
252
+
253
+ private final String value ;
254
+ private final long commandExecutionTime ;
255
+ private boolean called = false ;
256
+
257
+ SuccessfulCommand (String value ) {
258
+ this (value , -1 );
259
+ }
260
+
261
+ SuccessfulCommand (String value , long commandExecutionTime ) {
262
+ this .value = value ;
263
+ this .commandExecutionTime = commandExecutionTime ;
264
+ }
265
+
266
+ boolean wasCalled () {
267
+ return called ;
268
+ }
269
+
270
+ @ Override
271
+ public Object call () throws Exception {
272
+ called = true ;
273
+ if (commandExecutionTime > 0 ) {
274
+ Thread .sleep (commandExecutionTime );
275
+ }
250
276
return value ;
251
- });
252
- return command ;
277
+ }
253
278
}
254
279
255
280
private Callable <String > erroneousCommand (Exception exception ) throws Exception {
256
- Callable <String > command = mock (Callable .class );
257
- when (command .call ()).thenThrow (exception );
258
- return command ;
281
+ return new ErroneousCommand (exception );
282
+ }
283
+
284
+ private class ErroneousCommand implements Callable {
285
+
286
+ private final Exception exception ;
287
+ private boolean called = false ;
288
+
289
+ ErroneousCommand (Exception exceptionToThrow ) {
290
+ exception = exceptionToThrow ;
291
+ }
292
+
293
+ boolean wasCalled () {
294
+ return called ;
295
+ }
296
+
297
+ @ Override
298
+ public Object call () throws Exception {
299
+ called = true ;
300
+ throw exception ;
301
+ }
259
302
}
260
303
}
0 commit comments