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 ThrowingConsumer_Test extends TestBase {
27  
28  	//------------------------------------------------------------------------------------------------------------------
29  	// Basic tests.
30  	//------------------------------------------------------------------------------------------------------------------
31  	@Test void a01_basic() {
32  		var callCount = new AtomicInteger();
33  		var receivedValue = new Object[1];
34  
35  		ThrowingConsumer<String> consumer = (t) -> {
36  			callCount.incrementAndGet();
37  			receivedValue[0] = t;
38  		};
39  
40  		consumer.accept("test");
41  		assertEquals(1, callCount.get());
42  		assertEquals("test", receivedValue[0]);
43  	}
44  
45  	@Test void a02_withNullValue() {
46  		var callCount = new AtomicInteger();
47  		var receivedValue = new Object[1];
48  
49  		ThrowingConsumer<String> consumer = (t) -> {
50  			callCount.incrementAndGet();
51  			receivedValue[0] = t;
52  		};
53  
54  		consumer.accept(null);
55  		assertEquals(1, callCount.get());
56  		assertNull(receivedValue[0]);
57  	}
58  
59  	//------------------------------------------------------------------------------------------------------------------
60  	// Exception handling tests.
61  	//------------------------------------------------------------------------------------------------------------------
62  	@Test void b01_throwsCheckedException() {
63  		ThrowingConsumer<String> consumer = (t) -> {
64  			throw new Exception("Test exception");
65  		};
66  
67  		RuntimeException ex = assertThrows(RuntimeException.class, () -> {
68  			consumer.accept("test");
69  		});
70  
71  		assertTrue(ex.getCause() instanceof Exception);
72  		assertEquals("Test exception", ex.getCause().getMessage());
73  	}
74  
75  	@Test void b02_throwsRuntimeException() {
76  		ThrowingConsumer<String> consumer = (t) -> {
77  			throw new RuntimeException("Test runtime exception");
78  		};
79  
80  		RuntimeException ex = assertThrows(RuntimeException.class, () -> {
81  			consumer.accept("test");
82  		});
83  
84  		// RuntimeExceptions should be re-thrown as-is (not wrapped)
85  		assertEquals("Test runtime exception", ex.getMessage());
86  		assertNull(ex.getCause());
87  	}
88  
89  	@Test void b03_throwsError() {
90  		ThrowingConsumer<String> consumer = (t) -> {
91  			throw new Error("Test error");
92  		};
93  
94  		// Error is not an Exception, so it's not caught and propagates directly
95  		Error ex = assertThrows(Error.class, () -> {
96  			consumer.accept("test");
97  		});
98  
99  		assertEquals("Test error", ex.getMessage());
100 	}
101 
102 	@Test void b04_throwsNullPointerException() {
103 		ThrowingConsumer<String> consumer = (t) -> {
104 			throw new NullPointerException("NPE");
105 		};
106 
107 		RuntimeException ex = assertThrows(RuntimeException.class, () -> {
108 			consumer.accept("test");
109 		});
110 
111 		// NullPointerException is a RuntimeException, so it should be re-thrown as-is
112 		assertTrue(ex instanceof NullPointerException);
113 		assertEquals("NPE", ex.getMessage());
114 	}
115 
116 	@Test void b05_throwsIllegalArgumentException() {
117 		ThrowingConsumer<String> consumer = (t) -> {
118 			throw new IllegalArgumentException("IAE");
119 		};
120 
121 		RuntimeException ex = assertThrows(RuntimeException.class, () -> {
122 			consumer.accept("test");
123 		});
124 
125 		// IllegalArgumentException is a RuntimeException, so it should be re-thrown as-is
126 		assertTrue(ex instanceof IllegalArgumentException);
127 		assertEquals("IAE", ex.getMessage());
128 	}
129 
130 	//------------------------------------------------------------------------------------------------------------------
131 	// Functional interface tests.
132 	//------------------------------------------------------------------------------------------------------------------
133 	@Test void c01_usedAsConsumer() {
134 		var callCount = new AtomicInteger();
135 		var receivedValue = new Object[1];
136 
137 		// ThrowingConsumer extends Consumer, so it can be used where Consumer is expected
138 		java.util.function.Consumer<String> consumer = (t) -> {
139 			callCount.incrementAndGet();
140 			receivedValue[0] = t;
141 		};
142 
143 		consumer.accept("test");
144 		assertEquals(1, callCount.get());
145 		assertEquals("test", receivedValue[0]);
146 	}
147 
148 	@Test void c02_lambdaExpression() {
149 		var callCount = new AtomicInteger();
150 
151 		ThrowingConsumer<Integer> consumer = (x) -> {
152 			callCount.addAndGet(x);
153 		};
154 
155 		consumer.accept(5);
156 		consumer.accept(10);
157 		assertEquals(15, callCount.get());
158 	}
159 
160 	@Test void c03_methodReference() {
161 		var callCount = new AtomicInteger();
162 
163 		class Counter {
164 			void increment(int value) {
165 				callCount.addAndGet(value);
166 			}
167 		}
168 
169 		Counter counter = new Counter();
170 		ThrowingConsumer<Integer> consumer = counter::increment;
171 
172 		consumer.accept(7);
173 		assertEquals(7, callCount.get());
174 	}
175 }
176