001package org.junit;
002
003import org.hamcrest.Matcher;
004import org.hamcrest.MatcherAssert;
005import org.junit.internal.ArrayComparisonFailure;
006import org.junit.internal.ExactComparisonCriteria;
007import org.junit.internal.InexactComparisonCriteria;
008
009/**
010 * A set of assertion methods useful for writing tests. Only failed assertions
011 * are recorded. These methods can be used directly:
012 * <code>Assert.assertEquals(...)</code>, however, they read better if they
013 * are referenced through static import:<br/>
014 *
015 * <pre>
016 * import static org.junit.Assert.*;
017 *    ...
018 *    assertEquals(...);
019 * </pre>
020 *
021 * @see AssertionError
022 * @since 4.0
023 */
024public class Assert {
025    /**
026     * Protect constructor since it is a static only class
027     */
028    protected Assert() {
029    }
030
031    /**
032     * Asserts that a condition is true. If it isn't it throws an
033     * {@link AssertionError} with the given message.
034     *
035     * @param message the identifying message for the {@link AssertionError} (<code>null</code>
036     * okay)
037     * @param condition condition to be checked
038     */
039    static public void assertTrue(String message, boolean condition) {
040        if (!condition) {
041            fail(message);
042        }
043    }
044
045    /**
046     * Asserts that a condition is true. If it isn't it throws an
047     * {@link AssertionError} without a message.
048     *
049     * @param condition condition to be checked
050     */
051    static public void assertTrue(boolean condition) {
052        assertTrue(null, condition);
053    }
054
055    /**
056     * Asserts that a condition is false. If it isn't it throws an
057     * {@link AssertionError} with the given message.
058     *
059     * @param message the identifying message for the {@link AssertionError} (<code>null</code>
060     * okay)
061     * @param condition condition to be checked
062     */
063    static public void assertFalse(String message, boolean condition) {
064        assertTrue(message, !condition);
065    }
066
067    /**
068     * Asserts that a condition is false. If it isn't it throws an
069     * {@link AssertionError} without a message.
070     *
071     * @param condition condition to be checked
072     */
073    static public void assertFalse(boolean condition) {
074        assertFalse(null, condition);
075    }
076
077    /**
078     * Fails a test with the given message.
079     *
080     * @param message the identifying message for the {@link AssertionError} (<code>null</code>
081     * okay)
082     * @see AssertionError
083     */
084    static public void fail(String message) {
085        if (message == null) {
086            throw new AssertionError();
087        }
088        throw new AssertionError(message);
089    }
090
091    /**
092     * Fails a test with no message.
093     */
094    static public void fail() {
095        fail(null);
096    }
097
098    /**
099     * 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     */
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
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
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     */
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>first</code> and <code>second</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 first first value to check
156     * @param second the value to check against <code>first</code>
157     */
158    static public void assertNotEquals(String message, Object first,
159            Object second) {
160        if (equalsRegardingNull(first, second)) {
161            failEquals(message, first);
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>first</code> and <code>second</code> are <code>null</code>,
169     * they are considered equal.
170     *
171     * @param first first value to check
172     * @param second the value to check against <code>first</code>
173     */
174    static public void assertNotEquals(Object first, Object second) {
175        assertNotEquals(null, first, second);
176    }
177
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 first first value to check
195     * @param second the value to check against <code>first</code>
196     */
197    static public void assertNotEquals(String message, long first, long second) {
198        assertNotEquals(message, (Long) first, (Long) second);
199    }
200
201    /**
202     * Asserts that two longs are <b>not</b> equals. If they are, an
203     * {@link AssertionError} without a message is thrown.
204     *
205     * @param first first value to check
206     * @param second the value to check against <code>first</code>
207     */
208    static public void assertNotEquals(long first, long second) {
209        assertNotEquals(null, first, second);
210    }
211
212    /**
213     * Asserts that two doubles or floats are <b>not</b> equal to within a positive delta.
214     * If they are, an {@link AssertionError} is thrown with the given
215     * message. If the expected value is infinity then the delta value is
216     * ignored. NaNs are considered equal:
217     * <code>assertNotEquals(Double.NaN, Double.NaN, *)</code> fails
218     *
219     * @param message the identifying message for the {@link AssertionError} (<code>null</code>
220     * okay)
221     * @param first first value to check
222     * @param second the value to check against <code>first</code>
223     * @param delta the maximum delta between <code>expected</code> and
224     * <code>actual</code> for which both numbers are still
225     * considered equal.
226     */
227    static public void assertNotEquals(String message, double first,
228            double second, double delta) {
229        if (!doubleIsDifferent(first, second, delta)) {
230            failEquals(message, new Double(first));
231        }
232    }
233
234    /**
235     * Asserts that two doubles or floats are <b>not</b> equal to within a positive delta.
236     * If they are, an {@link AssertionError} is thrown. If the expected
237     * value is infinity then the delta value is ignored.NaNs are considered
238     * equal: <code>assertNotEquals(Double.NaN, Double.NaN, *)</code> fails
239     *
240     * @param first first value to check
241     * @param second the value to check against <code>first</code>
242     * @param delta the maximum delta between <code>expected</code> and
243     * <code>actual</code> for which both numbers are still
244     * considered equal.
245     */
246    static public void assertNotEquals(double first, double second, double delta) {
247        assertNotEquals(null, first, second, delta);
248    }
249
250    /**
251     * Asserts that two object arrays are equal. If they are not, an
252     * {@link AssertionError} is thrown with the given message. If
253     * <code>expecteds</code> and <code>actuals</code> are <code>null</code>,
254     * they are considered equal.
255     *
256     * @param message the identifying message for the {@link AssertionError} (<code>null</code>
257     * okay)
258     * @param expecteds Object array or array of arrays (multi-dimensional array) with
259     * expected values.
260     * @param actuals Object array or array of arrays (multi-dimensional array) with
261     * actual values
262     */
263    public static void assertArrayEquals(String message, Object[] expecteds,
264            Object[] actuals) throws ArrayComparisonFailure {
265        internalArrayEquals(message, expecteds, actuals);
266    }
267
268    /**
269     * Asserts that two object arrays are equal. If they are not, an
270     * {@link AssertionError} is thrown. If <code>expected</code> and
271     * <code>actual</code> are <code>null</code>, they are considered
272     * equal.
273     *
274     * @param expecteds Object array or array of arrays (multi-dimensional array) with
275     * expected values
276     * @param actuals Object array or array of arrays (multi-dimensional array) with
277     * actual values
278     */
279    public static void assertArrayEquals(Object[] expecteds, Object[] actuals) {
280        assertArrayEquals(null, expecteds, actuals);
281    }
282
283    /**
284     * Asserts that two byte arrays are equal. If they are not, an
285     * {@link AssertionError} is thrown with the given message.
286     *
287     * @param message the identifying message for the {@link AssertionError} (<code>null</code>
288     * okay)
289     * @param expecteds byte array with expected values.
290     * @param actuals byte array with actual values
291     */
292    public static void assertArrayEquals(String message, byte[] expecteds,
293            byte[] actuals) throws ArrayComparisonFailure {
294        internalArrayEquals(message, expecteds, actuals);
295    }
296
297    /**
298     * Asserts that two byte arrays are equal. If they are not, an
299     * {@link AssertionError} is thrown.
300     *
301     * @param expecteds byte array with expected values.
302     * @param actuals byte array with actual values
303     */
304    public static void assertArrayEquals(byte[] expecteds, byte[] actuals) {
305        assertArrayEquals(null, expecteds, actuals);
306    }
307
308    /**
309     * Asserts that two char arrays are equal. If they are not, an
310     * {@link AssertionError} is thrown with the given message.
311     *
312     * @param message the identifying message for the {@link AssertionError} (<code>null</code>
313     * okay)
314     * @param expecteds char array with expected values.
315     * @param actuals char array with actual values
316     */
317    public static void assertArrayEquals(String message, char[] expecteds,
318            char[] actuals) throws ArrayComparisonFailure {
319        internalArrayEquals(message, expecteds, actuals);
320    }
321
322    /**
323     * Asserts that two char arrays are equal. If they are not, an
324     * {@link AssertionError} is thrown.
325     *
326     * @param expecteds char array with expected values.
327     * @param actuals char array with actual values
328     */
329    public static void assertArrayEquals(char[] expecteds, char[] actuals) {
330        assertArrayEquals(null, expecteds, actuals);
331    }
332
333    /**
334     * Asserts that two short arrays are equal. If they are not, an
335     * {@link AssertionError} is thrown with the given message.
336     *
337     * @param message the identifying message for the {@link AssertionError} (<code>null</code>
338     * okay)
339     * @param expecteds short array with expected values.
340     * @param actuals short array with actual values
341     */
342    public static void assertArrayEquals(String message, short[] expecteds,
343            short[] actuals) throws ArrayComparisonFailure {
344        internalArrayEquals(message, expecteds, actuals);
345    }
346
347    /**
348     * Asserts that two short arrays are equal. If they are not, an
349     * {@link AssertionError} is thrown.
350     *
351     * @param expecteds short array with expected values.
352     * @param actuals short array with actual values
353     */
354    public static void assertArrayEquals(short[] expecteds, short[] actuals) {
355        assertArrayEquals(null, expecteds, actuals);
356    }
357
358    /**
359     * Asserts that two int arrays are equal. If they are not, an
360     * {@link AssertionError} is thrown with the given message.
361     *
362     * @param message the identifying message for the {@link AssertionError} (<code>null</code>
363     * okay)
364     * @param expecteds int array with expected values.
365     * @param actuals int array with actual values
366     */
367    public static void assertArrayEquals(String message, int[] expecteds,
368            int[] actuals) throws ArrayComparisonFailure {
369        internalArrayEquals(message, expecteds, actuals);
370    }
371
372    /**
373     * Asserts that two int arrays are equal. If they are not, an
374     * {@link AssertionError} is thrown.
375     *
376     * @param expecteds int array with expected values.
377     * @param actuals int array with actual values
378     */
379    public static void assertArrayEquals(int[] expecteds, int[] actuals) {
380        assertArrayEquals(null, expecteds, actuals);
381    }
382
383    /**
384     * Asserts that two long arrays are equal. If they are not, an
385     * {@link AssertionError} is thrown with the given message.
386     *
387     * @param message the identifying message for the {@link AssertionError} (<code>null</code>
388     * okay)
389     * @param expecteds long array with expected values.
390     * @param actuals long array with actual values
391     */
392    public static void assertArrayEquals(String message, long[] expecteds,
393            long[] actuals) throws ArrayComparisonFailure {
394        internalArrayEquals(message, expecteds, actuals);
395    }
396
397    /**
398     * Asserts that two long arrays are equal. If they are not, an
399     * {@link AssertionError} is thrown.
400     *
401     * @param expecteds long array with expected values.
402     * @param actuals long array with actual values
403     */
404    public static void assertArrayEquals(long[] expecteds, long[] actuals) {
405        assertArrayEquals(null, expecteds, actuals);
406    }
407
408    /**
409     * Asserts that two double arrays are equal. If they are not, an
410     * {@link AssertionError} is thrown with the given message.
411     *
412     * @param message the identifying message for the {@link AssertionError} (<code>null</code>
413     * okay)
414     * @param expecteds double array with expected values.
415     * @param actuals double array with actual values
416     */
417    public static void assertArrayEquals(String message, double[] expecteds,
418            double[] actuals, double delta) throws ArrayComparisonFailure {
419        new InexactComparisonCriteria(delta).arrayEquals(message, expecteds, actuals);
420    }
421
422    /**
423     * Asserts that two double arrays are equal. If they are not, an
424     * {@link AssertionError} is thrown.
425     *
426     * @param expecteds double array with expected values.
427     * @param actuals double array with actual values
428     */
429    public static void assertArrayEquals(double[] expecteds, double[] actuals, double delta) {
430        assertArrayEquals(null, expecteds, actuals, delta);
431    }
432
433    /**
434     * Asserts that two float arrays are equal. If they are not, an
435     * {@link AssertionError} is thrown with the given message.
436     *
437     * @param message the identifying message for the {@link AssertionError} (<code>null</code>
438     * okay)
439     * @param expecteds float array with expected values.
440     * @param actuals float array with actual values
441     */
442    public static void assertArrayEquals(String message, float[] expecteds,
443            float[] actuals, float delta) throws ArrayComparisonFailure {
444        new InexactComparisonCriteria(delta).arrayEquals(message, expecteds, actuals);
445    }
446
447    /**
448     * Asserts that two float arrays are equal. If they are not, an
449     * {@link AssertionError} is thrown.
450     *
451     * @param expecteds float array with expected values.
452     * @param actuals float array with actual values
453     */
454    public static void assertArrayEquals(float[] expecteds, float[] actuals, float delta) {
455        assertArrayEquals(null, expecteds, actuals, delta);
456    }
457
458    /**
459     * Asserts that two object arrays are equal. If they are not, an
460     * {@link AssertionError} is thrown with the given message. If
461     * <code>expecteds</code> and <code>actuals</code> are <code>null</code>,
462     * they are considered equal.
463     *
464     * @param message the identifying message for the {@link AssertionError} (<code>null</code>
465     * okay)
466     * @param expecteds Object array or array of arrays (multi-dimensional array) with
467     * expected values.
468     * @param actuals Object array or array of arrays (multi-dimensional array) with
469     * actual values
470     */
471    private static void internalArrayEquals(String message, Object expecteds,
472            Object actuals) throws ArrayComparisonFailure {
473        new ExactComparisonCriteria().arrayEquals(message, expecteds, actuals);
474    }
475
476    /**
477     * Asserts that two doubles are equal to within a positive delta.
478     * If they are not, an {@link AssertionError} is thrown with the given
479     * message. If the expected value is infinity then the delta value is
480     * ignored. NaNs are considered equal:
481     * <code>assertEquals(Double.NaN, Double.NaN, *)</code> passes
482     *
483     * @param message the identifying message for the {@link AssertionError} (<code>null</code>
484     * okay)
485     * @param expected expected value
486     * @param actual the value to check against <code>expected</code>
487     * @param delta the maximum delta between <code>expected</code> and
488     * <code>actual</code> for which both numbers are still
489     * considered equal.
490     */
491    static public void assertEquals(String message, double expected,
492            double actual, double delta) {
493        if (doubleIsDifferent(expected, actual, delta)) {
494            failNotEquals(message, new Double(expected), new Double(actual));
495        }
496    }
497
498    /**
499     * Asserts that two floats are equal to within a positive delta.
500     * If they are not, an {@link AssertionError} is thrown with the given
501     * message. If the expected value is infinity then the delta value is
502     * ignored. NaNs are considered equal:
503     * <code>assertEquals(Float.NaN, Float.NaN, *)</code> passes
504     *
505     * @param message the identifying message for the {@link AssertionError} (<code>null</code>
506     * okay)
507     * @param expected expected value
508     * @param actual the value to check against <code>expected</code>
509     * @param delta the maximum delta between <code>expected</code> and
510     * <code>actual</code> for which both numbers are still
511     * considered equal.
512     */
513    static public void assertEquals(String message, float expected,
514            float actual, float delta) {
515        if (Float.compare(expected, actual) == 0) {
516            return;
517        }
518        if (!(Math.abs(expected - actual) <= delta)) {
519            failNotEquals(message, new Float(expected), new Float(actual));
520        }
521    }
522
523    static private boolean doubleIsDifferent(double d1, double d2, double delta) {
524        if (Double.compare(d1, d2) == 0) {
525            return false;
526        }
527        if ((Math.abs(d1 - d2) <= delta)) {
528            return false;
529        }
530
531        return true;
532    }
533
534    /**
535     * Asserts that two longs are equal. If they are not, an
536     * {@link AssertionError} is thrown.
537     *
538     * @param expected expected long value.
539     * @param actual actual long value
540     */
541    static public void assertEquals(long expected, long actual) {
542        assertEquals(null, expected, actual);
543    }
544
545    /**
546     * Asserts that two longs are equal. If they are not, an
547     * {@link AssertionError} is thrown with the given message.
548     *
549     * @param message the identifying message for the {@link AssertionError} (<code>null</code>
550     * okay)
551     * @param expected long expected value.
552     * @param actual long actual value
553     */
554    static public void assertEquals(String message, long expected, long actual) {
555        assertEquals(message, (Long) expected, (Long) actual);
556    }
557
558    /**
559     * @deprecated Use
560     *             <code>assertEquals(double expected, double actual, double delta)</code>
561     *             instead
562     */
563    @Deprecated
564    static public void assertEquals(double expected, double actual) {
565        assertEquals(null, expected, actual);
566    }
567
568    /**
569     * @deprecated Use
570     *             <code>assertEquals(String message, double expected, double actual, double delta)</code>
571     *             instead
572     */
573    @Deprecated
574    static public void assertEquals(String message, double expected,
575            double actual) {
576        fail("Use assertEquals(expected, actual, delta) to compare floating-point numbers");
577    }
578
579    /**
580     * Asserts that two doubles are equal to within a positive delta.
581     * If they are not, an {@link AssertionError} is thrown. If the expected
582     * value is infinity then the delta value is ignored.NaNs are considered
583     * equal: <code>assertEquals(Double.NaN, Double.NaN, *)</code> passes
584     *
585     * @param expected expected value
586     * @param actual the value to check against <code>expected</code>
587     * @param delta the maximum delta between <code>expected</code> and
588     * <code>actual</code> for which both numbers are still
589     * considered equal.
590     */
591    static public void assertEquals(double expected, double actual, double delta) {
592        assertEquals(null, expected, actual, delta);
593    }
594
595    /**
596     * Asserts that two floats are equal to within a positive delta.
597     * If they are not, an {@link AssertionError} is thrown. If the expected
598     * value is infinity then the delta value is ignored. NaNs are considered
599     * equal: <code>assertEquals(Float.NaN, Float.NaN, *)</code> passes
600     *
601     * @param expected expected value
602     * @param actual the value to check against <code>expected</code>
603     * @param delta the maximum delta between <code>expected</code> and
604     * <code>actual</code> for which both numbers are still
605     * considered equal.
606     */
607
608    static public void assertEquals(float expected, float actual, float delta) {
609        assertEquals(null, expected, actual, delta);
610    }
611
612    /**
613     * Asserts that an object isn't null. If it is an {@link AssertionError} is
614     * thrown with the given message.
615     *
616     * @param message the identifying message for the {@link AssertionError} (<code>null</code>
617     * okay)
618     * @param object Object to check or <code>null</code>
619     */
620    static public void assertNotNull(String message, Object object) {
621        assertTrue(message, object != null);
622    }
623
624    /**
625     * Asserts that an object isn't null. If it is an {@link AssertionError} is
626     * thrown.
627     *
628     * @param object Object to check or <code>null</code>
629     */
630    static public void assertNotNull(Object object) {
631        assertNotNull(null, object);
632    }
633
634    /**
635     * Asserts that an object is null. If it is not, an {@link AssertionError}
636     * is thrown with the given message.
637     *
638     * @param message the identifying message for the {@link AssertionError} (<code>null</code>
639     * okay)
640     * @param object Object to check or <code>null</code>
641     */
642    static public void assertNull(String message, Object object) {
643        if (object == null) {
644            return;
645        }
646        failNotNull(message, object);
647    }
648
649    /**
650     * Asserts that an object is null. If it isn't an {@link AssertionError} is
651     * thrown.
652     *
653     * @param object Object to check or <code>null</code>
654     */
655    static public void assertNull(Object object) {
656        assertNull(null, object);
657    }
658
659    static private void failNotNull(String message, Object actual) {
660        String formatted = "";
661        if (message != null) {
662            formatted = message + " ";
663        }
664        fail(formatted + "expected null, but was:<" + actual + ">");
665    }
666
667    /**
668     * Asserts that two objects refer to the same object. If they are not, an
669     * {@link AssertionError} is thrown with the given message.
670     *
671     * @param message the identifying message for the {@link AssertionError} (<code>null</code>
672     * okay)
673     * @param expected the expected object
674     * @param actual the object to compare to <code>expected</code>
675     */
676    static public void assertSame(String message, Object expected, Object actual) {
677        if (expected == actual) {
678            return;
679        }
680        failNotSame(message, expected, actual);
681    }
682
683    /**
684     * Asserts that two objects refer to the same object. If they are not the
685     * same, an {@link AssertionError} without a message is thrown.
686     *
687     * @param expected the expected object
688     * @param actual the object to compare to <code>expected</code>
689     */
690    static public void assertSame(Object expected, Object actual) {
691        assertSame(null, expected, actual);
692    }
693
694    /**
695     * Asserts that two objects do not refer to the same object. If they do
696     * refer to the same object, an {@link AssertionError} is thrown with the
697     * given message.
698     *
699     * @param message the identifying message for the {@link AssertionError} (<code>null</code>
700     * okay)
701     * @param unexpected the object you don't expect
702     * @param actual the object to compare to <code>unexpected</code>
703     */
704    static public void assertNotSame(String message, Object unexpected,
705            Object actual) {
706        if (unexpected == actual) {
707            failSame(message);
708        }
709    }
710
711    /**
712     * Asserts that two objects do not refer to the same object. If they do
713     * refer to the same object, an {@link AssertionError} without a message is
714     * thrown.
715     *
716     * @param unexpected the object you don't expect
717     * @param actual the object to compare to <code>unexpected</code>
718     */
719    static public void assertNotSame(Object unexpected, Object actual) {
720        assertNotSame(null, unexpected, actual);
721    }
722
723    static private void failSame(String message) {
724        String formatted = "";
725        if (message != null) {
726            formatted = message + " ";
727        }
728        fail(formatted + "expected not same");
729    }
730
731    static private void failNotSame(String message, Object expected,
732            Object actual) {
733        String formatted = "";
734        if (message != null) {
735            formatted = message + " ";
736        }
737        fail(formatted + "expected same:<" + expected + "> was not:<" + actual
738                + ">");
739    }
740
741    static private void failNotEquals(String message, Object expected,
742            Object actual) {
743        fail(format(message, expected, actual));
744    }
745
746    static String format(String message, Object expected, Object actual) {
747        String formatted = "";
748        if (message != null && !message.equals("")) {
749            formatted = message + " ";
750        }
751        String expectedString = String.valueOf(expected);
752        String actualString = String.valueOf(actual);
753        if (expectedString.equals(actualString)) {
754            return formatted + "expected: "
755                    + formatClassAndValue(expected, expectedString)
756                    + " but was: " + formatClassAndValue(actual, actualString);
757        } else {
758            return formatted + "expected:<" + expectedString + "> but was:<"
759                    + actualString + ">";
760        }
761    }
762
763    private static String formatClassAndValue(Object value, String valueString) {
764        String className = value == null ? "null" : value.getClass().getName();
765        return className + "<" + valueString + ">";
766    }
767
768    /**
769     * Asserts that two object arrays are equal. If they are not, an
770     * {@link AssertionError} is thrown with the given message. If
771     * <code>expecteds</code> and <code>actuals</code> are <code>null</code>,
772     * they are considered equal.
773     *
774     * @param message the identifying message for the {@link AssertionError} (<code>null</code>
775     * okay)
776     * @param expecteds Object array or array of arrays (multi-dimensional array) with
777     * expected values.
778     * @param actuals Object array or array of arrays (multi-dimensional array) with
779     * actual values
780     * @deprecated use assertArrayEquals
781     */
782    @Deprecated
783    public static void assertEquals(String message, Object[] expecteds,
784            Object[] actuals) {
785        assertArrayEquals(message, expecteds, actuals);
786    }
787
788    /**
789     * Asserts that two object arrays are equal. If they are not, an
790     * {@link AssertionError} is thrown. If <code>expected</code> and
791     * <code>actual</code> are <code>null</code>, they are considered
792     * equal.
793     *
794     * @param expecteds Object array or array of arrays (multi-dimensional array) with
795     * expected values
796     * @param actuals Object array or array of arrays (multi-dimensional array) with
797     * actual values
798     * @deprecated use assertArrayEquals
799     */
800    @Deprecated
801    public static void assertEquals(Object[] expecteds, Object[] actuals) {
802        assertArrayEquals(expecteds, actuals);
803    }
804
805    /**
806     * Asserts that <code>actual</code> satisfies the condition specified by
807     * <code>matcher</code>. If not, an {@link AssertionError} is thrown with
808     * information about the matcher and failing value. Example:
809     *
810     * <pre>
811     *   assertThat(0, is(1)); // fails:
812     *     // failure message:
813     *     // expected: is &lt;1&gt;
814     *     // got value: &lt;0&gt;
815     *   assertThat(0, is(not(1))) // passes
816     * </pre>
817     *
818     * <code>org.hamcrest.Matcher</code> does not currently document the meaning
819     * of its type parameter <code>T</code>.  This method assumes that a matcher
820     * typed as <code>Matcher&lt;T&gt;</code> can be meaningfully applied only
821     * to values that could be assigned to a variable of type <code>T</code>.
822     *
823     * @param <T> the static type accepted by the matcher (this can flag obvious
824     * compile-time problems such as {@code assertThat(1, is("a"))}
825     * @param actual the computed value being compared
826     * @param matcher an expression, built of {@link Matcher}s, specifying allowed
827     * values
828     * @see org.hamcrest.CoreMatchers
829     * @see org.hamcrest.MatcherAssert
830     */
831    public static <T> void assertThat(T actual, Matcher<? super T> matcher) {
832        assertThat("", actual, matcher);
833    }
834
835    /**
836     * Asserts that <code>actual</code> satisfies the condition specified by
837     * <code>matcher</code>. If not, an {@link AssertionError} is thrown with
838     * the reason and information about the matcher and failing value. Example:
839     *
840     * <pre>
841     *   assertThat(&quot;Help! Integers don't work&quot;, 0, is(1)); // fails:
842     *     // failure message:
843     *     // Help! Integers don't work
844     *     // expected: is &lt;1&gt;
845     *     // got value: &lt;0&gt;
846     *   assertThat(&quot;Zero is one&quot;, 0, is(not(1))) // passes
847     * </pre>
848     *
849     * <code>org.hamcrest.Matcher</code> does not currently document the meaning
850     * of its type parameter <code>T</code>.  This method assumes that a matcher
851     * typed as <code>Matcher&lt;T&gt;</code> can be meaningfully applied only
852     * to values that could be assigned to a variable of type <code>T</code>.
853     *
854     * @param reason additional information about the error
855     * @param <T> the static type accepted by the matcher (this can flag obvious
856     * compile-time problems such as {@code assertThat(1, is("a"))}
857     * @param actual the computed value being compared
858     * @param matcher an expression, built of {@link Matcher}s, specifying allowed
859     * values
860     * @see org.hamcrest.CoreMatchers
861     * @see org.hamcrest.MatcherAssert
862     */
863    public static <T> void assertThat(String reason, T actual,
864            Matcher<? super T> matcher) {
865        MatcherAssert.assertThat(reason, actual, matcher);
866    }
867}