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 java.util.concurrent.atomic.AtomicInteger;
22  
23  import org.apache.juneau.*;
24  import org.junit.jupiter.api.*;
25  
26  class Consumer4_Test extends TestBase {
27  
28  	//------------------------------------------------------------------------------------------------------------------
29  	// Basic tests.
30  	//------------------------------------------------------------------------------------------------------------------
31  	@Test void a01_basic() {
32  		var callCount = new AtomicInteger();
33  		var receivedA = new StringBuilder();
34  		var receivedB = new StringBuilder();
35  		var receivedC = new StringBuilder();
36  		var receivedD = new StringBuilder();
37  
38  		Consumer4<String,Integer,Boolean,Double> x = (a, b, c, d) -> {
39  			callCount.incrementAndGet();
40  			receivedA.append(a);
41  			receivedB.append(b);
42  			receivedC.append(c);
43  			receivedD.append(d);
44  		};
45  
46  		x.apply("foo", 42, true, 3.14);
47  		assertEquals(1, callCount.get());
48  		assertEquals("foo", receivedA.toString());
49  		assertEquals("42", receivedB.toString());
50  		assertEquals("true", receivedC.toString());
51  		assertEquals("3.14", receivedD.toString());
52  	}
53  
54  	@Test void a02_withNullValues() {
55  		var callCount = new AtomicInteger();
56  		var receivedA = new Object[1];
57  		var receivedB = new Object[1];
58  		var receivedC = new Object[1];
59  		var receivedD = new Object[1];
60  
61  		Consumer4<String,Integer,Boolean,Double> x = (a, b, c, d) -> {
62  			callCount.incrementAndGet();
63  			receivedA[0] = a;
64  			receivedB[0] = b;
65  			receivedC[0] = c;
66  			receivedD[0] = d;
67  		};
68  
69  		x.apply(null, null, null, null);
70  		assertEquals(1, callCount.get());
71  		assertNull(receivedA[0]);
72  		assertNull(receivedB[0]);
73  		assertNull(receivedC[0]);
74  		assertNull(receivedD[0]);
75  	}
76  
77  	//------------------------------------------------------------------------------------------------------------------
78  	// andThen tests.
79  	//------------------------------------------------------------------------------------------------------------------
80  	@Test void b01_andThen_bothCalled() {
81  		var firstCallCount = new AtomicInteger();
82  		var secondCallCount = new AtomicInteger();
83  
84  		Consumer4<String,Integer,Boolean,Double> first = (a, b, c, d) -> firstCallCount.incrementAndGet();
85  		Consumer4<String,Integer,Boolean,Double> second = (a, b, c, d) -> secondCallCount.incrementAndGet();
86  
87  		var composed = first.andThen(second);
88  		composed.apply("test", 123, false, 2.5);
89  
90  		assertEquals(1, firstCallCount.get());
91  		assertEquals(1, secondCallCount.get());
92  	}
93  
94  	@Test void b02_andThen_calledInOrder() {
95  		var callOrder = new StringBuilder();
96  
97  		Consumer4<String,Integer,Boolean,Double> first = (a, b, c, d) -> callOrder.append("first");
98  		Consumer4<String,Integer,Boolean,Double> second = (a, b, c, d) -> callOrder.append("second");
99  
100 		var composed = first.andThen(second);
101 		composed.apply("test", 123, false, 2.5);
102 
103 		assertEquals("firstsecond", callOrder.toString());
104 	}
105 
106 	@Test void b03_andThen_sameArgumentsPassed() {
107 		var firstArgs = new Object[4];
108 		var secondArgs = new Object[4];
109 
110 		Consumer4<String,Integer,Boolean,Double> first = (a, b, c, d) -> {
111 			firstArgs[0] = a;
112 			firstArgs[1] = b;
113 			firstArgs[2] = c;
114 			firstArgs[3] = d;
115 		};
116 		Consumer4<String,Integer,Boolean,Double> second = (a, b, c, d) -> {
117 			secondArgs[0] = a;
118 			secondArgs[1] = b;
119 			secondArgs[2] = c;
120 			secondArgs[3] = d;
121 		};
122 
123 		var composed = first.andThen(second);
124 		composed.apply("foo", 42, true, 3.14);
125 
126 		assertEquals("foo", firstArgs[0]);
127 		assertEquals(42, firstArgs[1]);
128 		assertEquals(true, firstArgs[2]);
129 		assertEquals(3.14, firstArgs[3]);
130 		assertEquals("foo", secondArgs[0]);
131 		assertEquals(42, secondArgs[1]);
132 		assertEquals(true, secondArgs[2]);
133 		assertEquals(3.14, secondArgs[3]);
134 	}
135 
136 	@Test void b04_andThen_chaining() {
137 		var callCount = new AtomicInteger();
138 
139 		Consumer4<String,Integer,Boolean,Double> first = (a, b, c, d) -> callCount.addAndGet(1);
140 		Consumer4<String,Integer,Boolean,Double> second = (a, b, c, d) -> callCount.addAndGet(10);
141 		Consumer4<String,Integer,Boolean,Double> third = (a, b, c, d) -> callCount.addAndGet(100);
142 
143 		var composed = first.andThen(second).andThen(third);
144 		composed.apply("test", 123, false, 2.5);
145 
146 		assertEquals(111, callCount.get());
147 	}
148 }
149