Skip to content

Commit 1565938

Browse files
eralmas7zhendrikse
authored andcommitted
Pre conditions (eugenp#1116)
* 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. * BAEL-637 Removed redundant tests.
1 parent 4cbb0ed commit 1565938

1 file changed

Lines changed: 15 additions & 68 deletions

File tree

‎guava/src/test/java/org/baeldung/guava/GuavaPreConditionsTest.java‎

Lines changed: 15 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3,132 +3,84 @@
33
import static org.assertj.core.api.Assertions.assertThatThrownBy;
44
import java.util.Arrays;
55
import org.junit.Test;
6-
import static com.google.common.base.Preconditions.*;
6+
import com.google.common.base.*;
77

88
public class GuavaPreConditionsTest {
99

1010
@Test
1111
public void whenCheckArgumentEvaluatesFalse_throwsException() {
1212
int age = -18;
1313

14-
assertThatThrownBy(() -> checkArgument(age > 0))
15-
.isInstanceOf(IllegalArgumentException.class)
16-
.hasMessage(null)
17-
.hasNoCause();
14+
assertThatThrownBy(() -> Preconditions.checkArgument(age > 0)).isInstanceOf(IllegalArgumentException.class).hasMessage(null).hasNoCause();
1815
}
1916

2017
@Test
2118
public void givenErrorMessage_whenCheckArgumentEvaluatesFalse_throwsException() {
2219
final int age = -18;
2320
final String message = "Age can't be zero or less than zero";
2421

25-
assertThatThrownBy(() -> checkArgument(age > 0, message))
26-
.isInstanceOf(IllegalArgumentException.class)
27-
.hasMessage(message)
28-
.hasNoCause();
22+
assertThatThrownBy(() -> Preconditions.checkArgument(age > 0, message)).isInstanceOf(IllegalArgumentException.class).hasMessage(message).hasNoCause();
2923
}
3024

3125
@Test
3226
public void givenTemplatedErrorMessage_whenCheckArgumentEvaluatesFalse_throwsException() {
3327
final int age = -18;
3428
final String message = "Age can't be zero or less than zero, you supplied %s.";
3529

36-
assertThatThrownBy(() -> checkArgument(age > 0, message, age))
37-
.isInstanceOf(IllegalArgumentException.class)
38-
.hasMessage(message, age)
39-
.hasNoCause();
30+
assertThatThrownBy(() -> Preconditions.checkArgument(age > 0, message, age)).isInstanceOf(IllegalArgumentException.class).hasMessage(message, age).hasNoCause();
4031
}
4132

4233
@Test
4334
public void givenArrayOfIntegers_whenCheckElementIndexEvaluatesFalse_throwsException() {
4435
final int[] numbers = { 1, 2, 3, 4, 5 };
4536

46-
assertThatThrownBy(() -> checkElementIndex(6, numbers.length - 1))
47-
.isInstanceOf(IndexOutOfBoundsException.class)
48-
.hasNoCause();
37+
assertThatThrownBy(() -> Preconditions.checkElementIndex(6, numbers.length - 1)).isInstanceOf(IndexOutOfBoundsException.class).hasNoCause();
4938
}
5039

5140
@Test
5241
public void givenArrayOfIntegersAndMessage_whenCheckElementIndexEvaluatesFalse_throwsException() {
5342
final int[] numbers = { 1, 2, 3, 4, 5 };
5443
final String message = "Please check the bound of an array and retry";
5544

56-
assertThatThrownBy(() -> checkElementIndex(6, numbers.length - 1, message))
57-
.isInstanceOf(IndexOutOfBoundsException.class)
58-
.hasMessageStartingWith(message)
59-
.hasNoCause();
60-
}
61-
62-
@Test
63-
public void givenNullString_whenCheckNotNullCalled_throwsException() {
64-
final String nullObject = null;
65-
66-
assertThatThrownBy(() -> checkNotNull(nullObject))
67-
.isInstanceOf(NullPointerException.class)
68-
.hasMessage(null)
69-
.hasNoCause();
45+
assertThatThrownBy(() -> Preconditions.checkElementIndex(6, numbers.length - 1, message)).isInstanceOf(IndexOutOfBoundsException.class).hasMessageStartingWith(message).hasNoCause();
7046
}
7147

7248
@Test
7349
public void givenNullString_whenCheckNotNullCalledWithMessage_throwsException() {
7450
final String nullObject = null;
7551
final String message = "Please check the Object supplied, its null!";
7652

77-
assertThatThrownBy(() -> checkNotNull(nullObject, message))
78-
.isInstanceOf(NullPointerException.class)
79-
.hasMessage(message)
80-
.hasNoCause();
53+
assertThatThrownBy(() -> Preconditions.checkNotNull(nullObject, message)).isInstanceOf(NullPointerException.class).hasMessage(message).hasNoCause();
8154
}
8255

8356
@Test
8457
public void givenNullString_whenCheckNotNullCalledWithTemplatedMessage_throwsException() {
8558
final String nullObject = null;
8659
final String message = "Please check the Object supplied, its %s!";
8760

88-
assertThatThrownBy(() -> checkNotNull(nullObject, message, nullObject))
89-
.isInstanceOf(NullPointerException.class)
90-
.hasMessage(message, nullObject)
91-
.hasNoCause();
61+
assertThatThrownBy(() -> Preconditions.checkNotNull(nullObject, message, nullObject)).isInstanceOf(NullPointerException.class).hasMessage(message, nullObject).hasNoCause();
9262
}
9363

9464
@Test
9565
public void givenArrayOfIntegers_whenCheckPositionIndexEvaluatesFalse_throwsException() {
9666
final int[] numbers = { 1, 2, 3, 4, 5 };
9767

98-
assertThatThrownBy(() -> checkPositionIndex(6, numbers.length - 1))
99-
.isInstanceOf(IndexOutOfBoundsException.class)
100-
.hasNoCause();
68+
assertThatThrownBy(() -> Preconditions.checkPositionIndex(6, numbers.length - 1)).isInstanceOf(IndexOutOfBoundsException.class).hasNoCause();
10169
}
10270

10371
@Test
10472
public void givenArrayOfIntegersAndMessage_whenCheckPositionIndexEvaluatesFalse_throwsException() {
10573
final int[] numbers = { 1, 2, 3, 4, 5 };
10674
final String message = "Please check the bound of an array and retry";
10775

108-
assertThatThrownBy(() -> checkPositionIndex(6, numbers.length - 1, message))
109-
.isInstanceOf(IndexOutOfBoundsException.class)
110-
.hasMessageStartingWith(message)
111-
.hasNoCause();
76+
assertThatThrownBy(() -> Preconditions.checkPositionIndex(6, numbers.length - 1, message)).isInstanceOf(IndexOutOfBoundsException.class).hasMessageStartingWith(message).hasNoCause();
11277
}
11378

11479
@Test
11580
public void givenArrayOfIntegers_whenCheckPositionIndexesEvaluatesFalse_throwsException() {
11681
final int[] numbers = { 1, 2, 3, 4, 5 };
11782

118-
assertThatThrownBy(() -> checkPositionIndexes(6, 0, numbers.length - 1))
119-
.isInstanceOf(IndexOutOfBoundsException.class)
120-
.hasNoCause();
121-
}
122-
123-
@Test
124-
public void givenValidStates_whenCheckStateEvaluatesFalse_throwsException() {
125-
final int[] validStates = { -1, 0, 1 };
126-
final int givenState = 10;
127-
128-
assertThatThrownBy(() -> checkState(Arrays.binarySearch(validStates, givenState) > 0))
129-
.isInstanceOf(IllegalStateException.class)
130-
.hasMessage(null)
131-
.hasNoCause();
83+
assertThatThrownBy(() -> Preconditions.checkPositionIndexes(6, 0, numbers.length - 1)).isInstanceOf(IndexOutOfBoundsException.class).hasNoCause();
13284
}
13385

13486
@Test
@@ -137,10 +89,7 @@ public void givenValidStatesAndMessage_whenCheckStateEvaluatesFalse_throwsExcept
13789
final int givenState = 10;
13890
final String message = "You have entered an invalid state";
13991

140-
assertThatThrownBy(() -> checkState(Arrays.binarySearch(validStates, givenState) > 0, message))
141-
.isInstanceOf(IllegalStateException.class)
142-
.hasMessageStartingWith(message)
143-
.hasNoCause();
92+
assertThatThrownBy(() -> Preconditions.checkState(Arrays.binarySearch(validStates, givenState) > 0, message)).isInstanceOf(IllegalStateException.class).hasMessageStartingWith(message).hasNoCause();
14493
}
14594

14695
@Test
@@ -149,9 +98,7 @@ public void givenValidStatesAndTemplatedMessage_whenCheckStateEvaluatesFalse_thr
14998
final int givenState = 10;
15099
final String message = "State can't be %s, It can be one of %s.";
151100

152-
assertThatThrownBy(() -> checkState(Arrays.binarySearch(validStates, givenState) > 0, message, givenState, Arrays.toString(validStates)))
153-
.isInstanceOf(IllegalStateException.class)
154-
.hasMessage(message, givenState, Arrays.toString(validStates))
155-
.hasNoCause();
101+
assertThatThrownBy(() -> Preconditions.checkState(Arrays.binarySearch(validStates, givenState) > 0, message, givenState, Arrays.toString(validStates))).isInstanceOf(IllegalStateException.class)
102+
.hasMessage(message, givenState, Arrays.toString(validStates)).hasNoCause();
156103
}
157-
}
104+
}

0 commit comments

Comments
 (0)