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