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