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 ThrowingFunction4_Test extends TestBase {
25  
26  	//------------------------------------------------------------------------------------------------------------------
27  	// Basic tests.
28  	//------------------------------------------------------------------------------------------------------------------
29  	@Test void a01_basic() {
30  		var function = (ThrowingFunction4<String, Integer, Boolean, Double, String>)(a, b, c, d) -> {
31  			return a + "-" + b + "-" + c + "-" + d;
32  		};
33  
34  		assertEquals("test-42-true-3.14", function.apply("test", 42, true, 3.14));
35  	}
36  
37  	@Test void a02_returnsNull() {
38  		ThrowingFunction4<String, Integer, Boolean, Double, String> function = (a, b, c, d) -> null;
39  
40  		assertNull(function.apply("test", 42, true, 3.14));
41  	}
42  
43  	//------------------------------------------------------------------------------------------------------------------
44  	// Exception handling tests.
45  	//------------------------------------------------------------------------------------------------------------------
46  	@Test void b01_throwsCheckedException() {
47  		var function = (ThrowingFunction4<String, Integer, Boolean, Double, String>)(a, b, c, d) -> {
48  			throw new Exception("Test exception");
49  		};
50  
51  		var ex = assertThrows(RuntimeException.class, () -> {
52  			function.apply("test", 42, true, 3.14);
53  		});
54  
55  		assertTrue(ex.getCause() instanceof Exception);
56  		assertEquals("Test exception", ex.getCause().getMessage());
57  	}
58  
59  	@Test void b02_throwsRuntimeException() {
60  		var function = (ThrowingFunction4<String, Integer, Boolean, Double, String>)(a, b, c, d) -> {
61  			throw new RuntimeException("Test runtime exception");
62  		};
63  
64  		var ex = assertThrows(RuntimeException.class, () -> {
65  			function.apply("test", 42, true, 3.14);
66  		});
67  
68  		assertEquals("Test runtime exception", ex.getMessage());
69  		assertNull(ex.getCause());
70  	}
71  
72  	//------------------------------------------------------------------------------------------------------------------
73  	// Functional interface tests.
74  	//------------------------------------------------------------------------------------------------------------------
75  	@Test void c01_usedAsFunction4() {
76  		var function = (Function4<String, Integer, Boolean, Double, String>)(a, b, c, d) -> a + "-" + b;
77  
78  		assertEquals("test-42", function.apply("test", 42, true, 3.14));
79  	}
80  
81  	@Test void c02_lambdaExpression() {
82  		ThrowingFunction4<Integer, Integer, Integer, Integer, Integer> function = (a, b, c, d) -> a + b + c + d;
83  
84  		assertEquals(10, function.apply(1, 2, 3, 4));
85  	}
86  
87  	@Test void c03_andThen() {
88  		var add = (ThrowingFunction4<Integer, Integer, Integer, Integer, Integer>)(a, b, c, d) -> a + b + c + d;
89  		var toString = (java.util.function.Function<Integer, String>)Object::toString;
90  
91  		var composed = add.andThen(toString);
92  		assertEquals("10", composed.apply(1, 2, 3, 4));
93  	}
94  }
95