diff --git a/src/main/java/org/kiwiproject/retry/Attempt.java b/src/main/java/org/kiwiproject/retry/Attempt.java index d1e2684..129c7a2 100644 --- a/src/main/java/org/kiwiproject/retry/Attempt.java +++ b/src/main/java/org/kiwiproject/retry/Attempt.java @@ -1,6 +1,7 @@ package org.kiwiproject.retry; import static com.google.common.base.Preconditions.checkState; +import static java.util.Objects.requireNonNull; /** * An attempt of a call, which resulted either in a result returned by the call, @@ -24,7 +25,12 @@ private Attempt(T result, int attemptNumber, long delaySinceFirstAttempt) { } private Attempt(Exception exception, int attemptNumber, long delaySinceFirstAttempt) { - this(null, exception, attemptNumber, delaySinceFirstAttempt); + this( + null, + requireNonNull(exception, "exception must not be null"), + attemptNumber, + delaySinceFirstAttempt + ); } private Attempt(T result, Exception exception, int attemptNumber, long delaySinceFirstAttempt) { @@ -37,7 +43,7 @@ private Attempt(T result, Exception exception, int attemptNumber, long delaySinc /** * Create a new {@link Attempt} that has a result. * - * @param result the result of the attempt + * @param result the result of the attempt, may be null * @param attemptNumber the number of this attempt * @param delaySinceFirstAttempt the delay in milliseconds since the first attempt was made * @param the type of result diff --git a/src/test/java/org/kiwiproject/retry/AttemptTest.java b/src/test/java/org/kiwiproject/retry/AttemptTest.java new file mode 100644 index 0000000..05af798 --- /dev/null +++ b/src/test/java/org/kiwiproject/retry/AttemptTest.java @@ -0,0 +1,79 @@ +package org.kiwiproject.retry; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; +import static org.assertj.core.api.Assertions.assertThatNullPointerException; +import static org.junit.jupiter.api.Assertions.assertAll; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.concurrent.ThreadLocalRandom; + +@DisplayName("Attempt") +class AttemptTest { + + private ThreadLocalRandom rand; + int attemptNumber; + long delaySinceFirstAttempt; + + @BeforeEach + void setUp() { + rand = ThreadLocalRandom.current(); + attemptNumber = rand.nextInt(1, 5); + delaySinceFirstAttempt = rand.nextLong(1_000, 5_000); + } + + @Test + void shouldCreateWithResult() { + var result = rand.nextInt(0, 101); + var attempt = Attempt.newResultAttempt(result, attemptNumber, delaySinceFirstAttempt); + + assertAll( + () -> assertThat(attempt.hasResult()).isTrue(), + () -> assertThat(attempt.hasException()).isFalse(), + () -> assertThat(attempt.getResult()).isEqualTo(result), + () -> assertThatIllegalStateException().isThrownBy(() -> attempt.getException()), + () -> assertThat(attempt.getAttemptNumber()).isEqualTo(attemptNumber), + () -> assertThat(attempt.getDelaySinceFirstAttempt()).isEqualTo(delaySinceFirstAttempt) + ); + } + + @Test + void shouldAllowNullResult() { + var attempt = Attempt.newResultAttempt(null, attemptNumber, delaySinceFirstAttempt); + + assertAll( + () -> assertThat(attempt.hasResult()).isTrue(), + () -> assertThat(attempt.hasException()).isFalse(), + () -> assertThat(attempt.getResult()).isNull(), + () -> assertThatIllegalStateException().isThrownBy(() -> attempt.getException()), + () -> assertThat(attempt.getAttemptNumber()).isEqualTo(attemptNumber), + () -> assertThat(attempt.getDelaySinceFirstAttempt()).isEqualTo(delaySinceFirstAttempt) + ); + } + + @Test + void shouldCreateWithException() { + var exception = new IOException("I/O error - device not ready"); + var attempt = Attempt.newExceptionAttempt(exception, attemptNumber, delaySinceFirstAttempt); + + assertAll( + () -> assertThat(attempt.hasResult()).isFalse(), + () -> assertThat(attempt.hasException()).isTrue(), + () -> assertThatIllegalStateException().isThrownBy(() -> attempt.getResult()), + () -> assertThat(attempt.getException()).isSameAs(exception), + () -> assertThat(attempt.getAttemptNumber()).isEqualTo(attemptNumber), + () -> assertThat(attempt.getDelaySinceFirstAttempt()).isEqualTo(delaySinceFirstAttempt) + ); + } + + @Test + void shouldNotPermitExceptionAttempt_WithNullException() { + assertThatNullPointerException() + .isThrownBy(() -> Attempt.newExceptionAttempt(null, attemptNumber, delaySinceFirstAttempt)) + .withMessage("exception must not be null"); + } +}