1 package org.junit; 2 3 import java.lang.reflect.Array ; 4 5 import org.junit.internal.ArrayComparisonFailure; 6 7 19 public class Assert { 20 23 protected Assert() { 24 } 25 26 32 static public void assertTrue(String message, boolean condition) { 33 if (!condition) 34 fail(message); 35 } 36 37 42 static public void assertTrue(boolean condition) { 43 assertTrue(null, condition); 44 } 45 46 52 static public void assertFalse(String message, boolean condition) { 53 assertTrue(message, !condition); 54 } 55 56 61 static public void assertFalse(boolean condition) { 62 assertFalse(null, condition); 63 } 64 65 70 static public void fail(String message) { 71 throw new AssertionError (message == null ? "" : message); 72 } 73 74 77 static public void fail() { 78 fail(null); 79 } 80 81 89 static public void assertEquals(String message, Object expected, Object actual) { 90 if (expected == null && actual == null) 91 return; 92 if (expected != null && isEquals(expected, actual)) 93 return; 94 else if (expected instanceof String && actual instanceof String ) { 95 String cleanMessage= message == null ? "" : message; 96 throw new ComparisonFailure(cleanMessage, (String )expected, (String )actual); 97 } 98 else 99 failNotEquals(message, expected, actual); 100 } 101 102 private static boolean isEquals(Object expected, Object actual) { 103 if (expected instanceof Number && actual instanceof Number ) 104 return ((Number ) expected).longValue() == ((Number ) actual).longValue(); 105 return expected.equals(actual); 106 } 107 108 115 static public void assertEquals(Object expected, Object actual) { 116 assertEquals(null, expected, actual); 117 } 118 119 127 public static void assertArrayEquals(String message, Object [] expecteds, 128 Object [] actuals) throws ArrayComparisonFailure { 129 internalArrayEquals(message, expecteds, actuals); 130 } 131 132 139 public static void assertArrayEquals(Object [] expecteds, Object [] actuals) { 140 assertArrayEquals(null, expecteds, actuals); 141 } 142 143 152 public static void assertArrayEquals(String message, byte[] expecteds, 153 byte[] actuals) throws ArrayComparisonFailure { 154 internalArrayEquals(message, expecteds, actuals); 155 } 156 157 165 public static void assertArrayEquals(byte[] expecteds, byte[] actuals) { 166 assertArrayEquals(null, expecteds, actuals); 167 } 168 169 178 public static void assertArrayEquals(String message, char[] expecteds, 179 char[] actuals) throws ArrayComparisonFailure { 180 internalArrayEquals(message, expecteds, actuals); 181 } 182 183 191 public static void assertArrayEquals(char[] expecteds, char[] actuals) { 192 assertArrayEquals(null, expecteds, actuals); 193 } 194 195 204 public static void assertArrayEquals(String message, short[] expecteds, 205 short[] actuals) throws ArrayComparisonFailure { 206 internalArrayEquals(message, expecteds, actuals); 207 } 208 209 217 public static void assertArrayEquals(short[] expecteds, short[] actuals) { 218 assertArrayEquals(null, expecteds, actuals); 219 } 220 221 230 public static void assertArrayEquals(String message, int[] expecteds, 231 int[] actuals) throws ArrayComparisonFailure { 232 internalArrayEquals(message, expecteds, actuals); 233 } 234 235 243 public static void assertArrayEquals(int[] expecteds, int[] actuals) { 244 assertArrayEquals(null, expecteds, actuals); 245 } 246 247 255 public static void assertArrayEquals(long[] expecteds, long[] actuals) { 256 assertArrayEquals(null, expecteds, actuals); 257 } 258 259 268 public static void assertArrayEquals(String message, long[] expecteds, 269 long[] actuals) throws ArrayComparisonFailure { 270 internalArrayEquals(message, expecteds, actuals); 271 } 272 273 281 private static void internalArrayEquals(String message, Object expecteds, 282 Object actuals) throws ArrayComparisonFailure { 283 if (expecteds == actuals) 284 return; 285 String header = message == null ? "" : message + ": "; 286 if (expecteds == null) 287 fail(header + "expected array was null"); 288 if (actuals == null) 289 fail(header + "actual array was null"); 290 int actualsLength= Array.getLength(actuals); 291 int expectedsLength= Array.getLength(expecteds); 292 if (actualsLength != expectedsLength) 293 fail(header + "array lengths differed, expected.length=" + expectedsLength + " actual.length=" + actualsLength); 294 295 for (int i= 0; i < expectedsLength; i++) { 296 Object expected= Array.get(expecteds, i); 297 Object actual= Array.get(actuals, i); 298 if (isArray(expected) && isArray(actual)) { 299 try { 300 internalArrayEquals(message, expected, actual); 301 } catch (ArrayComparisonFailure e) { 302 e.addDimension(i); 303 throw e; 304 } 305 } else 306 try { 307 assertEquals(expected, actual); 308 } catch (AssertionError e) { 309 throw new ArrayComparisonFailure(header, e, i); 310 } 311 } 312 } 313 314 private static boolean isArray(Object expected) { 315 return expected != null && expected.getClass().isArray(); 316 } 317 318 330 static public void assertEquals(String message, double expected, double actual, double delta) { 331 if (Double.compare(expected, actual) == 0) 332 return; 333 if (!(Math.abs(expected - actual) <= delta)) 334 failNotEquals(message, new Double (expected), new Double (actual)); 335 } 336 337 348 static public void assertEquals(double expected, double actual, double delta) { 349 assertEquals(null, expected, actual, delta); 350 } 351 352 358 static public void assertNotNull(String message, Object object) { 359 assertTrue(message, object != null); 360 } 361 362 367 static public void assertNotNull(Object object) { 368 assertNotNull(null, object); 369 } 370 371 377 static public void assertNull(String message, Object object) { 378 assertTrue(message, object == null); 379 } 380 381 386 static public void assertNull(Object object) { 387 assertNull(null, object); 388 } 389 390 397 static public void assertSame(String message, Object expected, Object actual) { 398 if (expected == actual) 399 return; 400 failNotSame(message, expected, actual); 401 } 402 403 409 static public void assertSame(Object expected, Object actual) { 410 assertSame(null, expected, actual); 411 } 412 413 421 static public void assertNotSame(String message, Object unexpected, Object actual) { 422 if (unexpected == actual) 423 failSame(message); 424 } 425 426 432 static public void assertNotSame(Object unexpected, Object actual) { 433 assertNotSame(null, unexpected, actual); 434 } 435 436 static private void failSame(String message) { 437 String formatted= ""; 438 if (message != null) 439 formatted= message + " "; 440 fail(formatted + "expected not same"); 441 } 442 443 static private void failNotSame(String message, Object expected, Object actual) { 444 String formatted= ""; 445 if (message != null) 446 formatted= message + " "; 447 fail(formatted + "expected same:<" + expected + "> was not:<" + actual + ">"); 448 } 449 450 static private void failNotEquals(String message, Object expected, Object actual) { 451 fail(format(message, expected, actual)); 452 } 453 454 static String format(String message, Object expected, Object actual) { 455 String formatted= ""; 456 if (message != null && ! message.equals("")) 457 formatted= message + " "; 458 String expectedString= String.valueOf(expected); 459 String actualString= String.valueOf(actual); 460 if (expectedString.equals(actualString)) 461 return formatted + "expected: " + expected.getClass().getName() + "<" + expectedString + "> but was: " + actual.getClass().getName() + "<" + actualString + ">"; 462 else 463 return formatted + "expected:<" + expectedString + "> but was:<" + actualString + ">"; 464 } 465 466 475 @Deprecated 476 public static void assertEquals(String message, Object [] expecteds, Object [] actuals) { 477 assertArrayEquals(message, expecteds, actuals); 478 } 479 480 488 @Deprecated 489 public static void assertEquals(Object [] expecteds, Object [] actuals) { 490 assertArrayEquals(expecteds, actuals); 491 } 492 493 } 494 | Popular Tags |