KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > junit > Assert


1 package org.junit;
2
3 import java.lang.reflect.Array JavaDoc;
4
5 import org.junit.internal.ArrayComparisonFailure;
6
7 /**
8  * A set of assertion methods useful for writing tests. Only failed assertions are recorded.
9  * These methods can be used directly: <code>Assert.assertEquals(...)</code>, however, they
10  * read better if they are referenced through static import:<br/>
11  * <pre>
12  * import static org.junit.Assert.*;
13  * ...
14  * assertEquals(...);
15  * </pre>
16  *
17  * @see AssertionError
18  */

19 public class Assert {
20     /**
21      * Protect constructor since it is a static only class
22      */

23     protected Assert() {
24     }
25     
26     /**
27      * Asserts that a condition is true. If it isn't it throws an
28      * {@link AssertionError} with the given message.
29      * @param message the identifying message or <code>null</code> for the {@link AssertionError}
30      * @param condition condition to be checked
31      */

32     static public void assertTrue(String JavaDoc message, boolean condition) {
33         if (!condition)
34             fail(message);
35     }
36
37     /**
38      * Asserts that a condition is true. If it isn't it throws an
39      * {@link AssertionError} without a message.
40      * @param condition condition to be checked
41      */

42     static public void assertTrue(boolean condition) {
43         assertTrue(null, condition);
44     }
45
46     /**
47      * Asserts that a condition is false. If it isn't it throws an
48      * {@link AssertionError} with the given message.
49      * @param message the identifying message or <code>null</code> for the {@link AssertionError}
50      * @param condition condition to be checked
51      */

52     static public void assertFalse(String JavaDoc message, boolean condition) {
53         assertTrue(message, !condition);
54     }
55
56     /**
57      * Asserts that a condition is false. If it isn't it throws an
58      * {@link AssertionError} without a message.
59      * @param condition condition to be checked
60      */

61     static public void assertFalse(boolean condition) {
62         assertFalse(null, condition);
63     }
64
65     /**
66      * Fails a test with the given message.
67      * @param message the identifying message or <code>null</code> for the {@link AssertionError}
68      * @see AssertionError
69      */

70     static public void fail(String JavaDoc message) {
71         throw new AssertionError JavaDoc(message == null ? "" : message);
72     }
73
74     /**
75      * Fails a test with no message.
76      */

77     static public void fail() {
78         fail(null);
79     }
80     
81     /**
82      * Asserts that two objects are equal. If they are not, an {@link AssertionError}
83      * is thrown with the given message. If <code>expected</code> and <code>actual</code>
84      * are <code>null</code>, they are considered equal.
85      * @param message the identifying message or <code>null</code> for the {@link AssertionError}
86      * @param expected expected value
87      * @param actual actual value
88      */

89     static public void assertEquals(String JavaDoc message, Object JavaDoc expected, Object JavaDoc actual) {
90         if (expected == null && actual == null)
91             return;
92         if (expected != null && isEquals(expected, actual))
93             return;
94         else if (expected instanceof String JavaDoc && actual instanceof String JavaDoc) {
95             String JavaDoc cleanMessage= message == null ? "" : message;
96             throw new ComparisonFailure(cleanMessage, (String JavaDoc)expected, (String JavaDoc)actual);
97         }
98         else
99             failNotEquals(message, expected, actual);
100     }
101
102     private static boolean isEquals(Object JavaDoc expected, Object JavaDoc actual) {
103         if (expected instanceof Number JavaDoc && actual instanceof Number JavaDoc)
104             return ((Number JavaDoc) expected).longValue() == ((Number JavaDoc) actual).longValue();
105         return expected.equals(actual);
106     }
107
108     /**
109      * Asserts that two objects are equal. If they are not, an {@link AssertionError}
110      * without a message is thrown. If <code>expected</code> and <code>actual</code>
111      * are <code>null</code>, they are considered equal.
112      * @param expected expected value
113      * @param actual the value to check against <code>expected</code>
114      */

115     static public void assertEquals(Object JavaDoc expected, Object JavaDoc actual) {
116         assertEquals(null, expected, actual);
117     }
118
119     /**
120      * Asserts that two object arrays are equal. If they are not, an
121      * {@link AssertionError} is thrown with the given message. If <code>expecteds</code> and
122      * <code>actuals</code> are <code>null</code>, they are considered equal.
123      * @param message the identifying message or <code>null</code> for the {@link AssertionError}
124      * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values.
125      * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
126      */

127     public static void assertArrayEquals(String JavaDoc message, Object JavaDoc[] expecteds,
128             Object JavaDoc[] actuals) throws ArrayComparisonFailure {
129         internalArrayEquals(message, expecteds, actuals);
130     }
131     
132     /**
133      * Asserts that two object arrays are equal. If they are not, an {@link AssertionError}
134      * is thrown. If <code>expected</code> and <code>actual</code> are <code>null</code>,
135      * they are considered equal.
136      * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values
137      * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
138      */

139     public static void assertArrayEquals(Object JavaDoc[] expecteds, Object JavaDoc[] actuals) {
140         assertArrayEquals(null, expecteds, actuals);
141     }
142
143     /**
144      * TODO: fix javadoc
145      * Asserts that two object arrays are equal. If they are not, an
146      * {@link AssertionError} is thrown with the given message. If <code>expecteds</code> and
147      * <code>actuals</code> are <code>null</code>, they are considered equal.
148      * @param message the identifying message or <code>null</code> for the {@link AssertionError}
149      * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values.
150      * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
151      */

152     public static void assertArrayEquals(String JavaDoc message, byte[] expecteds,
153             byte[] actuals) throws ArrayComparisonFailure {
154         internalArrayEquals(message, expecteds, actuals);
155     }
156
157     /**
158      * TODO: fix javadoc
159      * Asserts that two object arrays are equal. If they are not, an {@link AssertionError}
160      * is thrown. If <code>expected</code> and <code>actual</code> are <code>null</code>,
161      * they are considered equal.
162      * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values
163      * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
164      */

165     public static void assertArrayEquals(byte[] expecteds, byte[] actuals) {
166         assertArrayEquals(null, expecteds, actuals);
167     }
168     
169     /**
170      * TODO: fix javadoc
171      * Asserts that two object arrays are equal. If they are not, an
172      * {@link AssertionError} is thrown with the given message. If <code>expecteds</code> and
173      * <code>actuals</code> are <code>null</code>, they are considered equal.
174      * @param message the identifying message or <code>null</code> for the {@link AssertionError}
175      * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values.
176      * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
177      */

178     public static void assertArrayEquals(String JavaDoc message, char[] expecteds,
179             char[] actuals) throws ArrayComparisonFailure {
180         internalArrayEquals(message, expecteds, actuals);
181     }
182
183     /**
184      * TODO: fix javadoc
185      * Asserts that two object arrays are equal. If they are not, an {@link AssertionError}
186      * is thrown. If <code>expected</code> and <code>actual</code> are <code>null</code>,
187      * they are considered equal.
188      * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values
189      * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
190      */

191     public static void assertArrayEquals(char[] expecteds, char[] actuals) {
192         assertArrayEquals(null, expecteds, actuals);
193     }
194     
195     /**
196      * TODO: fix javadoc
197      * Asserts that two object arrays are equal. If they are not, an
198      * {@link AssertionError} is thrown with the given message. If <code>expecteds</code> and
199      * <code>actuals</code> are <code>null</code>, they are considered equal.
200      * @param message the identifying message or <code>null</code> for the {@link AssertionError}
201      * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values.
202      * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
203      */

204     public static void assertArrayEquals(String JavaDoc message, short[] expecteds,
205             short[] actuals) throws ArrayComparisonFailure {
206         internalArrayEquals(message, expecteds, actuals);
207     }
208
209     /**
210      * TODO: fix javadoc
211      * Asserts that two object arrays are equal. If they are not, an {@link AssertionError}
212      * is thrown. If <code>expected</code> and <code>actual</code> are <code>null</code>,
213      * they are considered equal.
214      * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values
215      * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
216      */

217     public static void assertArrayEquals(short[] expecteds, short[] actuals) {
218         assertArrayEquals(null, expecteds, actuals);
219     }
220     
221     /**
222      * TODO: fix javadoc
223      * Asserts that two object arrays are equal. If they are not, an
224      * {@link AssertionError} is thrown with the given message. If <code>expecteds</code> and
225      * <code>actuals</code> are <code>null</code>, they are considered equal.
226      * @param message the identifying message or <code>null</code> for the {@link AssertionError}
227      * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values.
228      * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
229      */

230     public static void assertArrayEquals(String JavaDoc message, int[] expecteds,
231             int[] actuals) throws ArrayComparisonFailure {
232         internalArrayEquals(message, expecteds, actuals);
233     }
234
235     /**
236      * TODO: fix javadoc
237      * Asserts that two object arrays are equal. If they are not, an {@link AssertionError}
238      * is thrown. If <code>expected</code> and <code>actual</code> are <code>null</code>,
239      * they are considered equal.
240      * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values
241      * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
242      */

243     public static void assertArrayEquals(int[] expecteds, int[] actuals) {
244         assertArrayEquals(null, expecteds, actuals);
245     }
246
247     /**
248      * TODO: fix javadoc
249      * Asserts that two object arrays are equal. If they are not, an {@link AssertionError}
250      * is thrown. If <code>expected</code> and <code>actual</code> are <code>null</code>,
251      * they are considered equal.
252      * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values
253      * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
254      */

255     public static void assertArrayEquals(long[] expecteds, long[] actuals) {
256         assertArrayEquals(null, expecteds, actuals);
257     }
258     
259     /**
260      * TODO: fix javadoc
261      * Asserts that two object arrays are equal. If they are not, an
262      * {@link AssertionError} is thrown with the given message. If <code>expecteds</code> and
263      * <code>actuals</code> are <code>null</code>, they are considered equal.
264      * @param message the identifying message or <code>null</code> for the {@link AssertionError}
265      * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values.
266      * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
267      */

268     public static void assertArrayEquals(String JavaDoc message, long[] expecteds,
269             long[] actuals) throws ArrayComparisonFailure {
270         internalArrayEquals(message, expecteds, actuals);
271     }
272     
273     /**
274      * Asserts that two object arrays are equal. If they are not, an
275      * {@link AssertionError} is thrown with the given message. If <code>expecteds</code> and
276      * <code>actuals</code> are <code>null</code>, they are considered equal.
277      * @param message the identifying message or <code>null</code> for the {@link AssertionError}
278      * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values.
279      * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
280      */

281     private static void internalArrayEquals(String JavaDoc message, Object JavaDoc expecteds,
282             Object JavaDoc actuals) throws ArrayComparisonFailure {
283         if (expecteds == actuals)
284             return;
285         String JavaDoc 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 JavaDoc expected= Array.get(expecteds, i);
297             Object JavaDoc 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 JavaDoc e) {
309                     throw new ArrayComparisonFailure(header, e, i);
310                 }
311         }
312     }
313
314     private static boolean isArray(Object JavaDoc expected) {
315         return expected != null && expected.getClass().isArray();
316     }
317
318     /**
319      * Asserts that two doubles or floats are equal to within a positive delta. If they
320      * are not, an {@link AssertionError} is thrown with the given message. If the
321      * expected value is infinity then the delta value is ignored. NaNs are
322      * considered equal:
323      * <code>assertEquals(Double.NaN, Double.NaN, *)</code> passes
324      * @param message the identifying message or <code>null</code> for the {@link AssertionError}
325      * @param expected expected value
326      * @param actual the value to check against <code>expected</code>
327      * @param delta the maximum delta between <code>expected</code> and <code>actual</code> for which
328      * both numbers are still considered equal.
329      */

