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