Home | History | Annotate | Download | only in framework
      1 package junit.framework;
      2 
      3 /**
      4  * A set of assert methods.  Messages are only displayed when an assert fails.
      5  */
      6 
      7 public class Assert {
      8     /**
      9      * Protect constructor since it is a static only class
     10      */
     11     protected Assert() {
     12     }
     13 
     14     /**
     15      * Asserts that a condition is true. If it isn't it throws
     16      * an AssertionFailedError with the given message.
     17      */
     18     static public void assertTrue(String message, boolean condition) {
     19         if (!condition)
     20             fail(message);
     21     }
     22     /**
     23      * Asserts that a condition is true. If it isn't it throws
     24      * an AssertionFailedError.
     25      */
     26     static public void assertTrue(boolean condition) {
     27         assertTrue(null, condition);
     28     }
     29     /**
     30      * Asserts that a condition is false. If it isn't it throws
     31      * an AssertionFailedError with the given message.
     32      */
     33     static public void assertFalse(String message, boolean condition) {
     34         assertTrue(message, !condition);
     35     }
     36     /**
     37      * Asserts that a condition is false. If it isn't it throws
     38      * an AssertionFailedError.
     39      */
     40     static public void assertFalse(boolean condition) {
     41         assertFalse(null, condition);
     42     }
     43     /**
     44      * Fails a test with the given message.
     45      */
     46     static public void fail(String message) {
     47         throw new AssertionFailedError(message);
     48     }
     49     /**
     50      * Fails a test with no message.
     51      */
     52     static public void fail() {
     53         fail(null);
     54     }
     55     /**
     56      * Asserts that two objects are equal. If they are not
     57      * an AssertionFailedError is thrown with the given message.
     58      */
     59     static public void assertEquals(String message, Object expected, Object actual) {
     60         if (expected == null && actual == null)
     61             return;
     62         if (expected != null && expected.equals(actual))
     63             return;
     64         failNotEquals(message, expected, actual);
     65     }
     66     /**
     67      * Asserts that two objects are equal. If they are not
     68      * an AssertionFailedError is thrown.
     69      */
     70     static public void assertEquals(Object expected, Object actual) {
     71         assertEquals(null, expected, actual);
     72     }
     73     /**
     74      * Asserts that two Strings are equal.
     75      */
     76     static public void assertEquals(String message, String expected, String actual) {
     77         if (expected == null && actual == null)
     78             return;
     79         if (expected != null && expected.equals(actual))
     80             return;
     81         throw new ComparisonFailure(message, expected, actual);
     82     }
     83     /**
     84      * Asserts that two Strings are equal.
     85      */
     86     static public void assertEquals(String expected, String actual) {
     87         assertEquals(null, expected, actual);
     88     }
     89     /**
     90      * Asserts that two doubles are equal concerning a delta.  If they are not
     91      * an AssertionFailedError is thrown with the given message.  If the expected
     92      * value is infinity then the delta value is ignored.
     93      */
     94     static public void assertEquals(String message, double expected, double actual, double delta) {
     95         // handle infinity specially since subtracting to infinite values gives NaN and the
     96         // the following test fails
     97         if (Double.isInfinite(expected)) {
     98             if (!(expected == actual))
     99                 failNotEquals(message, new Double(expected), new Double(actual));
    100         } else if (!(Math.abs(expected-actual) <= delta)) // Because comparison with NaN always returns false
    101             failNotEquals(message, new Double(expected), new Double(actual));
    102     }
    103     /**
    104      * Asserts that two doubles are equal concerning a delta. If the expected
    105      * value is infinity then the delta value is ignored.
    106      */
    107     static public void assertEquals(double expected, double actual, double delta) {
    108         assertEquals(null, expected, actual, delta);
    109     }
    110     /**
    111      * Asserts that two floats are equal concerning a delta. If they are not
    112      * an AssertionFailedError is thrown with the given message.  If the expected
    113      * value is infinity then the delta value is ignored.
    114      */
    115     static public void assertEquals(String message, float expected, float actual, float delta) {
    116          // handle infinity specially since subtracting to infinite values gives NaN and the
    117         // the following test fails
    118         if (Float.isInfinite(expected)) {
    119             if (!(expected == actual))
    120                 failNotEquals(message, new Float(expected), new Float(actual));
    121         } else if (!(Math.abs(expected-actual) <= delta))
    122               failNotEquals(message, new Float(expected), new Float(actual));
    123     }
    124     /**
    125      * Asserts that two floats are equal concerning a delta. If the expected
    126      * value is infinity then the delta value is ignored.
    127      */
    128     static public void assertEquals(float expected, float actual, float delta) {
    129         assertEquals(null, expected, actual, delta);
    130     }
    131     /**
    132      * Asserts that two longs are equal. If they are not
    133      * an AssertionFailedError is thrown with the given message.
    134      */
    135     static public void assertEquals(String message, long expected, long actual) {
    136         assertEquals(message, new Long(expected), new Long(actual));
    137     }
    138     /**
    139      * Asserts that two longs are equal.
    140      */
    141     static public void assertEquals(long expected, long actual) {
    142         assertEquals(null, expected, actual);
    143     }
    144     /**
    145      * Asserts that two booleans are equal. If they are not
    146      * an AssertionFailedError is thrown with the given message.
    147      */
    148     static public void assertEquals(String message, boolean expected, boolean actual) {
    149             assertEquals(message, new Boolean(expected), new Boolean(actual));
    150       }
    151     /**
    152      * Asserts that two booleans are equal.
    153       */
    154     static public void assertEquals(boolean expected, boolean actual) {
    155         assertEquals(null, expected, actual);
    156     }
    157     /**
    158      * Asserts that two bytes are equal. If they are not
    159      * an AssertionFailedError is thrown with the given message.
    160      */
    161       static public void assertEquals(String message, byte expected, byte actual) {
    162         assertEquals(message, new Byte(expected), new Byte(actual));
    163     }
    164     /**
    165         * Asserts that two bytes are equal.
    166      */
    167     static public void assertEquals(byte expected, byte actual) {
    168         assertEquals(null, expected, actual);
    169     }
    170     /**
    171      * Asserts that two chars are equal. If they are not
    172      * an AssertionFailedError is thrown with the given message.
    173      */
    174       static public void assertEquals(String message, char expected, char actual) {
    175             assertEquals(message, new Character(expected), new Character(actual));
    176       }
    177     /**
    178      * Asserts that two chars are equal.
    179      */
    180       static public void assertEquals(char expected, char actual) {
    181         assertEquals(null, expected, actual);
    182     }
    183     /**
    184      * Asserts that two shorts are equal. If they are not
    185      * an AssertionFailedError is thrown with the given message.
    186      */
    187     static public void assertEquals(String message, short expected, short actual) {
    188             assertEquals(message, new Short(expected), new Short(actual));
    189     }
    190       /**
    191      * Asserts that two shorts are equal.
    192      */
    193     static public void assertEquals(short expected, short actual) {
    194         assertEquals(null, expected, actual);
    195     }
    196     /**
    197      * Asserts that two ints are equal. If they are not
    198      * an AssertionFailedError is thrown with the given message.
    199      */
    200       static public void assertEquals(String message, int expected, int actual) {
    201         assertEquals(message, new Integer(expected), new Integer(actual));
    202       }
    203       /**
    204         * Asserts that two ints are equal.
    205      */
    206       static public void assertEquals(int expected, int actual) {
    207           assertEquals(null, expected, actual);
    208     }
    209     /**
    210      * Asserts that an object isn't null.
    211      */
    212     static public void assertNotNull(Object object) {
    213         assertNotNull(null, object);
    214     }
    215     /**
    216      * Asserts that an object isn't null. If it is
    217      * an AssertionFailedError is thrown with the given message.
    218      */
    219     static public void assertNotNull(String message, Object object) {
    220         assertTrue(message, object != null);
    221     }
    222     /**
    223      * Asserts that an object is null.
    224      */
    225     static public void assertNull(Object object) {
    226         assertNull(null, object);
    227     }
    228     /**
    229      * Asserts that an object is null.  If it is not
    230      * an AssertionFailedError is thrown with the given message.
    231      */
    232     static public void assertNull(String message, Object object) {
    233         assertTrue(message, object == null);
    234     }
    235     /**
    236      * Asserts that two objects refer to the same object. If they are not
    237      * an AssertionFailedError is thrown with the given message.
    238      */
    239     static public void assertSame(String message, Object expected, Object actual) {
    240         if (expected == actual)
    241             return;
    242         failNotSame(message, expected, actual);
    243     }
    244     /**
    245      * Asserts that two objects refer to the same object. If they are not
    246      * the same an AssertionFailedError is thrown.
    247      */
    248     static public void assertSame(Object expected, Object actual) {
    249         assertSame(null, expected, actual);
    250     }
    251      /**
    252       * Asserts that two objects refer to the same object. If they are not
    253       * an AssertionFailedError is thrown with the given message.
    254       */
    255     static public void assertNotSame(String message, Object expected, Object actual) {
    256         if (expected == actual)
    257             failSame(message);
    258     }
    259     /**
    260      * Asserts that two objects refer to the same object. If they are not
    261      * the same an AssertionFailedError is thrown.
    262      */
    263     static public void assertNotSame(Object expected, Object actual) {
    264         assertNotSame(null, expected, actual);
    265     }
    266 
    267     static private void failSame(String message) {
    268         String formatted= "";
    269          if (message != null)
    270              formatted= message+" ";
    271          fail(formatted+"expected not same");
    272     }
    273 
    274     static private void failNotSame(String message, Object expected, Object actual) {
    275         String formatted= "";
    276         if (message != null)
    277             formatted= message+" ";
    278         fail(formatted+"expected same:<"+expected+"> was not:<"+actual+">");
    279     }
    280 
    281     static private void failNotEquals(String message, Object expected, Object actual) {
    282         fail(format(message, expected, actual));
    283     }
    284 
    285     static String format(String message, Object expected, Object actual) {
    286         String formatted= "";
    287         if (message != null)
    288             formatted= message+" ";
    289         return formatted+"expected:<"+expected+"> but was:<"+actual+">";
    290     }
    291 }
    292