KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junitx > framework > ArrayAssert


1 /*
2  * The JUnit-addons Software License, Version 1.0
3  * (based on the Apache Software License, Version 1.1)
4  *
5  * Copyright (c) 2002-2003 Vladimir R. Bossicard. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by Vladimir R.
22  * Bossicard as well as other contributors
23  * (http://junit-addons.sourceforge.net/)."
24  * Alternately, this acknowlegement may appear in the software itself,
25  * if and wherever such third-party acknowlegements normally appear.
26  *
27  * 4. The names "JUnit-addons" must not be used to endorse or promote
28  * products derived from this software without prior written
29  * permission. For written permission, please contact
30  * vbossica@users.sourceforge.net.
31  *
32  * 5. Products derived from this software may not be called "JUnit-addons"
33  * nor may "JUnit-addons" appear in their names without prior written
34  * permission of the project managers.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ======================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals. For more information on the JUnit-addons Project, please
52  * see <http://junit-addons.sourceforge.net/>.
53  */

54
55 package junitx.framework;
56
57 import junit.framework.Assert;
58
59 import java.util.ArrayList JavaDoc;
60 import java.util.Arrays JavaDoc;
61
62 /**
63  * A set of assert methods specially targetted to asserting arrays.
64  *
65  * @version $Revision: 1.11 $ $Date: 2003/04/14 01:06:32 $
66  * @author <a HREF="mailto:vbossica@users.sourceforge.net">Vladimir R. Bossicard</a>
67  */

