24
24
import java .util .concurrent .Callable ;
25
25
import java .util .concurrent .atomic .AtomicBoolean ;
26
26
import org .junit .After ;
27
+ import org .junit .Before ;
28
+ import org .junit .Rule ;
27
29
import org .junit .Test ;
30
+ import org .junit .rules .TestName ;
28
31
import org .junit .runner .RunWith ;
29
32
import org .junit .runners .Parameterized ;
30
33
import org .junit .runners .Parameterized .Parameters ;
@@ -36,6 +39,9 @@ public class GitCommandsExecutorTest {
36
39
private final TaskListener listener ;
37
40
private final ByteArrayOutputStream logStream ;
38
41
42
+ @ Rule
43
+ public TestName testName = new TestName ();
44
+
39
45
public GitCommandsExecutorTest (int threads ) {
40
46
this .threads = threads ;
41
47
this .logStream = new ByteArrayOutputStream ();
@@ -47,6 +53,15 @@ public void verifyCorrectExecutorServiceShutdown() {
47
53
loggedNoOutput ();
48
54
}
49
55
56
+ private String value = null ;
57
+ private String error = null ;
58
+
59
+ @ Before
60
+ public void defineValues () {
61
+ value = "some value " + testName .getMethodName ();
62
+ error = "some error " + testName .getMethodName ();
63
+ }
64
+
50
65
@ Parameters (name = "threads={0}" )
51
66
public static Iterable <Integer > threadsParameter () {
52
67
return asList (1 , 2 , 100 );
@@ -56,7 +71,7 @@ public static Iterable<Integer> threadsParameter() {
56
71
public void allCommandsSucceed () throws Exception {
57
72
List <Callable <String >> commands = new ArrayList <>();
58
73
for (int i = 0 ; i < threads ; i ++) {
59
- commands .add (successfulCommand ( "some value" ));
74
+ commands .add (new GoodCommand ( value ));
60
75
}
61
76
62
77
new GitCommandsExecutor (threads , listener ).invokeAll (commands );
@@ -70,29 +85,29 @@ public void allCommandsSucceed() throws Exception {
70
85
public void allCommandsFail () throws Exception {
71
86
List <Callable <String >> commands = new ArrayList <>();
72
87
for (int i = 0 ; i < threads ; i ++) {
73
- commands .add (erroneousCommand (new RuntimeException ("some error" )));
88
+ commands .add (new BadCommand (new RuntimeException (error )));
74
89
}
75
90
76
91
Exception e = assertThrows (GitException .class , () -> {
77
92
new GitCommandsExecutor (threads , listener ).invokeAll (commands );
78
93
});
79
- assertThat (e .getMessage (), is ("java.lang.RuntimeException: some error" ));
94
+ assertThat (e .getMessage (), is ("java.lang.RuntimeException: " + error ));
80
95
assertThat (e .getCause (), instanceOf (RuntimeException .class ));
81
96
}
82
97
83
98
@ Test
84
99
public void firstCommandFails () throws Exception {
85
100
long commandExecutionTime = 60_000 ;
86
101
List <Callable <String >> commands = asList (
87
- erroneousCommand (new RuntimeException ("some error" )),
88
- successfulCommand ( "some value" , commandExecutionTime ),
89
- successfulCommand ( "some value" , commandExecutionTime ));
102
+ new BadCommand (new RuntimeException (error )),
103
+ new GoodCommand ( value , commandExecutionTime ),
104
+ new GoodCommand ( value , commandExecutionTime ));
90
105
91
106
long executionStartMillis = System .currentTimeMillis ();
92
107
Exception e = assertThrows (GitException .class , () -> {
93
108
new GitCommandsExecutor (threads , listener ).invokeAll (commands );
94
109
});
95
- assertThat (e .getMessage (), is ("java.lang.RuntimeException: some error" ));
110
+ assertThat (e .getMessage (), is ("java.lang.RuntimeException: " + error ));
96
111
assertThat (e .getCause (), instanceOf (RuntimeException .class ));
97
112
long executionStopMillis = System .currentTimeMillis ();
98
113
@@ -106,15 +121,13 @@ public void firstCommandFails() throws Exception {
106
121
107
122
@ Test
108
123
public void lastCommandFails () throws Exception {
109
- List <Callable <String >> commands = asList (
110
- successfulCommand ("some value" ),
111
- successfulCommand ("some value" ),
112
- erroneousCommand (new RuntimeException ("some error" )));
124
+ List <Callable <String >> commands =
125
+ asList (new GoodCommand (value ), new GoodCommand (value ), new BadCommand (new RuntimeException (error )));
113
126
114
127
Exception e = assertThrows (GitException .class , () -> {
115
128
new GitCommandsExecutor (threads , listener ).invokeAll (commands );
116
129
});
117
- assertThat (e .getMessage (), is ("java.lang.RuntimeException: some error" ));
130
+ assertThat (e .getMessage (), is ("java.lang.RuntimeException: " + error ));
118
131
assertThat (e .getCause (), instanceOf (RuntimeException .class ));
119
132
120
133
for (Callable <String > command : commands ) {
@@ -126,7 +139,7 @@ public void lastCommandFails() throws Exception {
126
139
public void moreCommandsThanThreads () throws Exception {
127
140
List <Callable <String >> commands = new ArrayList <>();
128
141
for (int i = 0 ; i < threads + 1 ; i ++) {
129
- commands .add (successfulCommand ( "some value" ));
142
+ commands .add (new GoodCommand ( value ));
130
143
}
131
144
132
145
new GitCommandsExecutor (threads , listener ).invokeAll (commands );
@@ -140,7 +153,7 @@ public void moreCommandsThanThreads() throws Exception {
140
153
public void lessCommandsThanThreads () throws Exception {
141
154
List <Callable <String >> commands = new ArrayList <>();
142
155
for (int i = 0 ; i < threads - 1 ; i ++) {
143
- commands .add (successfulCommand ( "some value" ));
156
+ commands .add (new GoodCommand ( value ));
144
157
}
145
158
146
159
new GitCommandsExecutor (threads , listener ).invokeAll (commands );
@@ -155,7 +168,7 @@ public void callerThreadWasInterrupted() throws Exception {
155
168
long commandExecutionTime = 60_000 ;
156
169
List <Callable <String >> commands = new ArrayList <>();
157
170
for (int i = 0 ; i < threads + 1 ; i ++) {
158
- commands .add (successfulCommand ( "some value" , commandExecutionTime ));
171
+ commands .add (new GoodCommand ( value , commandExecutionTime ));
159
172
}
160
173
161
174
AtomicBoolean isCallerInterrupted = new AtomicBoolean (false );
@@ -190,7 +203,7 @@ public void callerThreadWasInterrupted() throws Exception {
190
203
@ Test
191
204
public void commandWasInterrupted () throws Exception {
192
205
Exception commandException = new InterruptedException ("some interrupt" );
193
- List <Callable <String >> commands = Collections .singletonList (erroneousCommand (commandException ));
206
+ List <Callable <String >> commands = Collections .singletonList (new BadCommand (commandException ));
194
207
Exception e = assertThrows (
195
208
InterruptedException .class , () -> new GitCommandsExecutor (threads , listener ).invokeAll (commands ));
196
209
assertThat (e .getMessage (), is (nullValue ()));
@@ -203,62 +216,55 @@ public void commandWasInterrupted() throws Exception {
203
216
204
217
@ Test
205
218
public void commandHadGitProblem () throws Exception {
206
- Exception commandException = new GitException ("some error" );
207
- List <Callable <String >> commands = Collections .singletonList (erroneousCommand (commandException ));
219
+ Exception commandException = new GitException (error );
220
+ List <Callable <String >> commands = Collections .singletonList (new BadCommand (commandException ));
208
221
209
222
Exception e = assertThrows (GitException .class , () -> {
210
223
new GitCommandsExecutor (threads , listener ).invokeAll (commands );
211
224
});
212
- assertThat (e .getMessage (), is ("hudson.plugins.git.GitException: some error" ));
225
+ assertThat (e .getMessage (), is ("hudson.plugins.git.GitException: " + error ));
213
226
assertThat (e .getCause (), is (theInstance (commandException )));
214
227
}
215
228
216
229
@ Test
217
230
public void commandHadUnknownProblem () throws Exception {
218
- Exception commandException = new RuntimeException ("some error" );
219
- List <Callable <String >> commands = Collections .singletonList (erroneousCommand (commandException ));
231
+ Exception commandException = new RuntimeException (error );
232
+ List <Callable <String >> commands = Collections .singletonList (new BadCommand (commandException ));
220
233
221
234
Exception e = assertThrows (GitException .class , () -> {
222
235
new GitCommandsExecutor (threads , listener ).invokeAll (commands );
223
236
});
224
- assertThat (e .getMessage (), is ("java.lang.RuntimeException: some error" ));
237
+ assertThat (e .getMessage (), is ("java.lang.RuntimeException: " + error ));
225
238
assertThat (e .getCause (), is (theInstance (commandException )));
226
239
}
227
240
228
- private Callable <String > successfulCommand (String value ) throws Exception {
229
- return new SuccessfulCommand (value );
230
- }
231
-
232
- private Callable <String > successfulCommand (String value , long commandExecutionTime ) throws Exception {
233
- return new SuccessfulCommand (value , commandExecutionTime );
234
- }
235
-
236
241
private void loggedNoOutput () {
237
242
String loggedOutput = logStream .toString ();
238
243
assertThat (loggedOutput , is (emptyString ()));
239
244
}
240
245
241
246
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 ());
247
+ if (command instanceof GoodCommand good ) {
248
+ assertTrue (good .wasCalled ());
249
+ } else if (command instanceof BadCommand bad ) {
250
+ assertTrue (bad .wasCalled ());
246
251
} else {
247
252
fail ("Unexpected command type " + command );
248
253
}
249
254
}
250
255
251
- private class SuccessfulCommand implements Callable {
256
+ /* A callable that always returns a value, sometimes after a delay */
257
+ private class GoodCommand implements Callable {
252
258
253
259
private final String value ;
254
260
private final long commandExecutionTime ;
255
261
private boolean called = false ;
256
262
257
- SuccessfulCommand (String value ) {
263
+ GoodCommand (String value ) {
258
264
this (value , -1 );
259
265
}
260
266
261
- SuccessfulCommand (String value , long commandExecutionTime ) {
267
+ GoodCommand (String value , long commandExecutionTime ) {
262
268
this .value = value ;
263
269
this .commandExecutionTime = commandExecutionTime ;
264
270
}
@@ -277,16 +283,13 @@ public Object call() throws Exception {
277
283
}
278
284
}
279
285
280
- private Callable <String > erroneousCommand (Exception exception ) throws Exception {
281
- return new ErroneousCommand (exception );
282
- }
283
-
284
- private class ErroneousCommand implements Callable {
286
+ /* A callable that always throws an exception */
287
+ private class BadCommand implements Callable {
285
288
286
289
private final Exception exception ;
287
290
private boolean called = false ;
288
291
289
- ErroneousCommand (Exception exceptionToThrow ) {
292
+ BadCommand (Exception exceptionToThrow ) {
290
293
exception = exceptionToThrow ;
291
294
}
292
295
0 commit comments