diff --git a/src/test/java/com/blogspot/nurkiewicz/asyncretry/policy/RetryPolicyPredicatesTest.java b/src/test/java/com/blogspot/nurkiewicz/asyncretry/policy/RetryPolicyPredicatesTest.java index c714e34..0779d37 100644 --- a/src/test/java/com/blogspot/nurkiewicz/asyncretry/policy/RetryPolicyPredicatesTest.java +++ b/src/test/java/com/blogspot/nurkiewicz/asyncretry/policy/RetryPolicyPredicatesTest.java @@ -18,7 +18,7 @@ public class RetryPolicyPredicatesTest extends AbstractRetryPolicyTest { private RetryContext retryContextMock; @Test - public void shouldAbortIfPredicateTrue() throws Exception { + public void shouldAbortIfAbortPredicateTrue() throws Exception { //given final RetryPolicy retryPolicy = new RetryPolicy().abortIf(t -> true); @@ -29,6 +29,95 @@ public void shouldAbortIfPredicateTrue() throws Exception { assertThat(shouldRetry).isFalse(); } + @Test + public void shouldRetryIfRetryPredicateTrue() throws Exception { + //given + final RetryPolicy retryPolicy = new RetryPolicy().retryIf(t -> true); + + //when + final boolean shouldRetry = retryPolicy.shouldContinue(retryContextMock); + + //then + assertThat(shouldRetry).isTrue(); + } + + @Test + public void shouldRetryIfBothPredicatesAbstainButClassShouldRetry() throws Exception { + //given + final RetryPolicy retryPolicy = new RetryPolicy(). + retryIf(t -> false). + abortIf(t -> false); + given(retryContextMock.getLastThrowable()).willReturn(new RuntimeException()); + + //when + final boolean shouldRetry = retryPolicy.shouldContinue(retryContextMock); + + //then + assertThat(shouldRetry).isTrue(); + } + + @Test + public void shouldAbortIfBothPredicatesAbstainButClassShouldAbort() throws Exception { + //given + final RetryPolicy retryPolicy = new RetryPolicy(). + abortOn(NullPointerException.class). + retryIf(t -> false). + abortIf(t -> false); + given(retryContextMock.getLastThrowable()).willReturn(new NullPointerException()); + + //when + final boolean shouldRetry = retryPolicy.shouldContinue(retryContextMock); + + //then + assertThat(shouldRetry).isFalse(); + } + + @Test + public void shouldRetryIfPredicateTrueEvenIfClassShouldAbort() throws Exception { + //given + final RetryPolicy retryPolicy = new RetryPolicy(). + abortOn(NullPointerException.class). + retryIf(t -> true); + given(retryContextMock.getLastThrowable()).willReturn(new NullPointerException()); + + //when + final boolean shouldRetry = retryPolicy.shouldContinue(retryContextMock); + + //then + assertThat(shouldRetry).isTrue(); + } + + @Test + public void shouldAbortIfPredicateTrueEvenIfClassShouldRetry() throws Exception { + //given + final RetryPolicy retryPolicy = new RetryPolicy(). + retryOn(NullPointerException.class). + abortIf(t -> true); + given(retryContextMock.getLastThrowable()).willReturn(new NullPointerException()); + + //when + final boolean shouldRetry = retryPolicy.shouldContinue(retryContextMock); + + //then + assertThat(shouldRetry).isFalse(); + } + + @Test + public void whenAbortAndRetryPredicatesBothYieldTrueThenAbortWins() throws Exception { + //given + final RetryPolicy retryPolicy = new RetryPolicy(). + retryOn(NullPointerException.class). + retryIf(t -> t.getMessage().contains("Foo")). + abortIf(t -> t.getMessage().contains("Foo")); + given(retryContextMock.getLastThrowable()).willReturn(new NullPointerException("Foo")); + + //when + final boolean shouldRetry = retryPolicy.shouldContinue(retryContextMock); + + //then + assertThat(shouldRetry).isFalse(); + } + @Test public void shouldProceedIfPredicateFalseAndChildAccepts() throws Exception { //given @@ -54,6 +143,22 @@ public void shouldAbortIfPredicateFalseButShouldNotRetry() throws Exception { assertThat(shouldRetry).isFalse(); } + @Test + public void shouldAbortIfPredicateTrueButShouldNotRetry() throws Exception { + //given + final RetryPolicy retryPolicy = new RetryPolicy(). + retryIf(t -> true). + dontRetry(); + given(retryContextMock.getLastThrowable()).willReturn(new NullPointerException()); + given(retryContextMock.getRetryCount()).willReturn(1); + + //when + final boolean shouldRetry = retryPolicy.shouldContinue(retryContextMock); + + //then + assertThat(shouldRetry).isFalse(); + } + @Test public void shouldExamineExceptionAndDecide() throws Exception { //given