330     static public void assertEquals(String JavaDoc 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 JavaDoc(expected), new Double JavaDoc(actual));
335     }
336
337     /**
338      * Asserts that two doubles or floats are equal to within a positive delta. If they
339      * are not, an {@link AssertionError} is thrown. If the
340      * expected value is infinity then the delta value is ignored.NaNs are
341      * considered equal:
342      * <code>assertEquals(Double.NaN, Double.NaN, *)</code> passes
343      * @param expected expected value
344      * @param actual the value to check against <code>expected</code>
345      * @param delta the maximum delta between <code>expected</code> and <code>actual</code> for which
346      * both numbers are still considered equal.
347      */

348     static public void assertEquals(double expected, double actual, double delta) {
349         assertEquals(null, expected, actual, delta);
350     }
351
352     /**
353      * Asserts that an object isn't null. If it is an {@link AssertionError} is
354      * thrown with the given message.
355      * @param message the identifying message or <code>null</code> for the {@link AssertionError}
356      * @param object Object to check or <code>null</code>
357      */

358     static public void assertNotNull(String JavaDoc message, Object JavaDoc object) {
359         assertTrue(message, object != null);
360     }
361
362     /**
363      * Asserts that an object isn't null. If it is an {@link AssertionError} is
364      * thrown.
365      * @param object Object to check or <code>null</code>
366      */

