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