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 ThrowingFunction3_Test extends TestBase {
25  
26  	//------------------------------------------------------------------------------------------------------------------
27  	// Basic tests.
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  	// Exception handling tests.
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  	// Functional interface tests.
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