367     static public void assertNotNull(Object JavaDoc object) {
368         assertNotNull(null, object);
369     }
370     
371     /**
372      * Asserts that an object is null. If it is not, an {@link AssertionError} is
373      * thrown with the given message.
374      * @param message the identifying message or <code>null</code> for the {@link AssertionError}
375      * @param object Object to check or <code>null</code>
376      */

377     static public void assertNull(String JavaDoc message, Object JavaDoc object) {
378         assertTrue(message, object == null);
379     }
380
381     /**
382      * Asserts that an object is null. If it isn't an {@link AssertionError} is
383      * thrown.
384      * @param object Object to check or <code>null</code>
385      */

386     static public void assertNull(Object JavaDoc object) {
387         assertNull(null, object);
388     }
389     
390     /**
391      * Asserts that two objects refer to the same object. If they are not, an
392      * {@link AssertionError} is thrown with the given message.
393      * @param message the identifying message or <code>null</code> for the {@link AssertionError}
394      * @param expected the expected object
395      * @param actual the object to compare to <code>expected</code>
396      */

397     static public void assertSame(String JavaDoc message, Object JavaDoc expected, Object JavaDoc actual) {
398         if (expected == actual)
399             return;
400         failNotSame(message, expected, actual);
401     }
402
403     /**
404      * Asserts that two objects refer to the same object. If they are not the
405      * same, an {@link AssertionError} without a message is thrown.
406      * @param expected the expected object
407      * @param actual the object to compare to <code>expected</code>
408      */

