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>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>${assertj.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -100,6 +106,7 @@
|
||||||
<org.hamcrest.version>1.3</org.hamcrest.version>
|
<org.hamcrest.version>1.3</org.hamcrest.version>
|
||||||
<junit.version>4.12</junit.version>
|
<junit.version>4.12</junit.version>
|
||||||
<mockito.version>1.10.19</mockito.version>
|
<mockito.version>1.10.19</mockito.version>
|
||||||
|
<assertj.version>3.6.1</assertj.version>
|
||||||
|
|
||||||
<!-- maven plugins -->
|
<!-- maven plugins -->
|
||||||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||||
|
|
|
@ -1,17 +1,22 @@
|
||||||
package org.baeldung.guava;
|
package org.baeldung.guava;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
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.*;
|
||||||
|
|
||||||
public class GuavaPreConditionsTest {
|
public class GuavaPreConditionsTest {
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test
|
||||||
public void whenCheckArgumentEvaluatesFalse_throwsException() {
|
public void whenCheckArgumentEvaluatesFalse_throwsException() {
|
||||||
int age = -18;
|
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
|
@Test
|
||||||
|
@ -20,8 +25,10 @@ public class GuavaPreConditionsTest {
|
||||||
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 {
|
try {
|
||||||
checkArgument(age > 0, message);
|
checkArgument(age > 0, message);
|
||||||
} catch (IllegalArgumentException illegalArgumentException) {
|
} catch (Exception exception) {
|
||||||
assertEquals(message, illegalArgumentException.getMessage());
|
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.";
|
final String message = "Age can't be zero or less than zero, you supplied %s.";
|
||||||
try {
|
try {
|
||||||
checkArgument(age > 0, message, age);
|
checkArgument(age > 0, message, age);
|
||||||
} catch (IllegalArgumentException illegalArgumentException) {
|
} catch (Exception exception) {
|
||||||
final String formattedMessage = String.format(message, age);
|
assertThat(exception).isInstanceOf(IllegalArgumentException.class);
|
||||||
assertEquals(formattedMessage, illegalArgumentException.getMessage());
|
assertThat(exception).hasNoCause();
|
||||||
|
assertThat(exception).hasMessage(message, age);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IndexOutOfBoundsException.class)
|
@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 };
|
||||||
checkElementIndex(6, numbers.length - 1);
|
try {
|
||||||
|
checkElementIndex(6, numbers.length - 1);
|
||||||
|
} catch (Exception exception) {
|
||||||
|
assertThat(exception).isInstanceOf(IndexOutOfBoundsException.class);
|
||||||
|
assertThat(exception).hasNoCause();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -49,15 +62,23 @@ public class GuavaPreConditionsTest {
|
||||||
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 {
|
try {
|
||||||
checkElementIndex(6, numbers.length - 1, message);
|
checkElementIndex(6, numbers.length - 1, message);
|
||||||
} catch (IndexOutOfBoundsException indexOutOfBoundsException) {
|
} catch (Exception exception) {
|
||||||
assertTrue(indexOutOfBoundsException.getMessage().startsWith(message));
|
assertThat(exception).isInstanceOf(IndexOutOfBoundsException.class);
|
||||||
|
assertThat(exception).hasNoCause();
|
||||||
|
assertThat(exception.getMessage()).startsWith(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = NullPointerException.class)
|
@Test
|
||||||
public void givenNullString_whenCheckNotNullCalled_throwsException() {
|
public void givenNullString_whenCheckNotNullCalled_throwsException() {
|
||||||
final String nullObject = null;
|
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
|
@Test
|
||||||
|
@ -66,8 +87,10 @@ public class GuavaPreConditionsTest {
|
||||||
final String message = "Please check the Object supplied, its null!";
|
final String message = "Please check the Object supplied, its null!";
|
||||||
try {
|
try {
|
||||||
checkNotNull(nullObject, message);
|
checkNotNull(nullObject, message);
|
||||||
} catch (NullPointerException nullPointerException) {
|
} catch (Exception exception) {
|
||||||
assertEquals(message, nullPointerException.getMessage());
|
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!";
|
final String message = "Please check the Object supplied, its %s!";
|
||||||
try {
|
try {
|
||||||
checkNotNull(nullObject, message, nullObject);
|
checkNotNull(nullObject, message, nullObject);
|
||||||
} catch (NullPointerException nullPointerException) {
|
} catch (Exception exception) {
|
||||||
final String formattedMessage = String.format(message, nullObject);
|
assertThat(exception).isInstanceOf(NullPointerException.class);
|
||||||
assertEquals(formattedMessage, nullPointerException.getMessage());
|
assertThat(exception).hasNoCause();
|
||||||
|
assertThat(exception).hasMessage(message, nullObject);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IndexOutOfBoundsException.class)
|
@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 };
|
||||||
checkPositionIndex(6, numbers.length - 1);
|
try {
|
||||||
|
checkPositionIndex(6, numbers.length - 1);
|
||||||
|
} catch (Exception exception) {
|
||||||
|
assertThat(exception).isInstanceOf(IndexOutOfBoundsException.class);
|
||||||
|
assertThat(exception).hasNoCause();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -95,22 +124,35 @@ public class GuavaPreConditionsTest {
|
||||||
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 {
|
try {
|
||||||
checkPositionIndex(6, numbers.length - 1, message);
|
checkPositionIndex(6, numbers.length - 1, message);
|
||||||
} catch (IndexOutOfBoundsException indexOutOfBoundsException) {
|
} catch (Exception exception) {
|
||||||
assertTrue(indexOutOfBoundsException.getMessage().startsWith(message));
|
assertThat(exception).isInstanceOf(IndexOutOfBoundsException.class);
|
||||||
|
assertThat(exception).hasNoCause();
|
||||||
|
assertThat(exception.getMessage()).startsWith(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IndexOutOfBoundsException.class)
|
@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 };
|
||||||
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() {
|
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;
|
||||||
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
|
@Test
|
||||||
|
@ -120,8 +162,10 @@ public class GuavaPreConditionsTest {
|
||||||
final String message = "You have entered an invalid state";
|
final String message = "You have entered an invalid state";
|
||||||
try {
|
try {
|
||||||
checkState(Arrays.binarySearch(validStates, givenState) < 0, message);
|
checkState(Arrays.binarySearch(validStates, givenState) < 0, message);
|
||||||
} catch (IllegalStateException IllegalStateException) {
|
} catch (Exception exception) {
|
||||||
assertEquals(message, IllegalStateException.getMessage());
|
assertThat(exception).isInstanceOf(IllegalStateException.class);
|
||||||
|
assertThat(exception).hasNoCause();
|
||||||
|
assertThat(exception).hasMessage(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,9 +176,10 @@ public class GuavaPreConditionsTest {
|
||||||
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 {
|
try {
|
||||||
checkState(Arrays.binarySearch(validStates, givenState) < 0, message, givenState, Arrays.toString(validStates));
|
checkState(Arrays.binarySearch(validStates, givenState) < 0, message, givenState, Arrays.toString(validStates));
|
||||||
} catch (IllegalStateException IllegalStateException) {
|
} catch (Exception exception) {
|
||||||
final String formattedMessage = String.format(message, givenState, Arrays.toString(validStates));
|
assertThat(exception).isInstanceOf(IllegalStateException.class);
|
||||||
assertEquals(formattedMessage, IllegalStateException.getMessage());
|
assertThat(exception).hasNoCause();
|
||||||
|
assertThat(exception).hasMessage(message, givenState, Arrays.toString(validStates));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue