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 Consumer3_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  
37  		Consumer3<String,Integer,Boolean> x = (a, b, c) -> {
38  			callCount.incrementAndGet();
39  			receivedA.append(a);
40  			receivedB.append(b);
41  			receivedC.append(c);
42  		};
43  
44  		x.apply("foo", 42, true);
45  		assertEquals(1, callCount.get());
46  		assertEquals("foo", receivedA.toString());
47  		assertEquals("42", receivedB.toString());
48  		assertEquals("true", receivedC.toString());
49  	}
50  
51  	@Test void a02_withNullValues() {
52  		var callCount = new AtomicInteger();
53  		var receivedA = new Object[1];
54  		var receivedB = new Object[1];
55  		var receivedC = new Object[1];
56  
57  		Consumer3<String,Integer,Boolean> x = (a, b, c) -> {
58  			callCount.incrementAndGet();
59  			receivedA[0] = a;
60  			receivedB[0] = b;
61  			receivedC[0] = c;
62  		};
63  
64  		x.apply(null, null, null);
65  		assertEquals(1, callCount.get());
66  		assertNull(receivedA[0]);
67  		assertNull(receivedB[0]);
68  		assertNull(receivedC[0]);
69  	}
70  
71  	//------------------------------------------------------------------------------------------------------------------
72  	// andThen tests.
73  	//------------------------------------------------------------------------------------------------------------------
74  	@Test void b01_andThen_bothCalled() {
75  		var firstCallCount = new AtomicInteger();
76  		var secondCallCount = new AtomicInteger();
77  
78  		Consumer3<String,Integer,Boolean> first = (a, b, c) -> firstCallCount.incrementAndGet();
79  		Consumer3<String,Integer,Boolean> second = (a, b, c) -> secondCallCount.incrementAndGet();
80  
81  		var composed = first.andThen(second);
82  		composed.apply("test", 123, false);
83  
84  		assertEquals(1, firstCallCount.get());
85  		assertEquals(1, secondCallCount.get());
86  	}
87  
88  	@Test void b02_andThen_calledInOrder() {
89  		var callOrder = new StringBuilder();
90  
91  		Consumer3<String,Integer,Boolean> first = (a, b, c) -> callOrder.append("first");
92  		Consumer3<String,Integer,Boolean> second = (a, b, c) -> callOrder.append("second");
93  
94  		var composed = first.andThen(second);
95  		composed.apply("test", 123, false);
96  
97  		assertEquals("firstsecond", callOrder.toString());
98  	}
99  
100 	@Test void b03_andThen_sameArgumentsPassed() {
101 		var firstArgs = new Object[3];
102 		var secondArgs = new Object[3];
103 
104 		Consumer3<String,Integer,Boolean> first = (a, b, c) -> {
105 			firstArgs[0] = a;
106 			firstArgs[1] = b;
107 			firstArgs[2] = c;
108 		};
109 		Consumer3<String,Integer,Boolean> second = (a, b, c) -> {
110 			secondArgs[0] = a;
111 			secondArgs[1] = b;
112 			secondArgs[2] = c;
113 		};
114 
115 		var composed = first.andThen(second);
116 		composed.apply("foo", 42, true);
117 
118 		assertEquals("foo", firstArgs[0]);
119 		assertEquals(42, firstArgs[1]);
120 		assertEquals(true, firstArgs[2]);
121 		assertEquals("foo", secondArgs[0]);
122 		assertEquals(42, secondArgs[1]);
123 		assertEquals(true, secondArgs[2]);
124 	}
125 
126 	@Test void b04_andThen_chaining() {
127 		var callCount = new AtomicInteger();
128 
129 		Consumer3<String,Integer,Boolean> first = (a, b, c) -> callCount.addAndGet(1);
130 		Consumer3<String,Integer,Boolean> second = (a, b, c) -> callCount.addAndGet(10);
131 		Consumer3<String,Integer,Boolean> third = (a, b, c) -> callCount.addAndGet(100);
132 
133 		var composed = first.andThen(second).andThen(third);
134 		composed.apply("test", 123, false);
135 
136 		assertEquals(111, callCount.get());
137 	}
138 }
139