409     static public void assertSame(Object JavaDoc expected, Object JavaDoc actual) {
410         assertSame(null, expected, actual);
411     }
412
413     /**
414      * Asserts that two objects do not refer to the same object. If they do
415      * refer to the same object, an {@link AssertionError} is thrown with the given
416      * message.
417      * @param message the identifying message or <code>null</code> for the {@link AssertionError}
418      * @param unexpected the object you don't expect
419      * @param actual the object to compare to <code>unexpected</code>
420      */

421     static public void assertNotSame(String JavaDoc message, Object JavaDoc unexpected, Object JavaDoc actual) {
422         if (unexpected == actual)
423             failSame(message);
424     }
425
426     /**
427      * Asserts that two objects do not refer to the same object. If they do
428      * refer to the same object, an {@link AssertionError} without a message is thrown.
429      * @param unexpected the object you don't expect
430      * @param actual the object to compare to <code>unexpected</code>
431      */

432     static public void assertNotSame(Object JavaDoc unexpected, Object JavaDoc actual) {
433         assertNotSame(null, unexpected, actual);
434     }
435
436     static private void failSame(String JavaDoc message) {
437         String JavaDoc formatted= "";
438         if (message != null)
439             formatted= message + " ";
440         fail(formatted + "expected not same");
441     }
442
443     static private void failNotSame(String JavaDoc message, Object JavaDoc expected, Object JavaDoc actual) {
444         String JavaDoc 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 JavaDoc message, Object JavaDoc expected, Object JavaDoc actual) {
451         fail(format(message, expected, actual));
452     }
453
454     static String JavaDoc format(String JavaDoc message, Object JavaDoc expected, Object JavaDoc actual) {
455         String JavaDoc formatted= "";
456         if (message != null && ! message.equals(""))
457             formatted= message + " ";
458         String JavaDoc expectedString= String.valueOf(expected);
459         String JavaDoc 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     /**
467      * Asserts that two object arrays are equal. If they are not, an
468      * {@link AssertionError} is thrown with the given message. If <code>expecteds</code> and
469      * <code>actuals</code> are <code>null</code>, they are considered equal.
470      * @param message the identifying message or <code>null</code> for the {@link AssertionError}
471      * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values.
472      * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
473      * @deprecated use assertArrayEquals
474      */

475     @Deprecated JavaDoc
476     public static void assertEquals(String JavaDoc message, Object JavaDoc[] expecteds, Object JavaDoc[] actuals) {
477         assertArrayEquals(message, expecteds, actuals);
478     }
479
480     /**
481      * Asserts that two object arrays are equal. If they are not, an {@link AssertionError}
482      * is thrown. If <code>expected</code> and <code>actual</code> are <code>null</code>,
483      * they are considered equal.
484      * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values
485      * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
486      * @deprecated use assertArrayEquals
487      */

488     @Deprecated JavaDoc
489     public static void assertEquals(Object JavaDoc[] expecteds, Object JavaDoc[] actuals) {
490         assertArrayEquals(expecteds, actuals);
491     }
492
493 }
494
Popular Tags