View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.lang3;
20  
21  import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException;
22  import static org.apache.commons.lang3.LangAssertions.assertIndexOutOfBoundsException;
23  import static org.apache.commons.lang3.LangAssertions.assertNullPointerException;
24  import static org.junit.jupiter.api.Assertions.assertEquals;
25  import static org.junit.jupiter.api.Assertions.assertFalse;
26  import static org.junit.jupiter.api.Assertions.assertNotNull;
27  import static org.junit.jupiter.api.Assertions.assertSame;
28  import static org.junit.jupiter.api.Assertions.assertThrows;
29  import static org.junit.jupiter.api.Assertions.assertTrue;
30  
31  import java.lang.reflect.Constructor;
32  import java.lang.reflect.Modifier;
33  import java.util.Collection;
34  import java.util.Collections;
35  import java.util.List;
36  import java.util.Map;
37  import java.util.Set;
38  
39  import org.junit.jupiter.api.Nested;
40  import org.junit.jupiter.api.Test;
41  
42  /**
43   * Tests {@link Validate}.
44   */
45  class ValidateTest extends AbstractLangTest {
46  
47      @Nested
48      final class ExclusiveBetween {
49  
50          @Nested
51          final class WithComparable {
52  
53              @Nested
54              final class WithMessage {
55  
56                  @Test
57                  void shouldNotThrowExceptionWhenValueIsBetweenBounds() {
58                      Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, "2", "MSG");
59                  }
60  
61                  @Test
62                  void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsAboveUpperBound() {
63                      final IllegalArgumentException ex = assertIllegalArgumentException(
64                          () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, "4", "MSG"));
65                      assertEquals("MSG", ex.getMessage());
66                  }
67  
68                  @Test
69                  void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsBelowLowerBound() {
70                      final IllegalArgumentException ex = assertIllegalArgumentException(
71                          () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, "0", "MSG"));
72                      assertEquals("MSG", ex.getMessage());
73                  }
74  
75                  @Test
76                  void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsLowerBound() {
77                      final IllegalArgumentException ex = assertIllegalArgumentException(
78                          () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, LOWER_BOUND, "MSG"));
79                      assertEquals("MSG", ex.getMessage());
80                  }
81  
82                  @Test
83                  void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsUpperBound() {
84                      final IllegalArgumentException ex = assertIllegalArgumentException(
85                          () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, UPPER_BOUND, "MSG"));
86                      assertEquals("MSG", ex.getMessage());
87                  }
88              }
89              @Nested
90              final class WithoutMessage {
91  
92                  @Test
93                  void shouldNotThrowExceptionWhenValueIsBetweenBounds() {
94                      Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, "2");
95                  }
96  
97                  @Test
98                  void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsAboveUpperBound() {
99                      final IllegalArgumentException ex = assertIllegalArgumentException(
100                         () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, "4"));
101                     assertEquals("The value 4 is not in the specified exclusive range of 1 to 3", ex.getMessage());
102                 }
103 
104                 @Test
105                 void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsBelowLowerBound() {
106                     final IllegalArgumentException ex = assertIllegalArgumentException(
107                         () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, "0"));
108                     assertEquals("The value 0 is not in the specified exclusive range of 1 to 3", ex.getMessage());
109                 }
110 
111                 @Test
112                 void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsLowerBound() {
113                     final IllegalArgumentException ex = assertIllegalArgumentException(
114                         () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, LOWER_BOUND));
115                     assertEquals("The value 1 is not in the specified exclusive range of 1 to 3", ex.getMessage());
116                 }
117 
118                 @Test
119                 void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsUpperBound() {
120                     final IllegalArgumentException ex = assertIllegalArgumentException(
121                         () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, UPPER_BOUND));
122                     assertEquals("The value 3 is not in the specified exclusive range of 1 to 3", ex.getMessage());
123                 }
124             }
125 
126             private static final String LOWER_BOUND = "1";
127 
128             private static final String UPPER_BOUND = "3";
129         }
130 
131         @Nested
132         final class WithDouble {
133 
134             @Nested
135             final class WithMessage {
136 
137                 @Test
138                 void shouldNotThrowExceptionWhenValueIsBetweenBounds() {
139                     Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, 2.1, "MSG");
140                 }
141 
142                 @Test
143                 void shouldThrowIllegalArgumentExceptionWhenValueIsLowerBound() {
144                     final IllegalArgumentException ex = assertIllegalArgumentException(
145                         () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, LOWER_BOUND, "MSG"));
146                     assertEquals("MSG", ex.getMessage());
147                 }
148 
149                 @Test
150                 void shouldThrowIllegalArgumentExceptionWhenValueIsUpperBound() {
151                     final IllegalArgumentException ex = assertIllegalArgumentException(
152                         () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, UPPER_BOUND, "MSG"));
153                     assertEquals("MSG", ex.getMessage());
154                 }
155 
156                 @Test
157                 void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsAboveUpperBound() {
158                     final IllegalArgumentException ex = assertIllegalArgumentException(
159                         () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, 4.1, "MSG"));
160                     assertEquals("MSG", ex.getMessage());
161                 }
162 
163                 @Test
164                 void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsBelowLowerBound() {
165                     final IllegalArgumentException ex = assertIllegalArgumentException(
166                         () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, 0.01, "MSG"));
167                     assertEquals("MSG", ex.getMessage());
168                 }
169             }
170             @Nested
171             final class WithoutMessage {
172 
173                 @Test
174                 void shouldNotThrowExceptionWhenValueIsBetweenBounds() {
175                     Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, 2.1);
176                 }
177 
178                 @Test
179                 void shouldThrowIllegalArgumentExceptionWhenValueIsLowerBound() {
180                     final IllegalArgumentException ex = assertIllegalArgumentException(
181                         () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, LOWER_BOUND));
182                     assertEquals("The value 0.1 is not in the specified exclusive range of 0.1 to 3.1", ex.getMessage());
183                 }
184 
185                 @Test
186                 void shouldThrowIllegalArgumentExceptionWhenValueIsUpperBound() {
187                     final IllegalArgumentException ex = assertIllegalArgumentException(
188                         () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, UPPER_BOUND));
189                     assertEquals("The value 3.1 is not in the specified exclusive range of 0.1 to 3.1", ex.getMessage());
190                 }
191 
192                 @Test
193                 void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsAboveUpperBound() {
194                     final IllegalArgumentException ex = assertIllegalArgumentException(
195                         () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, 4.1));
196                     assertEquals("The value 4.1 is not in the specified exclusive range of 0.1 to 3.1", ex.getMessage());
197                 }
198 
199                 @Test
200                 void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsBelowLowerBound() {
201                     final IllegalArgumentException ex = assertIllegalArgumentException(
202                         () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, 0.01));
203                     assertEquals("The value 0.01 is not in the specified exclusive range of 0.1 to 3.1", ex.getMessage());
204                 }
205             }
206 
207             private static final double LOWER_BOUND = 0.1;
208 
209             private static final double UPPER_BOUND = 3.1;
210         }
211 
212         @Nested
213         final class WithLong {
214 
215             @Nested
216             final class WithMessage {
217 
218                 @Test
219                 void shouldNotThrowExceptionWhenValueIsBetweenBounds() {
220                     Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, 2, "MSG");
221                 }
222 
223                 @Test
224                 void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsAboveUpperBound() {
225                     final IllegalArgumentException ex = assertIllegalArgumentException(
226                         () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, 4, "MSG"));
227                     assertEquals("MSG", ex.getMessage());
228                 }
229 
230                 @Test
231                 void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsBelowLowerBound() {
232                     final IllegalArgumentException ex = assertIllegalArgumentException(
233                         () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, 0, "MSG"));
234                     assertEquals("MSG", ex.getMessage());
235                 }
236 
237                 @Test
238                 void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsLowerBound() {
239                     final IllegalArgumentException ex = assertIllegalArgumentException(
240                         () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, LOWER_BOUND, "MSG"));
241                     assertEquals("MSG", ex.getMessage());
242                 }
243 
244                 @Test
245                 void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsUpperBound() {
246                     final IllegalArgumentException ex = assertIllegalArgumentException(
247                         () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, UPPER_BOUND, "MSG"));
248                     assertEquals("MSG", ex.getMessage());
249                 }
250             }
251             @Nested
252             final class WithoutMessage {
253 
254                 @Test
255                 void shouldNotThrowExceptionWhenValueIsBetweenBounds() {
256                     Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, 2);
257                 }
258 
259                 @Test
260                 void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsAboveUpperBound() {
261                     final IllegalArgumentException ex = assertIllegalArgumentException(
262                         () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, 4));
263                     assertEquals("The value 4 is not in the specified exclusive range of 1 to 3", ex.getMessage());
264                 }
265 
266                 @Test
267                 void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsBelowLowerBound() {
268                     final IllegalArgumentException ex = assertIllegalArgumentException(
269                         () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, 0));
270                     assertEquals("The value 0 is not in the specified exclusive range of 1 to 3", ex.getMessage());
271                 }
272 
273                 @Test
274                 void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsLowerBound() {
275                     final IllegalArgumentException ex = assertIllegalArgumentException(
276                         () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, LOWER_BOUND));
277                     assertEquals("The value 1 is not in the specified exclusive range of 1 to 3", ex.getMessage());
278                 }
279 
280                 @Test
281                 void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsUpperBound() {
282                     final IllegalArgumentException ex = assertIllegalArgumentException(
283                         () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, UPPER_BOUND));
284                     assertEquals("The value 3 is not in the specified exclusive range of 1 to 3", ex.getMessage());
285                 }
286             }
287 
288             private static final long LOWER_BOUND = 1;
289 
290             private static final long UPPER_BOUND = 3;
291         }
292     }
293 
294     @Nested
295     final class Finite {
296 
297         @Nested
298         final class WithMessage {
299 
300             @Test
301             void shouldNotThrowExceptionForFiniteValue() {
302                 Validate.finite(0.0, "MSG");
303             }
304 
305             @Test
306             void shouldThrowIllegalArgumentExceptionWithDefaultMessageForNaN() {
307                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.finite(Double.NaN, "MSG"));
308                 assertEquals("MSG", ex.getMessage());
309             }
310 
311             @Test
312             void shouldThrowIllegalArgumentExceptionWithDefaultMessageForNegativeInfinity() {
313                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.finite(Double.NEGATIVE_INFINITY, "MSG"));
314                 assertEquals("MSG", ex.getMessage());
315             }
316 
317             @Test
318             void shouldThrowIllegalArgumentExceptionWithDefaultMessageForPositiveInfinity() {
319                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.finite(Double.POSITIVE_INFINITY, "MSG"));
320                 assertEquals("MSG", ex.getMessage());
321             }
322         }
323 
324         @Nested
325         final class WithoutMessage {
326 
327             @Test
328             void shouldNotThrowExceptionForFiniteValue() {
329                 Validate.finite(0.0);
330             }
331 
332             @Test
333             void shouldThrowIllegalArgumentExceptionWithDefaultMessageForNaN() {
334                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.finite(Double.NaN));
335                 assertEquals("The value is invalid: NaN", ex.getMessage());
336             }
337 
338             @Test
339             void shouldThrowIllegalArgumentExceptionWithDefaultMessageForNegativeInfinity() {
340                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.finite(Double.NEGATIVE_INFINITY));
341                 assertEquals("The value is invalid: -Infinity", ex.getMessage());
342             }
343 
344             @Test
345             void shouldThrowIllegalArgumentExceptionWithDefaultMessageForPositiveInfinity() {
346                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.finite(Double.POSITIVE_INFINITY));
347                 assertEquals("The value is invalid: Infinity", ex.getMessage());
348             }
349         }
350     }
351 
352     @Nested
353     final class InclusiveBetween {
354 
355         @Nested
356         final class WithComparable {
357 
358             @Nested
359             final class WithMessage {
360 
361                 @Test
362                 void shouldNotThrowExceptionWhenValueIsBetweenBounds() {
363                     Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, "2", "MSG");
364                 }
365 
366                 @Test
367                 void shouldNotThrowExceptionWhenValueIsLowerBound() {
368                     Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, LOWER_BOUND, "MSG");
369                 }
370 
371                 @Test
372                 void shouldNotThrowExceptionWhenValueIsUpperBound() {
373                     Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, UPPER_BOUND, "MSG");
374                 }
375 
376                 @Test
377                 void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsAboveUpperBound() {
378                     final IllegalArgumentException ex = assertIllegalArgumentException(
379                         () -> Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, "4", "MSG"));
380                     assertEquals("MSG", ex.getMessage());
381                 }
382 
383                 @Test
384                 void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsBelowLowerBound() {
385                     final IllegalArgumentException ex = assertIllegalArgumentException(
386                         () -> Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, "0", "MSG"));
387                     assertEquals("MSG", ex.getMessage());
388                 }
389             }
390             @Nested
391             final class WithoutMessage {
392 
393                 @Test
394                 void shouldNotThrowExceptionWhenValueIsBetweenBounds() {
395                     Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, "2");
396                 }
397 
398                 @Test
399                 void shouldNotThrowExceptionWhenValueIsLowerBound() {
400                     Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, LOWER_BOUND);
401                 }
402 
403                 @Test
404                 void shouldNotThrowExceptionWhenValueIsUpperBound() {
405                     Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, UPPER_BOUND);
406                 }
407 
408                 @Test
409                 void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsAboveUpperBound() {
410                     final IllegalArgumentException ex = assertIllegalArgumentException(
411                         () -> Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, "4"));
412                     assertEquals("The value 4 is not in the specified inclusive range of 1 to 3", ex.getMessage());
413                 }
414 
415                 @Test
416                 void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsBelowLowerBound() {
417                     final IllegalArgumentException ex = assertIllegalArgumentException(
418                         () -> Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, "0"));
419                     assertEquals("The value 0 is not in the specified inclusive range of 1 to 3", ex.getMessage());
420                 }
421             }
422 
423             private static final String LOWER_BOUND = "1";
424 
425             private static final String UPPER_BOUND = "3";
426         }
427 
428         @Nested
429         final class WithDouble {
430 
431             @Nested
432             final class WithMessage {
433 
434                 @Test
435                 void shouldNotThrowExceptionWhenValueIsBetweenBounds() {
436                     Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, 2.1, "MSG");
437                 }
438 
439                 @Test
440                 void shouldNotThrowExceptionWhenValueIsLowerBound() {
441                     Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, LOWER_BOUND, "MSG");
442                 }
443 
444                 @Test
445                 void shouldNotThrowExceptionWhenValueIsUpperBound() {
446                     Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, UPPER_BOUND, "MSG");
447                 }
448 
449                 @Test
450                 void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsAboveUpperBound() {
451                     final IllegalArgumentException ex = assertIllegalArgumentException(
452                         () -> Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, 4.1, "MSG"));
453                     assertEquals("MSG", ex.getMessage());
454                 }
455 
456                 @Test
457                 void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsBelowLowerBound() {
458                     final IllegalArgumentException ex = assertIllegalArgumentException(
459                         () -> Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, 0.01, "MSG"));
460                     assertEquals("MSG", ex.getMessage());
461                 }
462             }
463             @Nested
464             final class WithoutMessage {
465 
466                 @Test
467                 void shouldNotThrowExceptionWhenValueIsBetweenBounds() {
468                     Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, 2.1);
469                 }
470 
471                 @Test
472                 void shouldNotThrowExceptionWhenValueIsLowerBound() {
473                     Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, LOWER_BOUND);
474                 }
475 
476                 @Test
477                 void shouldNotThrowExceptionWhenValueIsUpperBound() {
478                     Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, UPPER_BOUND);
479                 }
480 
481                 @Test
482                 void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsAboveUpperBound() {
483                     final IllegalArgumentException ex = assertIllegalArgumentException(
484                         () -> Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, 4.1));
485                     assertEquals("The value 4.1 is not in the specified inclusive range of 0.1 to 3.1", ex.getMessage());
486                 }
487 
488                 @Test
489                 void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsBelowLowerBound() {
490                     final IllegalArgumentException ex = assertIllegalArgumentException(
491                         () -> Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, 0.01));
492                     assertEquals("The value 0.01 is not in the specified inclusive range of 0.1 to 3.1", ex.getMessage());
493                 }
494             }
495 
496             private static final double LOWER_BOUND = 0.1;
497 
498             private static final double UPPER_BOUND = 3.1;
499         }
500 
501         @Nested
502         final class WithLong {
503 
504             @Nested
505             final class WithMessage {
506 
507                 @Test
508                 void shouldNotThrowExceptionWhenValueIsBetweenBounds() {
509                     Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, 2, "MSG");
510                 }
511 
512                 @Test
513                 void shouldNotThrowExceptionWhenValueIsLowerBound() {
514                     Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, LOWER_BOUND, "MSG");
515                 }
516 
517                 @Test
518                 void shouldNotThrowExceptionWhenValueIsUpperBound() {
519                     Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, UPPER_BOUND, "MSG");
520                 }
521 
522                 @Test
523                 void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsAboveUpperBound() {
524                     final IllegalArgumentException ex = assertIllegalArgumentException(
525                         () -> Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, 4, "MSG"));
526                     assertEquals("MSG", ex.getMessage());
527                 }
528 
529                 @Test
530                 void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsBelowLowerBound() {
531                     final IllegalArgumentException ex = assertIllegalArgumentException(
532                         () -> Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, 0, "MSG"));
533                     assertEquals("MSG", ex.getMessage());
534                 }
535             }
536             @Nested
537             final class WithoutMessage {
538 
539                 @Test
540                 void shouldNotThrowExceptionWhenValueIsBetweenBounds() {
541                     Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, 2);
542                 }
543 
544                 @Test
545                 void shouldNotThrowExceptionWhenValueIsLowerBound() {
546                     Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, LOWER_BOUND);
547                 }
548 
549                 @Test
550                 void shouldNotThrowExceptionWhenValueIsUpperBound() {
551                     Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, UPPER_BOUND);
552                 }
553 
554                 @Test
555                 void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsAboveUpperBound() {
556                     final IllegalArgumentException ex = assertIllegalArgumentException(
557                         () -> Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, 4));
558                     assertEquals("The value 4 is not in the specified inclusive range of 1 to 3", ex.getMessage());
559                 }
560 
561                 @Test
562                 void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsBelowLowerBound() {
563                     final IllegalArgumentException ex = assertIllegalArgumentException(
564                         () -> Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, 0));
565                     assertEquals("The value 0 is not in the specified inclusive range of 1 to 3", ex.getMessage());
566                 }
567             }
568 
569             private static final long LOWER_BOUND = 1;
570 
571             private static final long UPPER_BOUND = 3;
572         }
573     }
574 
575     @Nested
576     final class IsAssignable {
577 
578         @Nested
579         final class WithMessage {
580 
581             @Test
582             void shouldNotThrowExceptionWhenClassIsAssignable() {
583                 Validate.isAssignableFrom(CharSequence.class, String.class, "MSG");
584             }
585 
586             @Test
587             void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenClassIsNotAssignable() {
588                 final IllegalArgumentException ex = assertIllegalArgumentException(
589                     () -> Validate.isAssignableFrom(List.class, String.class, "MSG"));
590                 assertEquals("MSG", ex.getMessage());
591             }
592         }
593 
594         @Nested
595         final class WithoutMessage {
596 
597             @Test
598             void shouldNotThrowExceptionWhenClassIsAssignable() {
599                 Validate.isAssignableFrom(CharSequence.class, String.class);
600             }
601 
602             @Test
603             void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenClassIsNotAssignable() {
604                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.isAssignableFrom(List.class, String.class));
605                 assertEquals("Cannot assign a java.lang.String to a java.util.List", ex.getMessage());
606             }
607 
608             @Test
609             void shouldThrowIllegalArgumentExceptionWithNullSuperType() {
610                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.isAssignableFrom(null, String.class));
611                 assertEquals("Cannot assign a java.lang.String to a null type", ex.getMessage());
612             }
613 
614             @Test
615             void shouldThrowIllegalArgumentExceptionWithNullType() {
616                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.isAssignableFrom(List.class, null));
617                 assertEquals("Cannot assign a null type to a java.util.List", ex.getMessage());
618             }
619 
620             @Test
621             void shouldThrowIllegalArgumentExceptionWithNullTypes() {
622                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.isAssignableFrom(null, null));
623                 assertEquals("Cannot assign a null type to a null type", ex.getMessage());
624             }
625         }
626     }
627 
628     @Nested
629     final class IsInstanceOf {
630 
631         @Nested
632         final class WithMessage {
633 
634             @Test
635             void shouldNotThrowExceptionWhenValueIsInstanceOfClass() {
636                 Validate.isInstanceOf(String.class, "hi", "MSG");
637             }
638 
639             @Test
640             void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsNotInstanceOfClass() {
641                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.isInstanceOf(List.class, "hi", "MSG"));
642                 assertEquals("MSG", ex.getMessage());
643             }
644         }
645 
646         @Nested
647         final class WithMessageTemplate {
648 
649             @Test
650             void shouldNotThrowExceptionWhenValueIsInstanceOfClass() {
651                 Validate.isInstanceOf(String.class, "hi", "Error %s=%s", "Name", "Value");
652             }
653 
654             @Test
655             void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsNotInstanceOfClass() {
656                 final IllegalArgumentException ex = assertIllegalArgumentException(
657                     () -> Validate.isInstanceOf(List.class, "hi", "Error %s=%s", "Name", "Value"));
658                 assertEquals("Error Name=Value", ex.getMessage());
659             }
660         }
661 
662         @Nested
663         final class WithoutMessage {
664 
665             @Test
666             void shouldNotThrowExceptionWhenValueIsInstanceOfClass() {
667                 Validate.isInstanceOf(String.class, "hi");
668             }
669 
670             @Test
671             void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsNotInstanceOfClass() {
672                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.isInstanceOf(List.class, "hi"));
673                 assertEquals("Expected type: java.util.List, actual: java.lang.String", ex.getMessage());
674             }
675         }
676     }
677 
678     @Nested
679     final class IsTrue {
680 
681         @Nested
682         final class WithDoubleTemplate {
683 
684             @Test
685             void shouldNotThrowForTrueExpression() {
686                 Validate.isTrue(true, "MSG", 7.4d);
687             }
688 
689             @Test
690             void shouldThrowExceptionWithDoubleInsertedIntoTemplateMessageForFalseExpression() {
691                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.isTrue(false, "MSG %s", 7.4d));
692                 assertEquals("MSG 7.4", ex.getMessage());
693             }
694         }
695 
696         @Nested
697         final class WithLongTemplate {
698 
699             @Test
700             void shouldNotThrowForTrueExpression() {
701                 Validate.isTrue(true, "MSG", 6);
702             }
703 
704             @Test
705             void shouldThrowExceptionWithLongInsertedIntoTemplateMessageForFalseExpression() {
706                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.isTrue(false, "MSG %s", 6));
707                 assertEquals("MSG 6", ex.getMessage());
708             }
709         }
710 
711         @Nested
712         final class WithMessage {
713 
714             @Test
715             void shouldNotThrowForTrueExpression() {
716                 Validate.isTrue(true, "MSG");
717             }
718 
719             @Test
720             void shouldThrowExceptionWithGivenMessageContainingSpecialCharacterForFalseExpression() {
721                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.isTrue(false, "%"));
722                 assertEquals("%", ex.getMessage());
723             }
724 
725             @Test
726             void shouldThrowExceptionWithGivenMessageForFalseExpression() {
727                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.isTrue(false, "MSG"));
728                 assertEquals("MSG", ex.getMessage());
729             }
730         }
731 
732         @Nested
733         final class WithMessageSupplier {
734 
735             @Test
736             void shouldNotThrowForTrueExpression() {
737                 Validate.isTrue(true, () -> "MSG");
738             }
739 
740             @Test
741             void shouldThrowExceptionWithDoubleInsertedIntoTemplateMessageForFalseExpression() {
742                 final IllegalArgumentException ex = assertIllegalArgumentException(
743                     () -> Validate.isTrue(false, () -> String.format("MSG %s %s", "Object 1", "Object 2")));
744                 assertEquals("MSG Object 1 Object 2", ex.getMessage());
745             }
746         }
747 
748         @Nested
749         final class WithObjectTemplate {
750 
751             @Test
752             void shouldNotThrowForTrueExpression() {
753                 Validate.isTrue(true, "MSG", "Object 1", "Object 2");
754             }
755 
756             @Test
757             void shouldThrowExceptionWithDoubleInsertedIntoTemplateMessageForFalseExpression() {
758                 final IllegalArgumentException ex = assertIllegalArgumentException(
759                     () -> Validate.isTrue(false, "MSG %s %s", "Object 1", "Object 2"));
760                 assertEquals("MSG Object 1 Object 2", ex.getMessage());
761             }
762         }
763 
764         @Nested
765         final class WithoutMessage {
766 
767             @Test
768             void shouldNotThrowForTrueExpression() {
769                 Validate.isTrue(true);
770             }
771 
772             @Test
773             void shouldThrowExceptionWithDefaultMessageForFalseExpression() {
774                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.isTrue(false));
775                 assertEquals("The validated expression is false", ex.getMessage());
776             }
777 
778         }
779     }
780 
781     @Nested
782     final class MatchesPattern {
783 
784         @Nested
785         final class WithMessage {
786 
787             @Test
788             void shouldNotThrowExceptionWhenStringMatchesPattern() {
789                 Validate.matchesPattern("hi", "[a-z]*", "MSG");
790             }
791 
792             @Test
793             void shouldThrowIllegalArgumentExceptionWhenStringDoesNotMatchPattern() {
794                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.matchesPattern("hi", "[0-9]*", "MSG"));
795                 assertEquals("MSG", ex.getMessage());
796             }
797         }
798 
799         @Nested
800         final class WithoutMessage {
801 
802             @Test
803             void shouldNotThrowExceptionWhenStringMatchesPattern() {
804                 Validate.matchesPattern("hi", "[a-z]*");
805             }
806 
807             @Test
808             void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenStringDoesNotMatchPattern() {
809                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.matchesPattern("hi", "[0-9]*"));
810                 assertEquals("The string hi does not match the pattern [0-9]*", ex.getMessage());
811             }
812         }
813     }
814 
815     @Nested
816     final class NoNullElements {
817 
818         @Nested
819         final class WithArray {
820 
821             @Nested
822             final class WithMessage {
823 
824                 @Test
825                 void shouldNotThrowExceptionForNonEmptyArray() {
826                     Validate.noNullElements(new String[] {"a", "b"}, "MSG");
827                 }
828 
829                 @Test
830                 void shouldReturnSameInstance() {
831                     final String[] array = {"a", "b"};
832                     assertSame(array, Validate.noNullElements(array, "MSG"));
833                 }
834 
835                 @Test
836                 void shouldThrowIllegalArgumentExceptionWithGivenMessageForArrayWithNullElement() {
837                     final IllegalArgumentException ex = assertIllegalArgumentException(
838                         () -> Validate.noNullElements(new String[] {"a", null}, "MSG"));
839                     assertEquals("MSG", ex.getMessage());
840                 }
841 
842                 @Test
843                 void shouldThrowNullPointerExceptionWithDefaultMessageForNullArray() {
844                     final NullPointerException ex = assertNullPointerException(() -> Validate.noNullElements((Object[]) null, "MSG"));
845                     assertEquals("array", ex.getMessage());
846                 }
847             }
848 
849             @Nested
850             final class WithoutMessage {
851 
852                 @Test
853                 void shouldNotThrowExceptionForNonEmptyArray() {
854                     Validate.noNullElements(new String[] {"a", "b"});
855                 }
856 
857                 @Test
858                 void shouldReturnSameInstance() {
859                     final String[] expected = {"a", "b"};
860                     assertSame(expected, Validate.noNullElements(expected));
861                 }
862 
863                 @Test
864                 void shouldThrowIllegalArgumentExceptionWithDefaultMessageForArrayWithNullElement() {
865                     final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.noNullElements(new String[] {"a", null}));
866                     assertEquals("The validated array contains null element at index: 1", ex.getMessage());
867                 }
868 
869                 @Test
870                 void shouldThrowNullPointerExceptionWithDefaultMessageForNullArray() {
871                     final NullPointerException ex = assertNullPointerException(() -> Validate.noNullElements((Object[]) null));
872                     assertEquals("array", ex.getMessage());
873                 }
874             }
875         }
876 
877         @Nested
878         final class WithCollection {
879 
880             @Nested
881             final class WithMessage {
882 
883                 @Test
884                 void shouldNotThrowExceptionForNonEmptyCollection() {
885                     Validate.noNullElements(Collections.singleton("a"), "MSG");
886                 }
887 
888                 @Test
889                 void shouldReturnSameInstance() {
890                     final Set<String> col = Collections.singleton("a");
891                     assertSame(col, Validate.noNullElements(col, "MSG"));
892                 }
893 
894                 @Test
895                 void shouldThrowIllegalArgumentExceptionWithGivenMessageForCollectionWithNullElement() {
896                     final IllegalArgumentException ex = assertIllegalArgumentException(
897                         () -> Validate.noNullElements(Collections.singleton(null), "MSG"));
898                     assertEquals("MSG", ex.getMessage());
899                 }
900 
901                 @Test
902                 void shouldThrowNullPointerExceptionWithDefaultMessageForNullCollection() {
903                     final NullPointerException ex = assertNullPointerException(() -> Validate.noNullElements((Collection<?>) null, "MSG"));
904                     assertEquals("iterable", ex.getMessage());
905                 }
906             }
907 
908             @Nested
909             final class WithoutMessage {
910 
911                 @Test
912                 void shouldNotThrowExceptionForNonEmptyCollection() {
913                     Validate.noNullElements(Collections.singleton("a"));
914                 }
915 
916                 @Test
917                 void shouldReturnSameInstance() {
918                     final Set<String> col = Collections.singleton("a");
919                     assertSame(col, Validate.noNullElements(col));
920                 }
921 
922                 @Test
923                 void shouldThrowIllegalArgumentExceptionWithDefaultMessageForCollectionWithNullElement() {
924                     final IllegalArgumentException ex = assertIllegalArgumentException(
925                         () -> Validate.noNullElements(Collections.singleton(null)));
926                     assertEquals("The validated collection contains null element at index: 0", ex.getMessage());
927                 }
928 
929                 @Test
930                 void shouldThrowNullPointerExceptionWithDefaultMessageForNullCollection() {
931                     final NullPointerException ex = assertNullPointerException(() -> Validate.noNullElements((Collection<?>) null));
932                     assertEquals("iterable", ex.getMessage());
933                 }
934             }
935         }
936     }
937 
938     @Nested
939     final class NotBlank {
940 
941         @Nested
942         final class WithMessage {
943 
944             @Test
945             void shouldNotThrowExceptionForNonEmptyString() {
946                 Validate.notBlank("abc", "MSG");
947             }
948 
949             @Test
950             void shouldNotThrowExceptionForNonEmptyStringContainingSpaces() {
951                 Validate.notBlank("  abc   ", "MSG");
952             }
953 
954             @Test
955             void shouldNotThrowExceptionForNonEmptyStringContainingWhitespaceChars() {
956                 Validate.notBlank(" \n \t abc \r \n ", "MSG");
957             }
958 
959             @Test
960             void shouldReturnNonBlankValue() {
961                 assertSame("abc", Validate.notBlank("abc", "MSG"));
962             }
963 
964             @Test
965             void shouldThrowIllegalArgumentExceptionWithGivenMessageForBlankString() {
966                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notBlank("   ", "MSG"));
967                 assertEquals("MSG", ex.getMessage());
968             }
969 
970             @Test
971             void shouldThrowIllegalArgumentExceptionWithGivenMessageForEmptyString() {
972                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notBlank("", "MSG"));
973                 assertEquals("MSG", ex.getMessage());
974             }
975 
976             @Test
977             void shouldThrowIllegalArgumentExceptionWithGivenMessageForStringContainingOnlyWhitespaceChars() {
978                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notBlank(" \n \t \r \n ", "MSG"));
979                 assertEquals("MSG", ex.getMessage());
980             }
981 
982             @Test
983             void shouldThrowNullPointerExceptionWithGivenMessageForNullString() {
984                 final NullPointerException ex = assertNullPointerException(() -> Validate.notBlank(null, "MSG"));
985                 assertEquals("MSG", ex.getMessage());
986             }
987         }
988 
989         @Nested
990         final class WithoutMessage {
991 
992             @Test
993             void shouldNotThrowExceptionForNonEmptyString() {
994                 Validate.notBlank("abc");
995             }
996 
997             @Test
998             void shouldNotThrowExceptionForNonEmptyStringContainingSpaces() {
999                 Validate.notBlank("  abc   ");
1000             }
1001 
1002             @Test
1003             void shouldNotThrowExceptionForNonEmptyStringContainingWhitespaceChars() {
1004                 Validate.notBlank(" \n \t abc \r \n ");
1005             }
1006 
1007             @Test
1008             void shouldReturnNonBlankValue() {
1009                 assertSame("abc", Validate.notBlank("abc"));
1010             }
1011 
1012             @Test
1013             void shouldThrowIllegalArgumentExceptionWithDefaultMessageForBlankString() {
1014                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notBlank("   "));
1015                 assertEquals("The validated character sequence is blank", ex.getMessage());
1016             }
1017 
1018             @Test
1019             void shouldThrowIllegalArgumentExceptionWithDefaultMessageForEmptyString() {
1020                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notBlank(""));
1021                 assertEquals("The validated character sequence is blank", ex.getMessage());
1022             }
1023 
1024             @Test
1025             void shouldThrowIllegalArgumentExceptionWithDefaultMessageForStringContainingOnlyWhitespaceChars() {
1026                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notBlank(" \n \t \r \n "));
1027                 assertEquals("The validated character sequence is blank", ex.getMessage());
1028             }
1029 
1030             @Test
1031             void shouldThrowNullPointerExceptionWithDefaultMessageForNullString() {
1032                 final NullPointerException ex = assertNullPointerException(() -> Validate.notBlank(null));
1033                 assertEquals("The validated character sequence is blank", ex.getMessage());
1034             }
1035         }
1036     }
1037 
1038     @Nested
1039     final class NotEmpty {
1040 
1041         @Nested
1042         final class WithArray {
1043 
1044             @Nested
1045             final class WithMessage {
1046 
1047                 @Test
1048                 void shouldNotThrowExceptionForArrayContainingNullReference() {
1049                     Validate.notEmpty(new Object[] {null}, "MSG");
1050                 }
1051 
1052                 @Test
1053                 void shouldReturnTheSameInstance() {
1054                     final String[] expected = {"hi"};
1055                     assertSame(expected, Validate.notEmpty(expected, "MSG"));
1056                 }
1057 
1058                 @Test
1059                 void shouldThrowIllegalArgumentExceptionWithDefaultMessageForEmptyArray() {
1060                     final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notEmpty(new Object[0], "MSG"));
1061                     assertEquals("MSG", ex.getMessage());
1062                 }
1063 
1064                 @Test
1065                 void shouldThrowNullPointerExceptionWithGivenMessageForNullArray() {
1066                     final NullPointerException ex = assertNullPointerException(() -> Validate.notEmpty((Object[]) null, "MSG"));
1067                     assertEquals("MSG", ex.getMessage());
1068                 }
1069             }
1070 
1071             @Nested
1072             final class WithoutMessage {
1073 
1074                 @Test
1075                 void shouldNotThrowExceptionForArrayContainingNullReference() {
1076                     Validate.notEmpty(new Object[] {null});
1077                 }
1078 
1079                 @Test
1080                 void shouldReturnTheSameInstance() {
1081                     final String[] expected = {"hi"};
1082                     assertSame(expected, Validate.notEmpty(expected));
1083                 }
1084 
1085                 @Test
1086                 void shouldThrowIllegalArgumentExceptionWithDefaultMessageForEmptyArray() {
1087                     final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notEmpty(new Object[0]));
1088                     assertEquals("The validated array is empty", ex.getMessage());
1089                 }
1090 
1091                 @Test
1092                 void shouldThrowNullPointerExceptionWithDefaultMessageForNullArray() {
1093                     final NullPointerException ex = assertNullPointerException(() -> Validate.notEmpty((Object[]) null));
1094                     assertEquals("The validated array is empty", ex.getMessage());
1095                 }
1096             }
1097         }
1098 
1099         @Nested
1100         final class WithCharSequence {
1101 
1102             @Nested
1103             final class WithMessage {
1104 
1105                 @Test
1106                 void shouldNotThrowExceptionForNonEmptyString() {
1107                     Validate.notEmpty("Hi", "MSG");
1108                 }
1109 
1110                 @Test
1111                 void shouldReturnTheSameInstance() {
1112                     assertSame("Hi", Validate.notEmpty("Hi", "MSG"));
1113                 }
1114 
1115                 @Test
1116                 void shouldThrowIllegalArgumentExceptionWithGivenMessageForEmptyString() {
1117                     final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notEmpty("", "MSG"));
1118                     assertEquals("MSG", ex.getMessage());
1119                 }
1120 
1121                 @Test
1122                 void shouldThrowNullPointerExceptionWithGivenMessageForNullCharSequence() {
1123                     final NullPointerException ex = assertNullPointerException(() -> Validate.notEmpty((CharSequence) null, "MSG"));
1124                     assertEquals("MSG", ex.getMessage());
1125                 }
1126             }
1127 
1128             @Nested
1129             final class WithoutMessage {
1130 
1131                 @Test
1132                 void shouldNotThrowExceptionForNonEmptyString() {
1133                     Validate.notEmpty("Hi");
1134                 }
1135 
1136                 @Test
1137                 void shouldReturnTheSameInstance() {
1138                     assertSame("Hi", Validate.notEmpty("Hi"));
1139                 }
1140 
1141                 @Test
1142                 void shouldThrowIllegalArgumentExceptionWithDefaultMessageForEmptyString() {
1143                     final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notEmpty(""));
1144                     assertEquals("The validated character sequence is empty", ex.getMessage());
1145                 }
1146 
1147                 @Test
1148                 void shouldThrowNullPointerExceptionWithDefaultMessageForNullCharSequence() {
1149                     final NullPointerException ex = assertNullPointerException(() -> Validate.notEmpty((CharSequence) null));
1150                     assertEquals("The validated character sequence is empty", ex.getMessage());
1151                 }
1152             }
1153         }
1154 
1155         @Nested
1156         final class WithCollection {
1157 
1158             @Nested
1159             final class WithMessage {
1160 
1161                 @Test
1162                 void shouldNotThrowExceptionForCollectionContainingNullReference() {
1163                     Validate.notEmpty(Collections.singleton(null), "MSG");
1164                 }
1165 
1166                 @Test
1167                 void shouldReturnTheSameInstance() {
1168                     final Set<String> singleton = Collections.singleton("Hi");
1169                     assertSame(singleton, Validate.notEmpty(singleton, "MSG"));
1170                 }
1171 
1172                 @Test
1173                 void shouldThrowIllegalArgumentExceptionWithGivenMessageForEmptyCollection() {
1174                     final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notEmpty(Collections.emptySet(), "MSG"));
1175                     assertEquals("MSG", ex.getMessage());
1176                 }
1177 
1178                 @Test
1179                 void shouldThrowNullPointerExceptionWithGivenMessageForNullCollection() {
1180                     final NullPointerException ex = assertNullPointerException(() -> Validate.notEmpty((Collection<?>) null, "MSG"));
1181                     assertEquals("MSG", ex.getMessage());
1182                 }
1183             }
1184 
1185             @Nested
1186             final class WithoutMessage {
1187 
1188                 @Test
1189                 void shouldNotThrowExceptionForCollectionContainingNullReference() {
1190                     Validate.notEmpty(Collections.singleton(null));
1191                 }
1192 
1193                 @Test
1194                 void shouldReturnTheSameInstance() {
1195                     final Set<String> singleton = Collections.singleton("Hi");
1196                     assertSame(singleton, Validate.notEmpty(singleton));
1197                 }
1198 
1199                 @Test
1200                 void shouldThrowIllegalArgumentExceptionWithDefaultMessageForEmptyCollection() {
1201                     final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notEmpty(Collections.emptySet()));
1202                     assertEquals("The validated collection is empty", ex.getMessage());
1203                 }
1204 
1205                 @Test
1206                 void shouldThrowNullPointerExceptionWithDefaultMessageForNullCollection() {
1207                     final NullPointerException ex = assertNullPointerException(() -> Validate.notEmpty((Collection<?>) null));
1208                     assertEquals("The validated collection is empty", ex.getMessage());
1209                 }
1210             }
1211         }
1212 
1213         @Nested
1214         final class WithMap {
1215 
1216             @Nested
1217             final class WithMessage {
1218 
1219                 @Test
1220                 void shouldNotThrowExceptionForMapContainingNullMapping() {
1221                     Validate.notEmpty(Collections.singletonMap("key", null), "MSG");
1222                 }
1223 
1224                 @Test
1225                 void shouldReturnTheSameInstance() {
1226                     final Map<String, String> singletonMap = Collections.singletonMap("key", "value");
1227                     assertSame(singletonMap, Validate.notEmpty(singletonMap, "MSG"));
1228                 }
1229 
1230                 @Test
1231                 void shouldThrowIllegalArgumentExceptionWithGivenMessageForEmptyMap() {
1232                     final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notEmpty(Collections.emptyMap(), "MSG"));
1233                     assertEquals("MSG", ex.getMessage());
1234                 }
1235 
1236                 @Test
1237                 void shouldThrowNullPointerExceptionWithGivenMessageForNullMap() {
1238                     final NullPointerException ex = assertNullPointerException(() -> Validate.notEmpty((Map<?, ?>) null, "MSG"));
1239                     assertEquals("MSG", ex.getMessage());
1240                 }
1241             }
1242 
1243             @Nested
1244             final class WithoutMessage {
1245 
1246                 @Test
1247                 void shouldNotThrowExceptionForMapContainingNullMapping() {
1248                     Validate.notEmpty(Collections.singletonMap("key", null));
1249                 }
1250 
1251                 @Test
1252                 void shouldReturnTheSameInstance() {
1253                     final Map<String, String> singletonMap = Collections.singletonMap("key", "value");
1254                     assertSame(singletonMap, Validate.notEmpty(singletonMap));
1255                 }
1256 
1257                 @Test
1258                 void shouldThrowIllegalArgumentExceptionWithDefaultMessageForEmptyMap() {
1259                     final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notEmpty(Collections.emptyMap()));
1260                     assertEquals("The validated map is empty", ex.getMessage());
1261                 }
1262 
1263                 @Test
1264                 void shouldThrowNullPointerExceptionWithDefaultMessageForNullMap() {
1265                     final NullPointerException ex = assertNullPointerException(() -> Validate.notEmpty((Map<?, ?>) null));
1266                     assertEquals("The validated map is empty", ex.getMessage());
1267                 }
1268             }
1269         }
1270     }
1271 
1272     @Nested
1273     final class NotNaN {
1274 
1275         @Nested
1276         final class WithMessage {
1277 
1278             @Test
1279             void shouldNotThrowExceptionForNegativeInfinity() {
1280                 Validate.notNaN(Double.NEGATIVE_INFINITY, "MSG");
1281             }
1282 
1283             @Test
1284             void shouldNotThrowExceptionForNumber() {
1285                 Validate.notNaN(0.0, "MSG");
1286             }
1287 
1288             @Test
1289             void shouldNotThrowExceptionForPositiveInfinity() {
1290                 Validate.notNaN(Double.POSITIVE_INFINITY, "MSG");
1291             }
1292 
1293             @Test
1294             void shouldThrowIllegalArgumentExceptionWithGivenMessageForNaN() {
1295                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notNaN(Double.NaN, "MSG"));
1296                 assertEquals("MSG", ex.getMessage());
1297             }
1298         }
1299 
1300         @Nested
1301         final class WithoutMessage {
1302 
1303             @Test
1304             void shouldNotThrowExceptionForNegativeInfinity() {
1305                 Validate.notNaN(Double.NEGATIVE_INFINITY);
1306             }
1307 
1308             @Test
1309             void shouldNotThrowExceptionForNumber() {
1310                 Validate.notNaN(0.0);
1311             }
1312 
1313             @Test
1314             void shouldNotThrowExceptionForPositiveInfinity() {
1315                 Validate.notNaN(Double.POSITIVE_INFINITY);
1316             }
1317 
1318             @Test
1319             void shouldThrowIllegalArgumentExceptionWithDefaultMessageForNaN() {
1320                 final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notNaN(Double.NaN));
1321                 assertEquals("The validated value is not a number", ex.getMessage());
1322             }
1323         }
1324     }
1325 
1326     @Nested
1327     final class NotNull {
1328 
1329         @Nested
1330         final class WithMessage {
1331 
1332             @Test
1333             void shouldNotThrowForNonNullReference() {
1334                 Validate.notNull(new Object(), "MSG");
1335             }
1336 
1337             @Test
1338             void shouldReturnTheSameInstance() {
1339                 assertSame("Hi", Validate.notNull("Hi", "MSG"));
1340             }
1341 
1342             @Test
1343             void shouldThrowExceptionWithGivenMessageForNullReference() {
1344                 final NullPointerException ex = assertNullPointerException(() -> Validate.notNull(null, "MSG"));
1345                 assertEquals("MSG", ex.getMessage());
1346             }
1347         }
1348 
1349         @Nested
1350         final class WithoutMessage {
1351 
1352             @Test
1353             void shouldNotThrowForNonNullReference() {
1354                 Validate.notNull(new Object());
1355             }
1356 
1357             @Test
1358             void shouldReturnTheSameInstance() {
1359                 assertSame("Hi", Validate.notNull("Hi"));
1360             }
1361 
1362             @Test
1363             void shouldThrowExceptionWithDefaultMessageForNullReference() {
1364                 final NullPointerException ex = assertNullPointerException(() -> Validate.notNull(null));
1365                 assertEquals("The validated object is null", ex.getMessage());
1366             }
1367         }
1368     }
1369 
1370     @Nested
1371     final class UtilClassConventions {
1372 
1373         @Test
1374         void hasOnlyOnePublicConstructor() {
1375             final Constructor<?>[] cons = Validate.class.getDeclaredConstructors();
1376             assertEquals(1, cons.length);
1377         }
1378 
1379         @Test
1380         void instancesCanBeConstructed() {
1381             assertNotNull(new Validate());
1382         }
1383 
1384         @Test
1385         void isNonFinalClass() {
1386             assertFalse(Modifier.isFinal(Validate.class.getModifiers()));
1387         }
1388 
1389         @Test
1390         void isPublicClass() {
1391             assertTrue(Modifier.isPublic(Validate.class.getModifiers()));
1392         }
1393     }
1394 
1395     @Nested
1396     final class ValidIndex {
1397 
1398         @Nested
1399         final class WithArray {
1400 
1401             @Nested
1402             final class WithMessage {
1403 
1404                 @Test
1405                 void shouldNotThrowExceptionForValidIndex() {
1406                     Validate.validIndex(new String[] {"a"}, 0, "MSG");
1407                 }
1408 
1409                 @Test
1410                 void shouldReturnSameInstance() {
1411                     final String[] array = {"a"};
1412                     assertSame(array, Validate.validIndex(array, 0, "MSG"));
1413                 }
1414 
1415                 @Test
1416                 void shouldThrowIndexOutOfBoundsExceptionWithGivenMessageForIndexOutOfBounds() {
1417                     final IndexOutOfBoundsException ex = assertIndexOutOfBoundsException(() -> Validate.validIndex(new String[] {"a"}, 1, "MSG"));
1418                     assertEquals("MSG", ex.getMessage());
1419                 }
1420 
1421                 @Test
1422                 void shouldThrowIndexOutOfBoundsExceptionWithGivenMessageForNegativeIndex() {
1423                     final IndexOutOfBoundsException ex = assertIndexOutOfBoundsException(
1424                         () -> Validate.validIndex(new String[] {"a"}, -1, "MSG"));
1425                     assertEquals("MSG", ex.getMessage());
1426                 }
1427 
1428                 @Test
1429                 void shouldThrowNullPointerExceptionWithDefaultMessageForNullArray() {
1430                     final NullPointerException ex = assertNullPointerException(() -> Validate.validIndex((Object[]) null, 1, "MSG"));
1431                     assertEquals("array", ex.getMessage());
1432                 }
1433             }
1434 
1435             @Nested
1436             final class WithoutMessage {
1437 
1438                 @Test
1439                 void shouldNotThrowExceptionForValidIndex() {
1440                     Validate.validIndex(new String[] {"a"}, 0);
1441                 }
1442 
1443                 @Test
1444                 void shouldReturnSameInstance() {
1445                     final String[] array = {"a"};
1446                     assertSame(array, Validate.validIndex(array, 0));
1447                 }
1448 
1449                 @Test
1450                 void shouldThrowIndexOutOfBoundsExceptionWithDefaultMessageForIndexOutOfBounds() {
1451                     final IndexOutOfBoundsException ex = assertIndexOutOfBoundsException(() -> Validate.validIndex(new String[] {"a"}, 1));
1452                     assertEquals("The validated array index is invalid: 1", ex.getMessage());
1453                 }
1454 
1455                 @Test
1456                 void shouldThrowIndexOutOfBoundsExceptionWithDefaultMessageForNegativeIndex() {
1457                     final IndexOutOfBoundsException ex = assertIndexOutOfBoundsException(() -> Validate.validIndex(new String[] {"a"}, -1));
1458                     assertEquals("The validated array index is invalid: -1", ex.getMessage());
1459                 }
1460 
1461                 @Test
1462                 void shouldThrowNullPointerExceptionWithDefaultForNullArray() {
1463                     final NullPointerException ex = assertNullPointerException(() -> Validate.validIndex((Object[]) null, 1));
1464                     assertEquals("array", ex.getMessage());
1465                 }
1466             }
1467         }
1468 
1469         @Nested
1470         final class WithCharSequence {
1471 
1472             @Nested
1473             final class WithMessage {
1474 
1475                 @Test
1476                 void shouldNotThrowExceptionForValidIndex() {
1477                     Validate.validIndex("a", 0, "MSG");
1478                 }
1479 
1480                 @Test
1481                 void shouldReturnSameInstance() {
1482                     final String str = "a";
1483                     assertSame(str, Validate.validIndex(str, 0, "MSG"));
1484                 }
1485 
1486                 @Test
1487                 void shouldThrowIndexOutOfBoundsExceptionWithGivenMessageForIndexOutOfBounds() {
1488                     final IndexOutOfBoundsException ex = assertIndexOutOfBoundsException(() -> Validate.validIndex("a", 1, "MSG"));
1489                     assertEquals("MSG", ex.getMessage());
1490                 }
1491 
1492                 @Test
1493                 void shouldThrowIndexOutOfBoundsExceptionWithGivenMessageForNegativeIndex() {
1494                     final IndexOutOfBoundsException ex = assertIndexOutOfBoundsException(() -> Validate.validIndex("a", -1, "MSG"));
1495                     assertEquals("MSG", ex.getMessage());
1496                 }
1497 
1498                 @Test
1499                 void shouldThrowNullPointerExceptionWithDefaultMessageForNullStr() {
1500                     final NullPointerException ex = assertNullPointerException(() -> Validate.validIndex((String) null, 1, "MSG"));
1501                     assertEquals("chars", ex.getMessage());
1502                 }
1503             }
1504 
1505             @Nested
1506             final class WithoutMessage {
1507 
1508                 @Test
1509                 void shouldNotThrowExceptionForValidIndex() {
1510                     Validate.validIndex("a", 0);
1511                 }
1512 
1513                 @Test
1514                 void shouldReturnSameInstance() {
1515                     final String str = "a";
1516                     assertSame(str, Validate.validIndex(str, 0));
1517                 }
1518 
1519                 @Test
1520                 void shouldThrowIndexOutOfBoundsExceptionWithDefaultMessageForIndexOutOfBounds() {
1521                     final IndexOutOfBoundsException ex = assertIndexOutOfBoundsException(() -> Validate.validIndex("a", 1));
1522                     assertEquals("The validated character sequence index is invalid: 1", ex.getMessage());
1523                 }
1524 
1525                 @Test
1526                 void shouldThrowIndexOutOfBoundsExceptionWithDefaultMessageForNegativeIndex() {
1527                     final IndexOutOfBoundsException ex = assertIndexOutOfBoundsException(() -> Validate.validIndex("a", -1));
1528                     assertEquals("The validated character sequence index is invalid: -1", ex.getMessage());
1529                 }
1530 
1531                 @Test
1532                 void shouldThrowNullPointerExceptionWithDefaultForNullString() {
1533                     final NullPointerException ex = assertNullPointerException(() -> Validate.validIndex((String) null, 1));
1534                     assertEquals("chars", ex.getMessage());
1535                 }
1536             }
1537         }
1538 
1539         @Nested
1540         final class WithCollection {
1541 
1542             @Nested
1543             final class WithMessage {
1544 
1545                 @Test
1546                 void shouldNotThrowExceptionForValidIndex() {
1547                     Validate.validIndex(Collections.singleton("a"), 0, "MSG");
1548                 }
1549 
1550                 @Test
1551                 void shouldReturnSameInstance() {
1552                     final Set<String> col = Collections.singleton("a");
1553                     assertSame(col, Validate.validIndex(col, 0, "MSG"));
1554                 }
1555 
1556                 @Test
1557                 void shouldThrowIndexOutOfBoundsExceptionWithGivenMessageForIndexOutOfBounds() {
1558                     final IndexOutOfBoundsException ex = assertIndexOutOfBoundsException(() -> Validate.validIndex(Collections.singleton("a"), 1, "MSG"));
1559                     assertEquals("MSG", ex.getMessage());
1560                 }
1561 
1562                 @Test
1563                 void shouldThrowIndexOutOfBoundsExceptionWithGivenMessageForNegativeIndex() {
1564                     final IndexOutOfBoundsException ex = assertIndexOutOfBoundsException(
1565                         () -> Validate.validIndex(Collections.singleton("a"), -1, "MSG"));
1566                     assertEquals("MSG", ex.getMessage());
1567                 }
1568 
1569                 @Test
1570                 void shouldThrowNullPointerExceptionWithDefaultMessageForNullCollection() {
1571                     final NullPointerException ex = assertNullPointerException(() -> Validate.validIndex((Collection<?>) null, 1, "MSG"));
1572                     assertEquals("collection", ex.getMessage());
1573                 }
1574             }
1575 
1576             @Nested
1577             final class WithoutMessage {
1578 
1579                 @Test
1580                 void shouldNotThrowExceptionForValidIndex() {
1581                     Validate.validIndex(Collections.singleton("a"), 0);
1582                 }
1583 
1584                 @Test
1585                 void shouldReturnSameInstance() {
1586                     final Set<String> col = Collections.singleton("a");
1587                     assertSame(col, Validate.validIndex(col, 0));
1588                 }
1589 
1590                 @Test
1591                 void shouldThrowIndexOutOfBoundsExceptionWithDefaultMessageForIndexOutOfBounds() {
1592                     final IndexOutOfBoundsException ex = assertIndexOutOfBoundsException(
1593                         () -> Validate.validIndex(Collections.singleton("a"), 1));
1594                     assertEquals("The validated collection index is invalid: 1", ex.getMessage());
1595                 }
1596 
1597                 @Test
1598                 void shouldThrowIndexOutOfBoundsExceptionWithDefaultMessageForNegativeIndex() {
1599                     final IndexOutOfBoundsException ex = assertIndexOutOfBoundsException(
1600                         () -> Validate.validIndex(Collections.singleton("a"), -1));
1601                     assertEquals("The validated collection index is invalid: -1", ex.getMessage());
1602                 }
1603 
1604                 @Test
1605                 void shouldThrowNullPointerExceptionWithDefaultForNullCollection() {
1606                     final NullPointerException ex = assertNullPointerException(() -> Validate.validIndex((Collection<?>) null, 1));
1607                     assertEquals("collection", ex.getMessage());
1608                 }
1609             }
1610         }
1611     }
1612 
1613     @Nested
1614     final class ValidState {
1615 
1616         @Nested
1617         final class WithoutMessage {
1618 
1619             @Test
1620             void shouldNotThrowExceptionForTrueExpression() {
1621                 Validate.validState(true);
1622             }
1623 
1624             @Test
1625             void shouldThrowExceptionForTrueExpression() {
1626                 assertThrows(IllegalStateException.class, () -> Validate.validState(false));
1627             }
1628 
1629         }
1630 
1631         @Nested
1632         final class WitMessage {
1633             @Test
1634             void shouldNotThrowExceptionForValidIndex() {
1635                 Validate.validState(true, "The Message");
1636             }
1637 
1638             @Test
1639             void shouldThrowExceptionForTrueExpression() {
1640                 assertThrows(IllegalStateException.class, () -> Validate.validState(false, "The Message"));
1641             }
1642 
1643         }
1644     }
1645 }