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