2424import java .util .concurrent .Callable ;
2525import java .util .concurrent .atomic .AtomicBoolean ;
2626import org .junit .After ;
27+ import org .junit .Before ;
28+ import org .junit .Rule ;
2729import org .junit .Test ;
30+ import org .junit .rules .TestName ;
2831import org .junit .runner .RunWith ;
2932import org .junit .runners .Parameterized ;
3033import org .junit .runners .Parameterized .Parameters ;
@@ -36,6 +39,9 @@ public class GitCommandsExecutorTest {
3639 private final TaskListener listener ;
3740 private final ByteArrayOutputStream logStream ;
3841
42+ @ Rule
43+ public TestName testName = new TestName ();
44+
3945 public GitCommandsExecutorTest (int threads ) {
4046 this .threads = threads ;
4147 this .logStream = new ByteArrayOutputStream ();
@@ -47,6 +53,15 @@ public void verifyCorrectExecutorServiceShutdown() {
4753 loggedNoOutput ();
4854 }
4955
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+
5065 @ Parameters (name = "threads={0}" )
5166 public static Iterable <Integer > threadsParameter () {
5267 return asList (1 , 2 , 100 );
@@ -56,7 +71,7 @@ public static Iterable<Integer> threadsParameter() {
5671 public void allCommandsSucceed () throws Exception {
5772 List <Callable <String >> commands = new ArrayList <>();
5873 for (int i = 0 ; i < threads ; i ++) {
59- commands .add (successfulCommand ( "some value" ));
74+ commands .add (new GoodCommand ( value ));
6075 }
6176
6277 new GitCommandsExecutor (threads , listener ).invokeAll (commands );
@@ -70,29 +85,29 @@ public void allCommandsSucceed() throws Exception {
7085 public void allCommandsFail () throws Exception {
7186 List <Callable <String >> commands = new ArrayList <>();
7287 for (int i = 0 ; i < threads ; i ++) {
73- commands .add (erroneousCommand (new RuntimeException ("some error" )));
88+ commands .add (new BadCommand (new RuntimeException (error )));
7489 }
7590
7691 Exception e = assertThrows (GitException .class , () -> {
7792 new GitCommandsExecutor (threads , listener ).invokeAll (commands );
7893 });
79- assertThat (e .getMessage (), is ("java.lang.RuntimeException: some error" ));
94+ assertThat (e .getMessage (), is ("java.lang.RuntimeException: " + error ));
8095 assertThat (e .getCause (), instanceOf (RuntimeException .class ));
8196 }
8297
8398 @ Test
8499 public void firstCommandFails () throws Exception {
85100 long commandExecutionTime = 60_000 ;
86101 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 ));
90105
91106 long executionStartMillis = System .currentTimeMillis ();
92107 Exception e = assertThrows (GitException .class , () -> {
93108 new GitCommandsExecutor (threads , listener ).invokeAll (commands );
94109 });
95- assertThat (e .getMessage (), is ("java.lang.RuntimeException: some error" ));
110+ assertThat (e .getMessage (), is ("java.lang.RuntimeException: " + error ));
96111 assertThat (e .getCause (), instanceOf (RuntimeException .class ));
97112 long executionStopMillis = System .currentTimeMillis ();
98113
@@ -106,15 +121,13 @@ public void firstCommandFails() throws Exception {
106121
107122 @ Test
108123 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 )));
113126
114127 Exception e = assertThrows (GitException .class , () -> {
115128 new GitCommandsExecutor (threads , listener ).invokeAll (commands );
116129 });
117- assertThat (e .getMessage (), is ("java.lang.RuntimeException: some error" ));
130+ assertThat (e .getMessage (), is ("java.lang.RuntimeException: " + error ));
118131 assertThat (e .getCause (), instanceOf (RuntimeException .class ));
119132
120133 for (Callable <String > command : commands ) {
@@ -126,7 +139,7 @@ public void lastCommandFails() throws Exception {
126139 public void moreCommandsThanThreads () throws Exception {
127140 List <Callable <String >> commands = new ArrayList <>();
128141 for (int i = 0 ; i < threads + 1 ; i ++) {
129- commands .add (successfulCommand ( "some value" ));
142+ commands .add (new GoodCommand ( value ));
130143 }
131144
132145 new GitCommandsExecutor (threads , listener ).invokeAll (commands );
@@ -140,7 +153,7 @@ public void moreCommandsThanThreads() throws Exception {
140153 public void lessCommandsThanThreads () throws Exception {
141154 List <Callable <String >> commands = new ArrayList <>();
142155 for (int i = 0 ; i < threads - 1 ; i ++) {
143- commands .add (successfulCommand ( "some value" ));
156+ commands .add (new GoodCommand ( value ));
144157 }
145158
146159 new GitCommandsExecutor (threads , listener ).invokeAll (commands );
@@ -155,7 +168,7 @@ public void callerThreadWasInterrupted() throws Exception {
155168 long commandExecutionTime = 60_000 ;
156169 List <Callable <String >> commands = new ArrayList <>();
157170 for (int i = 0 ; i < threads + 1 ; i ++) {
158- commands .add (successfulCommand ( "some value" , commandExecutionTime ));
171+ commands .add (new GoodCommand ( value , commandExecutionTime ));
159172 }
160173
161174 AtomicBoolean isCallerInterrupted = new AtomicBoolean (false );
@@ -190,7 +203,7 @@ public void callerThreadWasInterrupted() throws Exception {
190203 @ Test
191204 public void commandWasInterrupted () throws Exception {
192205 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 ));
194207 Exception e = assertThrows (
195208 InterruptedException .class , () -> new GitCommandsExecutor (threads , listener ).invokeAll (commands ));
196209 assertThat (e .getMessage (), is (nullValue ()));
@@ -203,62 +216,55 @@ public void commandWasInterrupted() throws Exception {
203216
204217 @ Test
205218 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 ));
208221
209222 Exception e = assertThrows (GitException .class , () -> {
210223 new GitCommandsExecutor (threads , listener ).invokeAll (commands );
211224 });
212- assertThat (e .getMessage (), is ("hudson.plugins.git.GitException: some error" ));
225+ assertThat (e .getMessage (), is ("hudson.plugins.git.GitException: " + error ));
213226 assertThat (e .getCause (), is (theInstance (commandException )));
214227 }
215228
216229 @ Test
217230 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 ));
220233
221234 Exception e = assertThrows (GitException .class , () -> {
222235 new GitCommandsExecutor (threads , listener ).invokeAll (commands );
223236 });
224- assertThat (e .getMessage (), is ("java.lang.RuntimeException: some error" ));
237+ assertThat (e .getMessage (), is ("java.lang.RuntimeException: " + error ));
225238 assertThat (e .getCause (), is (theInstance (commandException )));
226239 }
227240
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-
236241 private void loggedNoOutput () {
237242 String loggedOutput = logStream .toString ();
238243 assertThat (loggedOutput , is (emptyString ()));
239244 }
240245
241246 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 ());
246251 } else {
247252 fail ("Unexpected command type " + command );
248253 }
249254 }
250255
251- private class SuccessfulCommand implements Callable {
256+ /* A callable that always returns a value, sometimes after a delay */
257+ private class GoodCommand implements Callable {
252258
253259 private final String value ;
254260 private final long commandExecutionTime ;
255261 private boolean called = false ;
256262
257- SuccessfulCommand (String value ) {
263+ GoodCommand (String value ) {
258264 this (value , -1 );
259265 }
260266
261- SuccessfulCommand (String value , long commandExecutionTime ) {
267+ GoodCommand (String value , long commandExecutionTime ) {
262268 this .value = value ;
263269 this .commandExecutionTime = commandExecutionTime ;
264270 }
@@ -277,16 +283,13 @@ public Object call() throws Exception {
277283 }
278284 }
279285
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 {
285288
286289 private final Exception exception ;
287290 private boolean called = false ;
288291
289- ErroneousCommand (Exception exceptionToThrow ) {
292+ BadCommand (Exception exceptionToThrow ) {
290293 exception = exceptionToThrow ;
291294 }
292295
0 commit comments