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 ThrowingConsumer3_Test extends TestBase {
27  
28  	//------------------------------------------------------------------------------------------------------------------
29  	// Basic tests.
30  	//------------------------------------------------------------------------------------------------------------------
31  	@Test void a01_basic() {
32  		var callCount = new AtomicInteger();
33  		var receivedValues = new Object[3];
34  
35  		var consumer = (ThrowingConsumer3<String, Integer, Boolean>)(a, b, c) -> {
36  			callCount.incrementAndGet();
37  			receivedValues[0] = a;
38  			receivedValues[1] = b;
39  			receivedValues[2] = c;
40  		};
41  
42  		consumer.apply("test", 42, true);
43  		assertEquals(1, callCount.get());
44  		assertEquals("test", receivedValues[0]);
45  		assertEquals(42, receivedValues[1]);
46  		assertEquals(true, receivedValues[2]);
47  	}
48  
49  	@Test void a02_withNullValues() {
50  		var callCount = new AtomicInteger();
51  		var receivedValues = new Object[3];
52  
53  		var consumer = (ThrowingConsumer3<String, Integer, Boolean>)(a, b, c) -> {
54  			callCount.incrementAndGet();
55  			receivedValues[0] = a;
56  			receivedValues[1] = b;
57  			receivedValues[2] = c;
58  		};
59  
60  		consumer.apply(null, null, null);
61  		assertEquals(1, callCount.get());
62  		assertNull(receivedValues[0]);
63  		assertNull(receivedValues[1]);
64  		assertNull(receivedValues[2]);
65  	}
66  
67  	//------------------------------------------------------------------------------------------------------------------
68  	// Exception handling tests.
69  	//------------------------------------------------------------------------------------------------------------------
70  	@Test void b01_throwsCheckedException() {
71  		var consumer = (ThrowingConsumer3<String, Integer, Boolean>)(a, b, c) -> {
72  			throw new Exception("Test exception");
73  		};
74  
75  		var ex = assertThrows(RuntimeException.class, () -> {
76  			consumer.apply("test", 42, true);
77  		});
78  
79  		assertTrue(ex.getCause() instanceof Exception);
80  		assertEquals("Test exception", ex.getCause().getMessage());
81  	}
82  
83  	@Test void b02_throwsRuntimeException() {
84  		var consumer = (ThrowingConsumer3<String, Integer, Boolean>)(a, b, c) -> {
85  			throw new RuntimeException("Test runtime exception");
86  		};
87  
88  		var ex = assertThrows(RuntimeException.class, () -> {
89  			consumer.apply("test", 42, true);
90  		});
91  
92  		assertEquals("Test runtime exception", ex.getMessage());
93  		assertNull(ex.getCause());
94  	}
95  
96  	//------------------------------------------------------------------------------------------------------------------
97  	// Functional interface tests.
98  	//------------------------------------------------------------------------------------------------------------------
99  	@Test void c01_usedAsConsumer3() {
100 		var callCount = new AtomicInteger();
101 
102 		var consumer = (Consumer3<String, Integer, Boolean>)(a, b, c) -> {
103 			callCount.incrementAndGet();
104 		};
105 
106 		consumer.apply("test", 42, true);
107 		assertEquals(1, callCount.get());
108 	}
109 
110 	@Test void c02_lambdaExpression() {
111 		var sum = new AtomicInteger(0);
112 
113 		ThrowingConsumer3<Integer, Integer, Integer> consumer = (a, b, c) -> {
114 			sum.addAndGet(a + b + c);
115 		};
116 
117 		consumer.apply(1, 2, 3);
118 		consumer.apply(4, 5, 6);
119 		assertEquals(21, sum.get());
120 	}
121 
122 	@Test void c03_andThen() {
123 		var callCount1 = new AtomicInteger();
124 		var callCount2 = new AtomicInteger();
125 
126 		ThrowingConsumer3<String, Integer, Boolean> consumer1 = (a, b, c) -> callCount1.incrementAndGet();
127 		ThrowingConsumer3<String, Integer, Boolean> consumer2 = (a, b, c) -> callCount2.incrementAndGet();
128 
129 		var composed = consumer1.andThen(consumer2);
130 		composed.apply("test", 42, true);
131 
132 		assertEquals(1, callCount1.get());
133 		assertEquals(1, callCount2.get());
134 	}
135 }
136