1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 Function4_Test extends TestBase {
28
29
30
31
32 @Test void a01_basic() {
33 Function4<Integer,Integer,Integer,Integer,Integer> x = (a,b,c,d)->a+b+c+d;
34 assertEquals(10, x.apply(1,2,3,4));
35 }
36
37 @Test void a02_withNullValues() {
38 Function4<String,Integer,Boolean,Double,String> x = (a, b, c, d) -> a + b + c + d;
39 assertEquals("null42true3.14", x.apply(null, 42, true, 3.14));
40 assertEquals("foo0nullnull", x.apply("foo", 0, null, null));
41 assertEquals("nullnullnullnull", x.apply(null, null, null, null));
42 }
43
44 @Test void a03_returnsNull() {
45 Function4<String,Integer,Boolean,Double,String> x = (a, b, c, d) -> null;
46 assertNull(x.apply("foo", 42, true, 3.14));
47 }
48
49
50
51
52 @Test void b01_andThen_basic() {
53 Function4<Integer,Integer,Integer,Integer,Integer> first = (a, b, c, d) -> a + b + c + d;
54 Function<Integer,String> after = x -> "result: " + x;
55 Function4<Integer,Integer,Integer,Integer,String> composed = first.andThen(after);
56
57 assertEquals("result: 10", composed.apply(1, 2, 3, 4));
58 }
59
60 @Test void b02_andThen_differentReturnType() {
61 Function4<String,Integer,Boolean,Double,Integer> first = (a, b, c, d) -> a.length() + b + (c ? 1 : 0) + (int)d.doubleValue();
62 Function<Integer,Boolean> after = x -> x > 10;
63 Function4<String,Integer,Boolean,Double,Boolean> composed = first.andThen(after);
64
65 assertTrue(composed.apply("hello", 1, true, 5.0));
66 assertFalse(composed.apply("hi", 1, false, 1.0));
67 }
68
69 @Test void b03_andThen_chaining() {
70 Function4<Integer,Integer,Integer,Integer,Integer> first = (a, b, c, d) -> a + b + c + d;
71 Function<Integer,Integer> second = x -> x * 2;
72 Function<Integer,String> third = x -> "value: " + x;
73 Function4<Integer,Integer,Integer,Integer,String> composed = first.andThen(second).andThen(third);
74
75 assertEquals("value: 20", composed.apply(1, 2, 3, 4));
76 }
77
78 @Test void b04_andThen_withNullAfter() {
79 Function4<Integer,Integer,Integer,Integer,Integer> first = (a, b, c, d) -> a + b + c + d;
80 assertThrowsWithMessage(IllegalArgumentException.class, "Argument 'after' cannot be null", () -> {
81 first.andThen(null);
82 });
83 }
84
85 @Test void b05_andThen_afterReturnsNull() {
86 Function4<Integer,Integer,Integer,Integer,Integer> first = (a, b, c, d) -> a + b + c + d;
87 Function<Integer,String> after = x -> null;
88 Function4<Integer,Integer,Integer,Integer,String> composed = first.andThen(after);
89
90 assertNull(composed.apply(1, 2, 3, 4));
91 }
92 }