KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > util > test > ObjectToolsTest


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.util.test;
8
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.Collection JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.HashSet JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import junit.framework.TestCase;
18
19 import com.inversoft.util.ObjectTools;
20
21
22 /**
23  * This class tests the ObjectTools toolkit
24  *
25  * @author Brian Pontarelli
26  */

27 public class ObjectToolsTest extends TestCase {
28
29     public ObjectToolsTest(String JavaDoc name) {
30         super(name);
31     }
32
33     public void testQuick() {
34         Object JavaDoc obj1 = null;
35         String JavaDoc obj2 = null;
36         Object JavaDoc obj3 = helper(obj2);
37
38         assertTrue("Both null", obj3 == obj1);
39     }
40
41     public void testAreObjectsEqualStrings() {
42
43         String JavaDoc foo1 = new String JavaDoc("foo");
44         String JavaDoc foo2 = new String JavaDoc("foo");
45         String JavaDoc bar = new String JavaDoc("bar");
46
47         assertTrue("null, null should be equal", ObjectTools.areObjectsEqual(null, null));
48         assertTrue("one string null should be false", !ObjectTools.areObjectsEqual(null, foo1));
49         assertTrue("other string null should be false", !ObjectTools.areObjectsEqual(foo1, null));
50         assertTrue("same object should be true", ObjectTools.areObjectsEqual(foo1, foo1));
51         assertTrue("same string should be true", ObjectTools.areObjectsEqual(foo1, foo2));
52         assertTrue("different strings should be false", !ObjectTools.areObjectsEqual(foo1, bar));
53     }
54
55     public void testAreObjectsEqualStringsIntegers() {
56
57         String JavaDoc str1 = new String JavaDoc("1");
58         Integer JavaDoc int1 = new Integer JavaDoc(1);
59         Integer JavaDoc int11 = new Integer JavaDoc(1);
60         Integer JavaDoc int2 = new Integer JavaDoc(2);
61
62         String JavaDoc [] strings = {"1", "2", "3"};
63         Integer JavaDoc [] ints = { new Integer JavaDoc(1), new Integer JavaDoc(2), new Integer JavaDoc(3) };
64
65         assertTrue("Should be true, same", ObjectTools.areObjectsEqual(int1, int1));
66         assertTrue("Should be true, equal", ObjectTools.areObjectsEqual(int1, int11));
67         assertTrue("should be true, string == int", ObjectTools.areObjectsEqual(str1, int1));
68         assertTrue("should be true, string == int again", ObjectTools.areObjectsEqual(int1, str1));
69
70         assertTrue("should be false, string != int", !ObjectTools.areObjectsEqual(str1, int2));
71         assertTrue("should be false, string != int again", !ObjectTools.areObjectsEqual(int2, str1));
72
73         assertTrue("should be true, in array mix", ObjectTools.areObjectsEqual(int1, strings));
74         assertTrue("should be true, in array mix again", ObjectTools.areObjectsEqual(strings, int2));
75         assertTrue("should be true, in array remix", ObjectTools.areObjectsEqual(str1, ints));
76         assertTrue("should be true, in array remix again", ObjectTools.areObjectsEqual(ints, str1));
77     }
78
79     public void testAreObjectsEqualBeans() {
80
81         Bean bean1 = new Bean();
82         Bean bean2 = new Bean();
83
84         assertTrue("null, null should be equal", ObjectTools.areObjectsEqual(null, null));
85         assertTrue("both getName() null should be true", ObjectTools.areObjectsEqual(bean1, bean2));
86
87         bean1.setName("foo");
88         assertTrue("one getName() null should be false", !ObjectTools.areObjectsEqual(bean1, bean2));
89
90         bean2.setName("foo");
91         assertTrue("both getName() 'foo' should be true", ObjectTools.areObjectsEqual(bean1, bean2));
92
93         assertFalse(ObjectTools.areObjectsEqual(null, bean2));
94         assertFalse(ObjectTools.areObjectsEqual(bean1, null));
95     }
96
97     public void testAreObjectsEqualArrays() {
98
99         String JavaDoc [] strings = {"foo", "bar", "alpha", "beta"};
100         String JavaDoc [] strings2 = {"foo", "bar", "alpha", "beta"};
101         String JavaDoc value = "foo";
102         String JavaDoc value2 = "alpha";
103
104         assertTrue("Should be true, contains foo", ObjectTools.areObjectsEqual(value, strings));
105         assertTrue("Should be true, contains foo", ObjectTools.areObjectsEqual(strings, value));
106         assertTrue("Should be true, contains alpha", ObjectTools.areObjectsEqual(value2, strings));
107         assertTrue("Should be true, contains alpha", ObjectTools.areObjectsEqual(strings, value2));
108
109         assertTrue("Should be false, doesn't contain null", !ObjectTools.areObjectsEqual(null, strings));
110
111         assertTrue("Should be true, cast to objects", ObjectTools.areObjectsEqual(helper(value), helper(strings)));
112         assertTrue("Should be true, for both arrays", ObjectTools.areObjectsEqual(strings2, strings));
113
114         strings[2] = null;
115         assertTrue("Should be false, one is null and other is not", !ObjectTools.areObjectsEqual(null, strings));
116     }
117
118     public void testAreObjectsEqualObjectArrays() {
119
120         Bean [] beans = { new Bean("foo"), new Bean("bar"), new Bean("alpha"), new Bean("beta") };
121         Bean [] beans2 = { new Bean("foo"), new Bean("bar"), new Bean("alpha"), new Bean("beta") };
122         Bean [] beans3 = { new Bean("1"), new Bean("2"), new Bean("3"), new Bean("4") };
123         Bean [] beans4 = { new Bean(), new Bean(), new Bean(), new Bean() };
124         String JavaDoc value = "foo";
125         String JavaDoc value2 = "alpha";
126
127         assertTrue("Should be true, same array", ObjectTools.areObjectsEqual(beans, beans2));
128         assertTrue("Should be false, not same array", !ObjectTools.areObjectsEqual(beans, beans3));
129         assertTrue("Should be false, not same array", !ObjectTools.areObjectsEqual(beans, beans4));
130         assertTrue("Should be true, contains foo", ObjectTools.areObjectsEqual(beans, value));
131         assertTrue("Should be true, contains foo", ObjectTools.areObjectsEqual(value, beans));
132         assertTrue("Should be true, contains alpha", ObjectTools.areObjectsEqual(beans, value2));
133
134         assertTrue("Should be true, contains object", ObjectTools.areObjectsEqual(beans, beans[1]));
135         assertTrue("Should be true, contains object again", ObjectTools.areObjectsEqual(beans, beans2[1]));
136     }
137
138     public void testAreObjectsEqualCollection() {
139
140         String JavaDoc value = "foo";
141         Collection JavaDoc list = new ArrayList JavaDoc();
142         list.add(value); list.add("bar"); list.add("alpha"); list.add("beta");
143         Collection JavaDoc list2 = new ArrayList JavaDoc();
144         list2.add("foo"); list2.add("bar"); list2.add("alpha"); list2.add("beta");
145         Collection JavaDoc list3 = new ArrayList JavaDoc();
146         list3.add("1"); list3.add("2"); list3.add("3"); list3.add("4");
147         Collection JavaDoc list4 = new ArrayList JavaDoc();
148         list4.add("1"); list4.add("2"); list4.add("3");
149
150         assertTrue("Should be true, same collection", ObjectTools.areObjectsEqual(list, list2));
151         assertTrue("Should be false, not same collection", !ObjectTools.areObjectsEqual(list, list3));
152         assertTrue("Should be false, collection sizes", !ObjectTools.areObjectsEqual(list4, list3));
153         assertTrue("Should be true, object in collection", ObjectTools.areObjectsEqual(value, list));
154         assertTrue("Should be true, object in collection", ObjectTools.areObjectsEqual(list, value));
155         assertTrue("Should be true, object identical to one in collection", ObjectTools.areObjectsEqual(value, list2));
156     }
157
158     public void testAreObjectsEqualObjectCollection() {
159
160         Bean bean1 = new Bean("foo");
161         Collection JavaDoc list = new ArrayList JavaDoc();
162         list.add(bean1); list.add(new Bean("bar")); list.add(new Bean("alpha")); list.add(new Bean("beta"));
163         Collection JavaDoc list2 = new ArrayList JavaDoc();
164         list2.add(new Bean("foo")); list2.add(new Bean("bar")); list2.add(new Bean("alpha")); list2.add(new Bean("beta"));
165         Collection JavaDoc list3 = new ArrayList JavaDoc();
166         list3.add(new Bean("1")); list3.add(new Bean("2")); list3.add(new Bean("3")); list3.add(new Bean("4"));
167         Collection JavaDoc list4 = new ArrayList JavaDoc();
168         list4.add(new Bean("1")); list4.add(new Bean("2")); list4.add(new Bean("3"));
169         String JavaDoc value = "foo";
170         String JavaDoc value2 = "alpha";
171
172         assertTrue("Should be true, same collection", ObjectTools.areObjectsEqual(list, list2));
173         assertTrue("Should be false, not same collection", !ObjectTools.areObjectsEqual(list, list3));
174         assertTrue("Should be false, collection sizes", !ObjectTools.areObjectsEqual(list4, list3));
175         assertTrue("Should be true, object in collection", ObjectTools.areObjectsEqual(bean1, list));
176         assertTrue("Should be true, object in collection", ObjectTools.areObjectsEqual(list, bean1));
177         assertTrue("Should be true, object identical to one in collection", ObjectTools.areObjectsEqual(bean1, list2));
178         assertTrue("Should be true, contains foo", ObjectTools.areObjectsEqual(list, value));
179         assertTrue("Should be true, contains alpha", ObjectTools.areObjectsEqual(list, value2));
180     }
181
182     /**
183      * Tests the areObjectsEqual method with array and collection mixes using
184      * Strings
185      */

186     public void testAreObjectsEqualCollectionArray() {
187
188         Collection JavaDoc list = new ArrayList JavaDoc();
189         list.add("foo"); list.add("bar"); list.add("alpha"); list.add("beta");
190         String JavaDoc [] strings = {"foo", "bar", "alpha", "beta"};
191         String JavaDoc [] strings2 = {"foo2", "bar", "alpha", "beta"};
192
193         assertTrue("Should be true, same values", ObjectTools.areObjectsEqual(list, strings));
194         assertTrue("Should be false, not same values", !ObjectTools.areObjectsEqual(list, strings2));
195         assertTrue("Should be true, same values", ObjectTools.areObjectsEqual(strings, list));
196         assertTrue("Should be false, not same values", !ObjectTools.areObjectsEqual(strings2, list));
197     }
198
199     /**
200      * Tests the areObjectsEqual method with array and collection mixes using
201      * Objects
202      */

203     public void testAreObjectsEqualCollectionArrayObjects() {
204
205         Collection JavaDoc list = new ArrayList JavaDoc();
206         list.add(new Bean("foo"));
207         list.add(new Bean("bar"));
208         list.add(new Bean("alpha"));
209         list.add(new Bean("beta"));
210         Bean [] beans = { new Bean("foo"), new Bean("bar"), new Bean("alpha"), new Bean("beta") };
211         Bean [] beans2 = { new Bean("foo2"), new Bean("bar"), new Bean("alpha"), new Bean("beta") };
212
213         assertTrue("Should be true, same values", ObjectTools.areObjectsEqual(list, beans));
214         assertTrue("Should be false, not same values", !ObjectTools.areObjectsEqual(list, beans2));
215         assertTrue("Should be true, same values", ObjectTools.areObjectsEqual(beans, list));
216         assertTrue("Should be false, not same values", !ObjectTools.areObjectsEqual(beans2, list));
217     }
218
219     /**
220      * Tests the arrayContains method
221      */

222     public void testArrayContains() {
223
224         Bean [] beans = { new Bean("foo"), null, new Bean("bar"),
225                           new Bean("alpha"), new Bean("beta") };
226         Bean bean = new Bean("alpha");
227         String JavaDoc value = "beta";
228         String JavaDoc value2 = "foo";
229
230         assertTrue(ObjectTools.arrayContains(beans, bean));
231         assertTrue(ObjectTools.arrayContains(beans, value));
232         assertTrue(ObjectTools.arrayContains(beans, value2));
233         assertTrue(ObjectTools.arrayContains(beans, beans));
234         assertFalse(ObjectTools.arrayContains(beans, null));
235         assertFalse(ObjectTools.arrayContains(null, value));
236     }
237
238     /**
239      * Tests the arrayContainsString method
240      */

241     public void testArrayContainsString() {
242
243         Bean [] beans = { new Bean("foo"), null, new Bean("bar"),
244                           new Bean("alpha"), new Bean("beta") };
245         String JavaDoc value = "beta";
246         String JavaDoc value2 = "foo";
247         String JavaDoc value3 = "outside";
248
249         assertTrue(ObjectTools.arrayContainsString(beans, value));
250         assertTrue(ObjectTools.arrayContainsString(beans, value2));
251         assertFalse(ObjectTools.arrayContainsString(beans, null));
252         assertFalse(ObjectTools.arrayContainsString(null, value));
253         assertTrue(ObjectTools.arrayContainsString(null, null));
254         assertFalse(ObjectTools.arrayContainsString(beans, value3));
255     }
256
257     /**
258      * Tests the clean string method
259      */

260     public void testCleanString() {
261         String JavaDoc foo = "foo";
262         assertSame(foo, ObjectTools.cleanToString(foo));
263         assertSame("", ObjectTools.cleanToString(null));
264     }
265
266     /**
267      * Tests the toString method.
268      */

269     public void testToString() {
270         assertEquals("foo", ObjectTools.toString("foo"));
271         assertNull(ObjectTools.toString(null));
272     }
273
274     /**
275      * Tests the wrapArray method
276      */

277     public void testWrapArray() {
278
279         byte[] bytes = new byte[] {1, 2, 3, 4};
280         short[] shorts = new short[] {1, 2, 3, 4};
281         int[] ints = new int[] {1, 2, 3, 4};
282         long[] longs = new long[] {1, 2, 3, 4};
283         float[] floats = new float[] {1, 2, 3, 4};
284         double[] doubs = new double[] {1, 2, 3, 4};
285
286         Number JavaDoc[] wrappers = ObjectTools.wrapArray(bytes);
287         assertTrue(wrappers instanceof Byte JavaDoc[]);
288         assertEquals(4, wrappers.length);
289         for (int i = 0; i < wrappers.length; i++) {
290             assertEquals(i + 1, wrappers[i].intValue());
291         }
292
293         wrappers = ObjectTools.wrapArray(shorts);
294         assertTrue(wrappers instanceof Short JavaDoc[]);
295         assertEquals(4, wrappers.length);
296         for (int i = 0; i < wrappers.length; i++) {
297             assertEquals(i + 1, wrappers[i].intValue());
298         }
299
300         wrappers = ObjectTools.wrapArray(ints);
301         assertTrue(wrappers instanceof Integer JavaDoc[]);
302         assertEquals(4, wrappers.length);
303         for (int i = 0; i < wrappers.length; i++) {
304             assertEquals(i + 1, wrappers[i].intValue());
305         }
306
307         wrappers = ObjectTools.wrapArray(longs);
308         assertTrue(wrappers instanceof Long JavaDoc[]);
309         assertEquals(4, wrappers.length);
310         for (int i = 0; i < wrappers.length; i++) {
311             assertEquals(i + 1, wrappers[i].intValue());
312         }
313
314         wrappers = ObjectTools.wrapArray(floats);
315         assertTrue(wrappers instanceof Float JavaDoc[]);
316         assertEquals(4, wrappers.length);
317         for (int i = 0; i < wrappers.length; i++) {
318             assertEquals(i + 1, wrappers[i].intValue());
319         }
320
321         wrappers = ObjectTools.wrapArray(doubs);
322         assertTrue(wrappers instanceof Double JavaDoc[]);
323         assertEquals(4, wrappers.length);
324         for (int i = 0; i < wrappers.length; i++) {
325             assertEquals(i + 1, wrappers[i].intValue());
326         }
327
328         wrappers = ObjectTools.wrapArray("foo");
329         assertNull(wrappers);
330     }
331
332     /**
333      * Tests the get from collection method using a map.
334      */

335     public void testGetFromCollectionMap() {
336         Map JavaDoc map = new HashMap JavaDoc();
337         map.put("key", "value");
338
339         Object JavaDoc value = ObjectTools.getValueFromCollection(map, "key");
340         assertEquals("value", value);
341         value = ObjectTools.getValueFromCollection(map, "badKey");
342         assertNull(value);
343     }
344
345     /**
346      * Tests the get from collection method using a list.
347      */

348     public void testGetFromCollectionList() {
349         List JavaDoc list = new ArrayList JavaDoc();
350         list.add("key");
351         list.add("value");
352
353         Object JavaDoc value = ObjectTools.getValueFromCollection(list, new Integer JavaDoc(1));
354         assertEquals("value", value);
355         value = ObjectTools.getValueFromCollection(list, new Integer JavaDoc(0));
356         assertEquals("key", value);
357         try {
358             ObjectTools.getValueFromCollection(list, new Integer JavaDoc(2));
359             fail("Should have failed");
360         } catch (IndexOutOfBoundsException JavaDoc ioobe) {
361             // expected
362
}
363         try {
364             ObjectTools.getValueFromCollection(list, "key");
365             fail("Should have failed");
366         } catch (IllegalArgumentException JavaDoc iae) {
367             // expected
368
}
369     }
370
371     /**
372      * Tests the get from collection method using a collection.
373      */

374     public void testGetFromCollectionCollection() {
375         Collection JavaDoc collection = new HashSet JavaDoc();
376         collection.add("key");
377         collection.add("value");
378
379         Object JavaDoc value = ObjectTools.getValueFromCollection(collection, new Integer JavaDoc(1));
380         assertEquals("value", value);
381         value = ObjectTools.getValueFromCollection(collection, new Integer JavaDoc(0));
382         assertEquals("key", value);
383         try {
384             ObjectTools.getValueFromCollection(collection, new Integer JavaDoc(2));
385             fail("Should have failed");
386         } catch (IndexOutOfBoundsException JavaDoc ioobe) {
387             // expected
388
}
389         try {
390             ObjectTools.getValueFromCollection(collection, "key");
391             fail("Should have failed");
392         } catch (IllegalArgumentException JavaDoc iae) {
393             // expected
394
}
395     }
396
397     /**
398      * Tests the get from collection method using an array.
399      */

400     public void testGetFromCollectionArray() {
401         Object JavaDoc[] array = new Object JavaDoc[2];
402         array[0] = "key";
403         array[1] = "value";
404
405         Object JavaDoc value = ObjectTools.getValueFromCollection(array, new Integer JavaDoc(1));
406         assertEquals("value", value);
407         value = ObjectTools.getValueFromCollection(array, new Integer JavaDoc(0));
408         assertEquals("key", value);
409         try {
410             ObjectTools.getValueFromCollection(array, new Integer JavaDoc(2));
411             fail("Should have failed");
412         } catch (IndexOutOfBoundsException JavaDoc ioobe) {
413             // expected
414
}
415         try {
416             ObjectTools.getValueFromCollection(array, "key");
417             fail("Should have failed");
418         } catch (IllegalArgumentException JavaDoc iae) {
419             // expected
420
}
421     }
422
423     /**
424      * Tests the get from collection method using a bad data structure.
425      */

426     public void testGetFromCollection() {
427         Object JavaDoc obj = "foo";
428
429         try {
430             ObjectTools.getValueFromCollection(obj, new Integer JavaDoc(0));
431             fail("Should have failed");
432         } catch (IllegalArgumentException JavaDoc iae) {
433         }
434         try {
435             ObjectTools.getValueFromCollection(obj, "key");
436             fail("Should have failed");
437         } catch (IllegalArgumentException JavaDoc iae) {
438         }
439         try {
440             ObjectTools.getValueFromCollection(obj, null);
441             fail("Should have failed");
442         } catch (IllegalArgumentException JavaDoc iae) {
443         }
444     }
445
446     /**
447      * Tests the setValueIntoArray method
448      */

449     public void testSetValueIntoArray() {
450         Boolean JavaDoc bo = Boolean.TRUE;
451         Byte JavaDoc b = new Byte JavaDoc((byte) 0);
452         Character JavaDoc c = new Character JavaDoc('a');
453         Short JavaDoc s = new Short JavaDoc((short) 1);
454         Integer JavaDoc i = new Integer JavaDoc(2);
455         Long JavaDoc l = new Long JavaDoc(3);
456         Float JavaDoc f = new Float JavaDoc(4);
457         Double JavaDoc d = new Double JavaDoc(5);
458
459         boolean[] boa = new boolean[3];
460         boa[1] = false;
461         ObjectTools.setIntoArray(boa, 1, bo);
462         assertEquals(true, boa[1]);
463
464         byte[] ba = new byte[3];
465         ba[1] = 1;
466         ObjectTools.setIntoArray(ba, 1, b);
467         assertEquals(0, ba[1]);
468
469         char[] ca = new char[3];
470         ca[1] = 'S';
471         ObjectTools.setIntoArray(ca, 1, c);
472         assertEquals('a', ca[1]);
473
474         short[] sa = new short[3];
475         sa[1] = 0;
476         ObjectTools.setIntoArray(sa, 1, s);
477         assertEquals(1, sa[1]);
478
479         int[] ia = new int[3];
480         ia[1] = 0;
481         ObjectTools.setIntoArray(ia, 1, i);
482         assertEquals(2, ia[1]);
483
484         long[] la = new long[3];
485         la[1] = 0;
486         ObjectTools.setIntoArray(la, 1, l);
487         assertEquals(3, la[1]);
488
489         float[] fa = new float[3];
490         fa[1] = 0;
491         ObjectTools.setIntoArray(fa, 1, f);
492         assertEquals(4f, fa[1], 0);
493
494         double[] da = new double[3];
495         da[1] = 0;
496         ObjectTools.setIntoArray(da, 1, d);
497         assertEquals(5, da[1], 0);
498
499         try {
500             ObjectTools.setIntoArray("foo", 0, "bar");
501             fail("Should have failed");
502         } catch (IllegalArgumentException JavaDoc iae) {
503             // Expected
504
}
505
506         Object JavaDoc[] oa = new Object JavaDoc[3];
507         oa[1] = null;
508         ObjectTools.setIntoArray(oa, 1, "foo");
509         assertSame("foo", oa[1]);
510     }
511
512     /**
513      * Tests the setValueIntoCollection.
514      */

515     public void testSetValueIntoCollection() {
516         Map JavaDoc map = new HashMap JavaDoc();
517         ObjectTools.setValueIntoCollection(map, "key", "value");
518         assertEquals("value", map.get("key"));
519
520         List JavaDoc list = new ArrayList JavaDoc(10);
521         for (int i = 0; i < 10; i++) {
522             list.add("");
523         }
524         ObjectTools.setValueIntoCollection(list, new Integer JavaDoc(4), "value");
525         assertEquals("value", list.get(4));
526         try {
527             ObjectTools.setValueIntoCollection(list, "key", "value");
528             fail("Should have failed");
529         } catch (IllegalArgumentException JavaDoc iae) {
530             // Expected
531
}
532
533         Object JavaDoc[] array = new Object JavaDoc[10];
534         ObjectTools.setValueIntoCollection(array, new Integer JavaDoc(4), "value");
535         assertEquals("value", array[4]);
536
537         try {
538             ObjectTools.setValueIntoCollection(new HashSet JavaDoc(), new Integer JavaDoc(4),
539                 "value");
540             fail("Should have failed");
541         } catch (IllegalArgumentException JavaDoc iae) {
542             // Expected
543
}
544         try {
545             ObjectTools.setValueIntoCollection("foo", new Integer JavaDoc(4), "value");
546             fail("Should have failed");
547         } catch (IllegalArgumentException JavaDoc iae) {
548             // Expected
549
}
550     }
551
552     /**
553      * Tests the isCollection methods.
554      */

555     public void testIsCollection() {
556         Map JavaDoc map = new HashMap JavaDoc();
557         List JavaDoc list = new ArrayList JavaDoc();
558         Collection JavaDoc set = new HashSet JavaDoc();
559
560         assertTrue(ObjectTools.isCollection(map));
561         assertTrue(ObjectTools.isCollection(list));
562         assertTrue(ObjectTools.isCollection(set));
563         assertFalse(ObjectTools.isCollection("foo"));
564         assertFalse(ObjectTools.isCollection(new Object JavaDoc[2]));
565         assertFalse(ObjectTools.isCollection((Object JavaDoc) null));
566
567         assertTrue(ObjectTools.isCollection(HashMap JavaDoc.class));
568         assertTrue(ObjectTools.isCollection(ArrayList JavaDoc.class));
569         assertTrue(ObjectTools.isCollection(HashSet JavaDoc.class));
570         assertFalse(ObjectTools.isCollection(String JavaDoc.class));
571         assertFalse(ObjectTools.isCollection(Object JavaDoc[].class));
572     }
573
574     public void testIsArray() {
575         assertTrue(ObjectTools.isArray(new Object JavaDoc[0]));
576         assertTrue(ObjectTools.isArray(new int[0]));
577         assertFalse(ObjectTools.isArray(new Object JavaDoc()));
578         assertFalse(ObjectTools.isArray(null));
579     }
580
581     public void testIsDataStructure() {
582         Map JavaDoc map = new HashMap JavaDoc();
583         List JavaDoc list = new ArrayList JavaDoc();
584         Collection JavaDoc set = new HashSet JavaDoc();
585
586         assertTrue(ObjectTools.isDataStructure(map));
587         assertTrue(ObjectTools.isDataStructure(list));
588         assertTrue(ObjectTools.isDataStructure(set));
589         assertTrue(ObjectTools.isDataStructure(new Object JavaDoc[2]));
590         assertFalse(ObjectTools.isDataStructure("foo"));
591         assertFalse(ObjectTools.isDataStructure(null));
592
593         assertTrue(ObjectTools.isDataStructure(new Object JavaDoc[0]));
594         assertTrue(ObjectTools.isDataStructure(new int[0]));
595         assertFalse(ObjectTools.isDataStructure(new Object JavaDoc()));
596         assertFalse(ObjectTools.isDataStructure(null));
597     }
598
599     public void testCreateArray() {
600         List JavaDoc indices = new ArrayList JavaDoc();
601         indices.add(new Integer JavaDoc(3));
602         indices.add(new Integer JavaDoc(7));
603         indices.add(new Integer JavaDoc(11));
604         ObjectTools.ArrayInfo ai = ObjectTools.createArray(String JavaDoc[][][].class,
605             indices, 0);
606         assertEquals(4, ((Object JavaDoc[][][]) ai.array).length);
607         assertEquals(8, ((Object JavaDoc[][][]) ai.array)[0].length);
608         assertEquals(12, ((Object JavaDoc[][][]) ai.array)[0][0].length);
609         assertTrue(ai.complete);
610     }
611
612     public void testCreateArrayStart() {
613         List JavaDoc indices = new ArrayList JavaDoc();
614         indices.add(new Integer JavaDoc(3));
615         indices.add(new Integer JavaDoc(7));
616         indices.add(new Integer JavaDoc(11));
617         ObjectTools.ArrayInfo ai = ObjectTools.createArray(String JavaDoc[][][].class,
618             indices, 1);
619         assertEquals(8, ((Object JavaDoc[][][]) ai.array).length);
620         assertEquals(12, ((Object JavaDoc[][][]) ai.array)[0].length);
621         assertNull(((Object JavaDoc[][][]) ai.array)[0][0]);
622         assertFalse(ai.complete);
623     }
624
625     public void testCreateArrayTooManyIndices() {
626         List JavaDoc indices = new ArrayList JavaDoc();
627         indices.add(new Integer JavaDoc(3));
628         indices.add(new Integer JavaDoc(7));
629         indices.add(new Integer JavaDoc(2));
630         indices.add(new Integer JavaDoc(3));
631         indices.add(new Integer JavaDoc(1));
632         ObjectTools.ArrayInfo ai = ObjectTools.createArray(String JavaDoc[][][].class,
633             indices, 0);
634         assertEquals(String JavaDoc[][][].class, ai.array.getClass());
635         assertEquals(4, ((Object JavaDoc[][][]) ai.array).length);
636         assertEquals(8, ((Object JavaDoc[][][]) ai.array)[0].length);
637         assertEquals(3, ((Object JavaDoc[][][]) ai.array)[0][0].length);
638         assertNull(((Object JavaDoc[][][]) ai.array)[0][0][0]);
639         assertFalse(ai.complete);
640     }
641
642     private Object JavaDoc helper(Object JavaDoc obj1) {
643         return obj1;
644     }
645 }
Popular Tags