From 1c4fc198683b3be970f447856b20a3b8af7a0de4 Mon Sep 17 00:00:00 2001 From: Muhammed Almas Date: Mon, 30 Jan 2017 02:22:35 +0530 Subject: [PATCH] BAEL-637 Pre conditions (#1067) * 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. --- guava/pom.xml | 7 ++ .../guava/GuavaPreConditionsTest.java | 113 ++++++++++++------ 2 files changed, 86 insertions(+), 34 deletions(-) diff --git a/guava/pom.xml b/guava/pom.xml index 0a50fad2c3..f65a9a2081 100644 --- a/guava/pom.xml +++ b/guava/pom.xml @@ -57,6 +57,12 @@ test + + org.assertj + assertj-core + ${assertj.version} + test + @@ -100,6 +106,7 @@ 1.3 4.12 1.10.19 + 3.6.1 3.6.0 diff --git a/guava/src/test/java/org/baeldung/guava/GuavaPreConditionsTest.java b/guava/src/test/java/org/baeldung/guava/GuavaPreConditionsTest.java index 9e8080a3b3..b9c1ca3c9d 100644 --- a/guava/src/test/java/org/baeldung/guava/GuavaPreConditionsTest.java +++ b/guava/src/test/java/org/baeldung/guava/GuavaPreConditionsTest.java @@ -1,17 +1,22 @@ package org.baeldung.guava; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; import java.util.Arrays; import org.junit.Test; import static com.google.common.base.Preconditions.*; public class GuavaPreConditionsTest { - @Test(expected = IllegalArgumentException.class) + @Test public void whenCheckArgumentEvaluatesFalse_throwsException() { int age = -18; - checkArgument(age > 0); + try { + checkArgument(age > 0); + } catch (Exception exception) { + assertThat(exception).isInstanceOf(IllegalArgumentException.class); + assertThat(exception).hasNoCause(); + assertThat(exception).hasMessage(null); + } } @Test @@ -20,8 +25,10 @@ public class GuavaPreConditionsTest { final String message = "Age can't be zero or less than zero"; try { checkArgument(age > 0, message); - } catch (IllegalArgumentException illegalArgumentException) { - assertEquals(message, illegalArgumentException.getMessage()); + } catch (Exception exception) { + assertThat(exception).isInstanceOf(IllegalArgumentException.class); + assertThat(exception).hasNoCause(); + assertThat(exception).hasMessage(message); } } @@ -31,16 +38,22 @@ public class GuavaPreConditionsTest { final String message = "Age can't be zero or less than zero, you supplied %s."; try { checkArgument(age > 0, message, age); - } catch (IllegalArgumentException illegalArgumentException) { - final String formattedMessage = String.format(message, age); - assertEquals(formattedMessage, illegalArgumentException.getMessage()); + } catch (Exception exception) { + assertThat(exception).isInstanceOf(IllegalArgumentException.class); + assertThat(exception).hasNoCause(); + assertThat(exception).hasMessage(message, age); } } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void givenArrayOfIntegers_whenCheckElementIndexEvaluatesFalse_throwsException() { final int[] numbers = { 1, 2, 3, 4, 5 }; - checkElementIndex(6, numbers.length - 1); + try { + checkElementIndex(6, numbers.length - 1); + } catch (Exception exception) { + assertThat(exception).isInstanceOf(IndexOutOfBoundsException.class); + assertThat(exception).hasNoCause(); + } } @Test @@ -49,15 +62,23 @@ public class GuavaPreConditionsTest { final String message = "Please check the bound of an array and retry"; try { checkElementIndex(6, numbers.length - 1, message); - } catch (IndexOutOfBoundsException indexOutOfBoundsException) { - assertTrue(indexOutOfBoundsException.getMessage().startsWith(message)); + } catch (Exception exception) { + assertThat(exception).isInstanceOf(IndexOutOfBoundsException.class); + assertThat(exception).hasNoCause(); + assertThat(exception.getMessage()).startsWith(message); } } - @Test(expected = NullPointerException.class) + @Test public void givenNullString_whenCheckNotNullCalled_throwsException() { final String nullObject = null; - checkNotNull(nullObject); + try { + checkNotNull(nullObject); + } catch (Exception exception) { + assertThat(exception).isInstanceOf(NullPointerException.class); + assertThat(exception).hasNoCause(); + assertThat(exception).hasMessage(null); + } } @Test @@ -66,8 +87,10 @@ public class GuavaPreConditionsTest { final String message = "Please check the Object supplied, its null!"; try { checkNotNull(nullObject, message); - } catch (NullPointerException nullPointerException) { - assertEquals(message, nullPointerException.getMessage()); + } catch (Exception exception) { + assertThat(exception).isInstanceOf(NullPointerException.class); + assertThat(exception).hasNoCause(); + assertThat(exception).hasMessage(message); } } @@ -77,16 +100,22 @@ public class GuavaPreConditionsTest { final String message = "Please check the Object supplied, its %s!"; try { checkNotNull(nullObject, message, nullObject); - } catch (NullPointerException nullPointerException) { - final String formattedMessage = String.format(message, nullObject); - assertEquals(formattedMessage, nullPointerException.getMessage()); + } catch (Exception exception) { + assertThat(exception).isInstanceOf(NullPointerException.class); + assertThat(exception).hasNoCause(); + assertThat(exception).hasMessage(message, nullObject); } } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void givenArrayOfIntegers_whenCheckPositionIndexEvaluatesFalse_throwsException() { final int[] numbers = { 1, 2, 3, 4, 5 }; - checkPositionIndex(6, numbers.length - 1); + try { + checkPositionIndex(6, numbers.length - 1); + } catch (Exception exception) { + assertThat(exception).isInstanceOf(IndexOutOfBoundsException.class); + assertThat(exception).hasNoCause(); + } } @Test @@ -95,22 +124,35 @@ public class GuavaPreConditionsTest { final String message = "Please check the bound of an array and retry"; try { checkPositionIndex(6, numbers.length - 1, message); - } catch (IndexOutOfBoundsException indexOutOfBoundsException) { - assertTrue(indexOutOfBoundsException.getMessage().startsWith(message)); + } catch (Exception exception) { + assertThat(exception).isInstanceOf(IndexOutOfBoundsException.class); + assertThat(exception).hasNoCause(); + assertThat(exception.getMessage()).startsWith(message); } } - @Test(expected = IndexOutOfBoundsException.class) + @Test public void givenArrayOfIntegers_whenCheckPositionIndexesEvaluatesFalse_throwsException() { final int[] numbers = { 1, 2, 3, 4, 5 }; - checkPositionIndexes(6, 0, numbers.length - 1); + try { + checkPositionIndexes(6, 0, numbers.length - 1); + } catch (Exception exception) { + assertThat(exception).isInstanceOf(IndexOutOfBoundsException.class); + assertThat(exception).hasNoCause(); + } } - @Test(expected = IllegalStateException.class) + @Test public void givenValidStates_whenCheckStateEvaluatesFalse_throwsException() { final int[] validStates = { -1, 0, 1 }; final int givenState = 10; - checkState(Arrays.binarySearch(validStates, givenState) > 0); + try { + checkState(Arrays.binarySearch(validStates, givenState) > 0); + } catch (Exception exception) { + assertThat(exception).isInstanceOf(IllegalStateException.class); + assertThat(exception).hasNoCause(); + assertThat(exception).hasMessage(null); + } } @Test @@ -120,8 +162,10 @@ public class GuavaPreConditionsTest { final String message = "You have entered an invalid state"; try { checkState(Arrays.binarySearch(validStates, givenState) < 0, message); - } catch (IllegalStateException IllegalStateException) { - assertEquals(message, IllegalStateException.getMessage()); + } catch (Exception exception) { + assertThat(exception).isInstanceOf(IllegalStateException.class); + assertThat(exception).hasNoCause(); + assertThat(exception).hasMessage(message); } } @@ -132,10 +176,11 @@ public class GuavaPreConditionsTest { 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 (IllegalStateException IllegalStateException) { - final String formattedMessage = String.format(message, givenState, Arrays.toString(validStates)); - assertEquals(formattedMessage, IllegalStateException.getMessage()); + } catch (Exception exception) { + assertThat(exception).isInstanceOf(IllegalStateException.class); + assertThat(exception).hasNoCause(); + assertThat(exception).hasMessage(message, givenState, Arrays.toString(validStates)); } } -} +} \ No newline at end of file