1 package org.junit; 2 3 import org.hamcrest.Description; 4 import org.hamcrest.Matcher; 5 import org.hamcrest.StringDescription; 6 import org.junit.internal.ArrayComparisonFailure; 7 import org.junit.internal.ExactComparisonCriteria; 8 import org.junit.internal.InexactComparisonCriteria; 9 10 /** 11 * A set of assertion methods useful for writing tests. Only failed assertions 12 * are recorded. These methods can be used directly: 13 * <code>Assert.assertEquals(...)</code>, however, they read better if they 14 * are referenced through static import:<br/> 15 * 16 * <pre> 17 * import static org.junit.Assert.*; 18 * ... 19 * assertEquals(...); 20 * </pre> 21 * 22 * @see AssertionError 23 */ 24 public class Assert { 25 /** 26 * Protect constructor since it is a static only class 27 */ 28 protected Assert() { 29 } 30 31 /** 32 * Asserts that a condition is true. If it isn't it throws an 33 * {@link AssertionError} with the given message. 34 * 35 * @param message 36 * the identifying message for the {@link AssertionError} (<code>null</code> 37 * okay) 38 * @param condition 39 * condition to be checked 40 */ 41 static public void assertTrue(String message, boolean condition) { 42 if (!condition) 43 fail(message); 44 } 45 46 /** 47 * Asserts that a condition is true. If it isn't it throws an 48 * {@link AssertionError} without a message. 49 * 50 * @param condition 51 * condition to be checked 52 */ 53 static public void assertTrue(boolean condition) { 54 assertTrue(null, condition); 55 } 56 57 /** 58 * Asserts that a condition is false. If it isn't it throws an 59 * {@link AssertionError} with the given message. 60 * 61 * @param message 62 * the identifying message for the {@link AssertionError} (<code>null</code> 63 * okay) 64 * @param condition 65 * condition to be checked 66 */ 67 static public void assertFalse(String message, boolean condition) { 68 assertTrue(message, !condition); 69 } 70 71 /** 72 * Asserts that a condition is false. If it isn't it throws an 73 * {@link AssertionError} without a message. 74 * 75 * @param condition 76 * condition to be checked 77 */ 78 static public void assertFalse(boolean condition) { 79 assertFalse(null, condition); 80 } 81 82 /** 83 * Fails a test with the given message. 84 * 85 * @param message 86 * the identifying message for the {@link AssertionError} (<code>null</code> 87 * okay) 88 * @see AssertionError 89 */ 90 static public void fail(String message) { 91 if (message == null) 92 throw new AssertionError(); 93 throw new AssertionError(message); 94 } 95 96 /** 97 * Fails a test with no message. 98 */ 99 static public void fail() { 100 fail(null); 101 } 102 103 /** 104 * Asserts that two objects are equal. If they are not, an 105 * {@link AssertionError} is thrown with the given message. If 106 * <code>expected</code> and <code>actual</code> are <code>null</code>, 107 * they are considered equal. 108 * 109 * @param message 110 * the identifying message for the {@link AssertionError} (<code>null</code> 111 * okay) 112 * @param expected 113 * expected value 114 * @param actual 115 * actual value 116 */ 117 static public void assertEquals(String message, Object expected, 118 Object actual) { 119 if (expected == null && actual == null) 120 return; 121 if (expected != null && isEquals(expected, actual)) 122 return; 123 else if (expected instanceof String && actual instanceof String) { 124 String cleanMessage= message == null ? "" : message; 125 throw new ComparisonFailure(cleanMessage, (String) expected, 126 (String) actual); 127 } else 128 failNotEquals(message, expected, actual); 129 } 130 131 private static boolean isEquals(Object expected, Object actual) { 132 return expected.equals(actual); 133 } 134 135 /** 136 * Asserts that two objects are equal. If they are not, an 137 * {@link AssertionError} without a message is thrown. If 138 * <code>expected</code> and <code>actual</code> are <code>null</code>, 139 * they are considered equal. 140 * 141 * @param expected 142 * expected value 143 * @param actual 144 * the value to check against <code>expected</code> 145 */ 146 static public void assertEquals(Object expected, Object actual) { 147 assertEquals(null, expected, actual); 148 } 149 150 /** 151 * Asserts that two object arrays are equal. If they are not, an 152 * {@link AssertionError} is thrown with the given message. If 153 * <code>expecteds</code> and <code>actuals</code> are <code>null</code>, 154 * they are considered equal. 155 * 156 * @param message 157 * the identifying message for the {@link AssertionError} (<code>null</code> 158 * okay) 159 * @param expecteds 160 * Object array or array of arrays (multi-dimensional array) with 161 * expected values. 162 * @param actuals 163 * Object array or array of arrays (multi-dimensional array) with 164 * actual values 165 */ 166 public static void assertArrayEquals(String message, Object[] expecteds, 167 Object[] actuals) throws ArrayComparisonFailure { 168 internalArrayEquals(message, expecteds, actuals); 169 } 170 171 /** 172 * Asserts that two object arrays are equal. If they are not, an 173 * {@link AssertionError} is thrown. If <code>expected</code> and 174 * <code>actual</code> are <code>null</code>, they are considered 175 * equal. 176 * 177 * @param expecteds 178 * Object array or array of arrays (multi-dimensional array) with 179 * expected values 180 * @param actuals 181 * Object array or array of arrays (multi-dimensional array) with 182 * actual values 183 */ 184 public static void assertArrayEquals(Object[] expecteds, Object[] actuals) { 185 assertArrayEquals(null, expecteds, actuals); 186 } 187 188 /** 189 * Asserts that two byte arrays are equal. If they are not, an 190 * {@link AssertionError} is thrown with the given message. 191 * 192 * @param message 193 * the identifying message for the {@link AssertionError} (<code>null</code> 194 * okay) 195 * @param expecteds 196 * byte array with expected values. 197 * @param actuals 198 * byte array with actual values 199 */ 200 public static void assertArrayEquals(String message, byte[] expecteds, 201 byte[] actuals) throws ArrayComparisonFailure { 202 internalArrayEquals(message, expecteds, actuals); 203 } 204 205 /** 206 * Asserts that two byte arrays are equal. If they are not, an 207 * {@link AssertionError} is thrown. 208 * 209 * @param expecteds 210 * byte array with expected values. 211 * @param actuals 212 * byte array with actual values 213 */ 214 public static void assertArrayEquals(byte[] expecteds, byte[] actuals) { 215 assertArrayEquals(null, expecteds, actuals); 216 } 217 218 /** 219 * Asserts that two char arrays are equal. If they are not, an 220 * {@link AssertionError} is thrown with the given message. 221 * 222 * @param message 223 * the identifying message for the {@link AssertionError} (<code>null</code> 224 * okay) 225 * @param expecteds 226 * char array with expected values. 227 * @param actuals 228 * char array with actual values 229 */ 230 public static void assertArrayEquals(String message, char[] expecteds, 231 char[] actuals) throws ArrayComparisonFailure { 232 internalArrayEquals(message, expecteds, actuals); 233 } 234 235 /** 236 * Asserts that two char arrays are equal. If they are not, an 237 * {@link AssertionError} is thrown. 238 * 239 * @param expecteds 240 * char array with expected values. 241 * @param actuals 242 * char array with actual values 243 */ 244 public static void assertArrayEquals(char[] expecteds, char[] actuals) { 245 assertArrayEquals(null, expecteds, actuals); 246 } 247 248 /** 249 * Asserts that two short arrays are equal. If they are not, an 250 * {@link AssertionError} is thrown with the given message. 251 * 252 * @param message 253 * the identifying message for the {@link AssertionError} (<code>null</code> 254 * okay) 255 * @param expecteds 256 * short array with expected values. 257 * @param actuals 258 * short array with actual values 259 */ 260 public static void assertArrayEquals(String message, short[] expecteds, 261 short[] actuals) throws ArrayComparisonFailure { 262 internalArrayEquals(message, expecteds, actuals); 263 } 264 265 /** 266 * Asserts that two short arrays are equal. If they are not, an 267 * {@link AssertionError} is thrown. 268 * 269 * @param expecteds 270 * short array with expected values. 271 * @param actuals 272 * short array with actual values 273 */ 274 public static void assertArrayEquals(short[] expecteds, short[] actuals) { 275 assertArrayEquals(null, expecteds, actuals); 276 } 277 278 /** 279 * Asserts that two int arrays are equal. If they are not, an 280 * {@link AssertionError} is thrown with the given message. 281 * 282 * @param message 283 * the identifying message for the {@link AssertionError} (<code>null</code> 284 * okay) 285 * @param expecteds 286 * int array with expected values. 287 * @param actuals 288 * int array with actual values 289 */ 290 public static void assertArrayEquals(String message, int[] expecteds, 291 int[] actuals) throws ArrayComparisonFailure { 292 internalArrayEquals(message, expecteds, actuals); 293 } 294 295 /** 296 * Asserts that two int arrays are equal. If they are not, an 297 * {@link AssertionError} is thrown. 298 * 299 * @param expecteds 300 * int array with expected values. 301 * @param actuals 302 * int array with actual values 303 */ 304 public static void assertArrayEquals(int[] expecteds, int[] actuals) { 305 assertArrayEquals(null, expecteds, actuals); 306 } 307 308 /** 309 * Asserts that two long arrays are equal. If they are not, an 310 * {@link AssertionError} is thrown with the given message. 311 * 312 * @param message 313 * the identifying message for the {@link AssertionError} (<code>null</code> 314 * okay) 315 * @param expecteds 316 * long array with expected values. 317 * @param actuals 318 * long array with actual values 319 */ 320 public static void assertArrayEquals(String message, long[] expecteds, 321 long[] actuals) throws ArrayComparisonFailure { 322 internalArrayEquals(message, expecteds, actuals); 323 } 324 325 /** 326 * Asserts that two long arrays are equal. If they are not, an 327 * {@link AssertionError} is thrown. 328 * 329 * @param expecteds 330 * long array with expected values. 331 * @param actuals 332 * long array with actual values 333 */ 334 public static void assertArrayEquals(long[] expecteds, long[] actuals) { 335 assertArrayEquals(null, expecteds, actuals); 336 } 337 338 /** 339 * Asserts that two double arrays are equal. If they are not, an 340 * {@link AssertionError} is thrown with the given message. 341 * 342 * @param message 343 * the identifying message for the {@link AssertionError} (<code>null</code> 344 * okay) 345 * @param expecteds 346 * double array with expected values. 347 * @param actuals 348 * double array with actual values 349 */ 350 public static void assertArrayEquals(String message, double[] expecteds, 351 double[] actuals, double delta) throws ArrayComparisonFailure { 352 new InexactComparisonCriteria(delta).arrayEquals(message, expecteds, actuals); 353 } 354 355 /** 356 * Asserts that two double arrays are equal. If they are not, an 357 * {@link AssertionError} is thrown. 358 * 359 * @param expecteds 360 * double array with expected values. 361 * @param actuals 362 * double array with actual values 363 */ 364 public static void assertArrayEquals(double[] expecteds, double[] actuals, double delta) { 365 assertArrayEquals(null, expecteds, actuals, delta); 366 } 367 368 /** 369 * Asserts that two float arrays are equal. If they are not, an 370 * {@link AssertionError} is thrown with the given message. 371 * 372 * @param message 373 * the identifying message for the {@link AssertionError} (<code>null</code> 374 * okay) 375 * @param expecteds 376 * float array with expected values. 377 * @param actuals 378 * float array with actual values 379 */ 380 public static void assertArrayEquals(String message, float[] expecteds, 381 float[] actuals, float delta) throws ArrayComparisonFailure { 382 new InexactComparisonCriteria(delta).arrayEquals(message, expecteds, actuals); 383 } 384 385 /** 386 * Asserts that two float arrays are equal. If they are not, an 387 * {@link AssertionError} is thrown. 388 * 389 * @param expecteds 390 * float array with expected values. 391 * @param actuals 392 * float array with actual values 393 */ 394 public static void assertArrayEquals(float[] expecteds, float[] actuals, float delta) { 395 assertArrayEquals(null, expecteds, actuals, delta); 396 } 397 398 /** 399 * Asserts that two object arrays are equal. If they are not, an 400 * {@link AssertionError} is thrown with the given message. If 401 * <code>expecteds</code> and <code>actuals</code> are <code>null</code>, 402 * they are considered equal. 403 * 404 * @param message 405 * the identifying message for the {@link AssertionError} (<code>null</code> 406 * okay) 407 * @param expecteds 408 * Object array or array of arrays (multi-dimensional array) with 409 * expected values. 410 * @param actuals 411 * Object array or array of arrays (multi-dimensional array) with 412 * actual values 413 */ 414 private static void internalArrayEquals(String message, Object expecteds, 415 Object actuals) throws ArrayComparisonFailure { 416 new ExactComparisonCriteria().arrayEquals(message, expecteds, actuals); 417 } 418 419 /** 420 * Asserts that two doubles or floats are equal to within a positive delta. 421 * If they are not, an {@link AssertionError} is thrown with the given 422 * message. If the expected value is infinity then the delta value is 423 * ignored. NaNs are considered equal: 424 * <code>assertEquals(Double.NaN, Double.NaN, *)</code> passes 425 * 426 * @param message 427 * the identifying message for the {@link AssertionError} (<code>null</code> 428 * okay) 429 * @param expected 430 * expected value 431 * @param actual 432 * the value to check against <code>expected</code> 433 * @param delta 434 * the maximum delta between <code>expected</code> and 435 * <code>actual</code> for which both numbers are still 436 * considered equal. 437 */ 438 static public void assertEquals(String message, double expected, 439 double actual, double delta) { 440 if (Double.compare(expected, actual) == 0) 441 return; 442 if (!(Math.abs(expected - actual) <= delta)) 443 failNotEquals(message, new Double(expected), new Double(actual)); 444 } 445 446 /** 447 * Asserts that two longs are equal. If they are not, an 448 * {@link AssertionError} is thrown. 449 * 450 * @param expected 451 * expected long value. 452 * @param actual 453 * actual long value 454 */ 455 static public void assertEquals(long expected, long actual) { 456 assertEquals(null, expected, actual); 457 } 458 459 /** 460 * Asserts that two longs are equal. If they are not, an 461 * {@link AssertionError} is thrown with the given message. 462 * 463 * @param message 464 * the identifying message for the {@link AssertionError} (<code>null</code> 465 * okay) 466 * @param expected 467 * long expected value. 468 * @param actual 469 * long actual value 470 */ 471 static public void assertEquals(String message, long expected, long actual) { 472 assertEquals(message, (Long) expected, (Long) actual); 473 } 474 475 /** 476 * @deprecated Use 477 * <code>assertEquals(double expected, double actual, double delta)</code> 478 * instead 479 */ 480 @Deprecated 481 static public void assertEquals(double expected, double actual) { 482 assertEquals(null, expected, actual); 483 } 484 485 /** 486 * @deprecated Use 487 * <code>assertEquals(String message, double expected, double actual, double delta)</code> 488 * instead 489 */ 490 @Deprecated 491 static public void assertEquals(String message, double expected, 492 double actual) { 493 fail("Use assertEquals(expected, actual, delta) to compare floating-point numbers"); 494 } 495 496 /** 497 * Asserts that two doubles or floats are equal to within a positive delta. 498 * If they are not, an {@link AssertionError} is thrown. If the expected 499 * value is infinity then the delta value is ignored.NaNs are considered 500 * equal: <code>assertEquals(Double.NaN, Double.NaN, *)</code> passes 501 * 502 * @param expected 503 * expected value 504 * @param actual 505 * the value to check against <code>expected</code> 506 * @param delta 507 * the maximum delta between <code>expected</code> and 508 * <code>actual</code> for which both numbers are still 509 * considered equal. 510 */ 511 static public void assertEquals(double expected, double actual, double delta) { 512 assertEquals(null, expected, actual, delta); 513 } 514 515 /** 516 * Asserts that an object isn't null. If it is an {@link AssertionError} is 517 * thrown with the given message. 518 * 519 * @param message 520 * the identifying message for the {@link AssertionError} (<code>null</code> 521 * okay) 522 * @param object 523 * Object to check or <code>null</code> 524 */ 525 static public void assertNotNull(String message, Object object) { 526 assertTrue(message, object != null); 527 } 528 529 /** 530 * Asserts that an object isn't null. If it is an {@link AssertionError} is 531 * thrown. 532 * 533 * @param object 534 * Object to check or <code>null</code> 535 */ 536 static public void assertNotNull(Object object) { 537 assertNotNull(null, object); 538 } 539 540 /** 541 * Asserts that an object is null. If it is not, an {@link AssertionError} 542 * is thrown with the given message. 543 * 544 * @param message 545 * the identifying message for the {@link AssertionError} (<code>null</code> 546 * okay) 547 * @param object 548 * Object to check or <code>null</code> 549 */ 550 static public void assertNull(String message, Object object) { 551 assertTrue(message, object == null); 552 } 553 554 /** 555 * Asserts that an object is null. If it isn't an {@link AssertionError} is 556 * thrown. 557 * 558 * @param object 559 * Object to check or <code>null</code> 560 */ 561 static public void assertNull(Object object) { 562 assertNull(null, object); 563 } 564 565 /** 566 * Asserts that two objects refer to the same object. If they are not, an 567 * {@link AssertionError} is thrown with the given message. 568 * 569 * @param message 570 * the identifying message for the {@link AssertionError} (<code>null</code> 571 * okay) 572 * @param expected 573 * the expected object 574 * @param actual 575 * the object to compare to <code>expected</code> 576 */ 577 static public void assertSame(String message, Object expected, Object actual) { 578 if (expected == actual) 579 return; 580 failNotSame(message, expected, actual); 581 } 582 583 /** 584 * Asserts that two objects refer to the same object. If they are not the 585 * same, an {@link AssertionError} without a message is thrown. 586 * 587 * @param expected 588 * the expected object 589 * @param actual 590 * the object to compare to <code>expected</code> 591 */ 592 static public void assertSame(Object expected, Object actual) { 593 assertSame(null, expected, actual); 594 } 595 596 /** 597 * Asserts that two objects do not refer to the same object. If they do 598 * refer to the same object, an {@link AssertionError} is thrown with the 599 * given message. 600 * 601 * @param message 602 * the identifying message for the {@link AssertionError} (<code>null</code> 603 * okay) 604 * @param unexpected 605 * the object you don't expect 606 * @param actual 607 * the object to compare to <code>unexpected</code> 608 */ 609 static public void assertNotSame(String message, Object unexpected, 610 Object actual) { 611 if (unexpected == actual) 612 failSame(message); 613 } 614 615 /** 616 * Asserts that two objects do not refer to the same object. If they do 617 * refer to the same object, an {@link AssertionError} without a message is 618 * thrown. 619 * 620 * @param unexpected 621 * the object you don't expect 622 * @param actual 623 * the object to compare to <code>unexpected</code> 624 */ 625 static public void assertNotSame(Object unexpected, Object actual) { 626 assertNotSame(null, unexpected, actual); 627 } 628 629 static private void failSame(String message) { 630 String formatted= ""; 631 if (message != null) 632 formatted= message + " "; 633 fail(formatted + "expected not same"); 634 } 635 636 static private void failNotSame(String message, Object expected, 637 Object actual) { 638 String formatted= ""; 639 if (message != null) 640 formatted= message + " "; 641 fail(formatted + "expected same:<" + expected + "> was not:<" + actual 642 + ">"); 643 } 644 645 static private void failNotEquals(String message, Object expected, 646 Object actual) { 647 fail(format(message, expected, actual)); 648 } 649 650 static String format(String message, Object expected, Object actual) { 651 String formatted= ""; 652 if (message != null && !message.equals("")) 653 formatted= message + " "; 654 String expectedString= String.valueOf(expected); 655 String actualString= String.valueOf(actual); 656 if (expectedString.equals(actualString)) 657 return formatted + "expected: " 658 + formatClassAndValue(expected, expectedString) 659 + " but was: " + formatClassAndValue(actual, actualString); 660 else 661 return formatted + "expected:<" + expectedString + "> but was:<" 662 + actualString + ">"; 663 } 664 665 private static String formatClassAndValue(Object value, String valueString) { 666 String className= value == null ? "null" : value.getClass().getName(); 667 return className + "<" + valueString + ">"; 668 } 669 670 /** 671 * Asserts that two object arrays are equal. If they are not, an 672 * {@link AssertionError} is thrown with the given message. If 673 * <code>expecteds</code> and <code>actuals</code> are <code>null</code>, 674 * they are considered equal. 675 * 676 * @param message 677 * the identifying message for the {@link AssertionError} (<code>null</code> 678 * okay) 679 * @param expecteds 680 * Object array or array of arrays (multi-dimensional array) with 681 * expected values. 682 * @param actuals 683 * Object array or array of arrays (multi-dimensional array) with 684 * actual values 685 * @deprecated use assertArrayEquals 686 */ 687 @Deprecated 688 public static void assertEquals(String message, Object[] expecteds, 689 Object[] actuals) { 690 assertArrayEquals(message, expecteds, actuals); 691 } 692 693 /** 694 * Asserts that two object arrays are equal. If they are not, an 695 * {@link AssertionError} is thrown. If <code>expected</code> and 696 * <code>actual</code> are <code>null</code>, they are considered 697 * equal. 698 * 699 * @param expecteds 700 * Object array or array of arrays (multi-dimensional array) with 701 * expected values 702 * @param actuals 703 * Object array or array of arrays (multi-dimensional array) with 704 * actual values 705 * @deprecated use assertArrayEquals 706 */ 707 @Deprecated 708 public static void assertEquals(Object[] expecteds, Object[] actuals) { 709 assertArrayEquals(expecteds, actuals); 710 } 711 712 /** 713 * Asserts that <code>actual</code> satisfies the condition specified by 714 * <code>matcher</code>. If not, an {@link AssertionError} is thrown with 715 * information about the matcher and failing value. Example: 716 * 717 * <pre> 718 * assertThat(0, is(1)); // fails: 719 * // failure message: 720 * // expected: is <1> 721 * // got value: <0> 722 * assertThat(0, is(not(1))) // passes 723 * </pre> 724 * 725 * @param <T> 726 * the static type accepted by the matcher (this can flag obvious 727 * compile-time problems such as {@code assertThat(1, is("a"))} 728 * @param actual 729 * the computed value being compared 730 * @param matcher 731 * an expression, built of {@link Matcher}s, specifying allowed 732 * values 733 * 734 * @see org.hamcrest.CoreMatchers 735 * @see org.junit.matchers.JUnitMatchers 736 */ 737 public static <T> void assertThat(T actual, Matcher<T> matcher) { 738 assertThat("", actual, matcher); 739 } 740 741 /** 742 * Asserts that <code>actual</code> satisfies the condition specified by 743 * <code>matcher</code>. If not, an {@link AssertionError} is thrown with 744 * the reason and information about the matcher and failing value. Example: 745 * 746 * <pre> 747 * : 748 * assertThat("Help! Integers don't work", 0, is(1)); // fails: 749 * // failure message: 750 * // Help! Integers don't work 751 * // expected: is <1> 752 * // got value: <0> 753 * assertThat("Zero is one", 0, is(not(1))) // passes 754 * </pre> 755 * 756 * @param reason 757 * additional information about the error 758 * @param <T> 759 * the static type accepted by the matcher (this can flag obvious 760 * compile-time problems such as {@code assertThat(1, is("a"))} 761 * @param actual 762 * the computed value being compared 763 * @param matcher 764 * an expression, built of {@link Matcher}s, specifying allowed 765 * values 766 * 767 * @see org.hamcrest.CoreMatchers 768 * @see org.junit.matchers.JUnitMatchers 769 */ 770 public static <T> void assertThat(String reason, T actual, 771 Matcher<T> matcher) { 772 if (!matcher.matches(actual)) { 773 Description description= new StringDescription(); 774 description.appendText(reason); 775 description.appendText("\nExpected: "); 776 description.appendDescriptionOf(matcher); 777 description.appendText("\n got: "); 778 description.appendValue(actual); 779 description.appendText("\n"); 780 throw new java.lang.AssertionError(description.toString()); 781 } 782 } 783 } 784