Skip to content

Commit 3c503bc

Browse files
committed
Additional test to cover new API methods
1 parent ffbc04e commit 3c503bc

File tree

1 file changed

+170
-55
lines changed

1 file changed

+170
-55
lines changed

src/test/java/pl/wavesoftware/eid/utils/EidPreconditionsTest.java

Lines changed: 170 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import javax.annotation.Nonnull;
3232
import java.io.InterruptedIOException;
3333
import java.lang.reflect.Constructor;
34+
import java.text.ParseException;
3435

3536
import static org.assertj.core.api.Assertions.assertThat;
3637
import static org.hamcrest.Matchers.containsString;
@@ -42,7 +43,6 @@
4243
*
4344
* @author Krzysztof Suszyński <krzysztof.suszynski@wavesoftware.pl>
4445
*/
45-
@SuppressWarnings("ConstantConditions")
4646
public class EidPreconditionsTest {
4747

4848
@Rule
@@ -53,7 +53,7 @@ public class EidPreconditionsTest {
5353
@Test
5454
public void testCheckArgument() {
5555
// given
56-
boolean expression = false;
56+
boolean expression = falsyValue();
5757
// then
5858
thrown.expect(EidIllegalArgumentException.class);
5959
thrown.expectMessage(containsString(eid));
@@ -64,7 +64,7 @@ public void testCheckArgument() {
6464
@Test
6565
public void testCheckArgument_WithMessage() {
6666
// given
67-
boolean expression = false;
67+
boolean expression = falsyValue();
6868
// then
6969
thrown.expect(EidIllegalArgumentException.class);
7070
thrown.expectMessage(containsString(eid));
@@ -73,10 +73,20 @@ public void testCheckArgument_WithMessage() {
7373
EidPreconditions.checkArgument(expression, eid, "PI value is %.2f", Math.PI);
7474
}
7575

76+
@Test
77+
public void testCheckArgument_Ok_WithMessage() {
78+
// given
79+
boolean expression = truthyValue();
80+
// when
81+
EidPreconditions.checkArgument(expression, eid, "PI value is %.2f", Math.PI);
82+
// then
83+
assertThat(thrown).isNotNull();
84+
}
85+
7686
@Test
7787
public void testCheckArgument_Ok() {
7888
// when
79-
boolean expression = true;
89+
boolean expression = truthyValue();
8090
// when
8191
EidPreconditions.checkArgument(expression, eid);
8292
// then
@@ -86,7 +96,7 @@ public void testCheckArgument_Ok() {
8696
@Test
8797
public void testCheckState() {
8898
// given
89-
boolean expression = false;
99+
boolean expression = falsyValue();
90100
// then
91101
thrown.expect(EidIllegalStateException.class);
92102
thrown.expectMessage(containsString(eid));
@@ -97,7 +107,7 @@ public void testCheckState() {
97107
@Test
98108
public void testCheckState_WithMessage() {
99109
// given
100-
boolean expression = false;
110+
boolean expression = falsyValue();
101111
// then
102112
thrown.expect(EidIllegalStateException.class);
103113
thrown.expectMessage(containsString(eid));
@@ -106,10 +116,20 @@ public void testCheckState_WithMessage() {
106116
EidPreconditions.checkState(expression, eid, "PI is %.4f", Math.PI);
107117
}
108118

119+
@Test
120+
public void testCheckState_Ok_WithMessage() {
121+
// given
122+
boolean expression = truthyValue();
123+
// when
124+
EidPreconditions.checkState(expression, eid, "PI is %.4f", Math.PI);
125+
// then
126+
assertThat(thrown).isNotNull();
127+
}
128+
109129
@Test
110130
public void testCheckState_Ok() {
111131
// when
112-
boolean expression = true;
132+
boolean expression = truthyValue();
113133
// when
114134
EidPreconditions.checkState(expression, eid);
115135
// then
@@ -130,7 +150,7 @@ public void testCheckNotNull_Ok() {
130150
@Test
131151
public void testCheckNotNull() {
132152
// given
133-
Object reference = null;
153+
Object reference = nullyValue();
134154
// then
135155
thrown.expect(EidNullPointerException.class);
136156
thrown.expectMessage(containsString(eid));
@@ -141,7 +161,7 @@ public void testCheckNotNull() {
141161
@Test
142162
public void testCheckNotNull_WithMessage() {
143163
// given
144-
Object reference = null;
164+
Object reference = nullyValue();
145165
// then
146166
thrown.expect(EidNullPointerException.class);
147167
thrown.expectMessage(containsString(eid));
@@ -150,6 +170,16 @@ public void testCheckNotNull_WithMessage() {
150170
EidPreconditions.checkNotNull(reference, eid, "π <=> %.3f", Math.PI);
151171
}
152172

173+
@Test
174+
public void testCheckNotNull_Ok_WithMessage() {
175+
// given
176+
Object reference = "A test";
177+
// when
178+
Object checked = EidPreconditions.checkNotNull(reference, eid, "π <=> %.3f", Math.PI);
179+
// then
180+
assertThat(checked).isSameAs(reference);
181+
}
182+
153183
@Test
154184
public void testCheckElementIndex_Ok() {
155185
// given
@@ -165,8 +195,8 @@ public void testCheckElementIndex_Ok() {
165195
@Test
166196
public void testCheckElementIndex_Nulls() {
167197
// given
168-
Integer index = null;
169-
Integer size = null;
198+
Integer index = nullyValue();
199+
Integer size = nullyValue();
170200
Matcher<String> m = Matchers.equalTo(null);
171201
// then
172202
thrown.expect(NullPointerException.class);
@@ -200,6 +230,30 @@ public void testCheckElementIndex_WithMessage() {
200230
EidPreconditions.checkElementIndex(index, size, eid, "Pi (π): %.2f", Math.PI);
201231
}
202232

233+
@Test
234+
public void testCheckElementIndex_SizeIllegal_WithMessage() {
235+
// given
236+
int index = 0;
237+
int size = -45;
238+
// then
239+
thrown.expect(EidIllegalArgumentException.class);
240+
thrown.expectMessage(containsString(eid));
241+
thrown.expectMessage(containsString("Pi (π): 3.142"));
242+
// when
243+
EidPreconditions.checkElementIndex(index, size, eid, "Pi (π): %.3f", Math.PI);
244+
}
245+
246+
@Test
247+
public void testCheckElementIndex_Ok_WithMessage() {
248+
// given
249+
int index = 234;
250+
int size = 450;
251+
// when
252+
int checked = EidPreconditions.checkElementIndex(index, size, eid, "Pi (π): %.1f", Math.PI);
253+
// then
254+
assertThat(checked).isEqualTo(index);
255+
}
256+
203257
@Test
204258
public void testCheckElementIndex_SizeInvalid() {
205259
// given
@@ -236,42 +290,6 @@ public void testCheckElementIndex_Positive() {
236290
EidPreconditions.checkElementIndex(index, size, eid);
237291
}
238292

239-
@Test
240-
public void testTryToExecute_Ok() {
241-
// given
242-
final String expResult = "test string";
243-
EidPreconditions.RiskyCode<String> riskyCode = new EidPreconditions.RiskyCode<String>() {
244-
245-
@Override
246-
public String execute() {
247-
return expResult;
248-
}
249-
};
250-
// when
251-
String result = EidPreconditions.tryToExecute(riskyCode, eid);
252-
// then
253-
assertThat(result).isEqualTo(expResult);
254-
}
255-
256-
@Test
257-
public void testTryToExecute() {
258-
// given
259-
final String causeMessage = "a cause message";
260-
EidPreconditions.RiskyCode<String> riskyCode = new EidPreconditions.RiskyCode<String>() {
261-
262-
@Override
263-
public String execute() throws InterruptedIOException {
264-
throw new InterruptedIOException(causeMessage);
265-
}
266-
};
267-
// then
268-
thrown.expect(EidRuntimeException.class);
269-
thrown.expectCause(isA(InterruptedIOException.class));
270-
thrown.expectCause(hasMessage(equalTo(causeMessage)));
271-
// when
272-
EidPreconditions.tryToExecute(riskyCode, eid);
273-
}
274-
275293
@Test
276294
public void testCreate() throws NoSuchMethodException {
277295
// given
@@ -301,7 +319,7 @@ public boolean matches(Object item) {
301319
@Test
302320
public void testCheckArgument_boolean_Eid_Null() {
303321
// given
304-
boolean expression = false;
322+
boolean expression = falsyValue();
305323
Eid eidObject = getNullEid();
306324
// then
307325
thrown.expect(IllegalArgumentException.class);
@@ -314,7 +332,7 @@ public void testCheckArgument_boolean_Eid_Null() {
314332
public void testCheckArgument_boolean_Eid() {
315333
// given
316334
Eid eidObject = getEid();
317-
boolean expression = false;
335+
boolean expression = falsyValue();
318336
// then
319337
thrown.expect(EidIllegalArgumentException.class);
320338
thrown.expectMessage(containsString(eid));
@@ -326,7 +344,7 @@ public void testCheckArgument_boolean_Eid() {
326344
public void testCheckArgument_boolean_Eid_Ok() {
327345
// given
328346
Eid eidObject = getEid();
329-
boolean expression = true;
347+
boolean expression = truthyValue();
330348
// when
331349
EidPreconditions.checkArgument(expression, eidObject);
332350
// then
@@ -336,7 +354,7 @@ public void testCheckArgument_boolean_Eid_Ok() {
336354
@Test
337355
public void testCheckState_boolean_Eid() {
338356
// given
339-
boolean expression = false;
357+
boolean expression = falsyValue();
340358
Eid eidObject = getEid();
341359
// then
342360
thrown.expect(EidIllegalStateException.class);
@@ -348,7 +366,7 @@ public void testCheckState_boolean_Eid() {
348366
@Test
349367
public void testCheckState_boolean_Eid_Ok() {
350368
// given
351-
boolean expression = true;
369+
boolean expression = truthyValue();
352370
Eid eidObject = getEid();
353371
// when
354372
EidPreconditions.checkState(expression, eidObject);
@@ -359,7 +377,7 @@ public void testCheckState_boolean_Eid_Ok() {
359377
@Test
360378
public void testCheckNotNull_GenericType_Eid() {
361379
// given
362-
Object reference = null;
380+
Object reference = nullyValue();
363381
Eid eidObject = getEid();
364382
// then
365383
thrown.expect(EidNullPointerException.class);
@@ -418,6 +436,51 @@ public void testCheckElementIndex_3args_2_Ok() {
418436
assertThat(result).isEqualTo(index);
419437
}
420438

439+
/**
440+
* @deprecated Convert this test to new API after removal of {@link EidPreconditions.RiskyCode}
441+
*/
442+
@Test
443+
public void testTryToExecute_Ok() {
444+
// given
445+
final String expResult = "test string";
446+
EidPreconditions.RiskyCode<String> riskyCode = new EidPreconditions.RiskyCode<String>() {
447+
448+
@Override
449+
public String execute() {
450+
return expResult;
451+
}
452+
};
453+
// when
454+
String result = EidPreconditions.tryToExecute(riskyCode, eid);
455+
// then
456+
assertThat(result).isEqualTo(expResult);
457+
}
458+
459+
/**
460+
* @deprecated Convert this test to new API after removal of {@link EidPreconditions.RiskyCode}
461+
*/
462+
@Test
463+
public void testTryToExecute() {
464+
// given
465+
final String causeMessage = "a cause message";
466+
EidPreconditions.RiskyCode<String> riskyCode = new EidPreconditions.RiskyCode<String>() {
467+
468+
@Override
469+
public String execute() throws InterruptedIOException {
470+
throw new InterruptedIOException(causeMessage);
471+
}
472+
};
473+
// then
474+
thrown.expect(EidRuntimeException.class);
475+
thrown.expectCause(isA(InterruptedIOException.class));
476+
thrown.expectCause(hasMessage(equalTo(causeMessage)));
477+
// when
478+
EidPreconditions.tryToExecute(riskyCode, eid);
479+
}
480+
481+
/**
482+
* @deprecated Convert this test to new API after removal of {@link EidPreconditions.RiskyCode}
483+
*/
421484
@Test
422485
public void testTryToExecute_EidPreconditionsRiskyCode_Eid() {
423486
// given
@@ -438,6 +501,9 @@ public String execute() throws InterruptedIOException {
438501
EidPreconditions.tryToExecute(riskyCode, eidObject);
439502
}
440503

504+
/**
505+
* @deprecated Convert this test to new API after removal of {@link EidPreconditions.RiskyCode}
506+
*/
441507
@Test
442508
public void testTryToExecute_EidPreconditionsRiskyCode_Eid_Ok() {
443509
// given
@@ -456,14 +522,63 @@ public String execute() {
456522
assertThat(result).isEqualTo(tester);
457523
}
458524

459-
@SuppressWarnings("SameReturnValue")
525+
@Test
526+
public void testTryToExecute_UnsafeProcedure_String() {
527+
// given
528+
final String causeMessage = "An error occured while parsing JSON document at char 178";
529+
EidPreconditions.UnsafeProcedure procedure = new EidPreconditions.UnsafeProcedure() {
530+
@Override
531+
public void execute() throws ParseException {
532+
throw new ParseException(causeMessage, 178);
533+
}
534+
};
535+
// then
536+
thrown.expect(EidRuntimeException.class);
537+
thrown.expectCause(isA(ParseException.class));
538+
thrown.expectCause(hasMessage(equalTo(causeMessage)));
539+
// when
540+
EidPreconditions.tryToExecute(procedure, eid);
541+
}
542+
543+
@Test
544+
public void testTryToExecute_UnsafeProcedure_String_Ok() {
545+
// given
546+
EidPreconditions.UnsafeProcedure procedure = new EidPreconditions.UnsafeProcedure() {
547+
@Override
548+
public void execute() {
549+
// nothing special here, for unit test
550+
}
551+
};
552+
// when
553+
EidPreconditions.tryToExecute(procedure, eid);
554+
// then
555+
assertThat(procedure).isNotNull();
556+
}
557+
460558
private Eid getNullEid() {
461-
return null;
559+
return nullyValue();
462560
}
463561

464562
@Nonnull
465563
private Eid getEid() {
466564
return new Eid(eid);
467565
}
468566

567+
private static Boolean truthyValue() {
568+
return Boolean.TRUE;
569+
}
570+
571+
private static Boolean falsyValue() {
572+
return Boolean.FALSE;
573+
}
574+
575+
@SuppressWarnings("unchecked")
576+
@Nonnull
577+
private static <T> T nullyValue() {
578+
// This is hack to overcome Intellij null check :-P
579+
Nonnull ret = Object.class.getAnnotation(Nonnull.class);
580+
assertThat(ret).isNull();
581+
return (T) ret;
582+
}
583+
469584
}

0 commit comments

Comments
 (0)