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 ThrowingFunction_Test extends TestBase {
25
26
27
28
29 @Test void a01_basic() {
30 ThrowingFunction<String,Integer> function = (t) -> t.length();
31
32 assertEquals(5, function.apply("hello"));
33 assertEquals(0, function.apply(""));
34 }
35
36 @Test void a02_withNullValue() {
37 ThrowingFunction<String,Integer> function = (t) -> t == null ? 0 : t.length();
38
39 assertEquals(0, function.apply(null));
40 assertEquals(5, function.apply("hello"));
41 }
42
43 @Test void a03_returnsNull() {
44 ThrowingFunction<String,String> function = (t) -> null;
45
46 assertNull(function.apply("test"));
47 }
48
49
50
51
52 @Test void b01_throwsCheckedException() {
53 ThrowingFunction<String,String> function = (t) -> {
54 throw new Exception("Test exception");
55 };
56
57 RuntimeException ex = assertThrows(RuntimeException.class, () -> {
58 function.apply("test");
59 });
60
61 assertTrue(ex.getCause() instanceof Exception);
62 assertEquals("Test exception", ex.getCause().getMessage());
63 }
64
65 @Test void b02_throwsRuntimeException() {
66 ThrowingFunction<String,String> function = (t) -> {
67 throw new RuntimeException("Test runtime exception");
68 };
69
70 RuntimeException ex = assertThrows(RuntimeException.class, () -> {
71 function.apply("test");
72 });
73
74
75 assertEquals("Test runtime exception", ex.getMessage());
76 assertNull(ex.getCause());
77 }
78
79 @Test void b03_throwsError() {
80 ThrowingFunction<String,String> function = (t) -> {
81 throw new Error("Test error");
82 };
83
84
85 Error ex = assertThrows(Error.class, () -> {
86 function.apply("test");
87 });
88
89 assertEquals("Test error", ex.getMessage());
90 }
91
92 @Test void b04_throwsNullPointerException() {
93 ThrowingFunction<String,String> function = (t) -> {
94 throw new NullPointerException("NPE");
95 };
96
97 RuntimeException ex = assertThrows(RuntimeException.class, () -> {
98 function.apply("test");
99 });
100
101
102 assertTrue(ex instanceof NullPointerException);
103 assertEquals("NPE", ex.getMessage());
104 }
105
106 @Test void b05_throwsIllegalArgumentException() {
107 ThrowingFunction<String,String> function = (t) -> {
108 throw new IllegalArgumentException("IAE");
109 };
110
111 RuntimeException ex = assertThrows(RuntimeException.class, () -> {
112 function.apply("test");
113 });
114
115
116 assertTrue(ex instanceof IllegalArgumentException);
117 assertEquals("IAE", ex.getMessage());
118 }
119
120
121
122
123 @Test void c01_usedAsFunction() {
124
125 java.util.function.Function<String,Integer> function = (t) -> t.length();
126
127 assertEquals(5, function.apply("hello"));
128 }
129
130 @Test void c02_lambdaExpression() {
131 ThrowingFunction<Integer,Integer> function = (x) -> x * 2;
132
133 assertEquals(10, function.apply(5));
134 assertEquals(0, function.apply(0));
135 assertEquals(-6, function.apply(-3));
136 }
137
138 @Test void c03_methodReference() {
139 ThrowingFunction<String,Integer> function = String::length;
140
141 assertEquals(5, function.apply("hello"));
142 assertEquals(0, function.apply(""));
143 }
144
145 @Test void c04_compose() {
146 ThrowingFunction<Integer,Integer> multiply = (x) -> x * 2;
147 java.util.function.Function<Integer,Integer> add = (x) -> x + 1;
148
149
150
151 var composed = multiply.compose(add);
152 assertEquals(12, composed.apply(5));
153 }
154
155 @Test void c05_andThen() {
156 ThrowingFunction<Integer,Integer> multiply = (x) -> x * 2;
157 java.util.function.Function<Integer,Integer> add = (x) -> x + 1;
158
159
160 var composed = multiply.andThen(add);
161 assertEquals(11, composed.apply(5));
162 }
163
164 @Test void c06_identity() {
165 ThrowingFunction<String,String> identity = (t) -> t;
166
167 assertEquals("test", identity.apply("test"));
168 assertNull(identity.apply(null));
169 }
170 }
171