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 Function2_Test extends TestBase {
28  
29  	//------------------------------------------------------------------------------------------------------------------
30  	// Basic tests.
31  	//------------------------------------------------------------------------------------------------------------------
32  	@Test void a01_basic() {
33  		Function2<Integer,Integer,Integer> x = (a,b)->a+b;
34  		assertEquals(3, x.apply(1,2));
35  	}
36  
37  	@Test void a02_withNullValues() {
38  		Function2<String,Integer,String> x = (a, b) -> a + b;
39  		assertEquals("null42", x.apply(null, 42));
40  		assertEquals("foonull", x.apply("foo", null));  // String concatenation: "foo" + null = "foonull"
41  		assertEquals("nullnull", x.apply(null, null));
42  	}
43  
44  	@Test void a03_returnsNull() {
45  		Function2<String,Integer,String> x = (a, b) -> null;
46  		assertNull(x.apply("foo", 42));
47  	}
48  
49  	//------------------------------------------------------------------------------------------------------------------
50  	// andThen tests.
51  	//------------------------------------------------------------------------------------------------------------------
52  	@Test void b01_andThen_basic() {
53  		Function2<Integer,Integer,Integer> first = (a, b) -> a + b;
54  		Function<Integer,String> after = x -> "result: " + x;
55  		Function2<Integer,Integer,String> composed = first.andThen(after);
56  
57  		assertEquals("result: 3", composed.apply(1, 2));
58  	}
59  
60  	@Test void b02_andThen_differentReturnType() {
61  		Function2<String,Integer,Integer> first = (a, b) -> a.length() + b;
62  		Function<Integer,Boolean> after = x -> x > 5;
63  		Function2<String,Integer,Boolean> composed = first.andThen(after);
64  
65  		assertTrue(composed.apply("hello", 1));  // 5 + 1 = 6 > 5
66  		assertFalse(composed.apply("hi", 1));    // 2 + 1 = 3 <= 5
67  	}
68  
69  	@Test void b03_andThen_chaining() {
70  		Function2<Integer,Integer,Integer> first = (a, b) -> a + b;
71  		Function<Integer,Integer> second = x -> x * 2;
72  		Function<Integer,String> third = x -> "value: " + x;
73  		Function2<Integer,Integer,String> composed = first.andThen(second).andThen(third);
74  
75  		assertEquals("value: 6", composed.apply(1, 2));  // (1+2)*2 = 6
76  	}
77  
78  	@Test void b04_andThen_withNullAfter() {
79  		Function2<Integer,Integer,Integer> first = (a, b) -> a + b;
80  		assertThrowsWithMessage(IllegalArgumentException.class, "Argument 'after' cannot be null", () -> {
81  			first.andThen(null);
82  		});
83  	}
84  
85  	@Test void b05_andThen_afterReturnsNull() {
86  		Function2<Integer,Integer,Integer> first = (a, b) -> a + b;
87  		Function<Integer,String> after = x -> null;
88  		Function2<Integer,Integer,String> composed = first.andThen(after);
89  
90  		assertNull(composed.apply(1, 2));
91  	}
92  }