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.utils;
18  
19  import static org.apache.juneau.TestUtils.assertThrowsWithMessage;
20  import static org.apache.juneau.commons.utils.AssertionUtils.*;
21  import static org.apache.juneau.commons.utils.CollectionUtils.*;
22  import static org.junit.jupiter.api.Assertions.*;
23  
24  import org.apache.juneau.*;
25  import org.junit.jupiter.api.*;
26  
27  class AssertionUtils_Test extends TestBase {
28  
29  	//====================================================================================================
30  	// Constructor (line 56)
31  	//====================================================================================================
32  	@Test
33  	void a00_constructor() {
34  		// Test line 56: class instantiation
35  		// AssertionUtils has an implicit public no-arg constructor
36  		var instance = new AssertionUtils();
37  		assertNotNull(instance);
38  	}
39  
40  	//====================================================================================================
41  	// assertArg(boolean, String, Object...)
42  	//====================================================================================================
43  	@Test
44  	void a001_assertArg() {
45  		// Should not throw when expression is true
46  		assertArg(true, "Should not throw");
47  		assertArg(true, "Message with {0}", "arg");
48  		assertArg(true, "Test {0} {1} {2}", "a", "b", "c");
49  		
50  		// Should throw when expression is false
51  		assertThrowsWithMessage(IllegalArgumentException.class, "Test message", () -> {
52  			assertArg(false, "Test message");
53  		});
54  		
55  		assertThrowsWithMessage(IllegalArgumentException.class, l("Test message", "arg1"), () -> {
56  			assertArg(false, "Test message {0}", "arg1");
57  		});
58  		
59  		assertThrowsWithMessage(IllegalArgumentException.class, "Test", () -> {
60  			assertArg(false, "Test {0} {1} {2}", "a", "b", "c");
61  		});
62  	}
63  
64  	//====================================================================================================
65  	// assertArgNotNull(String, T)
66  	//====================================================================================================
67  	@Test
68  	void a002_assertArgNotNull() {
69  		// Should not throw when value is not null
70  		var value = "test";
71  		var result = assertArgNotNull("arg", value);
72  		assertSame(value, result);
73  		
74  		var obj = new Object();
75  		var result2 = assertArgNotNull("arg", obj);
76  		assertSame(obj, result2);
77  		
78  		var i = 123;
79  		var result3 = assertArgNotNull("int", i);
80  		assertEquals(i, result3);
81  		
82  		var d = 45.6;
83  		var result4 = assertArgNotNull("double", d);
84  		assertEquals(d, result4);
85  		
86  		// Should throw when value is null
87  		assertThrowsWithMessage(IllegalArgumentException.class, l("arg", "cannot be null"), () -> {
88  			assertArgNotNull("arg", null);
89  		});
90  	}
91  
92  	//====================================================================================================
93  	// assertArgNotNullOrBlank(String, String)
94  	//====================================================================================================
95  	@Test
96  	void a003_assertArgNotNullOrBlank() {
97  		// Should not throw when value is valid
98  		var value = "test";
99  		var result = assertArgNotNullOrBlank("arg", value);
100 		assertSame(value, result);
101 		
102 		// Should throw when value is null
103 		assertThrowsWithMessage(IllegalArgumentException.class, l("arg", "cannot be null"), () -> {
104 			assertArgNotNullOrBlank("arg", null);
105 		});
106 		
107 		// Should throw when value is empty
108 		assertThrowsWithMessage(IllegalArgumentException.class, l("arg", "cannot be blank"), () -> {
109 			assertArgNotNullOrBlank("arg", "");
110 		});
111 		
112 		// Should throw when value is whitespace
113 		assertThrowsWithMessage(IllegalArgumentException.class, l("arg", "cannot be blank"), () -> {
114 			assertArgNotNullOrBlank("arg", "   ");
115 		});
116 		
117 		assertThrowsWithMessage(IllegalArgumentException.class, "cannot be blank", () -> {
118 			assertArgNotNullOrBlank("arg", "\t");
119 		});
120 		
121 		assertThrowsWithMessage(IllegalArgumentException.class, "cannot be blank", () -> {
122 			assertArgNotNullOrBlank("arg", "\n");
123 		});
124 	}
125 
126 	//====================================================================================================
127 	// assertArgsNotNull(String, Object, String, Object) - 2 args
128 	//====================================================================================================
129 	@Test
130 	void a004_assertArgsNotNull_2args() {
131 		// Should not throw when both are not null
132 		assertArgsNotNull("arg1", "value1", "arg2", "value2");
133 		
134 		// Should throw when first is null
135 		assertThrowsWithMessage(IllegalArgumentException.class, l("arg1", "cannot be null"), () -> {
136 			assertArgsNotNull("arg1", null, "arg2", "value2");
137 		});
138 		
139 		// Should throw when second is null
140 		assertThrowsWithMessage(IllegalArgumentException.class, l("arg2", "cannot be null"), () -> {
141 			assertArgsNotNull("arg1", "value1", "arg2", null);
142 		});
143 		
144 		// Should fail on first null when both are null
145 		assertThrowsWithMessage(IllegalArgumentException.class, "arg1", () -> {
146 			assertArgsNotNull("arg1", null, "arg2", null);
147 		});
148 	}
149 
150 	//====================================================================================================
151 	// assertArgsNotNull(String, Object, String, Object, String, Object) - 3 args
152 	//====================================================================================================
153 	@Test
154 	void a005_assertArgsNotNull_3args() {
155 		// Should not throw when all are not null
156 		assertArgsNotNull("arg1", "value1", "arg2", "value2", "arg3", "value3");
157 		
158 		// Should throw when first is null
159 		assertThrowsWithMessage(IllegalArgumentException.class, "arg1", () -> {
160 			assertArgsNotNull("arg1", null, "arg2", "value2", "arg3", "value3");
161 		});
162 		
163 		// Should throw when second is null
164 		assertThrowsWithMessage(IllegalArgumentException.class, "arg2", () -> {
165 			assertArgsNotNull("arg1", "value1", "arg2", null, "arg3", "value3");
166 		});
167 		
168 		// Should throw when third is null
169 		assertThrowsWithMessage(IllegalArgumentException.class, "arg3", () -> {
170 			assertArgsNotNull("arg1", "value1", "arg2", "value2", "arg3", null);
171 		});
172 	}
173 
174 	//====================================================================================================
175 	// assertArgsNotNull(String, Object, String, Object, String, Object, String, Object) - 4 args
176 	//====================================================================================================
177 	@Test
178 	void a006_assertArgsNotNull_4args() {
179 		// Should not throw when all are not null
180 		assertArgsNotNull("arg1", "value1", "arg2", "value2", "arg3", "value3", "arg4", "value4");
181 		
182 		// Should throw when first is null
183 		assertThrowsWithMessage(IllegalArgumentException.class, "arg1", () -> {
184 			assertArgsNotNull("arg1", null, "arg2", "value2", "arg3", "value3", "arg4", "value4");
185 		});
186 		
187 		// Should throw when fourth is null
188 		assertThrowsWithMessage(IllegalArgumentException.class, "arg4", () -> {
189 			assertArgsNotNull("arg1", "value1", "arg2", "value2", "arg3", "value3", "arg4", null);
190 		});
191 	}
192 
193 	//====================================================================================================
194 	// assertArgsNotNull(String, Object, String, Object, String, Object, String, Object, String, Object) - 5 args
195 	//====================================================================================================
196 	@Test
197 	void a007_assertArgsNotNull_5args() {
198 		// Should not throw when all are not null
199 		assertArgsNotNull("arg1", "value1", "arg2", "value2", "arg3", "value3", "arg4", "value4", "arg5", "value5");
200 		
201 		// Should throw when first is null
202 		assertThrowsWithMessage(IllegalArgumentException.class, "arg1", () -> {
203 			assertArgsNotNull("arg1", null, "arg2", "value2", "arg3", "value3", "arg4", "value4", "arg5", "value5");
204 		});
205 		
206 		// Should throw when fifth is null
207 		assertThrowsWithMessage(IllegalArgumentException.class, "arg5", () -> {
208 			assertArgsNotNull("arg1", "value1", "arg2", "value2", "arg3", "value3", "arg4", "value4", "arg5", null);
209 		});
210 	}
211 
212 	//====================================================================================================
213 	// assertClassArrayArgIsType(String, Class<E>, Class<?>[])
214 	//====================================================================================================
215 	@Test
216 	void a008_assertClassArrayArgIsType() {
217 		// Should not throw when all classes are assignable
218 		var classes = a(String.class, Object.class);
219 		var result = assertClassArrayArgIsType("arg", Object.class, classes);
220 		assertSame(classes, result);
221 		
222 		// Should not throw with empty array
223 		var emptyClasses = new Class<?>[0];
224 		var result2 = assertClassArrayArgIsType("arg", Object.class, emptyClasses);
225 		assertSame(emptyClasses, result2);
226 		
227 		// Should not throw with subclasses
228 		var subclasses = a(Integer.class, Double.class);
229 		var result3 = assertClassArrayArgIsType("arg", Number.class, subclasses);
230 		assertSame(subclasses, result3);
231 		
232 		// Should not throw with same class
233 		var sameClasses = a(String.class);
234 		var result4 = assertClassArrayArgIsType("arg", String.class, sameClasses);
235 		assertSame(sameClasses, result4);
236 		
237 		// Should throw when class is not assignable
238 		var invalidClasses = a(String.class, Integer.class);
239 		assertThrowsWithMessage(IllegalArgumentException.class, l("arg", "String"), () -> {
240 			assertClassArrayArgIsType("arg", Number.class, invalidClasses);
241 		});
242 		
243 		// Should throw with index information
244 		var invalidClasses2 = a(String.class, Integer.class, Double.class);
245 		assertThrowsWithMessage(IllegalArgumentException.class, l("arg", "index", "0"), () -> {
246 			assertClassArrayArgIsType("arg", Number.class, invalidClasses2);
247 		});
248 	}
249 
250 	//====================================================================================================
251 	// assertOneOf(T, T...)
252 	//====================================================================================================
253 	@Test
254 	void a009_assertOneOf() {
255 		// Should return actual when it matches
256 		assertEquals("test", assertOneOf("test", "test", "other"));
257 		assertEquals(123, assertOneOf(123, 123, 456));
258 		assertEquals("a", assertOneOf("a", "a", "b", "c"));
259 		
260 		// Exact match
261 		assertEquals("test", assertOneOf("test", "test"));
262 		assertEquals(1, assertOneOf(1, 1, 2, 3));
263 		
264 		// Match in middle
265 		assertEquals(2, assertOneOf(2, 1, 2, 3));
266 		
267 		// Match at end
268 		assertEquals(3, assertOneOf(3, 1, 2, 3));
269 		
270 		// Should handle nulls
271 		assertNull(assertOneOf(null, null, "test"));
272 		assertNull(assertOneOf(null, "test", null));
273 		
274 		// Should return same instance
275 		var value = "test";
276 		var result = assertOneOf(value, "test", "other");
277 		assertSame(value, result);
278 		
279 		// Should work with objects
280 		var obj1 = new Object();
281 		var obj2 = new Object();
282 		var result2 = assertOneOf(obj1, obj1, obj2);
283 		assertSame(obj1, result2);
284 		
285 		// Should throw when value doesn't match
286 		assertThrowsWithMessage(AssertionError.class, l("Invalid value specified", "test"), () -> {
287 			assertOneOf("test", "other");
288 		});
289 		
290 		assertThrowsWithMessage(AssertionError.class, l("Invalid value specified", "test"), () -> {
291 			assertOneOf("test", "a", "b", "c");
292 		});
293 		
294 		assertThrows(AssertionError.class, () -> assertOneOf(10, 1, 2, 3, 4, 5));
295 		
296 		// Should throw with empty expected
297 		assertThrowsWithMessage(AssertionError.class, "Invalid value specified", () -> {
298 			assertOneOf("test");
299 		});
300 	}
301 
302 	//====================================================================================================
303 	// assertType(Class<T>, Object)
304 	//====================================================================================================
305 	@Test
306 	void a010_assertType() {
307 		// Should return object when it's an instance of type
308 		String value = "test";
309 		String result = assertType(String.class, value);
310 		assertSame(value, result);
311 		
312 		// Should work with subclasses
313 		Integer intValue = 123;
314 		Number numberResult = assertType(Number.class, intValue);
315 		assertSame(intValue, numberResult);
316 		
317 		// Should work with same class
318 		Object obj = new Object();
319 		Object result2 = assertType(Object.class, obj);
320 		assertSame(obj, result2);
321 		
322 		// Should work with primitive wrappers
323 		Integer intValue2 = 42;
324 		Integer result3 = assertType(Integer.class, intValue2);
325 		assertSame(intValue2, result3);
326 		
327 		// Should throw when type is null
328 		assertThrowsWithMessage(IllegalArgumentException.class, l("type", "cannot be null"), () -> {
329 			assertType(null, "test");
330 		});
331 		
332 		// Should throw when object is null
333 		assertThrowsWithMessage(IllegalArgumentException.class, l("o", "cannot be null"), () -> {
334 			assertType(String.class, null);
335 		});
336 		
337 		// Should throw when object is not an instance
338 		assertThrowsWithMessage(IllegalArgumentException.class, l("Object is not an instance of", "String", "Integer"), () -> {
339 			assertType(String.class, 123);
340 		});
341 		
342 		assertThrowsWithMessage(IllegalArgumentException.class, "Object is not an instance of", () -> {
343 			assertType(Integer.class, "test");
344 		});
345 	}
346 
347 	//====================================================================================================
348 	// assertType(Class<T>, Object, Supplier<? extends RuntimeException>)
349 	//====================================================================================================
350 	@Test
351 	void a011_assertType_withSupplier() {
352 		// Should return object when it's an instance of type
353 		String value = "test";
354 		String result = assertType(String.class, value, () -> new IllegalStateException("Should not throw"));
355 		assertSame(value, result);
356 		
357 		// Should throw when type is null
358 		assertThrowsWithMessage(IllegalArgumentException.class, l("type", "cannot be null"), () -> {
359 			assertType(null, "test", () -> new IllegalStateException("Custom"));
360 		});
361 		
362 		// Should throw when object is null
363 		assertThrowsWithMessage(IllegalArgumentException.class, l("o", "cannot be null"), () -> {
364 			assertType(String.class, null, () -> new IllegalStateException("Custom"));
365 		});
366 		
367 		// Should throw custom exception when object is not an instance
368 		IllegalStateException customException = new IllegalStateException("Custom exception");
369 		IllegalStateException thrown = assertThrows(IllegalStateException.class, () -> {
370 			assertType(String.class, 123, () -> customException);
371 		});
372 		assertSame(customException, thrown);
373 		
374 		// Should work with different exception types
375 		RuntimeException runtimeException = new RuntimeException("Custom runtime exception");
376 		RuntimeException thrown2 = assertThrows(RuntimeException.class, () -> {
377 			assertType(Integer.class, "test", () -> runtimeException);
378 		});
379 		assertSame(runtimeException, thrown2);
380 		
381 		// Should work with supplier that creates new exception
382 		IllegalStateException thrown3 = assertThrows(IllegalStateException.class, () -> {
383 			assertType(String.class, 123, () -> new IllegalStateException("Not a string"));
384 		});
385 		assertEquals("Not a string", thrown3.getMessage());
386 	}
387 
388 	//====================================================================================================
389 	// assertVarargsNotNull(String, T[])
390 	//====================================================================================================
391 	@Test
392 	void a012_assertVarargsNotNull() {
393 		// Should not throw when array and elements are not null
394 		var array = a("a", "b", "c");
395 		var result = assertArgNoNulls("arg", array);
396 		assertSame(array, result);
397 		
398 		// Should not throw with empty array
399 		var emptyArray = new String[0];
400 		var result2 = assertArgNoNulls("arg", emptyArray);
401 		assertSame(emptyArray, result2);
402 		
403 		// Should work with integer array
404 		var intArray = a(1, 2, 3);
405 		var result3 = assertArgNoNulls("arg", intArray);
406 		assertSame(intArray, result3);
407 		
408 		// Should work with object array
409 		var objArray = a(new Object(), new Object());
410 		var result4 = assertArgNoNulls("arg", objArray);
411 		assertSame(objArray, result4);
412 		
413 		// Should throw when array is null
414 		assertThrowsWithMessage(IllegalArgumentException.class, l("arg", "cannot be null"), () -> {
415 			assertArgNoNulls("arg", (String[])null);
416 		});
417 		
418 		// Should throw when element is null
419 		var nullElementArray = a("a", null, "c");
420 		assertThrowsWithMessage(IllegalArgumentException.class, l("arg", "parameter", "1"), () -> {
421 			assertArgNoNulls("arg", nullElementArray);
422 		});
423 		
424 		// Should fail on first null when multiple elements are null
425 		var multipleNullArray = a("a", null, null, "d");
426 		assertThrowsWithMessage(IllegalArgumentException.class, "1", () -> {
427 			assertArgNoNulls("arg", multipleNullArray);
428 		});
429 	}
430 }
431