package com.waldura.util; /** * This "static" class (ie. all its methods are declared static) implements * assertions. If you don't know what assertions are, grab a book and teach yourself. *

* Note: it is actually recommended to handle pre-conditions by raising runtime exceptions * explicitely. E.g. {@link IllegalArgumentException} and {@link IllegalStateException} * are prime candidates for this. *

* This class is declared final in the hope that the compiler will inline the method * calls, and even skip them entirely if IGNORE_ASSERTIONS is true. * IGNORE_ASSERTIONS is a compile-time variable; changing it should require * a complete recompilation of all users of this class. *

* We are aware that JDK 1.4 implements a (better) assertion facility. This class is provided * for * backwards compatibility with earlier JDKs. Or maybe we'll just use it to facade the * actual * assert() call. * * @author Quenio dos Santos * @author Renaud Waldura * * @created August 7, 2000 */ public final class Assert { // this flag controls the execution of assertion statements private static final boolean IGNORE_ASSERTIONS = false; /** * Thrown when an assertion failed. */ private static class AssertionFailedError extends Error { AssertionFailedError(String mesg) { super(mesg); } } /** * Assert that a condition is false. If it isn't, throw an AssertionFailedError. */ public static void isFalse(boolean condition) { isFalse(null, condition); } /** * Assert that a condition is true. If it isn't, throw an AssertionFailedError * with the given message. */ public static void isFalse(String message, boolean condition) { isTrue(message, !condition); } /** * Assert that a condition is true. If it isn't it, throw an AssertionFailedError. */ public static void isTrue(boolean condition) { isTrue(null, condition); } /** * Assert that a condition is true. If it isn't it, throw an AssertionFailedError * with the given message. */ public static void isTrue(String message, boolean condition) { if (!condition) { fail(message); } } /** * Assert 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 */ public static void equals(double expected, double actual, double delta) { equals(null, expected, actual, delta); } /** * Assert that two longs are equal. * * @param expected the expected value of an object * @param actual the actual value of an object */ public static void equals(long expected, long actual) { equals(null, expected, actual); } /** * Assert 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 */ public static void equals(Object expected, Object actual) { equals(null, expected, actual); } /** * Assert 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 */ public static void equals(String message, double expected, double actual, double delta) { if (Math.abs(expected - actual) > delta) { failNotEquals(message, new Double(expected), new Double(actual)); } } /** * Assert 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 */ public static void equals(String message, long expected, long actual) { equals(message, new Long(expected), new Long(actual)); } /** * Assert 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 */ public static void equals(String message, Object expected, Object actual) { if (expected == null && actual == null) { return; } if (expected != null && expected.equals(actual)) { return; } failNotEquals(message, expected, actual); } /** * Assert that an object isn't null. */ public static void notNull(Object object) { notNull(null, object); } /** * Assert that an object isn't null. */ public static void notNull(String message, Object object) { isTrue(message, object != null); } /** * Assert that an object is null. */ public static void isNull(Object object) { isNull(null, object); } /** * Assert that an object is null. */ public static void isNull(String message, Object object) { isTrue(message, object == null); } /** * Assert 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 */ public static void same(Object expected, Object actual) { same(null, expected, actual); } /** * Assert 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 */ public static void same(String message, Object expected, Object actual) { if (expected != actual) { failNotSame(message, expected, actual); } } /** * Fail a test with no message. */ public static void fail() { fail(null); } /** * Fail a test with the given message. */ public static void fail(String message) { if (!IGNORE_ASSERTIONS) { throw new AssertionFailedError(message); } } private static void failNotEquals(String message, Object expected, Object actual) { StringBuffer formatted = new StringBuffer(message) .append(" expected: <") .append(expected) .append("> but was: <") .append(actual) .append(">"); fail( formatted.toString() ); } private static void failNotSame(String message, Object expected, Object actual) { String formatted = ""; if (message != null) { formatted = message + " "; } fail(formatted + "expected same"); } }