1 package org.testng; 2 3 import static org.testng.internal.EclipseInterface.ASSERT_LEFT; 4 import static org.testng.internal.EclipseInterface.ASSERT_LEFT2; 5 import static org.testng.internal.EclipseInterface.ASSERT_MIDDLE; 6 import static org.testng.internal.EclipseInterface.ASSERT_RIGHT; 7 8 import org.testng.collections.Lists; 9 10 import java.lang.reflect.Array; 11 import java.util.Arrays; 12 import java.util.Collection; 13 import java.util.Iterator; 14 import java.util.List; 15 import java.util.Map; 16 import java.util.Set; 17 18 19 /** 20 * Assertion tool class. Presents assertion methods with a more natural parameter order. 21 * The order is always <B>actualValue</B>, <B>expectedValue</B> [, message]. 22 * 23 * @author <a href='mailto:the_mindstorm@evolva.ro'>Alexandru Popescu</a> 24 */ 25 public class Assert { 26 27 /** 28 * Protect constructor since it is a static only class 29 */ Assert()30 protected Assert() { 31 // hide constructor 32 } 33 34 /** 35 * Asserts that a condition is true. If it isn't, 36 * an AssertionError, with the given message, is thrown. 37 * @param condition the condition to evaluate 38 * @param message the assertion error message 39 */ assertTrue(boolean condition, String message)40 static public void assertTrue(boolean condition, String message) { 41 if(!condition) { 42 failNotEquals(condition, Boolean.TRUE, message); 43 } 44 } 45 46 /** 47 * Asserts that a condition is true. If it isn't, 48 * an AssertionError is thrown. 49 * @param condition the condition to evaluate 50 */ assertTrue(boolean condition)51 static public void assertTrue(boolean condition) { 52 assertTrue(condition, null); 53 } 54 55 /** 56 * Asserts that a condition is false. If it isn't, 57 * an AssertionError, with the given message, is thrown. 58 * @param condition the condition to evaluate 59 * @param message the assertion error message 60 */ assertFalse(boolean condition, String message)61 static public void assertFalse(boolean condition, String message) { 62 if(condition) { 63 failNotEquals(condition, Boolean.FALSE, message); // TESTNG-81 64 } 65 } 66 67 /** 68 * Asserts that a condition is false. If it isn't, 69 * an AssertionError is thrown. 70 * @param condition the condition to evaluate 71 */ assertFalse(boolean condition)72 static public void assertFalse(boolean condition) { 73 assertFalse(condition, null); 74 } 75 76 /** 77 * Fails a test with the given message and wrapping the original exception. 78 * 79 * @param message the assertion error message 80 * @param realCause the original exception 81 */ fail(String message, Throwable realCause)82 static public void fail(String message, Throwable realCause) { 83 AssertionError ae = new AssertionError(message); 84 ae.initCause(realCause); 85 86 throw ae; 87 } 88 89 /** 90 * Fails a test with the given message. 91 * @param message the assertion error message 92 */ fail(String message)93 static public void fail(String message) { 94 throw new AssertionError(message); 95 } 96 97 /** 98 * Fails a test with no message. 99 */ fail()100 static public void fail() { 101 fail(null); 102 } 103 104 /** 105 * Asserts that two objects are equal. If they are not, 106 * an AssertionError, with the given message, is thrown. 107 * @param actual the actual value 108 * @param expected the expected value 109 * @param message the assertion error message 110 */ assertEquals(Object actual, Object expected, String message)111 static public void assertEquals(Object actual, Object expected, String message) { 112 if (expected != null && expected.getClass().isArray()) { 113 assertArrayEquals(actual, expected, message); 114 return; 115 } 116 assertEqualsImpl(actual, expected, message); 117 } 118 119 /** 120 * Differs from {@link #assertEquals(Object, Object, String)} by not taking arrays into 121 * special consideration hence comparing them by reference. Intended to be called directly 122 * to test equality of collections content. 123 */ assertEqualsImpl(Object actual, Object expected, String message)124 private static void assertEqualsImpl(Object actual, Object expected, 125 String message) { 126 if((expected == null) && (actual == null)) { 127 return; 128 } 129 if(expected == null ^ actual == null) { 130 failNotEquals(actual, expected, message); 131 } 132 if (expected.equals(actual) && actual.equals(expected)) { 133 return; 134 } 135 failNotEquals(actual, expected, message); 136 } 137 assertArrayEquals(Object actual, Object expected, String message)138 private static void assertArrayEquals(Object actual, Object expected, String message) { 139 if (expected == actual) { 140 return; 141 } 142 if (null == expected) { 143 fail("expected a null array, but not null found. " + message); 144 } 145 if (null == actual) { 146 fail("expected not null array, but null found. " + message); 147 } 148 //is called only when expected is an array 149 if (actual.getClass().isArray()) { 150 int expectedLength = Array.getLength(expected); 151 if (expectedLength == Array.getLength(actual)) { 152 for (int i = 0 ; i < expectedLength ; i++) { 153 Object _actual = Array.get(actual, i); 154 Object _expected = Array.get(expected, i); 155 try { 156 assertEquals(_actual, _expected); 157 } catch (AssertionError ae) { 158 failNotEquals(actual, expected, message == null ? "" : message 159 + " (values at index " + i + " are not the same)"); 160 } 161 } 162 //array values matched 163 return; 164 } else { 165 failNotEquals(Array.getLength(actual), expectedLength, message == null ? "" : message 166 + " (Array lengths are not the same)"); 167 } 168 } 169 failNotEquals(actual, expected, message); 170 } 171 172 /** 173 * Asserts that two objects are equal. If they are not, 174 * an AssertionError is thrown. 175 * @param actual the actual value 176 * @param expected the expected value 177 */ assertEquals(Object actual, Object expected)178 static public void assertEquals(Object actual, Object expected) { 179 assertEquals(actual, expected, null); 180 } 181 182 /** 183 * Asserts that two Strings are equal. If they are not, 184 * an AssertionError, with the given message, is thrown. 185 * @param actual the actual value 186 * @param expected the expected value 187 * @param message the assertion error message 188 */ assertEquals(String actual, String expected, String message)189 static public void assertEquals(String actual, String expected, String message) { 190 assertEquals((Object) actual, (Object) expected, message); 191 } 192 193 /** 194 * Asserts that two Strings are equal. If they are not, 195 * an AssertionError is thrown. 196 * @param actual the actual value 197 * @param expected the expected value 198 */ assertEquals(String actual, String expected)199 static public void assertEquals(String actual, String expected) { 200 assertEquals(actual, expected, null); 201 } 202 203 /** 204 * Asserts that two doubles are equal concerning a delta. If they are not, 205 * an AssertionError, with the given message, is thrown. If the expected 206 * value is infinity then the delta value is ignored. 207 * @param actual the actual value 208 * @param expected the expected value 209 * @param delta the absolute tolerable difference between the actual and expected values 210 * @param message the assertion error message 211 */ assertEquals(double actual, double expected, double delta, String message)212 static public void assertEquals(double actual, double expected, double delta, String message) { 213 // handle infinity specially since subtracting to infinite values gives NaN and the 214 // the following test fails 215 if(Double.isInfinite(expected)) { 216 if(!(expected == actual)) { 217 failNotEquals(actual, expected, message); 218 } 219 } 220 else if (Double.isNaN(expected)) { 221 if (!Double.isNaN(actual)) { 222 failNotEquals(actual, expected, message); 223 } 224 } 225 else if(!(Math.abs(expected - actual) <= delta)) { 226 failNotEquals(actual, expected, message); 227 } 228 } 229 230 /** 231 * Asserts that two doubles are equal concerning a delta. If they are not, 232 * an AssertionError is thrown. If the expected value is infinity then the 233 * delta value is ignored. 234 * @param actual the actual value 235 * @param expected the expected value 236 * @param delta the absolute tolerable difference between the actual and expected values 237 */ assertEquals(double actual, double expected, double delta)238 static public void assertEquals(double actual, double expected, double delta) { 239 assertEquals(actual, expected, delta, null); 240 } 241 242 /** 243 * Asserts that two floats are equal concerning a delta. If they are not, 244 * an AssertionError, with the given message, is thrown. If the expected 245 * value is infinity then the delta value is ignored. 246 * @param actual the actual value 247 * @param expected the expected value 248 * @param delta the absolute tolerable difference between the actual and expected values 249 * @param message the assertion error message 250 */ assertEquals(float actual, float expected, float delta, String message)251 static public void assertEquals(float actual, float expected, float delta, String message) { 252 // handle infinity specially since subtracting to infinite values gives NaN and the 253 // the following test fails 254 if(Float.isInfinite(expected)) { 255 if(!(expected == actual)) { 256 failNotEquals(actual, expected, message); 257 } 258 } 259 else if(!(Math.abs(expected - actual) <= delta)) { 260 failNotEquals(actual, expected, message); 261 } 262 } 263 264 /** 265 * Asserts that two floats are equal concerning a delta. If they are not, 266 * an AssertionError is thrown. If the expected 267 * value is infinity then the delta value is ignored. 268 * @param actual the actual value 269 * @param expected the expected value 270 * @param delta the absolute tolerable difference between the actual and expected values 271 */ assertEquals(float actual, float expected, float delta)272 static public void assertEquals(float actual, float expected, float delta) { 273 assertEquals(actual, expected, delta, null); 274 } 275 276 /** 277 * Asserts that two longs are equal. If they are not, 278 * an AssertionError, with the given message, is thrown. 279 * @param actual the actual value 280 * @param expected the expected value 281 * @param message the assertion error message 282 */ assertEquals(long actual, long expected, String message)283 static public void assertEquals(long actual, long expected, String message) { 284 assertEquals(Long.valueOf(actual), Long.valueOf(expected), message); 285 } 286 287 /** 288 * Asserts that two longs are equal. If they are not, 289 * an AssertionError is thrown. 290 * @param actual the actual value 291 * @param expected the expected value 292 */ assertEquals(long actual, long expected)293 static public void assertEquals(long actual, long expected) { 294 assertEquals(actual, expected, null); 295 } 296 297 /** 298 * Asserts that two booleans are equal. If they are not, 299 * an AssertionError, with the given message, is thrown. 300 * @param actual the actual value 301 * @param expected the expected value 302 * @param message the assertion error message 303 */ assertEquals(boolean actual, boolean expected, String message)304 static public void assertEquals(boolean actual, boolean expected, String message) { 305 assertEquals( Boolean.valueOf(actual), Boolean.valueOf(expected), message); 306 } 307 308 /** 309 * Asserts that two booleans are equal. If they are not, 310 * an AssertionError is thrown. 311 * @param actual the actual value 312 * @param expected the expected value 313 */ assertEquals(boolean actual, boolean expected)314 static public void assertEquals(boolean actual, boolean expected) { 315 assertEquals(actual, expected, null); 316 } 317 318 /** 319 * Asserts that two bytes are equal. If they are not, 320 * an AssertionError, with the given message, is thrown. 321 * @param actual the actual value 322 * @param expected the expected value 323 * @param message the assertion error message 324 */ assertEquals(byte actual, byte expected, String message)325 static public void assertEquals(byte actual, byte expected, String message) { 326 assertEquals(Byte.valueOf(actual), Byte.valueOf(expected), message); 327 } 328 329 /** 330 * Asserts that two bytes are equal. If they are not, 331 * an AssertionError is thrown. 332 * @param actual the actual value 333 * @param expected the expected value 334 */ assertEquals(byte actual, byte expected)335 static public void assertEquals(byte actual, byte expected) { 336 assertEquals(actual, expected, null); 337 } 338 339 /** 340 * Asserts that two chars are equal. If they are not, 341 * an AssertionFailedError, with the given message, is thrown. 342 * @param actual the actual value 343 * @param expected the expected value 344 * @param message the assertion error message 345 */ assertEquals(char actual, char expected, String message)346 static public void assertEquals(char actual, char expected, String message) { 347 assertEquals(Character.valueOf(actual), Character.valueOf(expected), message); 348 } 349 350 /** 351 * Asserts that two chars are equal. If they are not, 352 * an AssertionError is thrown. 353 * @param actual the actual value 354 * @param expected the expected value 355 */ assertEquals(char actual, char expected)356 static public void assertEquals(char actual, char expected) { 357 assertEquals(actual, expected, null); 358 } 359 360 /** 361 * Asserts that two shorts are equal. If they are not, 362 * an AssertionFailedError, with the given message, is thrown. 363 * @param actual the actual value 364 * @param expected the expected value 365 * @param message the assertion error message 366 */ assertEquals(short actual, short expected, String message)367 static public void assertEquals(short actual, short expected, String message) { 368 assertEquals(Short.valueOf(actual), Short.valueOf(expected), message); 369 } 370 371 /** 372 * Asserts that two shorts are equal. If they are not, 373 * an AssertionError is thrown. 374 * @param actual the actual value 375 * @param expected the expected value 376 */ assertEquals(short actual, short expected)377 static public void assertEquals(short actual, short expected) { 378 assertEquals(actual, expected, null); 379 } 380 381 /** 382 * Asserts that two ints are equal. If they are not, 383 * an AssertionFailedError, with the given message, is thrown. 384 * @param actual the actual value 385 * @param expected the expected value 386 * @param message the assertion error message 387 */ assertEquals(int actual, int expected, String message)388 static public void assertEquals(int actual, int expected, String message) { 389 assertEquals(Integer.valueOf(actual), Integer.valueOf(expected), message); 390 } 391 392 /** 393 * Asserts that two ints are equal. If they are not, 394 * an AssertionError is thrown. 395 * @param actual the actual value 396 * @param expected the expected value 397 */ assertEquals(int actual, int expected)398 static public void assertEquals(int actual, int expected) { 399 assertEquals(actual, expected, null); 400 } 401 402 /** 403 * Asserts that an object isn't null. If it is, 404 * an AssertionError is thrown. 405 * @param object the assertion object 406 */ assertNotNull(Object object)407 static public void assertNotNull(Object object) { 408 assertNotNull(object, null); 409 } 410 411 /** 412 * Asserts that an object isn't null. If it is, 413 * an AssertionFailedError, with the given message, is thrown. 414 * @param object the assertion object 415 * @param message the assertion error message 416 */ assertNotNull(Object object, String message)417 static public void assertNotNull(Object object, String message) { 418 if (object == null) { 419 String formatted = ""; 420 if(message != null) { 421 formatted = message + " "; 422 } 423 fail(formatted + "expected object to not be null"); 424 } 425 assertTrue(object != null, message); 426 } 427 428 /** 429 * Asserts that an object is null. If it is not, 430 * an AssertionError, with the given message, is thrown. 431 * @param object the assertion object 432 */ assertNull(Object object)433 static public void assertNull(Object object) { 434 assertNull(object, null); 435 } 436 437 /** 438 * Asserts that an object is null. If it is not, 439 * an AssertionFailedError, with the given message, is thrown. 440 * @param object the assertion object 441 * @param message the assertion error message 442 */ assertNull(Object object, String message)443 static public void assertNull(Object object, String message) { 444 if (object != null) { 445 failNotSame(object, null, message); 446 } 447 } 448 449 /** 450 * Asserts that two objects refer to the same object. If they do not, 451 * an AssertionFailedError, with the given message, is thrown. 452 * @param actual the actual value 453 * @param expected the expected value 454 * @param message the assertion error message 455 */ assertSame(Object actual, Object expected, String message)456 static public void assertSame(Object actual, Object expected, String message) { 457 if(expected == actual) { 458 return; 459 } 460 failNotSame(actual, expected, message); 461 } 462 463 /** 464 * Asserts that two objects refer to the same object. If they do not, 465 * an AssertionError is thrown. 466 * @param actual the actual value 467 * @param expected the expected value 468 */ assertSame(Object actual, Object expected)469 static public void assertSame(Object actual, Object expected) { 470 assertSame(actual, expected, null); 471 } 472 473 /** 474 * Asserts that two objects do not refer to the same objects. If they do, 475 * an AssertionError, with the given message, is thrown. 476 * @param actual the actual value 477 * @param expected the expected value 478 * @param message the assertion error message 479 */ assertNotSame(Object actual, Object expected, String message)480 static public void assertNotSame(Object actual, Object expected, String message) { 481 if(expected == actual) { 482 failSame(actual, expected, message); 483 } 484 } 485 486 /** 487 * Asserts that two objects do not refer to the same object. If they do, 488 * an AssertionError is thrown. 489 * @param actual the actual value 490 * @param expected the expected value 491 */ assertNotSame(Object actual, Object expected)492 static public void assertNotSame(Object actual, Object expected) { 493 assertNotSame(actual, expected, null); 494 } 495 failSame(Object actual, Object expected, String message)496 static private void failSame(Object actual, Object expected, String message) { 497 String formatted = ""; 498 if(message != null) { 499 formatted = message + " "; 500 } 501 fail(formatted + ASSERT_LEFT2 + expected + ASSERT_MIDDLE + actual + ASSERT_RIGHT); 502 } 503 failNotSame(Object actual, Object expected, String message)504 static private void failNotSame(Object actual, Object expected, String message) { 505 String formatted = ""; 506 if(message != null) { 507 formatted = message + " "; 508 } 509 fail(formatted + ASSERT_LEFT + expected + ASSERT_MIDDLE + actual + ASSERT_RIGHT); 510 } 511 failNotEquals(Object actual , Object expected, String message )512 static private void failNotEquals(Object actual , Object expected, String message ) { 513 fail(format(actual, expected, message)); 514 } 515 format(Object actual, Object expected, String message)516 static String format(Object actual, Object expected, String message) { 517 String formatted = ""; 518 if (null != message) { 519 formatted = message + " "; 520 } 521 522 return formatted + ASSERT_LEFT + expected + ASSERT_MIDDLE + actual + ASSERT_RIGHT; 523 } 524 525 /** 526 * Asserts that two collections contain the same elements in the same order. If they do not, 527 * an AssertionError is thrown. 528 * 529 * @param actual the actual value 530 * @param expected the expected value 531 */ assertEquals(Collection<?> actual, Collection<?> expected)532 static public void assertEquals(Collection<?> actual, Collection<?> expected) { 533 assertEquals(actual, expected, null); 534 } 535 536 /** 537 * Asserts that two collections contain the same elements in the same order. If they do not, 538 * an AssertionError, with the given message, is thrown. 539 * @param actual the actual value 540 * @param expected the expected value 541 * @param message the assertion error message 542 */ assertEquals(Collection<?> actual, Collection<?> expected, String message)543 static public void assertEquals(Collection<?> actual, Collection<?> expected, String message) { 544 if(actual == expected) { 545 return; 546 } 547 548 if (actual == null || expected == null) { 549 if (message != null) { 550 fail(message); 551 } else { 552 fail("Collections not equal: expected: " + expected + " and actual: " + actual); 553 } 554 } 555 556 assertEquals(actual.size(), expected.size(), (message == null ? "" : message + ": ") + "lists don't have the same size"); 557 558 Iterator<?> actIt = actual.iterator(); 559 Iterator<?> expIt = expected.iterator(); 560 int i = -1; 561 while(actIt.hasNext() && expIt.hasNext()) { 562 i++; 563 Object e = expIt.next(); 564 Object a = actIt.next(); 565 String explanation = "Lists differ at element [" + i + "]: " + e + " != " + a; 566 String errorMessage = message == null ? explanation : message + ": " + explanation; 567 568 assertEqualsImpl(a, e, errorMessage); 569 } 570 } 571 572 /** Asserts that two iterators return the same elements in the same order. If they do not, 573 * an AssertionError is thrown. 574 * Please note that this assert iterates over the elements and modifies the state of the iterators. 575 * @param actual the actual value 576 * @param expected the expected value 577 */ assertEquals(Iterator<?> actual, Iterator<?> expected)578 static public void assertEquals(Iterator<?> actual, Iterator<?> expected) { 579 assertEquals(actual, expected, null); 580 } 581 582 /** Asserts that two iterators return the same elements in the same order. If they do not, 583 * an AssertionError, with the given message, is thrown. 584 * Please note that this assert iterates over the elements and modifies the state of the iterators. 585 * @param actual the actual value 586 * @param expected the expected value 587 * @param message the assertion error message 588 */ assertEquals(Iterator<?> actual, Iterator<?> expected, String message)589 static public void assertEquals(Iterator<?> actual, Iterator<?> expected, String message) { 590 if(actual == expected) { 591 return; 592 } 593 594 if(actual == null || expected == null) { 595 if(message != null) { 596 fail(message); 597 } else { 598 fail("Iterators not equal: expected: " + expected + " and actual: " + actual); 599 } 600 } 601 602 int i = -1; 603 while(actual.hasNext() && expected.hasNext()) { 604 605 i++; 606 Object e = expected.next(); 607 Object a = actual.next(); 608 String explanation = "Iterators differ at element [" + i + "]: " + e + " != " + a; 609 String errorMessage = message == null ? explanation : message + ": " + explanation; 610 611 assertEqualsImpl(a, e, errorMessage); 612 613 } 614 615 if(actual.hasNext()) { 616 617 String explanation = "Actual iterator returned more elements than the expected iterator."; 618 String errorMessage = message == null ? explanation : message + ": " + explanation; 619 fail(errorMessage); 620 621 } else if(expected.hasNext()) { 622 623 String explanation = "Expected iterator returned more elements than the actual iterator."; 624 String errorMessage = message == null ? explanation : message + ": " + explanation; 625 fail(errorMessage); 626 627 } 628 629 } 630 631 /** Asserts that two iterables return iterators with the same elements in the same order. If they do not, 632 * an AssertionError is thrown. 633 * @param actual the actual value 634 * @param expected the expected value 635 */ assertEquals(Iterable<?> actual, Iterable<?> expected)636 static public void assertEquals(Iterable<?> actual, Iterable<?> expected) { 637 assertEquals(actual, expected, null); 638 } 639 640 /** Asserts that two iterables return iterators with the same elements in the same order. If they do not, 641 * an AssertionError, with the given message, is thrown. 642 * @param actual the actual value 643 * @param expected the expected value 644 * @param message the assertion error message 645 */ assertEquals(Iterable<?> actual, Iterable<?> expected, String message)646 static public void assertEquals(Iterable<?> actual, Iterable<?> expected, String message) { 647 if(actual == expected) { 648 return; 649 } 650 651 if(actual == null || expected == null) { 652 if(message != null) { 653 fail(message); 654 } else { 655 fail("Iterables not equal: expected: " + expected + " and actual: " + actual); 656 } 657 } 658 659 Iterator<?> actIt = actual.iterator(); 660 Iterator<?> expIt = expected.iterator(); 661 662 assertEquals(actIt, expIt, message); 663 } 664 665 666 667 668 /** 669 * Asserts that two arrays contain the same elements in the same order. If they do not, 670 * an AssertionError, with the given message, is thrown. 671 * @param actual the actual value 672 * @param expected the expected value 673 * @param message the assertion error message 674 */ assertEquals(Object[] actual, Object[] expected, String message)675 static public void assertEquals(Object[] actual, Object[] expected, String message) { 676 if(actual == expected) { 677 return; 678 } 679 680 if ((actual == null && expected != null) || (actual != null && expected == null)) { 681 if (message != null) { 682 fail(message); 683 } else { 684 fail("Arrays not equal: " + Arrays.toString(expected) + " and " + Arrays.toString(actual)); 685 } 686 } 687 assertEquals(Arrays.asList(actual), Arrays.asList(expected), message); 688 } 689 690 /** 691 * Asserts that two arrays contain the same elements in no particular order. If they do not, 692 * an AssertionError, with the given message, is thrown. 693 * @param actual the actual value 694 * @param expected the expected value 695 * @param message the assertion error message 696 */ assertEqualsNoOrder(Object[] actual, Object[] expected, String message)697 static public void assertEqualsNoOrder(Object[] actual, Object[] expected, String message) { 698 if(actual == expected) { 699 return; 700 } 701 702 if ((actual == null && expected != null) || (actual != null && expected == null)) { 703 failAssertNoEqual( 704 "Arrays not equal: " + Arrays.toString(expected) + " and " + Arrays.toString(actual), 705 message); 706 } 707 708 if (actual.length != expected.length) { 709 failAssertNoEqual( 710 "Arrays do not have the same size:" + actual.length + " != " + expected.length, 711 message); 712 } 713 714 List<Object> actualCollection = Lists.newArrayList(); 715 for (Object a : actual) { 716 actualCollection.add(a); 717 } 718 for (Object o : expected) { 719 actualCollection.remove(o); 720 } 721 if (actualCollection.size() != 0) { 722 failAssertNoEqual( 723 "Arrays not equal: " + Arrays.toString(expected) + " and " + Arrays.toString(actual), 724 message); 725 } 726 } 727 failAssertNoEqual(String defaultMessage, String message)728 private static void failAssertNoEqual(String defaultMessage, String message) { 729 if (message != null) { 730 fail(message); 731 } else { 732 fail(defaultMessage); 733 } 734 } 735 736 /** 737 * Asserts that two arrays contain the same elements in the same order. If they do not, 738 * an AssertionError is thrown. 739 * 740 * @param actual the actual value 741 * @param expected the expected value 742 */ assertEquals(Object[] actual, Object[] expected)743 static public void assertEquals(Object[] actual, Object[] expected) { 744 assertEquals(actual, expected, null); 745 } 746 747 /** 748 * Asserts that two arrays contain the same elements in no particular order. If they do not, 749 * an AssertionError is thrown. 750 * @param actual the actual value 751 * @param expected the expected value 752 */ assertEqualsNoOrder(Object[] actual, Object[] expected)753 static public void assertEqualsNoOrder(Object[] actual, Object[] expected) { 754 assertEqualsNoOrder(actual, expected, null); 755 } 756 757 /** 758 * Asserts that two sets are equal. 759 */ assertEquals(Set<?> actual, Set<?> expected)760 static public void assertEquals(Set<?> actual, Set<?> expected) { 761 assertEquals(actual, expected, null); 762 } 763 764 /** 765 * Assert set equals 766 */ assertEquals(Set<?> actual, Set<?> expected, String message)767 static public void assertEquals(Set<?> actual, Set<?> expected, String message) { 768 if (actual == expected) { 769 return; 770 } 771 772 if (actual == null || expected == null) { 773 // Keep the back compatible 774 if (message == null) { 775 fail("Sets not equal: expected: " + expected + " and actual: " + actual); 776 } else { 777 failNotEquals(actual, expected, message); 778 } 779 } 780 781 if (!actual.equals(expected)) { 782 if (message == null) { 783 fail("Sets differ: expected " + expected + " but got " + actual); 784 } else { 785 failNotEquals(actual, expected, message); 786 } 787 } 788 } 789 790 /** 791 * Asserts that two maps are equal. 792 */ assertEquals(Map<?, ?> actual, Map<?, ?> expected)793 static public void assertEquals(Map<?, ?> actual, Map<?, ?> expected) { 794 if (actual == expected) { 795 return; 796 } 797 798 if (actual == null || expected == null) { 799 fail("Maps not equal: expected: " + expected + " and actual: " + actual); 800 } 801 802 if (actual.size() != expected.size()) { 803 fail("Maps do not have the same size:" + actual.size() + " != " + expected.size()); 804 } 805 806 Set<?> entrySet = actual.entrySet(); 807 for (Object anEntrySet : entrySet) { 808 Map.Entry<?, ?> entry = (Map.Entry<?, ?>) anEntrySet; 809 Object key = entry.getKey(); 810 Object value = entry.getValue(); 811 Object expectedValue = expected.get(key); 812 assertEqualsImpl(value, expectedValue, "Maps do not match for key:" + key + " actual:" + value 813 + " expected:" + expectedValue); 814 } 815 816 } 817 818 ///// 819 // assertNotEquals 820 // 821 assertNotEquals(Object actual1, Object actual2, String message)822 public static void assertNotEquals(Object actual1, Object actual2, String message) { 823 boolean fail = false; 824 try { 825 Assert.assertEquals(actual1, actual2); 826 fail = true; 827 } catch (AssertionError e) { 828 } 829 830 if (fail) { 831 Assert.fail(message); 832 } 833 } 834 assertNotEquals(Object actual1, Object actual2)835 public static void assertNotEquals(Object actual1, Object actual2) { 836 assertNotEquals(actual1, actual2, null); 837 } 838 assertNotEquals(String actual1, String actual2, String message)839 static void assertNotEquals(String actual1, String actual2, String message) { 840 assertNotEquals((Object) actual1, (Object) actual2, message); 841 } 842 assertNotEquals(String actual1, String actual2)843 static void assertNotEquals(String actual1, String actual2) { 844 assertNotEquals(actual1, actual2, null); 845 } 846 assertNotEquals(long actual1, long actual2, String message)847 static void assertNotEquals(long actual1, long actual2, String message) { 848 assertNotEquals(Long.valueOf(actual1), Long.valueOf(actual2), message); 849 } 850 assertNotEquals(long actual1, long actual2)851 static void assertNotEquals(long actual1, long actual2) { 852 assertNotEquals(actual1, actual2, null); 853 } 854 assertNotEquals(boolean actual1, boolean actual2, String message)855 static void assertNotEquals(boolean actual1, boolean actual2, String message) { 856 assertNotEquals(Boolean.valueOf(actual1), Boolean.valueOf(actual2), message); 857 } 858 assertNotEquals(boolean actual1, boolean actual2)859 static void assertNotEquals(boolean actual1, boolean actual2) { 860 assertNotEquals(actual1, actual2, null); 861 } 862 assertNotEquals(byte actual1, byte actual2, String message)863 static void assertNotEquals(byte actual1, byte actual2, String message) { 864 assertNotEquals(Byte.valueOf(actual1), Byte.valueOf(actual2), message); 865 } 866 assertNotEquals(byte actual1, byte actual2)867 static void assertNotEquals(byte actual1, byte actual2) { 868 assertNotEquals(actual1, actual2, null); 869 } 870 assertNotEquals(char actual1, char actual2, String message)871 static void assertNotEquals(char actual1, char actual2, String message) { 872 assertNotEquals(Character.valueOf(actual1), Character.valueOf(actual2), message); 873 } 874 assertNotEquals(char actual1, char actual2)875 static void assertNotEquals(char actual1, char actual2) { 876 assertNotEquals(actual1, actual2, null); 877 } 878 assertNotEquals(short actual1, short actual2, String message)879 static void assertNotEquals(short actual1, short actual2, String message) { 880 assertNotEquals(Short.valueOf(actual1), Short.valueOf(actual2), message); 881 } 882 assertNotEquals(short actual1, short actual2)883 static void assertNotEquals(short actual1, short actual2) { 884 assertNotEquals(actual1, actual2, null); 885 } 886 assertNotEquals(int actual1, int actual2, String message)887 static void assertNotEquals(int actual1, int actual2, String message) { 888 assertNotEquals(Integer.valueOf(actual1), Integer.valueOf(actual2), message); 889 } 890 assertNotEquals(int actual1, int actual2)891 static void assertNotEquals(int actual1, int actual2) { 892 assertNotEquals(actual1, actual2, null); 893 } 894 assertNotEquals(float actual1, float actual2, float delta, String message)895 static public void assertNotEquals(float actual1, float actual2, float delta, String message) { 896 boolean fail = false; 897 try { 898 Assert.assertEquals(actual1, actual2, delta, message); 899 fail = true; 900 } catch (AssertionError e) { 901 902 } 903 904 if (fail) { 905 Assert.fail(message); 906 } 907 } 908 assertNotEquals(float actual1, float actual2, float delta)909 static public void assertNotEquals(float actual1, float actual2, float delta) { 910 assertNotEquals(actual1, actual2, delta, null); 911 } 912 assertNotEquals(double actual1, double actual2, double delta, String message)913 static public void assertNotEquals(double actual1, double actual2, double delta, String message) { 914 boolean fail = false; 915 try { 916 Assert.assertEquals(actual1, actual2, delta, message); 917 fail = true; 918 } catch (AssertionError e) { 919 920 } 921 922 if (fail) { 923 Assert.fail(message); 924 } 925 } 926 assertNotEquals(double actual1, double actual2, double delta)927 static public void assertNotEquals(double actual1, double actual2, double delta) { 928 assertNotEquals(actual1, actual2, delta, null); 929 } 930 931 /** 932 * This interface facilitates the use of {@link #expectThrows} from Java 8. It allows 933 * method references to both void and non-void methods to be passed directly into 934 * expectThrows without wrapping, even if they declare checked exceptions. 935 * <p/> 936 * This interface is not meant to be implemented directly. 937 */ 938 public interface ThrowingRunnable { run()939 void run() throws Throwable; 940 } 941 942 /** 943 * Asserts that {@code runnable} throws an exception when invoked. If it does not, an 944 * {@link AssertionError} is thrown. 945 * 946 * @param runnable A function that is expected to throw an exception when invoked 947 * @since 6.9.5 948 */ assertThrows(ThrowingRunnable runnable)949 public static void assertThrows(ThrowingRunnable runnable) { 950 assertThrows(Throwable.class, runnable); 951 } 952 953 /** 954 * Asserts that {@code runnable} throws an exception of type {@code throwableClass} when 955 * executed. If it does not throw an exception, an {@link AssertionError} is thrown. If it 956 * throws the wrong type of exception, an {@code AssertionError} is thrown describing the 957 * mismatch; the exception that was actually thrown can be obtained by calling {@link 958 * AssertionError#getCause}. 959 * 960 * @param throwableClass the expected type of the exception 961 * @param runnable A function that is expected to throw an exception when invoked 962 * @since 6.9.5 963 */ 964 @SuppressWarnings("ThrowableResultOfMethodCallIgnored") assertThrows(Class<T> throwableClass, ThrowingRunnable runnable)965 public static <T extends Throwable> void assertThrows(Class<T> throwableClass, ThrowingRunnable runnable) { 966 expectThrows(throwableClass, runnable); 967 } 968 969 /** 970 * Asserts that {@code runnable} throws an exception of type {@code throwableClass} when 971 * executed and returns the exception. If {@code runnable} does not throw an exception, an 972 * {@link AssertionError} is thrown. If it throws the wrong type of exception, an {@code 973 * AssertionError} is thrown describing the mismatch; the exception that was actually thrown can 974 * be obtained by calling {@link AssertionError#getCause}. 975 * 976 * @param throwableClass the expected type of the exception 977 * @param runnable A function that is expected to throw an exception when invoked 978 * @return The exception thrown by {@code runnable} 979 * @since 6.9.5 980 */ expectThrows(Class<T> throwableClass, ThrowingRunnable runnable)981 public static <T extends Throwable> T expectThrows(Class<T> throwableClass, ThrowingRunnable runnable) { 982 try { 983 runnable.run(); 984 } catch (Throwable t) { 985 if (throwableClass.isInstance(t)) { 986 return throwableClass.cast(t); 987 } else { 988 String mismatchMessage = String.format("Expected %s to be thrown, but %s was thrown", 989 throwableClass.getSimpleName(), t.getClass().getSimpleName()); 990 991 throw new AssertionError(mismatchMessage, t); 992 } 993 } 994 String message = String.format("Expected %s to be thrown, but nothing was thrown", 995 throwableClass.getSimpleName()); 996 throw new AssertionError(message); 997 } 998 } 999