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.
This commit is contained in:
parent
05fa8fc4dd
commit
1c4fc19868
|
@ -57,6 +57,12 @@
|
|||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -100,6 +106,7 @@
|
|||
<org.hamcrest.version>1.3</org.hamcrest.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
<mockito.version>1.10.19</mockito.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
|
||||
<!-- maven plugins -->
|
||||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue