| package junit.framework; | |
| /** | |
| * A set of assert methods. | |
| */ | |
| public class Assert { | |
| /** | |
| * Protect constructor since it is a static only class | |
| */ | |
| protected Assert() { | |
| } | |
| /** | |
| * Asserts that a condition is true. If it isn't it throws | |
| * an AssertionFailedError with the given message. | |
| */ | |
| static public void assert(String message, boolean condition) { | |
| if (!condition) | |
| fail(message); | |
| } | |
| /** | |
| * Asserts that a condition is true. If it isn't it throws | |
| * an AssertionFailedError. | |
| */ | |
| static public void assert(boolean condition) { | |
| assert(null, condition); | |
| } | |
| /** | |
| * Asserts that two doubles are equal. | |
| * @param expected the expected value of an object | |
| * @param actual the actual value of an object | |
| * @param delta tolerated delta | |
| */ | |
| static public void assertEquals(double expected, double actual, double delta) { | |
| assertEquals(null, expected, actual, delta); | |
| } | |
| /** | |
| * Asserts that two longs are equal. | |
| * @param expected the expected value of an object | |
| * @param actual the actual value of an object | |
| */ | |
| static public void assertEquals(long expected, long actual) { | |
| assertEquals(null, expected, actual); | |
| } | |
| /** | |
| * Asserts that two objects are equal. If they are not | |
| * an AssertionFailedError is thrown. | |
| * @param expected the expected value of an object | |
| * @param actual the actual value of an object | |
| */ | |
| static public void assertEquals(Object expected, Object actual) { | |
| assertEquals(null, expected, actual); | |
| } | |
| /** | |
| * Asserts that two doubles are equal. | |
| * @param message the detail message for this assertion | |
| * @param expected the expected value of an object | |
| * @param actual the actual value of an object | |
| * @param delta tolerated delta | |
| */ | |
| static public void assertEquals(String message, double expected, double actual, double delta) { | |
| if (Math.abs(expected-actual) > delta) | |
| failNotEquals(message, new Double(expected), new Double(actual)); | |
| } | |
| /** | |
| * Asserts that two longs are equal. | |
| * @param message the detail message for this assertion | |
| * @param expected the expected value of an object | |
| * @param actual the actual value of an object | |
| */ | |
| static public void assertEquals(String message, long expected, long actual) { | |
| assertEquals(message, new Long(expected), new Long(actual)); | |
| } | |
| /** | |
| * Asserts that two objects are equal. If they are not | |
| * an AssertionFailedError is thrown. | |
| * @param message the detail message for this assertion | |
| * @param expected the expected value of an object | |
| * @param actual the actual value of an object | |
| */ | |
| static public void assertEquals(String message, Object expected, Object actual) { | |
| if (expected == null && actual == null) | |
| return; | |
| if (expected != null && expected.equals(actual)) | |
| return; | |
| failNotEquals(message, expected, actual); | |
| } | |
| /** | |
| * Asserts that an object isn't null. | |
| */ | |
| static public void assertNotNull(Object object) { | |
| assertNotNull(null, object); | |
| } | |
| /** | |
| * Asserts that an object isn't null. | |
| */ | |
| static public void assertNotNull(String message, Object object) { | |
| assert(message, object != null); | |
| } | |
| /** | |
| * Asserts that an object is null. | |
| */ | |
| static public void assertNull(Object object) { | |
| assertNull(null, object); | |
| } | |
| /** | |
| * Asserts that an object is null. | |
| */ | |
| static public void assertNull(String message, Object object) { | |
| assert(message, object == null); | |
| } | |
| /** | |
| * Asserts that two objects refer to the same object. If they are not | |
| * the same an AssertionFailedError is thrown. | |
| * @param expected the expected value of an object | |
| * @param actual the actual value of an object | |
| */ | |
| static public void assertSame(Object expected, Object actual) { | |
| assertSame(null, expected, actual); | |
| } | |
| /** | |
| * Asserts that two objects refer to the same object. If they are not | |
| * an AssertionFailedError is thrown. | |
| * @param message the detail message for this assertion | |
| * @param expected the expected value of an object | |
| * @param actual the actual value of an object | |
| */ | |
| static public void assertSame(String message, Object expected, Object actual) { | |
| if (expected == actual) | |
| return; | |
| failNotSame(message, expected, actual); | |
| } | |
| /** | |
| * Fails a test with no message. | |
| */ | |
| static public void fail() { | |
| fail(null); | |
| } | |
| /** | |
| * Fails a test with the given message. | |
| */ | |
| static public void fail(String message) { | |
| throw new AssertionFailedError(message); | |
| } | |
| static private void failNotEquals(String message, Object expected, Object actual) { | |
| String formatted= ""; | |
| if (message != null) | |
| formatted= message+" "; | |
| fail(formatted+"expected:<"+expected+"> but was:<"+actual+">"); | |
| } | |
| static private void failNotSame(String message, Object expected, Object actual) { | |
| String formatted= ""; | |
| if (message != null) | |
| formatted= message+" "; | |
| fail(formatted+"expected same"); | |
| } | |
| } |