1 package org.djutils.exceptions;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNull;
5 import static org.junit.Assert.assertTrue;
6 import static org.junit.Assert.fail;
7
8 import java.util.IllegalFormatException;
9
10 import org.junit.Test;
11
12
13
14
15
16 public class TryTest
17 {
18
19
20
21
22
23 @SuppressWarnings("checkstyle:methodlength")
24 @Test
25 public void tryAssignTest() throws RuntimeException
26 {
27 String nullPointer = null;
28 String initialValueOfResult = "initial value of result";
29 String result = initialValueOfResult;
30 String formatWithoutArg = "format";
31 String arg1 = "arg1";
32 String formatWithOneArg = "format %s";
33 String arg2 = "arg2";
34 String formatWith2Args = "format %s %s";
35 String arg3 = "arg3";
36 String formatWith3Args = "format %s %s %s";
37 String arg4 = "arg4";
38 String formatWith4Args = "format %s %s %s %s";
39
40
41
42 result = Try.assign(() -> String.format(formatWithoutArg), "Should not fail");
43 assertEquals("assign should have succeeded", formatWithoutArg, result);
44 result = initialValueOfResult;
45
46 result = Try.assign(() -> String.format(formatWithoutArg), RuntimeException.class, "Should not fail");
47 assertEquals("assign should have succeeded", formatWithoutArg, result);
48 result = initialValueOfResult;
49
50 result = Try.assign(() -> String.format(formatWithoutArg), "Should not fail %s", arg1);
51 assertEquals("assign should have succeeded", formatWithoutArg, result);
52 result = initialValueOfResult;
53
54 result = Try.assign(() -> String.format(formatWithoutArg), RuntimeException.class, "Should not fail %s", arg1);
55 assertEquals("assign should have succeeded", formatWithoutArg, result);
56 result = initialValueOfResult;
57
58 result = Try.assign(() -> String.format(formatWithoutArg), "Should not fail %s %s", arg1, arg2);
59 assertEquals("assign should have succeeded", formatWithoutArg, result);
60 result = initialValueOfResult;
61
62 result = Try.assign(() -> String.format(formatWithoutArg), RuntimeException.class, "Should not fail %s %s", arg1, arg2);
63 assertEquals("assign should have succeeded", formatWithoutArg, result);
64 result = initialValueOfResult;
65
66 result = Try.assign(() -> String.format(formatWithoutArg), "Should not fail %s %s %s", arg1, arg2, arg3);
67 assertEquals("assign should have succeeded", formatWithoutArg, result);
68 result = initialValueOfResult;
69
70 result = Try.assign(() -> String.format(formatWithoutArg), RuntimeException.class, "Should not fail %s %s %s", arg1,
71 arg2, arg3);
72 assertEquals("assign should have succeeded", formatWithoutArg, result);
73 result = initialValueOfResult;
74
75 result = Try.assign(() -> String.format(formatWithoutArg), "Should not fail %s %s %s %s", arg1, arg2, arg3, arg4);
76 assertEquals("assign should have succeeded", formatWithoutArg, result);
77 result = initialValueOfResult;
78
79 result = Try.assign(() -> String.format(formatWithoutArg), RuntimeException.class, "Should not fail %s %s %s %s", arg1,
80 arg2, arg3, arg4);
81 assertEquals("assign should have succeeded", formatWithoutArg, result);
82 result = initialValueOfResult;
83
84 result = Try.assign(() -> String.format(formatWithoutArg), "Should not fail %s %s %s %s %s", arg1, arg2, arg3, arg4,
85 arg4);
86 assertEquals("assign should have succeeded", formatWithoutArg, result);
87 result = initialValueOfResult;
88
89 result = Try.assign(() -> String.format(formatWithoutArg), RuntimeException.class, "Should not fail %s %s %s %s %s",
90 arg1, arg2, arg3, arg4, arg4);
91 assertEquals("assign should have succeeded", formatWithoutArg, result);
92 result = initialValueOfResult;
93
94
95
96 try
97 {
98 result = Try.assign(() -> String.format(nullPointer, "unused argument"), formatWithoutArg);
99 fail("String.format with nullPointer should have thrown a nullPointerException");
100 }
101 catch (RuntimeException rte)
102 {
103 assertTrue("message cause is NullPointerException, instead got: " + rte.getCause().toString(),
104 rte.getCause().toString().contains("NullPointerException"));
105 assertTrue("message contains format", rte.getMessage().contains(formatWithoutArg));
106 }
107 assertEquals("Result has not changed", initialValueOfResult, result);
108
109 try
110 {
111 result = Try.assign(() -> String.format(nullPointer, "unused argument"), formatWithOneArg, arg1);
112 fail("String.format with nullPointer should have thrown a nullPointerException");
113 }
114 catch (RuntimeException rte)
115 {
116 assertTrue("message cause is NullPointerException, instead got: " + rte.getCause().toString(),
117 rte.getCause().toString().contains("NullPointerException"));
118 assertTrue("message contains format", rte.getMessage().contains(formatWithoutArg));
119 assertTrue("message contains arg1", rte.getMessage().contains(arg1));
120 }
121 assertEquals("Result has not changed", initialValueOfResult, result);
122
123 try
124 {
125 result = Try.assign(() -> String.format(nullPointer, "unused argument"), formatWith2Args, arg1, arg2);
126 fail("String.format with nullPointer should have thrown a nullPointerException");
127 }
128 catch (RuntimeException rte)
129 {
130 assertTrue("message cause is NullPointerException, instead got: " + rte.getCause().toString(),
131 rte.getCause().toString().contains("NullPointerException"));
132 assertTrue("message contains format", rte.getMessage().contains(formatWithoutArg));
133 assertTrue("message contains arg1", rte.getMessage().contains(arg1));
134 assertTrue("message contains arg2", rte.getMessage().contains(arg2));
135 }
136 assertEquals("Result has not changed", initialValueOfResult, result);
137
138 try
139 {
140 result = Try.assign(() -> String.format(nullPointer, "unused argument"), formatWith3Args, arg1, arg2, arg3);
141 fail("String.format with nullPointer should have thrown a nullPointerException");
142 }
143 catch (RuntimeException rte)
144 {
145 assertTrue("message cause is NullPointerException, instead got: " + rte.getCause().toString(),
146 rte.getCause().toString().contains("NullPointerException"));
147 assertTrue("message contains format", rte.getMessage().contains(formatWithoutArg));
148 assertTrue("message contains arg1", rte.getMessage().contains(arg1));
149 assertTrue("message contains arg2", rte.getMessage().contains(arg2));
150 assertTrue("message contains arg3", rte.getMessage().contains(arg3));
151 }
152 assertEquals("Result has not changed", initialValueOfResult, result);
153
154 try
155 {
156 result = Try.assign(() -> String.format(nullPointer, "unused argument"), formatWith4Args, arg1, arg2, arg3, arg4);
157 fail("String.format with nullPointer should have thrown a nullPointerException");
158 }
159 catch (RuntimeException rte)
160 {
161 assertTrue("message cause is NullPointerException, instead got: " + rte.getCause().toString(),
162 rte.getCause().toString().contains("NullPointerException"));
163 assertTrue("message contains format", rte.getMessage().contains(formatWithoutArg));
164 assertTrue("message contains arg1", rte.getMessage().contains(arg1));
165 assertTrue("message contains arg2", rte.getMessage().contains(arg2));
166 assertTrue("message contains arg3", rte.getMessage().contains(arg3));
167 assertTrue("message contains arg4", rte.getMessage().contains(arg4));
168 }
169 assertEquals("Result has not changed", initialValueOfResult, result);
170
171 result = Try.assign(() -> String.format(formatWithoutArg), RuntimeException.class, "Should not fail");
172 assertEquals("assign should have succeeded", formatWithoutArg, result);
173 result = initialValueOfResult;
174
175 try
176 {
177 result = Try.assign(() -> String.format(nullPointer, "unused argument"), RuntimeException.class, formatWithoutArg);
178 fail("String.format with nullPointer should have thrown a nullPointerException");
179 }
180 catch (RuntimeException rte)
181 {
182 assertTrue("message cause is NullPointerException, instead got: " + rte.getCause().toString(),
183 rte.getCause().toString().contains("NullPointerException"));
184 assertTrue("message contains format", rte.getMessage().contains(formatWithoutArg));
185 }
186 assertEquals("Result has not changed", initialValueOfResult, result);
187
188 try
189 {
190 result = Try.assign(() -> String.format(nullPointer, "unused argument"), RuntimeException.class, formatWithOneArg,
191 arg1);
192 fail("String.format with nullPointer should have thrown a nullPointerException");
193 }
194 catch (RuntimeException rte)
195 {
196 assertTrue("message cause is NullPointerException, instead got: " + rte.getCause().toString(),
197 rte.getCause().toString().contains("NullPointerException"));
198 assertTrue("message contains format", rte.getMessage().contains(formatWithoutArg));
199 assertTrue("message contains arg1", rte.getMessage().contains(arg1));
200 }
201 assertEquals("Result has not changed", initialValueOfResult, result);
202
203 try
204 {
205 result = Try.assign(() -> String.format(nullPointer, "unused argument"), RuntimeException.class, formatWith2Args,
206 arg1, arg2);
207 fail("String.format with nullPointer should have thrown a nullPointerException");
208 }
209 catch (RuntimeException rte)
210 {
211 assertTrue("message cause is NullPointerException, instead got: " + rte.getCause().toString(),
212 rte.getCause().toString().contains("NullPointerException"));
213 assertTrue("message contains format", rte.getMessage().contains(formatWithoutArg));
214 assertTrue("message contains arg1", rte.getMessage().contains(arg1));
215 assertTrue("message contains arg2", rte.getMessage().contains(arg2));
216 }
217 assertEquals("Result has not changed", initialValueOfResult, result);
218
219 try
220 {
221 result = Try.assign(() -> String.format(nullPointer, "unused argument"), RuntimeException.class, formatWith3Args,
222 arg1, arg2, arg3);
223 fail("String.format with nullPointer should have thrown a nullPointerException");
224 }
225 catch (RuntimeException rte)
226 {
227 assertTrue("message cause is NullPointerException, instead got: " + rte.getCause().toString(),
228 rte.getCause().toString().contains("NullPointerException"));
229 assertTrue("message contains format", rte.getMessage().contains(formatWithoutArg));
230 assertTrue("message contains arg1", rte.getMessage().contains(arg1));
231 assertTrue("message contains arg2", rte.getMessage().contains(arg2));
232 assertTrue("message contains arg3", rte.getMessage().contains(arg3));
233 }
234 assertEquals("Result has not changed", initialValueOfResult, result);
235
236 try
237 {
238 result = Try.assign(() -> String.format(nullPointer, "unused argument"), RuntimeException.class, formatWith4Args,
239 arg1, arg2, arg3, arg4);
240 fail("String.format with nullPointer should have thrown a nullPointerException");
241 }
242 catch (RuntimeException rte)
243 {
244 assertTrue("message cause is NullPointerException, instead got: " + rte.getCause().toString(),
245 rte.getCause().toString().contains("NullPointerException"));
246 assertTrue("message contains format", rte.getMessage().contains(formatWithoutArg));
247 assertTrue("message contains arg1", rte.getMessage().contains(arg1));
248 assertTrue("message contains arg2", rte.getMessage().contains(arg2));
249 assertTrue("message contains arg3", rte.getMessage().contains(arg3));
250 assertTrue("message contains arg4", rte.getMessage().contains(arg4));
251 }
252 assertEquals("Result has not changed", initialValueOfResult, result);
253 }
254
255
256 @SuppressWarnings("checkstyle:visibilitymodifier")
257 protected String value;
258
259
260
261
262
263 protected void setResult(final String newResult)
264 {
265 this.value = newResult;
266 }
267
268
269
270
271
272 @SuppressWarnings("checkstyle:methodlength")
273 @Test
274 public void tryExecuteTest() throws RuntimeException
275 {
276 String nullPointer = null;
277 String initialValueOfResult = "initial value of result";
278 String result = initialValueOfResult;
279 String formatWithoutArg = "format";
280 String arg1 = "arg1";
281 String formatWithOneArg = "format %s";
282 String arg2 = "arg2";
283 String formatWith2Args = "format %s %s";
284 String arg3 = "arg3";
285 String formatWith3Args = "format %s %s %s";
286 String arg4 = "arg4";
287 String formatWith4Args = "format %s %s %s %s";
288
289
290
291 Try.execute(() -> setResult(String.format(formatWithoutArg)), "Should not fail");
292 assertEquals("assign should have succeeded", formatWithoutArg, this.value);
293 this.value = initialValueOfResult;
294
295 Try.execute(() -> setResult(String.format(formatWithoutArg)), RuntimeException.class, "Should not fail");
296 assertEquals("assign should have succeeded", formatWithoutArg, this.value);
297 this.value = initialValueOfResult;
298
299 Try.execute(() -> setResult(String.format(formatWithoutArg)), "Should not fail %s", arg1);
300 assertEquals("assign should have succeeded", formatWithoutArg, this.value);
301 this.value = initialValueOfResult;
302
303 Try.execute(() -> setResult(String.format(formatWithoutArg)), RuntimeException.class, "Should not fail %s", arg1);
304 assertEquals("assign should have succeeded", formatWithoutArg, this.value);
305 this.value = initialValueOfResult;
306
307 Try.execute(() -> setResult(String.format(formatWithoutArg)), "Should not fail %s %s", arg1, arg2);
308 assertEquals("assign should have succeeded", formatWithoutArg, this.value);
309 this.value = initialValueOfResult;
310
311 Try.execute(() -> setResult(String.format(formatWithoutArg)), RuntimeException.class, "Should not fail %s %s", arg1,
312 arg2);
313 assertEquals("assign should have succeeded", formatWithoutArg, this.value);
314 this.value = initialValueOfResult;
315
316 Try.execute(() -> setResult(String.format(formatWithoutArg)), "Should not fail %s %s %s", arg1, arg2, arg3);
317 assertEquals("assign should have succeeded", formatWithoutArg, this.value);
318 this.value = initialValueOfResult;
319
320 Try.execute(() -> setResult(String.format(formatWithoutArg)), RuntimeException.class, "Should not fail %s %s %s", arg1,
321 arg2, arg3);
322 assertEquals("assign should have succeeded", formatWithoutArg, this.value);
323 this.value = initialValueOfResult;
324
325 Try.execute(() -> setResult(String.format(formatWithoutArg)), "Should not fail %s %s %s %s", arg1, arg2, arg3, arg4);
326 assertEquals("assign should have succeeded", formatWithoutArg, this.value);
327 this.value = initialValueOfResult;
328
329 Try.execute(() -> setResult(String.format(formatWithoutArg)), RuntimeException.class, "Should not fail %s %s %s %s",
330 arg1, arg2, arg3, arg4);
331 assertEquals("assign should have succeeded", formatWithoutArg, this.value);
332 this.value = initialValueOfResult;
333
334 Try.execute(() -> setResult(String.format(formatWithoutArg)), "Should not fail %s %s %s %s %s", arg1, arg2, arg3, arg4,
335 arg4);
336 assertEquals("assign should have succeeded", formatWithoutArg, this.value);
337 this.value = initialValueOfResult;
338
339 Try.execute(() -> setResult(String.format(formatWithoutArg)), RuntimeException.class, "Should not fail %s %s %s %s %s",
340 arg1, arg2, arg3, arg4, arg4);
341 assertEquals("assign should have succeeded", formatWithoutArg, this.value);
342 this.value = initialValueOfResult;
343
344
345
346 try
347 {
348 Try.execute(() -> String.format(nullPointer, "unused argument"), RuntimeException.class, formatWithoutArg);
349 fail("String.format with nullPointer should have thrown a NullPointerException");
350 }
351 catch (RuntimeException rte)
352 {
353 assertTrue("message cause is NullPointerException, instead got: " + rte.getCause().toString(),
354 rte.getCause().toString().contains("NullPointerException"));
355 assertTrue("message contains format", rte.getMessage().contains(formatWithoutArg));
356 }
357 assertEquals("Result has not changed", initialValueOfResult, result);
358
359 try
360 {
361 Try.execute(() -> String.format(nullPointer, "unused argument"), RuntimeException.class, formatWithOneArg, arg1);
362 fail("String.format with nullPointer should have thrown a nullPointerException");
363 }
364 catch (RuntimeException rte)
365 {
366 assertTrue("message cause is NullPointerException, instead got: " + rte.getCause().toString(),
367 rte.getCause().toString().contains("NullPointerException"));
368 assertTrue("message contains format", rte.getMessage().contains(formatWithoutArg));
369 assertTrue("message contains arg1", rte.getMessage().contains(arg1));
370 }
371 assertEquals("Result has not changed", initialValueOfResult, result);
372
373 try
374 {
375 Try.execute(() -> String.format(nullPointer, "unused argument"), RuntimeException.class, formatWith2Args, arg1,
376 arg2);
377 fail("String.format with nullPointer should have thrown a nullPointerException");
378 }
379 catch (RuntimeException rte)
380 {
381 assertTrue("message cause is NullPointerException, instead got: " + rte.getCause().toString(),
382 rte.getCause().toString().contains("NullPointerException"));
383 assertTrue("message contains format", rte.getMessage().contains(formatWithoutArg));
384 assertTrue("message contains arg1", rte.getMessage().contains(arg1));
385 assertTrue("message contains arg2", rte.getMessage().contains(arg2));
386 }
387 assertEquals("Result has not changed", initialValueOfResult, result);
388
389 try
390 {
391 Try.execute(() -> String.format(nullPointer, "unused argument"), RuntimeException.class, formatWith3Args, arg1,
392 arg2, arg3);
393 fail("String.format with nullPointer should have thrown a nullPointerException");
394 }
395 catch (RuntimeException rte)
396 {
397 assertTrue("message cause is NullPointerException, instead got: " + rte.getCause().toString(),
398 rte.getCause().toString().contains("NullPointerException"));
399 assertTrue("message contains format", rte.getMessage().contains(formatWithoutArg));
400 assertTrue("message contains arg1", rte.getMessage().contains(arg1));
401 assertTrue("message contains arg2", rte.getMessage().contains(arg2));
402 assertTrue("message contains arg3", rte.getMessage().contains(arg3));
403 }
404 assertEquals("Result has not changed", initialValueOfResult, result);
405
406 try
407 {
408 Try.execute(() -> String.format(nullPointer, "unused argument"), RuntimeException.class, formatWith4Args, arg1,
409 arg2, arg3, arg4);
410 fail("String.format with nullPointer should have thrown a nullPointerException");
411 }
412 catch (RuntimeException rte)
413 {
414 assertTrue("message cause is NullPointerException, instead got: " + rte.getCause().toString(),
415 rte.getCause().toString().contains("NullPointerException"));
416 assertTrue("message contains format", rte.getMessage().contains(formatWithoutArg));
417 assertTrue("message contains arg1", rte.getMessage().contains(arg1));
418 assertTrue("message contains arg2", rte.getMessage().contains(arg2));
419 assertTrue("message contains arg3", rte.getMessage().contains(arg3));
420 assertTrue("message contains arg4", rte.getMessage().contains(arg4));
421 }
422 assertEquals("Result has not changed", initialValueOfResult, result);
423
424 try
425 {
426 Try.execute(() -> String.format(nullPointer, "unused argument"), formatWithoutArg);
427 fail("String.format with nullPointer should have thrown a nullPointerException");
428 }
429 catch (RuntimeException rte)
430 {
431 assertTrue("message cause is NullPointerException, instead got: " + rte.getCause().toString(),
432 rte.getCause().toString().contains("NullPointerException"));
433 assertTrue("message contains format", rte.getMessage().contains(formatWithoutArg));
434 }
435 assertEquals("Result has not changed", initialValueOfResult, result);
436
437 try
438 {
439 Try.execute(() -> String.format(nullPointer, "unused argument"), formatWithOneArg, arg1);
440 fail("String.format with nullPointer should have thrown a nullPointerException");
441 }
442 catch (RuntimeException rte)
443 {
444 assertTrue("message cause is NullPointerException, instead got: " + rte.getCause().toString(),
445 rte.getCause().toString().contains("NullPointerException"));
446 assertTrue("message contains format", rte.getMessage().contains(formatWithoutArg));
447 assertTrue("message contains arg1", rte.getMessage().contains(arg1));
448 }
449 assertEquals("Result has not changed", initialValueOfResult, result);
450
451 try
452 {
453 Try.execute(() -> String.format(nullPointer, "unused argument"), formatWith2Args, arg1, arg2);
454 fail("String.format with nullPointer should have thrown a nullPointerException");
455 }
456 catch (RuntimeException rte)
457 {
458 assertTrue("message cause is NullPointerException, instead got: " + rte.getCause().toString(),
459 rte.getCause().toString().contains("NullPointerException"));
460 assertTrue("message contains format", rte.getMessage().contains(formatWithoutArg));
461 assertTrue("message contains arg1", rte.getMessage().contains(arg1));
462 assertTrue("message contains arg2", rte.getMessage().contains(arg2));
463 }
464 assertEquals("Result has not changed", initialValueOfResult, result);
465
466 try
467 {
468 Try.execute(() -> String.format(nullPointer, "unused argument"), formatWith3Args, arg1, arg2, arg3);
469 fail("String.format with nullPointer should have thrown a nullPointerException");
470 }
471 catch (RuntimeException rte)
472 {
473 assertTrue("message cause is NullPointerException, instead got: " + rte.getCause().toString(),
474 rte.getCause().toString().contains("NullPointerException"));
475 assertTrue("message contains format", rte.getMessage().contains(formatWithoutArg));
476 assertTrue("message contains arg1", rte.getMessage().contains(arg1));
477 assertTrue("message contains arg2", rte.getMessage().contains(arg2));
478 assertTrue("message contains arg3", rte.getMessage().contains(arg3));
479 }
480 assertEquals("Result has not changed", initialValueOfResult, result);
481
482 try
483 {
484 Try.execute(() -> String.format(nullPointer, "unused argument"), formatWith4Args, arg1, arg2, arg3, arg4);
485 fail("String.format with nullPointer should have thrown a nullPointerException");
486 }
487 catch (RuntimeException rte)
488 {
489 assertTrue("message cause is NullPointerException, instead got: " + rte.getCause().toString(),
490 rte.getCause().toString().contains("NullPointerException"));
491 assertTrue("message contains format", rte.getMessage().contains(formatWithoutArg));
492 assertTrue("message contains arg1", rte.getMessage().contains(arg1));
493 assertTrue("message contains arg2", rte.getMessage().contains(arg2));
494 assertTrue("message contains arg3", rte.getMessage().contains(arg3));
495 assertTrue("message contains arg4", rte.getMessage().contains(arg4));
496 }
497 assertEquals("Result has not changed", initialValueOfResult, result);
498 }
499
500
501
502
503
504 @SuppressWarnings("checkstyle:methodlength")
505 @Test
506 public void trySucceedFailTestAssign() throws RuntimeException
507 {
508 String nullPointer = null;
509 String initialValueOfResult = "initial value of result";
510 String result = initialValueOfResult;
511 String formatWithoutArg = "format";
512 String arg1 = "arg1";
513 String formatWithOneArg = "format %s";
514
515
516
517 try
518 {
519 Try.testFail(() -> String.format(formatWithOneArg));
520 fail("testFail should have thrown an exception because no argument was given");
521 }
522 catch (Throwable e)
523 {
524
525 }
526
527 result = Try.testFail(() -> String.format(formatWithOneArg));
528 assertNull("Result has changed to null", result);
529 result = initialValueOfResult;
530
531 try
532 {
533 result = Try.testSucceed(() -> String.format(formatWithOneArg, arg1));
534 assertEquals("result should be changed", "format arg1", result);
535 }
536 catch (Throwable e)
537 {
538 fail("testSucceed should NOT have thrown an exception");
539 }
540 result = initialValueOfResult;
541
542 result = Try.testSucceed(() -> String.format(formatWithoutArg));
543 assertEquals("result should be changed", formatWithoutArg, result);
544 result = initialValueOfResult;
545
546
547
548 try
549 {
550 result = Try.testFail(() -> String.format(formatWithoutArg), NullPointerException.class);
551 fail("testFail should have thrown an exception because no NullPointerException was thrown in the assignment");
552 }
553 catch (Throwable e)
554 {
555
556 }
557 assertEquals("Result has not changed", initialValueOfResult, result);
558
559 result = Try.testFail(() -> String.format(nullPointer), NullPointerException.class);
560 assertNull("Result has changed to null", result);
561 result = initialValueOfResult;
562
563 try
564 {
565 result = Try.testSucceed(() -> String.format(formatWithOneArg, arg1), IllegalArgumentException.class);
566 assertEquals("result should be changed", "format arg1", result);
567 }
568 catch (Throwable e)
569 {
570 fail("testSucceed should NOT have thrown an exception");
571 }
572 result = initialValueOfResult;
573
574 result = Try.testSucceed(() -> String.format(formatWithoutArg), NullPointerException.class);
575 assertEquals("result should be changed", formatWithoutArg, result);
576 result = initialValueOfResult;
577
578
579
580 try
581 {
582 result = Try.testFail(() -> String.format(formatWithOneArg), "error");
583 fail("testFail should have thrown an exception because no arg was given");
584 }
585 catch (Throwable e)
586 {
587
588 }
589 assertNull("Result has changed to null", result);
590
591 result = Try.testFail(() -> String.format(formatWithOneArg), "error");
592 assertNull("Result has changed to null", result);
593 result = initialValueOfResult;
594
595 try
596 {
597 result = Try.testSucceed(() -> String.format(formatWithOneArg, arg1), "error");
598 assertEquals("result should be changed", "format arg1", result);
599 }
600 catch (Throwable e)
601 {
602 fail("testSucceed should NOT have thrown an exception");
603 }
604 result = initialValueOfResult;
605
606 result = Try.testSucceed(() -> String.format(formatWithOneArg, arg1), "error");
607 assertEquals("result should be changed", "format arg1", result);
608 result = initialValueOfResult;
609
610
611
612 try
613 {
614 result = Try.testFail(() -> String.format(formatWithOneArg), "error", NullPointerException.class);
615 fail("testFail should have thrown an exception because the wrong exception was given");
616 }
617 catch (Throwable e)
618 {
619
620 }
621 assertEquals("this.value should not be changed", initialValueOfResult, result);
622
623 result = Try.testFail(() -> String.format(formatWithOneArg), "error", IllegalFormatException.class);
624 assertNull("Result has changed to null", result);
625 result = initialValueOfResult;
626
627 try
628 {
629 result = Try.testSucceed(() -> String.format(formatWithOneArg, arg1), "error", IllegalFormatException.class);
630 assertEquals("result should be changed", "format arg1", result);
631 }
632 catch (Throwable e)
633 {
634 fail("testSucceed should NOT have thrown an exception");
635 }
636 result = initialValueOfResult;
637
638 try
639 {
640 result = Try.testSucceed(() -> String.format(formatWithOneArg), "error", NullPointerException.class);
641 fail("testSucceed should have thrown an exception because the wrong exception was given");
642 }
643 catch (Throwable e)
644 {
645
646 }
647 result = initialValueOfResult;
648
649 try
650 {
651 result = Try.testSucceed(() -> String.format(formatWithOneArg), "error", IllegalFormatException.class);
652 fail("testSucceed should have thrown an exception because the wrong number of arguments were provided");
653 }
654 catch (Throwable e)
655 {
656
657 }
658 assertNull("Result has changed to null", result);
659 result = initialValueOfResult;
660
661 result = Try.testSucceed(() -> String.format(formatWithOneArg, arg1), "error", IllegalArgumentException.class);
662 assertEquals("result should be changed", "format arg1", result);
663 result = initialValueOfResult;
664 }
665
666
667
668
669
670 @SuppressWarnings("checkstyle:methodlength")
671 @Test
672 public void trySucceedFailTestExecute() throws RuntimeException
673 {
674 String nullPointer = null;
675 String initialValueOfResult = "initial value of result";
676 String formatWithoutArg = "format";
677 String arg1 = "arg1";
678 String formatWithOneArg = "format %s";
679
680
681
682 this.value = initialValueOfResult;
683 try
684 {
685 Try.testFail(() -> setResult(String.format(formatWithOneArg)));
686 fail("testFail should have thrown an exception because no argument was given");
687 }
688 catch (Throwable e)
689 {
690
691 }
692 assertEquals("this.value should not be changed", initialValueOfResult, this.value);
693 this.value = initialValueOfResult;
694
695 Try.testFail(() -> setResult(String.format(formatWithOneArg)));
696 assertEquals("this.value should not be changed", initialValueOfResult, this.value);
697 this.value = initialValueOfResult;
698
699 try
700 {
701 Try.testSucceed(() -> setResult(String.format(formatWithOneArg, arg1)));
702 assertEquals("this.value should be changed", "format arg1", this.value);
703 }
704 catch (Throwable e)
705 {
706 fail("testSucceed should NOT have thrown an exception");
707 }
708 this.value = initialValueOfResult;
709
710 Try.testSucceed(() -> setResult(String.format(formatWithoutArg)));
711 assertEquals("this.value should be changed", formatWithoutArg, this.value);
712 this.value = initialValueOfResult;
713
714
715
716 try
717 {
718 Try.testFail(() -> setResult(String.format(formatWithoutArg)), NullPointerException.class);
719 fail("testFail should have thrown an exception because no NullPointerException was thrown in the assignment");
720 }
721 catch (Throwable e)
722 {
723
724 }
725 assertEquals("this.value should be changed", formatWithoutArg, this.value);
726 this.value = initialValueOfResult;
727
728 Try.testFail(() -> setResult(String.format(nullPointer)), NullPointerException.class);
729 assertEquals("this.value should not be changed", initialValueOfResult, this.value);
730 this.value = initialValueOfResult;
731
732 try
733 {
734 Try.testSucceed(() -> setResult(String.format(formatWithOneArg, arg1)), IllegalArgumentException.class);
735 assertEquals("this.value should be changed", "format arg1", this.value);
736 }
737 catch (Throwable e)
738 {
739 fail("testSucceed should NOT have thrown an exception");
740 }
741 this.value = initialValueOfResult;
742
743 Try.testSucceed(() -> setResult(String.format(formatWithoutArg)), NullPointerException.class);
744 assertEquals("this.value should be changed", formatWithoutArg, this.value);
745 this.value = initialValueOfResult;
746
747
748
749 try
750 {
751 Try.testFail(() -> setResult(String.format(formatWithOneArg)), "error");
752 fail("testFail should have thrown an exception because no arg was given");
753 }
754 catch (Throwable e)
755 {
756
757 }
758 assertEquals("this.value should not be changed", initialValueOfResult, this.value);
759 this.value = initialValueOfResult;
760
761 Try.testFail(() -> setResult(String.format(formatWithOneArg)), "error");
762 assertEquals("this.value should not be changed", initialValueOfResult, this.value);
763 this.value = initialValueOfResult;
764
765 try
766 {
767 Try.testSucceed(() -> setResult(String.format(formatWithOneArg, arg1)), "error");
768 assertEquals("this.value should be changed", "format arg1", this.value);
769 }
770 catch (Throwable e)
771 {
772 fail("testSucceed should NOT have thrown an exception");
773 }
774 this.value = initialValueOfResult;
775
776 Try.testSucceed(() -> setResult(String.format(formatWithOneArg, arg1)), "error");
777 assertEquals("this.value should be changed", "format arg1", this.value);
778 this.value = initialValueOfResult;
779
780
781
782 try
783 {
784 Try.testFail(() -> setResult(String.format(formatWithOneArg)), "error", NullPointerException.class);
785 fail("testFail should have thrown an exception because the wrong exception was given");
786 }
787 catch (Throwable e)
788 {
789
790 }
791 assertEquals("this.value should not be changed", initialValueOfResult, this.value);
792
793 Try.testFail(() -> setResult(String.format(formatWithOneArg)), "error", IllegalFormatException.class);
794 assertEquals("this.value should not be changed", initialValueOfResult, this.value);
795 this.value = initialValueOfResult;
796
797 try
798 {
799 Try.testSucceed(() -> setResult(String.format(formatWithOneArg, arg1)), "error", IllegalFormatException.class);
800 assertEquals("this.value should be changed", "format arg1", this.value);
801 }
802 catch (Throwable e)
803 {
804 fail("testSucceed should NOT have thrown an exception");
805 }
806 this.value = initialValueOfResult;
807
808 try
809 {
810 Try.testSucceed(() -> setResult(String.format(formatWithOneArg)), "error", NullPointerException.class);
811 fail("testSucceed should have thrown an exception because the wrong exception was given");
812 }
813 catch (Throwable e)
814 {
815
816 }
817 this.value = initialValueOfResult;
818
819 try
820 {
821 Try.testSucceed(() -> setResult(String.format(formatWithOneArg)), "error", IllegalFormatException.class);
822 fail("testSucceed should have thrown an exception because the wrong number of arguments were provided");
823 }
824 catch (Throwable e)
825 {
826
827 }
828 assertEquals("this.value should not be changed", initialValueOfResult, this.value);
829 this.value = initialValueOfResult;
830
831 Try.testSucceed(() -> setResult(String.format(formatWithOneArg, arg1)), "error", IllegalArgumentException.class);
832 assertEquals("this.value should be changed", "format arg1", this.value);
833 this.value = initialValueOfResult;
834 }
835
836
837
838
839
840 public static void main(final String[] args)
841 {
842 String nullPointer = null;
843 String arg1 = "arg1";
844 String arg2 = "arg2";
845 String arg3 = "arg3";
846 String arg4 = "arg4";
847 String formatWith4Args = "format %s %s %s %s";
848
849 for (int iteration = 1; true; iteration++)
850 {
851 System.out.println("Starting iteration " + iteration);
852 try
853 {
854 Try.execute(() -> String.format(nullPointer, "unused argument"), formatWith4Args, arg1, arg2, arg3, arg4);
855 fail("String.format with nullPointer should have thrown a nullPointerException");
856 }
857 catch (RuntimeException rte)
858 {
859 if (!rte.getCause().toString().contains("NullPointerException"))
860 {
861 System.out.println("Expected NullPointerException; got " + rte.getCause().toString());
862 }
863 assertTrue("message cause is NullPointerException, instead got: " + rte.getCause().toString(),
864 rte.getCause().toString().contains("NullPointerException"));
865 }
866 }
867 }
868
869 }