68 public class ArrayAssert {
69
70     /**
71      * Don't let anyone have access to this constructor.
72      */

73     private ArrayAssert() {
74     }
75
76     /**
77      * Asserts that two arrays are equal. Throws an
78      * <tt>AssertionFailedError</tt> if they are not.
79      *
80      * Two arrays are considered equals if:
81      * <ul>
82      * <li>their respective lengths are the same</li>
83      * <li>the respective messages must be equal</li>
84      * <li>the respective localized messages must be equal</li>
85      * </ul>
86      */

87     static public void assertEquals(String JavaDoc message,
88                                     Object JavaDoc[] expected,
89                                     Object JavaDoc[] actual) {
90         if (Arrays.equals(expected, actual)) {
91             return;
92         }
93
94         String JavaDoc formatted = "";
95         if (message != null) {
96             formatted = message + " ";
97         }
98
99         Assert.assertNotNull(formatted + "expected array: <not null> but was <null>", expected);
100         Assert.assertNotNull(formatted + "expected array: <null> but was <not null>", actual);
101         Assert.assertEquals(formatted + "[array length] ", expected.length, actual.length);
102         for (int i = 0; i < expected.length; i++) {
103             Assert.assertEquals(formatted + "[position " + i + "]", expected[i], actual[i]);
104         }
105     }
106
107     /**
108      * Asserts that two arrays are equal. Throws an
109      * <tt>AssertionFailedError</tt> if they are not.
110      */

111     static public void assertEquals(Object JavaDoc[] expected,
112                                     Object JavaDoc[] actual) {
113         assertEquals(null, expected, actual);
114     }
115
116     /**
117      * Asserts that two arrays are equal. Two arrays are considered equal if:
118      * <ul>
119      * <li>their respective lengths are the same</i>
120      * <li>all corresponding pairs of elements are equal (within the delta range)
121      * <li>both array references are <code>null</code>
122      * </ul>
123      *
124      * @param expected a double array of expected values
125      * @param actual a double array of actual values
126      * @param delta tolerated delta
127      */

128     public static void assertEquals(double[] expected,
129                                     double[] actual,
130                                     double delta) {
131         assertEquals(null, expected, actual, delta);
132     }
133
134     /**
135      * Asserts that two arrays are equal. Two arrays are considered equal if:
136      * <ul>
137      * <li>their respective lengths are the same</i>
138      * <li>all corresponding pairs of elements are equal (within the delta range)
139      * <li>both array references are <code>null</code>
140      * </ul>
141      *
142      * @param message message to display when arrays are not equal
143      * @param expected a double array of expected values
144      * @param actual a double array of actual values
145      * @param delta tolerated delta
146      */

147     public static void assertEquals(String JavaDoc message,
148                                     double[] expected,
149                                     double[] actual,
150                                     double delta) {
151         if (Arrays.equals(expected, actual)) {
152             return;
153         }
154
155         String JavaDoc formatted = "";
156         if (message != null) {
157             formatted = message + " ";
158         }
159
160         Assert.assertNotNull(formatted + "expected array: <not null> but was <null>", expected);
161         Assert.assertNotNull(formatted + "expected array: <null> but was <not null>", actual);
162         Assert.assertEquals(formatted + "[array length] ", expected.length, actual.length);
163         for (int i = 0; i < actual.length; i++) {
164             Assert.assertEquals(formatted + "[position " + i + "]", expected[i], actual[i], delta);
165         }
166     }
167
168     /**
169      * Asserts that two arrays are equal. Two arrays are considered equal if:
170      * <ul>
171      * <li>their respective lengths are the same</i>
172      * <li>all corresponding pairs of elements are equal (within the delta range)
173      * <li>both array references are <code>null</code>
174      * </ul>
175      *
176      * @param message message to display when arrays are not equal
177      * @param expected a float array of expected values
178      * @param actual a float array of actual values
179      * @param delta tolerated delta
180      */

181     public static void assertEquals(String JavaDoc message,
182                                     float[] expected,
183                                     float[] actual,
184                                     float delta) {
185         if (Arrays.equals(expected, actual)) {
186             return;
187         }
188
189         String JavaDoc formatted = "";
190         if (message != null) {
191             formatted = message + " ";
192         }
193
194         Assert.assertNotNull(formatted + "expected array: <not null> but was <null>", expected);
195         Assert.assertNotNull(formatted + "expected array: <null> but was <not null>", actual);
196         Assert.assertEquals(formatted + "[array length] ", expected.length, actual.length);
197         for (int i = 0; i < actual.length; i++) {
198             Assert.assertEquals(formatted + "[position " + i + "]", expected[i], actual[i], delta);
199         }
200     }
201
202     /**
203      * Asserts that two arrays are equal. Two arrays are considered equal if:
204      * <ul>
205      * <li>their respective lengths are the same</i>
206      * <li>all corresponding pairs of elements are equal (within the delta range)
207      * <li>both array references are <code>null</code>
208      * </ul>
209      *
210      * @param expected a float array of expected values
211      * @param actual a float array of actual values
212      * @param delta tolerated delta
213      */

214     public static void assertEquals(float[] expected,
215                                     float[] actual,
216                                     float delta) {
217         assertEquals(null, expected, actual, delta);
218     }
219
220     /**
221      * Asserts that two arrays are equal. Two arrays are considered equal if:
222      * <ul>
223      * <li>their respective lengths are the same</i>
224      * <li>all corresponding pairs of elements are equal
225      * <li>both array references are <code>null</code>
226      * </ul>
227      *
228      * @param message message to display when arrays are not equal
229      * @param expected a long array of expected values
230      * @param actual a long array of actual values
231      */

232     public static void assertEquals(String JavaDoc message,
233                                     long[] expected,
234                                     long[] actual) {
235         if (Arrays.equals(expected, actual)) {
236             return;
237         }
238
239         String JavaDoc formatted = "";
240         if (message != null) {
241             formatted = message + " ";
242         }
243
244         Assert.assertNotNull(formatted + "expected array: <not null> but was <null>", expected);
245         Assert.assertNotNull(formatted + "expected array: <null> but was <not null>", actual);
246         Assert.assertEquals(formatted + "[array length] ", expected.length, actual.length);
247         for (int i = 0; i < actual.length; i++) {
248             Assert.assertEquals(formatted + "[position " + i + "]", expected[i], actual[i]);
249         }
250     }
251
252     /**
253      * Asserts that two arrays are equal. Two arrays are considered equal if:
254      * <ul>
255      * <li>their respective lengths are the same</i>
256      * <li>all corresponding pairs of elements are equal
257      * <li>both array references are <code>null</code>
258      * </ul>
259      *
260      * @param expected a long array of expected values
261      * @param actual a long array of actual values
262      */

263     public static void assertEquals(long[] expected,
264                                     long[] actual) {
265         assertEquals(null, expected, actual);
266     }
267
268     /**
269      * Asserts that two arrays are equal. Two arrays are considered equal if:
270      * <ul>
271      * <li>their respective lengths are the same</i>
272      * <li>all corresponding pairs of elements are equal
273      * <li>both array references are <code>null</code>
274      * </ul>
275      *
276      * @param message message to display when arrays are not equal
277      * @param expected an int array of expected values
278      * @param actual an int array of actual values
279      */

280     public static void assertEquals(String JavaDoc message,
281                                     int[] expected,
282                                     int[] actual) {
283         if (Arrays.equals(expected, actual)) {
284             return;
285         }
286
287         String JavaDoc formatted = "";
288         if (message != null) {
289             formatted = message + " ";
290         }
291
292         Assert.assertNotNull(formatted + "expected array: <not null> but was <null>", expected);
293         Assert.assertNotNull(formatted + "expected array: <null> but was <not null>", actual);
294         Assert.assertEquals(formatted + "[array length] ", expected.length, actual.length);
295
296         for (int i = 0; i < actual.length; i++) {
297             Assert.assertEquals(formatted + "[position " + i + "]", expected[i], actual[i]);
298         }
299     }
300
301     /**
302      * Asserts that two arrays are equal. Two arrays are considered equal if:
303      * <ul>
304      * <li>their respective lengths are the same</i>
305      * <li>all corresponding pairs of elements are equal
306      * <li>both array references are <code>null</code>
307      * </ul>
308      *
309      * @param expected an int array of expected values
310      * @param actual an int array of actual values
311      */

312     public static void assertEquals(int[] expected,
313                                     int[] actual) {
314         assertEquals(null, expected, actual);
315     }
316
317     /**
318      * Asserts that two arrays are equal. Two arrays are considered equal if:
319      * <ul>
320      * <li>their respective lengths are the same</i>
321      * <li>all corresponding pairs of elements are equal
322      * <li>both array references are <code>null</code>
323      * </ul>
324      *
325      * @param message message to display when arrays are not equal
326      * @param expected a short array of expected values
327      * @param actual a short array of actual values
328      */

329     public static void assertEquals(String JavaDoc message,
330                                     short[] expected,
331                                     short[] actual) {
332         if (Arrays.equals(expected, actual)) {
333             return;
334         }
335
336         String JavaDoc formatted = "";
337         if (message != null) {
338             formatted = message + " ";
339         }
340
341         Assert.assertNotNull(formatted + "expected array: <not null> but was <null>", expected);
342         Assert.assertNotNull(formatted + "expected array: <null> but was <not null>", actual);
343         Assert.assertEquals(formatted + "[array length] ", expected.length, actual.length);
344
345         for (int i = 0; i < actual.length; i++) {
346             Assert.assertEquals(formatted + "[position " + i + "]", expected[i], actual[i]);
347         }
348     }
349
350     /**
351      * Asserts that two arrays are equal. Two arrays are considered equal if:
352      * <ul>
353      * <li>their respective lengths are the same</i>
354      * <li>all corresponding pairs of elements are equal
355      * <li>both array references are <code>null</code>
356      * </ul>
357      *
358      * @param expected a short array of expected values
359      * @param actual a short array of actual values
360      */

361     public static void assertEquals(short[] expected,
362                                     short[] actual) {
363         assertEquals(null, expected, actual);
364     }
365
366     /**
367      * Asserts that two arrays are equal. Two arrays are considered equal if:
368      * <ul>
369      * <li>their respective lengths are the same</i>
370      * <li>all corresponding pairs of elements are equal
371      * <li>both array references are <code>null</code>
372      * </ul>
373      *
374      * @param message message to display when arrays are not equal
375      * @param expected a short array of expected values
376      * @param actual a short array of actual values
377      */

378     public static void assertEquals(String JavaDoc message,
379                                     char[] expected,
380                                     char[] actual) {
381         if (Arrays.equals(expected, actual)) {
382             return;
383         }
384
385         String JavaDoc formatted = "";
386         if (message != null) {
387             formatted = message + " ";
388         }
389
390         Assert.assertNotNull(formatted + "expected array: <not null> but was <null>", expected);
391         Assert.assertNotNull(formatted + "expected array: <null> but was <not null>", actual);
392         Assert.assertEquals(formatted + "[array length] ", expected.length, actual.length);
393         for (int i = 0; i < actual.length; i++) {
394             Assert.assertEquals(formatted + "[position " + i + "]", expected[i], actual[i]);
395         }
396     }
397
398     /**
399      * Asserts that two arrays are equal. Two arrays are considered equal if:
400      * <ul>
401      * <li>their respective lengths are the same</i>
402      * <li>all corresponding pairs of elements are equal
403      * <li>both array references are <code>null</code>
404      * </ul>
405      *
406      * @param expected a short array of expected values
407      * @param actual a short array of actual values
408      */

409     public static void assertEquals(char[] expected,
410                                     char[] actual) {
411         assertEquals(null, expected, actual);
412     }
413
414     /**
415      * Asserts that two arrays are equal. Two arrays are considered equal if:
416      * <ul>
417      * <li>their respective lengths are the same</i>
418      * <li>all corresponding pairs of elements are equal
419      * <li>both array references are <code>null</code>
420      * </ul>
421      *
422      * @param message message to display when arrays are not equal
423      * @param expected a short array of expected values
424      * @param actual a short array of actual values
425      */

426     public static void assertEquals(String JavaDoc message,
427                                     boolean[] expected,
428                                     boolean[] actual) {
429         if (Arrays.equals(expected, actual)) {
430             return;
431         }
432
433         String JavaDoc formatted = "";
434         if (message != null) {
435             formatted = message + " ";
436         }
437
438         Assert.assertNotNull(formatted + "expected array: <not null> but was <null>", expected);
439         Assert.assertNotNull(formatted + "expected array: <null> but was <not null>", actual);
440         Assert.assertEquals(formatted + "[array length] ", expected.length, actual.length);
441         for (int i = 0; i < actual.length; i++) {
442             Assert.assertEquals(formatted + "[position " + i + "]", expected[i], actual[i]);
443         }
444     }
445
446     /**
447      * Asserts that two arrays are equal. Two arrays are considered equal if:
448      * <ul>
449      * <li>their respective lengths are the same</i>
450      * <li>all corresponding pairs of elements are equal
451      * <li>both array references are <code>null</code>
452      * </ul>
453      *
454      * @param expected a short array of expected values
455      * @param actual a short array of actual values
456      */

457     public static void assertEquals(boolean[] expected,
458                                     boolean[] actual) {
459         assertEquals(null, expected, actual);
460     }
461
462     /**
463      * Asserts that two arrays are equal. Two arrays are considered equal if:
464      * <ul>
465      * <li>their respective lengths are the same</i>
466      * <li>all corresponding pairs of elements are equal
467      * <li>both array references are <code>null</code>
468      * </ul>
469      *
470      * @param message message to display when arrays are not equal
471      * @param expected a byte array of expected values
472      * @param actual a byte array of actual values
473      */

474     public static void assertEquals(String JavaDoc message,
475                                     byte[] expected,
476                                     byte[] actual) {
477         if (Arrays.equals(expected, actual)) {
478             return;
479         }
480
481         String JavaDoc formatted = "";
482         if (message != null) {
483             formatted = message + " ";
484         }
485
486         Assert.assertNotNull(formatted + "expected array: <not null> but was <null>", expected);
487         Assert.assertNotNull(formatted + "expected array: <null> but was <not null>", actual);
488         Assert.assertEquals(formatted + "[array length] ", expected.length, actual.length);
489         for (int i = 0; i < actual.length; i++) {
490             Assert.assertEquals(formatted + "[position " + i + "]", expected[i], actual[i]);
491         }
492     }
493
494     /**
495      * Asserts that two arrays are equal. Two arrays are considered equal if:
496      * <ul>
497      * <li>their respective lengths are the same</i>
498      * <li>all corresponding pairs of elements are equal
499      * <li>both array references are <code>null</code>
500      * </ul>
501      *
502      * @param expected a byte array of expected values
503      * @param actual a byte array of actual values
504      */

505     public static void assertEquals(byte[] expected,
506                                     byte[] actual) {
507         assertEquals(null, expected, actual);
508     }
509
510     /**
511      * Asserts that two arrays have the same elements, but not necessarily in
512      * the same order. If order is important then use assertEquals(). If a
513      * difference is found, the object is listed by calling its toString() method.
514      * Arrays are equal if both are <i>null</i>.
515      *
516      * Equality of objects uses the equals method.
517      *
518      * @see #assertEquals(String message, Object[] expected, Object[] actual)
519      *
520      * @param message message to display when arrays are not equal
521      * @param expected an Object array of expected values
522      * @param actual an Object array of actual values
523      */

524     public static void assertEquivalenceArrays(String JavaDoc message,
525                                                Object JavaDoc[] expected,
526                                                Object JavaDoc[] actual) {
527         if (Arrays.equals(expected, actual)) {
528             return;
529         }
530
531         String JavaDoc formatted = "";
532         if (message != null) {
533             formatted = message + " ";
534         }
535
536         Assert.assertNotNull(formatted + "expected array: <not null> but was <null>", expected);
537         Assert.assertNotNull(formatted + "expected array: <null> but was <not null>", actual);
538
539         ArrayList JavaDoc missing = new ArrayList JavaDoc();
540         for (int i = 0; i < expected.length; i++) {
541             missing.add(expected[i]);
542         }
543
544         ArrayList JavaDoc extra = new ArrayList JavaDoc();
545         for (int i = 0; i < actual.length; i++) {
546             extra.add(actual[i]);
547         }
548
549         ArrayList JavaDoc missingClone = (ArrayList JavaDoc) missing.clone();
550         missing.removeAll(extra);
551         extra.removeAll(missingClone);
552
553         Assert.assertTrue(formatted + "[Missing elements: " + missing + "]", missing.size() == 0);
554         Assert.assertTrue(formatted + "[Extra elements: " + extra + "]", extra.size() == 0);
555     }
556
557     /**
558      * Assertion that both arrays has the same elements, but not necessarily in
559      * the same order. If order is important then use assertEquals(). If a
560      * difference is found, the object is listed by calling its toString() method.
561      * Arrays are equal if both are <i>null</i>.
562      *
563      * Equality of objects uses the equals method.
564      *
565      * @see #assertEquals(String message, Object[] expected, Object[] actual)
566      *
567      * @param expected an Object array of expected values
568      * @param actual an Object array of actual values
569      */

570     public static void assertEquivalenceArrays(Object JavaDoc[] expected,
571                                                Object JavaDoc[] actual) {
572         assertEquivalenceArrays(null, expected, actual);
573     }
574
575 }
576
Popular Tags