Skip to content

Commit 1c4fc19

Browse files
eralmas7zhendrikse
authored andcommitted
BAEL-637 Pre conditions (eugenp#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.
1 parent 05fa8fc commit 1c4fc19

2 files changed

Lines changed: 86 additions & 34 deletions

File tree

‎guava/pom.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@
5757
<scope>test</scope>
5858
</dependency>
5959

60+
<dependency>
61+
<groupId>org.assertj</groupId>
62+
<artifactId>assertj-core</artifactId>
63+
<version>${assertj.version}</version>
64+
<scope>test</scope>
65+
</dependency>
6066
</dependencies>
6167

6268
<build>
@@ -100,6 +106,7 @@
100106
<org.hamcrest.version>1.3</org.hamcrest.version>
101107
<junit.version>4.12</junit.version>
102108
<mockito.version>1.10.19</mockito.version>
109+
<assertj.version>3.6.1</assertj.version>
103110

104111
<!-- maven plugins -->
105112
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
Lines changed: 79 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
package org.baeldung.guava;
22

3-
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.assertTrue;
3+
import static org.assertj.core.api.Assertions.assertThat;
54
import java.util.Arrays;
65
import org.junit.Test;
76
import static com.google.common.base.Preconditions.*;
87

98
public class GuavaPreConditionsTest {
109

11-
@Test(expected = IllegalArgumentException.class)
10+
@Test
1211
public void whenCheckArgumentEvaluatesFalse_throwsException() {
1312
int age = -18;
14-
checkArgument(age > 0);
13+
try {
14+
checkArgument(age > 0);
15+
} catch (Exception exception) {
16+
assertThat(exception).isInstanceOf(IllegalArgumentException.class);
17+
assertThat(exception).hasNoCause();
18+
assertThat(exception).hasMessage(null);
19+
}
1520
}
1621

1722
@Test
@@ -20,8 +25,10 @@ public void givenErrorMessage_whenCheckArgumentEvaluatesFalse_throwsException()
2025
final String message = "Age can't be zero or less than zero";
2126
try {
2227
checkArgument(age > 0, message);
23-
} catch (IllegalArgumentException illegalArgumentException) {
24-
assertEquals(message, illegalArgumentException.getMessage());
28+
} catch (Exception exception) {
29+
assertThat(exception).isInstanceOf(IllegalArgumentException.class);
30+
assertThat(exception).hasNoCause();
31+
assertThat(exception).hasMessage(message);
2532
}
2633
}
2734

@@ -31,16 +38,22 @@ public void givenTemplatedErrorMessage_whenCheckArgumentEvaluatesFalse_throwsExc
3138
final String message = "Age can't be zero or less than zero, you supplied %s.";
3239
try {
3340
checkArgument(age > 0, message, age);
34-
} catch (IllegalArgumentException illegalArgumentException) {
35-
final String formattedMessage = String.format(message, age);
36-
assertEquals(formattedMessage, illegalArgumentException.getMessage());
41+
} catch (Exception exception) {
42+
assertThat(exception).isInstanceOf(IllegalArgumentException.class);
43+
assertThat(exception).hasNoCause();
44+
assertThat(exception).hasMessage(message, age);
3745
}
3846
}
3947

40-
@Test(expected = IndexOutOfBoundsException.class)
48+
@Test
4149
public void givenArrayOfIntegers_whenCheckElementIndexEvaluatesFalse_throwsException() {
4250
final int[] numbers = { 1, 2, 3, 4, 5 };
43-
checkElementIndex(6, numbers.length - 1);
51+
try {
52+
checkElementIndex(6, numbers.length - 1);
53+
} catch (Exception exception) {
54+
assertThat(exception).isInstanceOf(IndexOutOfBoundsException.class);
55+
assertThat(exception).hasNoCause();
56+
}
4457
}
4558

4659
@Test
@@ -49,15 +62,23 @@ public void givenArrayOfIntegersAndMessage_whenCheckElementIndexEvaluatesFalse_t
4962
final String message = "Please check the bound of an array and retry";
5063
try {
5164
checkElementIndex(6, numbers.length - 1, message);
52-
} catch (IndexOutOfBoundsException indexOutOfBoundsException) {
53-
assertTrue(indexOutOfBoundsException.getMessage().startsWith(message));
65+
} catch (Exception exception) {
66+
assertThat(exception).isInstanceOf(IndexOutOfBoundsException.class);
67+
assertThat(exception).hasNoCause();
68+
assertThat(exception.getMessage()).startsWith(message);
5469
}
5570
}
5671

57-
@Test(expected = NullPointerException.class)
72+
@Test
5873
public void givenNullString_whenCheckNotNullCalled_throwsException() {
5974
final String nullObject = null;
60-
checkNotNull(nullObject);
75+
try {
76+
checkNotNull(nullObject);
77+
} catch (Exception exception) {
78+
assertThat(exception).isInstanceOf(NullPointerException.class);
79+
assertThat(exception).hasNoCause();
80+
assertThat(exception).hasMessage(null);
81+
}
6182
}
6283

