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.
This commit is contained in:
parent
f970b1dbcf
commit
e3c1e6986b
|
@ -1,6 +1,6 @@
|
||||||
package org.baeldung.guava;
|
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 java.util.Arrays;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import static com.google.common.base.Preconditions.*;
|
import static com.google.common.base.Preconditions.*;
|
||||||
|
@ -10,149 +10,92 @@ public class GuavaPreConditionsTest {
|
||||||
@Test
|
@Test
|
||||||
public void whenCheckArgumentEvaluatesFalse_throwsException() {
|
public void whenCheckArgumentEvaluatesFalse_throwsException() {
|
||||||
int age = -18;
|
int age = -18;
|
||||||
try {
|
|
||||||
checkArgument(age > 0);
|
assertThatThrownBy(() -> checkArgument(age > 0)).isInstanceOf(IllegalArgumentException.class).hasMessage(null).hasNoCause();
|
||||||
} catch (Exception exception) {
|
|
||||||
assertThat(exception).isInstanceOf(IllegalArgumentException.class);
|
|
||||||
assertThat(exception).hasNoCause();
|
|
||||||
assertThat(exception).hasMessage(null);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenErrorMessage_whenCheckArgumentEvaluatesFalse_throwsException() {
|
public void givenErrorMessage_whenCheckArgumentEvaluatesFalse_throwsException() {
|
||||||
final int age = -18;
|
final int age = -18;
|
||||||
final String message = "Age can't be zero or less than zero";
|
final String message = "Age can't be zero or less than zero";
|
||||||
try {
|
|
||||||
checkArgument(age > 0, message);
|
assertThatThrownBy(() -> checkArgument(age > 0, message)).isInstanceOf(IllegalArgumentException.class).hasMessage(message).hasNoCause();
|
||||||
} catch (Exception exception) {
|
|
||||||
assertThat(exception).isInstanceOf(IllegalArgumentException.class);
|
|
||||||
assertThat(exception).hasNoCause();
|
|
||||||
assertThat(exception).hasMessage(message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenTemplatedErrorMessage_whenCheckArgumentEvaluatesFalse_throwsException() {
|
public void givenTemplatedErrorMessage_whenCheckArgumentEvaluatesFalse_throwsException() {
|
||||||
final int age = -18;
|
final int age = -18;
|
||||||
final String message = "Age can't be zero or less than zero, you supplied %s.";
|
final String message = "Age can't be zero or less than zero, you supplied %s.";
|
||||||
try {
|
|
||||||
checkArgument(age > 0, message, age);
|
assertThatThrownBy(() -> checkArgument(age > 0, message, age)).isInstanceOf(IllegalArgumentException.class).hasMessage(message, age).hasNoCause();
|
||||||
} catch (Exception exception) {
|
|
||||||
assertThat(exception).isInstanceOf(IllegalArgumentException.class);
|
|
||||||
assertThat(exception).hasNoCause();
|
|
||||||
assertThat(exception).hasMessage(message, age);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenArrayOfIntegers_whenCheckElementIndexEvaluatesFalse_throwsException() {
|
public void givenArrayOfIntegers_whenCheckElementIndexEvaluatesFalse_throwsException() {
|
||||||
final int[] numbers = { 1, 2, 3, 4, 5 };
|
final int[] numbers = { 1, 2, 3, 4, 5 };
|
||||||
try {
|
|
||||||
checkElementIndex(6, numbers.length - 1);
|
assertThatThrownBy(() -> checkElementIndex(6, numbers.length - 1)).isInstanceOf(IndexOutOfBoundsException.class).hasNoCause();
|
||||||
} catch (Exception exception) {
|
|
||||||
assertThat(exception).isInstanceOf(IndexOutOfBoundsException.class);
|
|
||||||
assertThat(exception).hasNoCause();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenArrayOfIntegersAndMessage_whenCheckElementIndexEvaluatesFalse_throwsException() {
|
public void givenArrayOfIntegersAndMessage_whenCheckElementIndexEvaluatesFalse_throwsException() {
|
||||||
final int[] numbers = { 1, 2, 3, 4, 5 };
|
final int[] numbers = { 1, 2, 3, 4, 5 };
|
||||||
final String message = "Please check the bound of an array and retry";
|
final String message = "Please check the bound of an array and retry";
|
||||||
try {
|
|
||||||
checkElementIndex(6, numbers.length - 1, message);
|
assertThatThrownBy(() -> checkElementIndex(6, numbers.length - 1, message)).isInstanceOf(IndexOutOfBoundsException.class).hasMessageStartingWith(message).hasNoCause();
|
||||||
} catch (Exception exception) {
|
|
||||||
assertThat(exception).isInstanceOf(IndexOutOfBoundsException.class);
|
|
||||||
assertThat(exception).hasNoCause();
|
|
||||||
assertThat(exception.getMessage()).startsWith(message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenNullString_whenCheckNotNullCalled_throwsException() {
|
public void givenNullString_whenCheckNotNullCalled_throwsException() {
|
||||||
final String nullObject = null;
|
final String nullObject = null;
|
||||||
try {
|
|
||||||
checkNotNull(nullObject);
|
assertThatThrownBy(() -> checkNotNull(nullObject)).isInstanceOf(NullPointerException.class).hasMessage(null).hasNoCause();
|
||||||
} catch (Exception exception) {
|
|
||||||
assertThat(exception).isInstanceOf(NullPointerException.class);
|
|
||||||
assertThat(exception).hasNoCause();
|
|
||||||
assertThat(exception).hasMessage(null);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenNullString_whenCheckNotNullCalledWithMessage_throwsException() {
|
public void givenNullString_whenCheckNotNullCalledWithMessage_throwsException() {
|
||||||
final String nullObject = null;
|
final String nullObject = null;
|
||||||
final String message = "Please check the Object supplied, its null!";
|
final String message = "Please check the Object supplied, its null!";
|
||||||
try {
|
|
||||||
checkNotNull(nullObject, message);
|
assertThatThrownBy(() -> checkNotNull(nullObject, message)).isInstanceOf(NullPointerException.class).hasMessage(message).hasNoCause();
|
||||||
} catch (Exception exception) {
|
|
||||||
assertThat(exception).isInstanceOf(NullPointerException.class);
|
|
||||||
assertThat(exception).hasNoCause();
|
|
||||||
assertThat(exception).hasMessage(message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenNullString_whenCheckNotNullCalledWithTemplatedMessage_throwsException() {
|
public void givenNullString_whenCheckNotNullCalledWithTemplatedMessage_throwsException() {
|
||||||
final String nullObject = null;
|
final String nullObject = null;
|
||||||
final String message = "Please check the Object supplied, its %s!";
|
final String message = "Please check the Object supplied, its %s!";
|
||||||
try {
|
|
||||||
checkNotNull(nullObject, message, nullObject);
|
assertThatThrownBy(() -> checkNotNull(nullObject, message, nullObject)).isInstanceOf(NullPointerException.class).hasMessage(message, nullObject).hasNoCause();
|
||||||
} catch (Exception exception) {
|
|
||||||
assertThat(exception).isInstanceOf(NullPointerException.class);
|
|
||||||
assertThat(exception).hasNoCause();
|
|
||||||
assertThat(exception).hasMessage(message, nullObject);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenArrayOfIntegers_whenCheckPositionIndexEvaluatesFalse_throwsException() {
|
public void givenArrayOfIntegers_whenCheckPositionIndexEvaluatesFalse_throwsException() {
|
||||||
final int[] numbers = { 1, 2, 3, 4, 5 };
|
final int[] numbers = { 1, 2, 3, 4, 5 };
|
||||||
try {
|
|
||||||
checkPositionIndex(6, numbers.length - 1);
|
assertThatThrownBy(() -> checkPositionIndex(6, numbers.length - 1)).isInstanceOf(IndexOutOfBoundsException.class).hasNoCause();
|
||||||
} catch (Exception exception) {
|
|
||||||
assertThat(exception).isInstanceOf(IndexOutOfBoundsException.class);
|
|
||||||
assertThat(exception).hasNoCause();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenArrayOfIntegersAndMessage_whenCheckPositionIndexEvaluatesFalse_throwsException() {
|
public void givenArrayOfIntegersAndMessage_whenCheckPositionIndexEvaluatesFalse_throwsException() {
|
||||||
final int[] numbers = { 1, 2, 3, 4, 5 };
|
final int[] numbers = { 1, 2, 3, 4, 5 };
|
||||||
final String message = "Please check the bound of an array and retry";
|
final String message = "Please check the bound of an array and retry";
|
||||||
try {
|
|
||||||
checkPositionIndex(6, numbers.length - 1, message);
|
assertThatThrownBy(() -> checkPositionIndex(6, numbers.length - 1, message)).isInstanceOf(IndexOutOfBoundsException.class).hasMessageStartingWith(message).hasNoCause();
|
||||||
} catch (Exception exception) {
|
|
||||||
assertThat(exception).isInstanceOf(IndexOutOfBoundsException.class);
|
|
||||||
assertThat(exception).hasNoCause();
|
|
||||||
assertThat(exception.getMessage()).startsWith(message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenArrayOfIntegers_whenCheckPositionIndexesEvaluatesFalse_throwsException() {
|
public void givenArrayOfIntegers_whenCheckPositionIndexesEvaluatesFalse_throwsException() {
|
||||||
final int[] numbers = { 1, 2, 3, 4, 5 };
|
final int[] numbers = { 1, 2, 3, 4, 5 };
|
||||||
try {
|
|
||||||
checkPositionIndexes(6, 0, numbers.length - 1);
|
assertThatThrownBy(() -> checkPositionIndexes(6, 0, numbers.length - 1)).isInstanceOf(IndexOutOfBoundsException.class).hasNoCause();
|
||||||
} catch (Exception exception) {
|
|
||||||
assertThat(exception).isInstanceOf(IndexOutOfBoundsException.class);
|
|
||||||
assertThat(exception).hasNoCause();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenValidStates_whenCheckStateEvaluatesFalse_throwsException() {
|
public void givenValidStates_whenCheckStateEvaluatesFalse_throwsException() {
|
||||||
final int[] validStates = { -1, 0, 1 };
|
final int[] validStates = { -1, 0, 1 };
|
||||||
final int givenState = 10;
|
final int givenState = 10;
|
||||||
try {
|
|
||||||
checkState(Arrays.binarySearch(validStates, givenState) > 0);
|
assertThatThrownBy(() -> checkState(Arrays.binarySearch(validStates, givenState) > 0)).isInstanceOf(IllegalStateException.class).hasMessage(null).hasNoCause();
|
||||||
} catch (Exception exception) {
|
|
||||||
assertThat(exception).isInstanceOf(IllegalStateException.class);
|
|
||||||
assertThat(exception).hasNoCause();
|
|
||||||
assertThat(exception).hasMessage(null);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -160,13 +103,8 @@ public class GuavaPreConditionsTest {
|
||||||
final int[] validStates = { -1, 0, 1 };
|
final int[] validStates = { -1, 0, 1 };
|
||||||
final int givenState = 10;
|
final int givenState = 10;
|
||||||
final String message = "You have entered an invalid state";
|
final String message = "You have entered an invalid state";
|
||||||
try {
|
|
||||||
checkState(Arrays.binarySearch(validStates, givenState) < 0, message);
|
assertThatThrownBy(() -> checkState(Arrays.binarySearch(validStates, givenState) > 0, message)).isInstanceOf(IllegalStateException.class).hasMessageStartingWith(message).hasNoCause();
|
||||||
} catch (Exception exception) {
|
|
||||||
assertThat(exception).isInstanceOf(IllegalStateException.class);
|
|
||||||
assertThat(exception).hasNoCause();
|
|
||||||
assertThat(exception).hasMessage(message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -174,13 +112,8 @@ public class GuavaPreConditionsTest {
|
||||||
final int[] validStates = { -1, 0, 1 };
|
final int[] validStates = { -1, 0, 1 };
|
||||||
final int givenState = 10;
|
final int givenState = 10;
|
||||||
final String message = "State can't be %s, It can be one of %s.";
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue