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.apache.juneau.TestUtils.assertThrowsWithMessage;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import java.util.function.*;
23  
24  import org.apache.juneau.*;
25  import org.junit.jupiter.api.*;
26  
27  class Function5_Test extends TestBase {
28  
29  	//------------------------------------------------------------------------------------------------------------------
30  	// Basic tests.
31  	//------------------------------------------------------------------------------------------------------------------
32  	@Test void a01_basic() {
33  		Function5<Integer,Integer,Integer,Integer,Integer,Integer> x = (a,b,c,d,e)->a+b+c+d+e;
34  		assertEquals(15, x.apply(1,2,3,4,5));
35  	}
36  
37  	@Test void a02_withNullValues() {
38  		Function5<String,Integer,Boolean,Double,Character,String> x = (a, b, c, d, e) -> a + b + c + d + e;
39  		assertEquals("null42true3.14X", x.apply(null, 42, true, 3.14, 'X'));
40  		assertEquals("foo0nullnullnull", x.apply("foo", 0, null, null, null));
41  		assertEquals("nullnullnullnullnull", x.apply(null, null, null, null, null));
42  	}
43  
44  	@Test void a03_returnsNull() {
45  		Function5<String,Integer,Boolean,Double,Character,String> x = (a, b, c, d, e) -> null;
46  		assertNull(x.apply("foo", 42, true, 3.14, 'X'));
47  	}
48  
49  	//------------------------------------------------------------------------------------------------------------------
50  	// andThen tests.
51  	//------------------------------------------------------------------------------------------------------------------
52  	@Test void b01_andThen_basic() {
53  		Function5<Integer,Integer,Integer,Integer,Integer,Integer> first = (a, b, c, d, e) -> a + b + c + d + e;
54  		Function<Integer,String> after = x -> "result: " + x;
55  		Function5<Integer,Integer,Integer,Integer,Integer,String> composed = first.andThen(after);
56  
57  		assertEquals("result: 15", composed.apply(1, 2, 3, 4, 5));
58  	}
59  
60  	@Test void b02_andThen_differentReturnType() {
61  		Function5<String,Integer,Boolean,Double,Character,Integer> first = (a, b, c, d, e) ->
62  			a.length() + b + (c ? 1 : 0) + (int)d.doubleValue() + (e != null ? 1 : 0);
63  		Function<Integer,Boolean> after = x -> x > 15;
64  		Function5<String,Integer,Boolean,Double,Character,Boolean> composed = first.andThen(after);
65  
66  		// 5 + 1 + 1 + 5 + 1 = 13 <= 15
67  		assertFalse(composed.apply("hello", 1, true, 5.0, 'X'));
68  		// 2 + 1 + 0 + 1 + 0 = 4 <= 15
69  		assertFalse(composed.apply("hi", 1, false, 1.0, null));
70  		// 10 + 5 + 1 + 10 + 1 = 27 > 15
71  		assertTrue(composed.apply("helloworld", 5, true, 10.0, 'Y'));
72  	}
73  
74  	@Test void b03_andThen_chaining() {
75  		Function5<Integer,Integer,Integer,Integer,Integer,Integer> first = (a, b, c, d, e) -> a + b + c + d + e;
76  		Function<Integer,Integer> second = x -> x * 2;
77  		Function<Integer,String> third = x -> "value: " + x;
78  		Function5<Integer,Integer,Integer,Integer,Integer,String> composed = first.andThen(second).andThen(third);
79  
80  		assertEquals("value: 30", composed.apply(1, 2, 3, 4, 5));  // (1+2+3+4+5)*2 = 30
81  	}
82  
83  	@Test void b04_andThen_withNullAfter() {
84  		Function5<Integer,Integer,Integer,Integer,Integer,Integer> first = (a, b, c, d, e) -> a + b + c + d + e;
85  		assertThrowsWithMessage(IllegalArgumentException.class, "Argument 'after' cannot be null", () -> {
86  			first.andThen(null);
87  		});
88  	}
89  
90  	@Test void b05_andThen_afterReturnsNull() {
91  		Function5<Integer,Integer,Integer,Integer,Integer,Integer> first = (a, b, c, d, e) -> a + b + c + d + e;
92  		Function<Integer,String> after = x -> null;
93  		Function5<Integer,Integer,Integer,Integer,Integer,String> composed = first.andThen(after);
94  
95  		assertNull(composed.apply(1, 2, 3, 4, 5));
96  	}
97  }