diff --git a/src/test/java/org/jenkinsci/plugins/gitclient/cgit/GitCommandsExecutorTest.java b/src/test/java/org/jenkinsci/plugins/gitclient/cgit/GitCommandsExecutorTest.java index f4346b99a6..d8b7b1c4f9 100644 --- a/src/test/java/org/jenkinsci/plugins/gitclient/cgit/GitCommandsExecutorTest.java +++ b/src/test/java/org/jenkinsci/plugins/gitclient/cgit/GitCommandsExecutorTest.java @@ -24,7 +24,10 @@ import java.util.concurrent.Callable; import java.util.concurrent.atomic.AtomicBoolean; import org.junit.After; +import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TestName; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @@ -36,6 +39,9 @@ public class GitCommandsExecutorTest { private final TaskListener listener; private final ByteArrayOutputStream logStream; + @Rule + public TestName testName = new TestName(); + public GitCommandsExecutorTest(int threads) { this.threads = threads; this.logStream = new ByteArrayOutputStream(); @@ -47,6 +53,15 @@ public void verifyCorrectExecutorServiceShutdown() { loggedNoOutput(); } + private String value = null; + private String error = null; + + @Before + public void defineValues() { + value = "some value " + testName.getMethodName(); + error = "some error " + testName.getMethodName(); + } + @Parameters(name = "threads={0}") public static Iterable threadsParameter() { return asList(1, 2, 100); @@ -56,7 +71,7 @@ public static Iterable threadsParameter() { public void allCommandsSucceed() throws Exception { List> commands = new ArrayList<>(); for (int i = 0; i < threads; i++) { - commands.add(successfulCommand("some value")); + commands.add(new GoodCommand(value)); } new GitCommandsExecutor(threads, listener).invokeAll(commands); @@ -70,13 +85,13 @@ public void allCommandsSucceed() throws Exception { public void allCommandsFail() throws Exception { List> commands = new ArrayList<>(); for (int i = 0; i < threads; i++) { - commands.add(erroneousCommand(new RuntimeException("some error"))); + commands.add(new BadCommand(new RuntimeException(error))); } Exception e = assertThrows(GitException.class, () -> { new GitCommandsExecutor(threads, listener).invokeAll(commands); }); - assertThat(e.getMessage(), is("java.lang.RuntimeException: some error")); + assertThat(e.getMessage(), is("java.lang.RuntimeException: " + error)); assertThat(e.getCause(), instanceOf(RuntimeException.class)); } @@ -84,15 +99,15 @@ public void allCommandsFail() throws Exception { public void firstCommandFails() throws Exception { long commandExecutionTime = 60_000; List> commands = asList( - erroneousCommand(new RuntimeException("some error")), - successfulCommand("some value", commandExecutionTime), - successfulCommand("some value", commandExecutionTime)); + new BadCommand(new RuntimeException(error)), + new GoodCommand(value, commandExecutionTime), + new GoodCommand(value, commandExecutionTime)); long executionStartMillis = System.currentTimeMillis(); Exception e = assertThrows(GitException.class, () -> { new GitCommandsExecutor(threads, listener).invokeAll(commands); }); - assertThat(e.getMessage(), is("java.lang.RuntimeException: some error")); + assertThat(e.getMessage(), is("java.lang.RuntimeException: " + error)); assertThat(e.getCause(), instanceOf(RuntimeException.class)); long executionStopMillis = System.currentTimeMillis(); @@ -106,15 +121,13 @@ public void firstCommandFails() throws Exception { @Test public void lastCommandFails() throws Exception { - List> commands = asList( - successfulCommand("some value"), - successfulCommand("some value"), - erroneousCommand(new RuntimeException("some error"))); + List> commands = + asList(new GoodCommand(value), new GoodCommand(value), new BadCommand(new RuntimeException(error))); Exception e = assertThrows(GitException.class, () -> { new GitCommandsExecutor(threads, listener).invokeAll(commands); }); - assertThat(e.getMessage(), is("java.lang.RuntimeException: some error")); + assertThat(e.getMessage(), is("java.lang.RuntimeException: " + error)); assertThat(e.getCause(), instanceOf(RuntimeException.class)); for (Callable command : commands) { @@ -126,7 +139,7 @@ public void lastCommandFails() throws Exception { public void moreCommandsThanThreads() throws Exception { List> commands = new ArrayList<>(); for (int i = 0; i < threads + 1; i++) { - commands.add(successfulCommand("some value")); + commands.add(new GoodCommand(value)); } new GitCommandsExecutor(threads, listener).invokeAll(commands); @@ -140,7 +153,7 @@ public void moreCommandsThanThreads() throws Exception { public void lessCommandsThanThreads() throws Exception { List> commands = new ArrayList<>(); for (int i = 0; i < threads - 1; i++) { - commands.add(successfulCommand("some value")); + commands.add(new GoodCommand(value)); } new GitCommandsExecutor(threads, listener).invokeAll(commands); @@ -155,7 +168,7 @@ public void callerThreadWasInterrupted() throws Exception { long commandExecutionTime = 60_000; List> commands = new ArrayList<>(); for (int i = 0; i < threads + 1; i++) { - commands.add(successfulCommand("some value", commandExecutionTime)); + commands.add(new GoodCommand(value, commandExecutionTime)); } AtomicBoolean isCallerInterrupted = new AtomicBoolean(false); @@ -190,7 +203,7 @@ public void callerThreadWasInterrupted() throws Exception { @Test public void commandWasInterrupted() throws Exception { Exception commandException = new InterruptedException("some interrupt"); - List> commands = Collections.singletonList(erroneousCommand(commandException)); + List> commands = Collections.singletonList(new BadCommand(commandException)); Exception e = assertThrows( InterruptedException.class, () -> new GitCommandsExecutor(threads, listener).invokeAll(commands)); assertThat(e.getMessage(), is(nullValue())); @@ -203,62 +216,55 @@ public void commandWasInterrupted() throws Exception { @Test public void commandHadGitProblem() throws Exception { - Exception commandException = new GitException("some error"); - List> commands = Collections.singletonList(erroneousCommand(commandException)); + Exception commandException = new GitException(error); + List> commands = Collections.singletonList(new BadCommand(commandException)); Exception e = assertThrows(GitException.class, () -> { new GitCommandsExecutor(threads, listener).invokeAll(commands); }); - assertThat(e.getMessage(), is("hudson.plugins.git.GitException: some error")); + assertThat(e.getMessage(), is("hudson.plugins.git.GitException: " + error)); assertThat(e.getCause(), is(theInstance(commandException))); } @Test public void commandHadUnknownProblem() throws Exception { - Exception commandException = new RuntimeException("some error"); - List> commands = Collections.singletonList(erroneousCommand(commandException)); + Exception commandException = new RuntimeException(error); + List> commands = Collections.singletonList(new BadCommand(commandException)); Exception e = assertThrows(GitException.class, () -> { new GitCommandsExecutor(threads, listener).invokeAll(commands); }); - assertThat(e.getMessage(), is("java.lang.RuntimeException: some error")); + assertThat(e.getMessage(), is("java.lang.RuntimeException: " + error)); assertThat(e.getCause(), is(theInstance(commandException))); } - private Callable successfulCommand(String value) throws Exception { - return new SuccessfulCommand(value); - } - - private Callable successfulCommand(String value, long commandExecutionTime) throws Exception { - return new SuccessfulCommand(value, commandExecutionTime); - } - private void loggedNoOutput() { String loggedOutput = logStream.toString(); assertThat(loggedOutput, is(emptyString())); } private void wasCalled(Callable command) { - if (command instanceof SuccessfulCommand succeeds) { - assertTrue(succeeds.wasCalled()); - } else if (command instanceof ErroneousCommand erroneous) { - assertTrue(erroneous.wasCalled()); + if (command instanceof GoodCommand good) { + assertTrue(good.wasCalled()); + } else if (command instanceof BadCommand bad) { + assertTrue(bad.wasCalled()); } else { fail("Unexpected command type " + command); } } - private class SuccessfulCommand implements Callable { + /* A callable that always returns a value, sometimes after a delay */ + private class GoodCommand implements Callable { private final String value; private final long commandExecutionTime; private boolean called = false; - SuccessfulCommand(String value) { + GoodCommand(String value) { this(value, -1); } - SuccessfulCommand(String value, long commandExecutionTime) { + GoodCommand(String value, long commandExecutionTime) { this.value = value; this.commandExecutionTime = commandExecutionTime; } @@ -277,16 +283,13 @@ public Object call() throws Exception { } } - private Callable erroneousCommand(Exception exception) throws Exception { - return new ErroneousCommand(exception); - } - - private class ErroneousCommand implements Callable { + /* A callable that always throws an exception */ + private class BadCommand implements Callable { private final Exception exception; private boolean called = false; - ErroneousCommand(Exception exceptionToThrow) { + BadCommand(Exception exceptionToThrow) { exception = exceptionToThrow; }