6384
@Test
@@ -66,8 +87,10 @@ public void givenNullString_whenCheckNotNullCalledWithMessage_throwsException()
6687
final String message = "Please check the Object supplied, its null!";
6788
try {
6889
checkNotNull(nullObject, message);
69-
} catch (NullPointerException nullPointerException) {
70-
assertEquals(message, nullPointerException.getMessage());
90+
} catch (Exception exception) {
91+
assertThat(exception).isInstanceOf(NullPointerException.class);
92+
assertThat(exception).hasNoCause();
93+
assertThat(exception).hasMessage(message);
7194
}
7295
}
7396

@@ -77,16 +100,22 @@ public void givenNullString_whenCheckNotNullCalledWithTemplatedMessage_throwsExc
77100
final String message = "Please check the Object supplied, its %s!";
78101
try {
79102
checkNotNull(nullObject, message, nullObject);
80-
} catch (NullPointerException nullPointerException) {
81-
final String formattedMessage = String.format(message, nullObject);
82-
assertEquals(formattedMessage, nullPointerException.getMessage());
103+
} catch (Exception exception) {
104+
assertThat(exception).isInstanceOf(NullPointerException.class);
105+
assertThat(exception).hasNoCause();
106+
assertThat(exception).hasMessage(message, nullObject);
83107
}
84108
}
85109

86-
@Test(expected = IndexOutOfBoundsException.class)
110+
@Test
87111
public void givenArrayOfIntegers_whenCheckPositionIndexEvaluatesFalse_throwsException() {
88112
final int[] numbers = { 1, 2, 3, 4, 5 };
89-
checkPositionIndex(6, numbers.length - 1);
113+
try {
114+
checkPositionIndex(6, numbers.length - 1);
115+
} catch (Exception exception) {
116+
assertThat(exception).isInstanceOf(IndexOutOfBoundsException.class);
117+
assertThat(exception).hasNoCause();
118+
}
90119
}
91120

92121
@Test
@@ -95,22 +124,35 @@ public void givenArrayOfIntegersAndMessage_whenCheckPositionIndexEvaluatesFalse_
95124
final String message = "Please check the bound of an array and retry";
96125
try {
97126
checkPositionIndex(6, numbers.length - 1, message);
98-
} catch (IndexOutOfBoundsException indexOutOfBoundsException) {
99-
assertTrue(indexOutOfBoundsException.getMessage().startsWith(message));
127+
} catch (Exception exception) {
128+
assertThat(exception).isInstanceOf(IndexOutOfBoundsException.class);
129+
assertThat(exception).hasNoCause();
130+
assertThat(exception.getMessage()).startsWith(message);
100131
}
101132
}
102133

103-
@Test(expected = IndexOutOfBoundsException.class)
134+
@Test
104135
public void givenArrayOfIntegers_whenCheckPositionIndexesEvaluatesFalse_throwsException() {
105136
final int[] numbers = { 1, 2, 3, 4, 5 };
106-
checkPositionIndexes(6, 0, numbers.length - 1);
137+
try {
138+
checkPositionIndexes(6, 0, numbers.length - 1);
139+
} catch (Exception exception) {
140+
assertThat(exception).isInstanceOf(IndexOutOfBoundsException.class);
141+
assertThat(exception).hasNoCause();
142+
}
107143
}
108144

109-
@Test(expected = IllegalStateException.class)
145+
@Test
110146
public void givenValidStates_whenCheckStateEvaluatesFalse_throwsException() {
111147
final int[] validStates = { -1, 0, 1 };
112148
final int givenState = 10;
113-
checkState(Arrays.binarySearch(validStates, givenState) > 0);
149+
try {
150+
checkState(Arrays.binarySearch(validStates, givenState) > 0);
151+
} catch (Exception exception) {
152+
assertThat(exception).isInstanceOf(IllegalStateException.class);
153+
assertThat(exception).hasNoCause();
154+
assertThat(exception).hasMessage(null);
155+
}
114156
}
115157

116158
@Test
@@ -120,8 +162,10 @@ public void givenValidStatesAndMessage_whenCheckStateEvaluatesFalse_throwsExcept
120162
final String message = "You have entered an invalid state";
121163
try {
122164
checkState(Arrays.binarySearch(validStates, givenState) < 0, message);
123-
} catch (IllegalStateException IllegalStateException) {
124-
assertEquals(message, IllegalStateException.getMessage());
165+
} catch (Exception exception) {
166+
assertThat(exception).isInstanceOf(IllegalStateException.class);
167+
assertThat(exception).hasNoCause();
168+
assertThat(exception).hasMessage(message);
125169
}
126170
}
127171

@@ -132,10 +176,11 @@ public void givenValidStatesAndTemplatedMessage_whenCheckStateEvaluatesFalse_thr
132176
final String message = "State can't be %s, It can be one of %s.";
133177
try {
134178
checkState(Arrays.binarySearch(validStates, givenState) < 0, message, givenState, Arrays.toString(validStates));
135-
} catch (IllegalStateException IllegalStateException) {
136-
final String formattedMessage = String.format(message, givenState, Arrays.toString(validStates));
137-
assertEquals(formattedMessage, IllegalStateException.getMessage());
179+
} catch (Exception exception) {
180+
assertThat(exception).isInstanceOf(IllegalStateException.class);
181+
assertThat(exception).hasNoCause();
182+
assertThat(exception).hasMessage(message, givenState, Arrays.toString(validStates));
138183
}
139184
}
140185

141-
}
186+
}

0 commit comments

Comments
 (0)