View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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  	// Basic tests.
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  	// Exception handling tests.
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  		// RuntimeExceptions should be re-thrown as-is (not wrapped)
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  		// Error is not an Exception, so it's not caught and propagates directly
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 		// NullPointerException is a RuntimeException, so it should be re-thrown as-is
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 		// IllegalArgumentException is a RuntimeException, so it should be re-thrown as-is
116 		assertTrue(ex instanceof IllegalArgumentException);
117 		assertEquals("IAE", ex.getMessage());
118 	}
119 
120 	//------------------------------------------------------------------------------------------------------------------
121 	// Functional interface tests.
122 	//------------------------------------------------------------------------------------------------------------------
123 	@Test void c01_usedAsFunction() {
124 		// ThrowingFunction extends Function, so it can be used where Function is expected
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 		// Compose: multiply.apply(add.apply(5)) = multiply.apply(6) = 12
150 		// compose applies the argument function first, then this function
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 		// AndThen: add.apply(multiply.apply(5)) = add.apply(10) = 11
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