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 ThrowingFunction2_Test extends TestBase {
25
26
27
28
29 @Test void a01_basic() {
30 var function = (ThrowingFunction2<String, Integer, String>)(a, b) -> a + "-" + b;
31
32 assertEquals("test-42", function.apply("test", 42));
33 assertEquals("hello-0", function.apply("hello", 0));
34 }
35
36 @Test void a02_withNullValues() {
37 var function = (ThrowingFunction2<String, Integer, String>)(a, b) -> {
38 String aStr = a == null ? "null" : a;
39 String bStr = b == null ? "null" : String.valueOf(b);
40 return aStr + "-" + bStr;
41 };
42
43 assertEquals("null-42", function.apply(null, 42));
44 assertEquals("test-null", function.apply("test", null));
45 }
46
47 @Test void a03_returnsNull() {
48 var function = (ThrowingFunction2<String, Integer, String>)(a, b) -> null;
49
50 assertNull(function.apply("test", 42));
51 }
52
53
54
55
56 @Test void b01_throwsCheckedException() {
57 var function = (ThrowingFunction2<String, Integer, String>)(a, b) -> {
58 throw new Exception("Test exception");
59 };
60
61 var ex = assertThrows(RuntimeException.class, () -> {
62 function.apply("test", 42);
63 });
64
65 assertTrue(ex.getCause() instanceof Exception);
66 assertEquals("Test exception", ex.getCause().getMessage());
67 }
68
69 @Test void b02_throwsRuntimeException() {
70 var function = (ThrowingFunction2<String, Integer, String>)(a, b) -> {
71 throw new RuntimeException("Test runtime exception");
72 };
73
74 var ex = assertThrows(RuntimeException.class, () -> {
75 function.apply("test", 42);
76 });
77
78 assertEquals("Test runtime exception", ex.getMessage());
79 assertNull(ex.getCause());
80 }
81
82 @Test void b03_throwsError() {
83 var function = (ThrowingFunction2<String, Integer, String>)(a, b) -> {
84 throw new Error("Test error");
85 };
86
87 var ex = assertThrows(Error.class, () -> {
88 function.apply("test", 42);
89 });
90
91 assertEquals("Test error", ex.getMessage());
92 }
93
94
95
96
97 @Test void c01_usedAsFunction2() {
98 var function = (Function2<String, Integer, String>)(a, b) -> a + "-" + b;
99
100 assertEquals("test-42", function.apply("test", 42));
101 }
102
103 @Test void c02_lambdaExpression() {
104 var function = (ThrowingFunction2<Integer, Integer, Integer>)(a, b) -> a + b;
105
106 assertEquals(5, function.apply(2, 3));
107 assertEquals(0, function.apply(0, 0));
108 assertEquals(-1, function.apply(-3, 2));
109 }
110
111 @Test void c03_methodReference() {
112 var function = (ThrowingFunction2<String, String, Boolean>)String::equals;
113
114 assertTrue(function.apply("test", "test"));
115 assertFalse(function.apply("test", "other"));
116 }
117
118 @Test void c04_andThen() {
119 var add = (ThrowingFunction2<Integer, Integer, Integer>)(a, b) -> a + b;
120 var toString = (java.util.function.Function<Integer, String>)Object::toString;
121
122 var composed = add.andThen(toString);
123 assertEquals("5", composed.apply(2, 3));
124 }
125 }
126