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.Optional;
22  import java.util.concurrent.atomic.AtomicInteger;
23  
24  import org.apache.juneau.*;
25  import org.junit.jupiter.api.*;
26  
27  class OptionalSupplier_Test extends TestBase {
28  
29  	//====================================================================================================
30  	// Static factory methods
31  	//====================================================================================================
32  	@Test
33  	void a01_of() {
34  		AtomicInteger callCount = new AtomicInteger();
35  		OptionalSupplier<String> supplier = OptionalSupplier.of(() -> {
36  			callCount.incrementAndGet();
37  			return "value";
38  		});
39  
40  		assertEquals("value", supplier.get());
41  		assertEquals(1, callCount.get());
42  		assertEquals("value", supplier.get());
43  		assertEquals(2, callCount.get()); // Called again (not cached)
44  	}
45  
46  	@Test
47  	void a02_of_nullSupplier() {
48  		assertThrows(IllegalArgumentException.class, () -> OptionalSupplier.of(null));
49  	}
50  
51  	@Test
52  	void a03_ofNullable() {
53  		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable("value");
54  		assertEquals("value", supplier.get());
55  	}
56  
57  	@Test
58  	void a04_ofNullable_null() {
59  		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable(null);
60  		assertNull(supplier.get());
61  	}
62  
63  	@Test
64  	void a05_empty() {
65  		OptionalSupplier<String> supplier = OptionalSupplier.empty();
66  		assertNull(supplier.get());
67  	}
68  
69  	//====================================================================================================
70  	// isPresent() / isEmpty()
71  	//====================================================================================================
72  	@Test
73  	void b01_isPresent_true() {
74  		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable("value");
75  		assertTrue(supplier.isPresent());
76  		assertFalse(supplier.isEmpty());
77  	}
78  
79  	@Test
80  	void b02_isPresent_false() {
81  		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable(null);
82  		assertFalse(supplier.isPresent());
83  		assertTrue(supplier.isEmpty());
84  	}
85  
86  	//====================================================================================================
87  	// map()
88  	//====================================================================================================
89  	@Test
90  	void c01_map_present() {
91  		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable("hello");
92  		OptionalSupplier<Integer> mapped = supplier.map(String::length);
93  		assertEquals(5, mapped.get());
94  	}
95  
96  	@Test
97  	void c02_map_empty() {
98  		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable(null);
99  		OptionalSupplier<Integer> mapped = supplier.map(String::length);
100 		assertNull(mapped.get());
101 	}
102 
103 	@Test
104 	void c03_map_nullMapper() {
105 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable("value");
106 		assertThrows(IllegalArgumentException.class, () -> supplier.map(null));
107 	}
108 
109 	@Test
110 	void c04_map_returnsNull() {
111 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable("value");
112 		OptionalSupplier<String> mapped = supplier.map(s -> null);
113 		assertNull(mapped.get());
114 	}
115 
116 	//====================================================================================================
117 	// flatMap()
118 	//====================================================================================================
119 	@Test
120 	void d01_flatMap_present() {
121 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable("hello");
122 		OptionalSupplier<Integer> mapped = supplier.flatMap(s -> OptionalSupplier.ofNullable(s.length()));
123 		assertEquals(5, mapped.get());
124 	}
125 
126 	@Test
127 	void d02_flatMap_empty() {
128 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable(null);
129 		OptionalSupplier<Integer> mapped = supplier.flatMap(s -> OptionalSupplier.ofNullable(s.length()));
130 		assertNull(mapped.get());
131 	}
132 
133 	@Test
134 	void d03_flatMap_returnsEmpty() {
135 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable("hello");
136 		OptionalSupplier<Integer> mapped = supplier.flatMap(s -> OptionalSupplier.empty());
137 		assertNull(mapped.get());
138 	}
139 
140 	@Test
141 	void d04_flatMap_returnsNull() {
142 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable("hello");
143 		OptionalSupplier<Integer> mapped = supplier.flatMap(s -> null);
144 		assertNull(mapped.get());
145 	}
146 
147 	@Test
148 	void d05_flatMap_nullMapper() {
149 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable("value");
150 		assertThrows(IllegalArgumentException.class, () -> supplier.flatMap(null));
151 	}
152 
153 	//====================================================================================================
154 	// filter()
155 	//====================================================================================================
156 	@Test
157 	void e01_filter_matches() {
158 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable("hello");
159 		OptionalSupplier<String> filtered = supplier.filter(s -> s.length() > 3);
160 		assertEquals("hello", filtered.get());
161 	}
162 
163 	@Test
164 	void e02_filter_noMatch() {
165 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable("hi");
166 		OptionalSupplier<String> filtered = supplier.filter(s -> s.length() > 3);
167 		assertNull(filtered.get());
168 	}
169 
170 	@Test
171 	void e03_filter_empty() {
172 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable(null);
173 		OptionalSupplier<String> filtered = supplier.filter(s -> s.length() > 3);
174 		assertNull(filtered.get());
175 	}
176 
177 	@Test
178 	void e04_filter_nullPredicate() {
179 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable("value");
180 		assertThrows(IllegalArgumentException.class, () -> supplier.filter(null));
181 	}
182 
183 	//====================================================================================================
184 	// orElse()
185 	//====================================================================================================
186 	@Test
187 	void f01_orElse_present() {
188 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable("value");
189 		assertEquals("value", supplier.orElse("default"));
190 	}
191 
192 	@Test
193 	void f02_orElse_empty() {
194 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable(null);
195 		assertEquals("default", supplier.orElse("default"));
196 	}
197 
198 	@Test
199 	void f03_orElse_nullDefault() {
200 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable(null);
201 		assertNull(supplier.orElse(null));
202 	}
203 
204 	//====================================================================================================
205 	// orElseGet()
206 	//====================================================================================================
207 	@Test
208 	void g01_orElseGet_present() {
209 		AtomicInteger callCount = new AtomicInteger();
210 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable("value");
211 		String result = supplier.orElseGet(() -> {
212 			callCount.incrementAndGet();
213 			return "default";
214 		});
215 		assertEquals("value", result);
216 		assertEquals(0, callCount.get()); // Should not be called
217 	}
218 
219 	@Test
220 	void g02_orElseGet_empty() {
221 		AtomicInteger callCount = new AtomicInteger();
222 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable(null);
223 		String result = supplier.orElseGet(() -> {
224 			callCount.incrementAndGet();
225 			return "default";
226 		});
227 		assertEquals("default", result);
228 		assertEquals(1, callCount.get());
229 	}
230 
231 	@Test
232 	void g03_orElseGet_nullSupplier() {
233 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable("value");
234 		assertThrows(IllegalArgumentException.class, () -> supplier.orElseGet(null));
235 	}
236 
237 	//====================================================================================================
238 	// orElseThrow()
239 	//====================================================================================================
240 	@Test
241 	void h01_orElseThrow_present() {
242 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable("value");
243 		assertEquals("value", supplier.orElseThrow(() -> new RuntimeException("should not throw")));
244 	}
245 
246 	@Test
247 	void h02_orElseThrow_empty() {
248 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable(null);
249 		assertThrows(RuntimeException.class, () -> supplier.orElseThrow(() -> new RuntimeException("expected")));
250 	}
251 
252 	@Test
253 	void h03_orElseThrow_nullSupplier() {
254 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable("value");
255 		assertThrows(IllegalArgumentException.class, () -> supplier.orElseThrow(null));
256 	}
257 
258 	//====================================================================================================
259 	// ifPresent()
260 	//====================================================================================================
261 	@Test
262 	void i01_ifPresent_present() {
263 		AtomicInteger callCount = new AtomicInteger();
264 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable("value");
265 		supplier.ifPresent(s -> callCount.incrementAndGet());
266 		assertEquals(1, callCount.get());
267 	}
268 
269 	@Test
270 	void i02_ifPresent_empty() {
271 		AtomicInteger callCount = new AtomicInteger();
272 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable(null);
273 		supplier.ifPresent(s -> callCount.incrementAndGet());
274 		assertEquals(0, callCount.get());
275 	}
276 
277 	@Test
278 	void i03_ifPresent_nullAction() {
279 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable("value");
280 		assertThrows(IllegalArgumentException.class, () -> supplier.ifPresent(null));
281 	}
282 
283 	//====================================================================================================
284 	// ifPresentOrElse()
285 	//====================================================================================================
286 	@Test
287 	void j01_ifPresentOrElse_present() {
288 		AtomicInteger presentCount = new AtomicInteger();
289 		AtomicInteger emptyCount = new AtomicInteger();
290 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable("value");
291 		supplier.ifPresentOrElse(
292 			s -> presentCount.incrementAndGet(),
293 			() -> emptyCount.incrementAndGet()
294 		);
295 		assertEquals(1, presentCount.get());
296 		assertEquals(0, emptyCount.get());
297 	}
298 
299 	@Test
300 	void j02_ifPresentOrElse_empty() {
301 		AtomicInteger presentCount = new AtomicInteger();
302 		AtomicInteger emptyCount = new AtomicInteger();
303 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable(null);
304 		supplier.ifPresentOrElse(
305 			s -> presentCount.incrementAndGet(),
306 			() -> emptyCount.incrementAndGet()
307 		);
308 		assertEquals(0, presentCount.get());
309 		assertEquals(1, emptyCount.get());
310 	}
311 
312 	@Test
313 	void j03_ifPresentOrElse_nullAction() {
314 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable("value");
315 		assertThrows(IllegalArgumentException.class, () -> supplier.ifPresentOrElse(null, () -> {}));
316 	}
317 
318 	@Test
319 	void j04_ifPresentOrElse_nullEmptyAction() {
320 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable("value");
321 		assertThrows(IllegalArgumentException.class, () -> supplier.ifPresentOrElse(s -> {}, null));
322 	}
323 
324 	//====================================================================================================
325 	// toOptional()
326 	//====================================================================================================
327 	@Test
328 	void k01_toOptional_present() {
329 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable("value");
330 		Optional<String> optional = supplier.toOptional();
331 		assertTrue(optional.isPresent());
332 		assertEquals("value", optional.get());
333 	}
334 
335 	@Test
336 	void k02_toOptional_empty() {
337 		OptionalSupplier<String> supplier = OptionalSupplier.ofNullable(null);
338 		Optional<String> optional = supplier.toOptional();
339 		assertFalse(optional.isPresent());
340 	}
341 }
342