From e3c1e6986b912259af758ee63615bc04b5afc342 Mon Sep 17 00:00:00 2001 From: Muhammed Almas Date: Tue, 31 Jan 2017 01:02:27 +0530 Subject: [PATCH] BAEL-637 Pre conditions (#1076) * BAL-36 File size api in java and apache commons IO * BAEL-282 grep in java - fixes after code review * BAEL-519 Added support for disruptor library * BAEL-519 Added support for disruptor library * BAEL-519 Added support for disruptor library * BAEL-519 Added support for disruptor library * BAEL-519 Added support for disruptor library * BAEL-519 Added support for disruptor library * BAEL-519 Added support for disruptor * BAEL-519 Moved all supporting classes to main source * BAEL-519 Moved all supporting classes to main source * BAEL-519 Moved asserts and test classes in test folder. * BAEL-519 moved test related producer and consumer to src. * BAEL-586 Guide to Guava BiMap. * BAEL-587 formatted code. * BAEL-519 LMAX Disruptor * BAEL-587 resolved merge * BAEL-587 Resolved merge * BAEL-519 Removed disruptor link. * BAEL-519 Reverted Guava changes * RFQ-587 Added disruptor as a separate module. * BAEL-519 Disruptor changes. * BAEL-519 Removed disruptor from core-java module. * BAEL-637 Guide to PreConditions * BAEL-637 Used assertJ for exception assertion. * BAEL-637 updated tests to use assertJ. --- .../guava/GuavaPreConditionsTest.java | 127 +++++------------- 1 file changed, 30 insertions(+), 97 deletions(-) diff --git a/guava/src/test/java/org/baeldung/guava/GuavaPreConditionsTest.java b/guava/src/test/java/org/baeldung/guava/GuavaPreConditionsTest.java index b9c1ca3c9d..1611bec672 100644 --- a/guava/src/test/java/org/baeldung/guava/GuavaPreConditionsTest.java +++ b/guava/src/test/java/org/baeldung/guava/GuavaPreConditionsTest.java @@ -1,6 +1,6 @@ package org.baeldung.guava; -import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.util.Arrays; import org.junit.Test; import static com.google.common.base.Preconditions.*; @@ -10,149 +10,92 @@ public class GuavaPreConditionsTest { @Test public void whenCheckArgumentEvaluatesFalse_throwsException() { int age = -18; - try { - checkArgument(age > 0); - } catch (Exception exception) { - assertThat(exception).isInstanceOf(IllegalArgumentException.class); - assertThat(exception).hasNoCause(); - assertThat(exception).hasMessage(null); - } + + assertThatThrownBy(() -> checkArgument(age > 0)).isInstanceOf(IllegalArgumentException.class).hasMessage(null).hasNoCause(); } @Test public void givenErrorMessage_whenCheckArgumentEvaluatesFalse_throwsException() { final int age = -18; final String message = "Age can't be zero or less than zero"; - try { - checkArgument(age > 0, message); - } catch (Exception exception) { - assertThat(exception).isInstanceOf(IllegalArgumentException.class); - assertThat(exception).hasNoCause(); - assertThat(exception).hasMessage(message); - } + + assertThatThrownBy(() -> checkArgument(age > 0, message)).isInstanceOf(IllegalArgumentException.class).hasMessage(message).hasNoCause(); } @Test public void givenTemplatedErrorMessage_whenCheckArgumentEvaluatesFalse_throwsException() { final int age = -18; final String message = "Age can't be zero or less than zero, you supplied %s."; - try { - checkArgument(age > 0, message, age); - } catch (Exception exception) { - assertThat(exception).isInstanceOf(IllegalArgumentException.class); - assertThat(exception).hasNoCause(); - assertThat(exception).hasMessage(message, age); - } + + assertThatThrownBy(() -> checkArgument(age > 0, message, age)).isInstanceOf(IllegalArgumentException.class).hasMessage(message, age).hasNoCause(); } @Test public void givenArrayOfIntegers_whenCheckElementIndexEvaluatesFalse_throwsException() { final int[] numbers = { 1, 2, 3, 4, 5 }; - try { - checkElementIndex(6, numbers.length - 1); - } catch (Exception exception) { - assertThat(exception).isInstanceOf(IndexOutOfBoundsException.class); - assertThat(exception).hasNoCause(); - } + + assertThatThrownBy(() -> checkElementIndex(6, numbers.length - 1)).isInstanceOf(IndexOutOfBoundsException.class).hasNoCause(); } @Test public void givenArrayOfIntegersAndMessage_whenCheckElementIndexEvaluatesFalse_throwsException() { final int[] numbers = { 1, 2, 3, 4, 5 }; final String message = "Please check the bound of an array and retry"; - try { - checkElementIndex(6, numbers.length - 1, message); - } catch (Exception exception) { - assertThat(exception).isInstanceOf(IndexOutOfBoundsException.class); - assertThat(exception).hasNoCause(); - assertThat(exception.getMessage()).startsWith(message); - } + + assertThatThrownBy(() -> checkElementIndex(6, numbers.length - 1, message)).isInstanceOf(IndexOutOfBoundsException.class).hasMessageStartingWith(message).hasNoCause(); } @Test public void givenNullString_whenCheckNotNullCalled_throwsException() { final String nullObject = null; - try { - checkNotNull(nullObject); - } catch (Exception exception) { - assertThat(exception).isInstanceOf(NullPointerException.class); - assertThat(exception).hasNoCause(); - assertThat(exception).hasMessage(null); - } + + assertThatThrownBy(() -> checkNotNull(nullObject)).isInstanceOf(NullPointerException.class).hasMessage(null).hasNoCause(); } @Test public void givenNullString_whenCheckNotNullCalledWithMessage_throwsException() { final String nullObject = null; final String message = "Please check the Object supplied, its null!"; - try { - checkNotNull(nullObject, message); - } catch (Exception exception) { - assertThat(exception).isInstanceOf(NullPointerException.class); - assertThat(exception).hasNoCause(); - assertThat(exception).hasMessage(message); - } + + assertThatThrownBy(() -> checkNotNull(nullObject, message)).isInstanceOf(NullPointerException.class).hasMessage(message).hasNoCause(); } @Test public void givenNullString_whenCheckNotNullCalledWithTemplatedMessage_throwsException() { final String nullObject = null; final String message = "Please check the Object supplied, its %s!"; - try { - checkNotNull(nullObject, message, nullObject); - } catch (Exception exception) { - assertThat(exception).isInstanceOf(NullPointerException.class); - assertThat(exception).hasNoCause(); - assertThat(exception).hasMessage(message, nullObject); - } + + assertThatThrownBy(() -> checkNotNull(nullObject, message, nullObject)).isInstanceOf(NullPointerException.class).hasMessage(message, nullObject).hasNoCause(); } @Test public void givenArrayOfIntegers_whenCheckPositionIndexEvaluatesFalse_throwsException() { final int[] numbers = { 1, 2, 3, 4, 5 }; - try { - checkPositionIndex(6, numbers.length - 1); - } catch (Exception exception) { - assertThat(exception).isInstanceOf(IndexOutOfBoundsException.class); - assertThat(exception).hasNoCause(); - } + + assertThatThrownBy(() -> checkPositionIndex(6, numbers.length - 1)).isInstanceOf(IndexOutOfBoundsException.class).hasNoCause(); } @Test public void givenArrayOfIntegersAndMessage_whenCheckPositionIndexEvaluatesFalse_throwsException() { final int[] numbers = { 1, 2, 3, 4, 5 }; final String message = "Please check the bound of an array and retry"; - try { - checkPositionIndex(6, numbers.length - 1, message); - } catch (Exception exception) { - assertThat(exception).isInstanceOf(IndexOutOfBoundsException.class); - assertThat(exception).hasNoCause(); - assertThat(exception.getMessage()).startsWith(message); - } + + assertThatThrownBy(() -> checkPositionIndex(6, numbers.length - 1, message)).isInstanceOf(IndexOutOfBoundsException.class).hasMessageStartingWith(message).hasNoCause(); } @Test public void givenArrayOfIntegers_whenCheckPositionIndexesEvaluatesFalse_throwsException() { final int[] numbers = { 1, 2, 3, 4, 5 }; - try { - checkPositionIndexes(6, 0, numbers.length - 1); - } catch (Exception exception) { - assertThat(exception).isInstanceOf(IndexOutOfBoundsException.class); - assertThat(exception).hasNoCause(); - } + + assertThatThrownBy(() -> checkPositionIndexes(6, 0, numbers.length - 1)).isInstanceOf(IndexOutOfBoundsException.class).hasNoCause(); } @Test public void givenValidStates_whenCheckStateEvaluatesFalse_throwsException() { final int[] validStates = { -1, 0, 1 }; final int givenState = 10; - try { - checkState(Arrays.binarySearch(validStates, givenState) > 0); - } catch (Exception exception) { - assertThat(exception).isInstanceOf(IllegalStateException.class); - assertThat(exception).hasNoCause(); - assertThat(exception).hasMessage(null); - } + + assertThatThrownBy(() -> checkState(Arrays.binarySearch(validStates, givenState) > 0)).isInstanceOf(IllegalStateException.class).hasMessage(null).hasNoCause(); } @Test @@ -160,13 +103,8 @@ public class GuavaPreConditionsTest { final int[] validStates = { -1, 0, 1 }; final int givenState = 10; final String message = "You have entered an invalid state"; - try { - checkState(Arrays.binarySearch(validStates, givenState) < 0, message); - } catch (Exception exception) { - assertThat(exception).isInstanceOf(IllegalStateException.class); - assertThat(exception).hasNoCause(); - assertThat(exception).hasMessage(message); - } + + assertThatThrownBy(() -> checkState(Arrays.binarySearch(validStates, givenState) > 0, message)).isInstanceOf(IllegalStateException.class).hasMessageStartingWith(message).hasNoCause(); } @Test @@ -174,13 +112,8 @@ public class GuavaPreConditionsTest { final int[] validStates = { -1, 0, 1 }; final int givenState = 10; final String message = "State can't be %s, It can be one of %s."; - try { - checkState(Arrays.binarySearch(validStates, givenState) < 0, message, givenState, Arrays.toString(validStates)); - } catch (Exception exception) { - assertThat(exception).isInstanceOf(IllegalStateException.class); - assertThat(exception).hasNoCause(); - assertThat(exception).hasMessage(message, givenState, Arrays.toString(validStates)); - } - } + assertThatThrownBy(() -> checkState(Arrays.binarySearch(validStates, givenState) > 0, message, givenState, Arrays.toString(validStates))).isInstanceOf(IllegalStateException.class).hasMessage(message, givenState, Arrays.toString(validStates)) + .hasNoCause(); + } } \ No newline at end of file