1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juneau.commons.function;
18
19 import static org.junit.jupiter.api.Assertions.*;
20
21 import org.apache.juneau.*;
22 import org.junit.jupiter.api.*;
23
24 class ThrowingFunction3_Test extends TestBase {
25
26
27
28
29 @Test void a01_basic() {
30 var function = (ThrowingFunction3<String, Integer, Boolean, String>)(a, b, c) -> {
31 return a + "-" + b + "-" + (c ? "Y" : "N");
32 };
33
34 assertEquals("test-42-Y", function.apply("test", 42, true));
35 assertEquals("hello-0-N", function.apply("hello", 0, false));
36 }
37
38 @Test void a02_returnsNull() {
39 ThrowingFunction3<String, Integer, Boolean, String> function = (a, b, c) -> null;
40
41 assertNull(function.apply("test", 42, true));
42 }
43
44
45
46
47 @Test void b01_throwsCheckedException() {
48 var function = (ThrowingFunction3<String, Integer, Boolean, String>)(a, b, c) -> {
49 throw new Exception("Test exception");
50 };
51
52 var ex = assertThrows(RuntimeException.class, () -> {
53 function.apply("test", 42, true);
54 });
55
56 assertTrue(ex.getCause() instanceof Exception);
57 assertEquals("Test exception", ex.getCause().getMessage());
58 }
59
60 @Test void b02_throwsRuntimeException() {
61 var function = (ThrowingFunction3<String, Integer, Boolean, String>)(a, b, c) -> {
62 throw new RuntimeException("Test runtime exception");
63 };
64
65 var ex = assertThrows(RuntimeException.class, () -> {
66 function.apply("test", 42, true);
67 });
68
69 assertEquals("Test runtime exception", ex.getMessage());
70 assertNull(ex.getCause());
71 }
72
73
74
75
76 @Test void c01_usedAsFunction3() {
77 var function = (Function3<String, Integer, Boolean, String>)(a, b, c) -> a + "-" + b + "-" + c;
78
79 assertEquals("test-42-true", function.apply("test", 42, true));
80 }
81
82 @Test void c02_lambdaExpression() {
83 ThrowingFunction3<Integer, Integer, Integer, Integer> function = (a, b, c) -> a + b + c;
84
85 assertEquals(6, function.apply(1, 2, 3));
86 assertEquals(0, function.apply(0, 0, 0));
87 }
88
89 @Test void c03_andThen() {
90 var add = (ThrowingFunction3<Integer, Integer, Integer, Integer>)(a, b, c) -> a + b + c;
91 var toString = (java.util.function.Function<Integer, String>)Object::toString;
92
93 var composed = add.andThen(toString);
94 assertEquals("6", composed.apply(1, 2, 3));
95 }
96 }
97