KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javatests > ListTest


1 //Copyright (c) Corporation for National Research Initiatives
2
package javatests;
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.Arrays JavaDoc;
6 import java.util.Collection JavaDoc;
7 import java.util.Collections JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.ListIterator JavaDoc;
11 import java.util.NoSuchElementException JavaDoc;
12
13 /**
14  * @author updikca1
15  */

16 public abstract class ListTest {
17     
18     public static ListTest getArrayListTest(final boolean makeReadOnly) {
19         return new ListTest() {
20             public List JavaDoc newInstance(Collection JavaDoc c) {
21                 List JavaDoc l = null;
22                 if(c == null) {
23                     l = new ArrayList JavaDoc();
24                 } else {
25                     l = new ArrayList JavaDoc(c);
26                 }
27                 return (makeReadOnly)
28                         ? Collections.unmodifiableList(l)
29                         : l;
30             }
31             public boolean isReadOnly() {
32                 return makeReadOnly;
33             }
34         };
35     }
36     
37     public static void verifyImutability(List JavaDoc l) {
38         
39         String JavaDoc message = "Expected UnsupportedOperationException.";
40         
41         try {
42             l.add(0, new Integer JavaDoc(0));
43             TestSupport.assertThat(false, message);
44         } catch (UnsupportedOperationException JavaDoc e) {}
45         
46         try {
47             l.add(new Integer JavaDoc(0));
48             TestSupport.assertThat(false, message);
49         } catch (UnsupportedOperationException JavaDoc e) {}
50         
51         try {
52             l.addAll(null);
53             TestSupport.assertThat(false, message);
54         } catch (UnsupportedOperationException JavaDoc e) {}
55         
56         try {
57             l.addAll(0, null);
58             TestSupport.assertThat(false, message);
59         } catch (UnsupportedOperationException JavaDoc e) {}
60         
61         try {
62             l.clear();
63             TestSupport.assertThat(false, message);
64         } catch (UnsupportedOperationException JavaDoc e) {}
65         
66         try {
67             l.remove(0);
68             TestSupport.assertThat(false, message);
69         } catch (UnsupportedOperationException JavaDoc e) {}
70         
71         try {
72             l.remove(new Object JavaDoc());
73             TestSupport.assertThat(false, message);
74         } catch (UnsupportedOperationException JavaDoc e) {}
75         
76         try {
77             l.removeAll(null);
78             TestSupport.assertThat(false, message);
79         } catch (UnsupportedOperationException JavaDoc e) {}
80         
81         try {
82             l.retainAll(null);
83             TestSupport.assertThat(false, message);
84         } catch (UnsupportedOperationException JavaDoc e) {}
85         try {
86             l.set(0,new Integer JavaDoc(0));
87             TestSupport.assertThat(false, message);
88         } catch (UnsupportedOperationException JavaDoc e) {}
89     }
90     
91     private final List JavaDoc nullList;
92     
93     protected List JavaDoc defaultList() {
94         List JavaDoc l = new ArrayList JavaDoc();
95         for (int i = 0; i < 4; i++) {
96             l.add(new Integer JavaDoc(i));
97         }
98         return newInstance(l);
99     }
100     
101     /**
102      * Implementations must supply an empty list if the collection is null.
103      * @param c Initial collection or null for empty.
104      * @return the List instance
105      */

106     public List JavaDoc newInstance(Collection JavaDoc c) {
107         throw new UnsupportedOperationException JavaDoc("This method must be overridden");
108     }
109     
110     /**
111      * @return true if the list is read-only (like PyTuple)
112      */

113     public boolean isReadOnly() {
114         throw new UnsupportedOperationException JavaDoc("This method must be overridden");
115     }
116     
117     {
118         nullList = newInstance(Arrays.asList(new Object JavaDoc[] {null}));
119     }
120     
121     public void testAll() {
122         
123         test_get();
124         test_equals();
125         test_size();
126         test_contains();
127         test_containsAll();
128         
129         try {
130             defaultList().hashCode();
131             test_hashCode();
132         } catch (Exception JavaDoc e) {
133             // skip unhashable types
134
}
135                 
136         test_subList();
137         test_lastIndexOf();
138         test_listIterator();
139         test_toArray();
140         test_toArray_typed();
141       
142         if(!isReadOnly()) {
143             test_add();
144             test_add_index();
145             test_set();
146             test_clear();
147             test_addAll();
148             test_addAll_index();
149             test_remove();
150             test_remove_index();
151             test_removeAll();
152             test_retainAll();
153         } else {
154             verifyImutability(newInstance(null));
155         }
156     }
157     
158     /** Tests get(int index) */
159     public void test_get() {
160         TestSupport.assertThat(defaultList().get(0).equals(new Integer JavaDoc(0)),
161                 "get() did not return expected value of Integer(0)");
162         try {
163             defaultList().get(-1);
164             TestSupport.assertThat(false,
165                     "get(<negative index>) did not throw IndexOutOfBoundsException");
166         } catch (IndexOutOfBoundsException JavaDoc e) {}
167         
168         try {
169             defaultList().get(-1);
170             TestSupport.assertThat(false,
171                     "get(<index too big>) did not throw IndexOutOfBoundsException");
172         } catch (IndexOutOfBoundsException JavaDoc e) {}
173     }
174     
175     /** Tests set(int index, Object element) */
176     public void test_set() {
177         
178         try {
179             newInstance(null).set(-1, "spam");
180             TestSupport.assertThat(false,
181                     "get(<negative index>) did not throw IndexOutOfBoundsException");
182         } catch (IndexOutOfBoundsException JavaDoc e) {}
183         
184         try {
185             newInstance(null).set(0, "spam");
186             TestSupport.assertThat(false,
187                 "set(<index too big>) did not throw IndexOutOfBoundsException");
188         } catch (IndexOutOfBoundsException JavaDoc e) {}
189         
190         List JavaDoc a = defaultList();
191         a.set(a.size() - 1 , "spam");
192         TestSupport.assertThat(a.get(a.size() - 1).equals("spam"),
193                 "set() object was not retrieved via get()");
194     }
195     
196     /** Tests add(Object o) */
197     public void test_add() {
198         List JavaDoc a = newInstance(null);
199         for (int i = 0; i < 4; i++) {
200             a.add(new Integer JavaDoc(i));
201         }
202         TestSupport.assertEquals(a, defaultList(), "add(Object o) failed");
203     }
204     
205     /** Tests isEmpty() */
206     public void test_isEmpty() {
207         List JavaDoc a = newInstance(null);
208         TestSupport.assertThat(a.isEmpty(),
209                 "isEmpty() is false on an emtpy List");
210         a.addAll(defaultList());
211         TestSupport.assertThat(!a.isEmpty(),
212                 "isEmpty() is true on a non-empty List)" );
213         a.clear();
214         TestSupport.assertThat(a.isEmpty(),
215                 "isEmpty() is false on an emtpy List");
216     }
217     
218     /** Tests size() */
219     public void test_size() {
220         List JavaDoc b = newInstance(null);
221         TestSupport.assertThat(b.size() == 0, "empty list size was not 0");
222         TestSupport.assertThat(defaultList().size() == 4,
223                 "default list did not have a size of 4");
224     }
225     
226     /** Tests add(int index, Object element) */
227     public void test_add_index() {
228         List JavaDoc a = newInstance(null);
229         List JavaDoc b = defaultList();
230         for (int i = 0; i < b.size(); i++) {
231             a.add(i, b.get(i));
232         }
233         
234         try {
235             a.add(a.size() + 1, new Integer JavaDoc(a.size() + 1));
236             TestSupport.assertThat(false, "expected IndexOutOfBoundsException");
237         } catch (IndexOutOfBoundsException JavaDoc e) {}
238         
239         try {
240             a.add(-1, new Integer JavaDoc(-1));
241             TestSupport.assertThat(false, "expected IndexOutOfBoundsException");
242         } catch (IndexOutOfBoundsException JavaDoc e) {}
243     }
244     
245     /** Tests equals(Object o)*/
246     public void test_equals() {
247         TestSupport.assertEquals(defaultList(), defaultList(),
248                 "Identical lists weren't equal()");
249         TestSupport.assertNotEquals(newInstance(null), defaultList(),
250                 "Different lists were equal()");
251         TestSupport.assertNotEquals(newInstance(null), new Object JavaDoc(),
252                 "List was equal to a non-List type");
253     }
254     
255     /** Tests addAll(Collection c) */
256     public void test_addAll() {
257         List JavaDoc a = defaultList();
258         List JavaDoc b = defaultList();
259         
260         TestSupport.assertThat(a.addAll(b) == true,
261                 "Mutating addAll(Collection) returned false");
262         TestSupport.assertThat(a.addAll(newInstance(null)) == false,
263                 "Idempotent addAll(Collection) returned true");
264         TestSupport.assertThat(b.addAll(b) == true,
265                 "Mutating addAll(Collection) returned false");
266         TestSupport.assertEquals(a, b,
267                 "Expected equal objects from addAll(collection)");
268         TestSupport.assertThat(a.size() == 8,
269                 "Expected List to have size 8 after addAll(Collection)");
270     }
271     
272     /** Tests indexOf(Object o) */
273     public void indexOf() {
274         TestSupport.assertThat(defaultList().indexOf(new Integer JavaDoc(3)) == 3,
275                 "indexOf(3) did not return 3");
276         TestSupport.assertThat(defaultList().indexOf(new Integer JavaDoc(42)) == -1,
277                 "indexOf() non-existing entry did not return -1");
278         TestSupport.assertThat(defaultList().indexOf(null) == -1,
279                 "indexOf() non-existing null did not return -1");
280         
281     }
282     
283     /** Tests contains(Object o) */
284     public void test_contains() {
285         TestSupport.assertThat(defaultList().contains(new Integer JavaDoc(42)) == false,
286                 "contains() returned true for non-existing entry");
287         TestSupport.assertThat(defaultList().contains(new Integer JavaDoc(0)) == true,
288                 "contains() returned false for existing entry");
289         TestSupport.assertThat(nullList.contains(null) == true,
290                 "contains() returned false for existing null entry");
291         TestSupport.assertThat(defaultList().contains(null) == false,
292                 "contains() returned true for non-existing null entry");
293     }
294     
295     /** Tests remove(Object o) */
296     public void test_remove() {
297         List JavaDoc a = defaultList();
298         a.add(null);
299         TestSupport.assertThat(a.remove(null) == true,
300                 "remove() existing null entry returned false");
301         TestSupport.assertThat(a.remove(null) == false,
302                 "remove() non-existing null entry returned false");
303         a.add("spam");
304         TestSupport.assertThat(a.remove("spam") == true,
305                 "remove() existing entry returned false");
306         TestSupport.assertThat(a.remove("spam") == false,
307                 "remove() non-existing entry returned true");
308     }
309     
310     
311     /** Tests remove(int index) */
312     public void test_remove_index() {
313         
314         List JavaDoc a = defaultList();
315         for (int i = 0, n = a.size(); i < n; i++) {
316             a.remove(0);
317         }
318         TestSupport.assertThat(a.size() == 0,
319                 "remove()-d all entries but size() not 0");
320         
321         try {
322             a.remove(0);
323             TestSupport.assertThat(false,
324                     "removing a non-existing index did not throw exception");
325         } catch(IndexOutOfBoundsException JavaDoc e) {}
326     }
327     
328     /** Tests lastIndexOf(Object o) */
329     public void test_lastIndexOf() {
330         // avoid calling any mutable methods
331
List JavaDoc l = new ArrayList JavaDoc(defaultList());
332         l.add(new Integer JavaDoc(0));
333         
334         // now get the immutable version
335
List JavaDoc a = newInstance(l);
336         
337         TestSupport.assertThat(a.lastIndexOf(new Integer JavaDoc(0)) == 4,
338                 "lastIndexOf() did not return 4");
339         TestSupport.assertThat(a.lastIndexOf(new Integer JavaDoc(42)) == -1,
340                 "lastIndexOf() non-existing value did not return -1");
341     }
342     
343     /** Tests removeAll(Collection c) */
344     public void test_removeAll() {
345         List JavaDoc a = defaultList();
346         TestSupport.assertThat(a.removeAll(a) == true,
347                 "mutating removeAll() did not return true");
348         TestSupport.assertThat(a.removeAll(a) == false,
349                 "idempotent removeAll did not return false");
350         TestSupport.assertThat(a.removeAll(nullList) == false,
351                 "idempotent removeAll did not return false");
352         
353         List JavaDoc yanl = newInstance(null);
354         yanl.addAll(nullList);
355         TestSupport.assertThat(yanl.removeAll(nullList) == true,
356                 "mutating removeAll() did not return true");
357         TestSupport.assertThat(yanl.size() == 0,
358                 "empty list had non-zero size");
359         TestSupport.assertThat(yanl.removeAll(newInstance(null)) == false,
360                 "idempotent removeAll did not return false");
361         
362     }
363     
364     /** Tests addAll(int index, Collection c) */
365     public void test_addAll_index() {
366         List JavaDoc a = defaultList();
367         List JavaDoc b = newInstance(null);
368         TestSupport.assertThat(b.addAll(0,a) == true,
369                 "mutating addAll(index, Collection) did not return true");
370         TestSupport.assertEquals(a, b,
371                 "addAll(index, Collection) instances failed equals test");
372         TestSupport.assertThat(a.addAll(0, newInstance(null)) == false,
373                 "idempotent addAll(index, Collection) did not return false");
374         TestSupport.assertThat(b.addAll(0,b) == true,
375                 "mutating addAll(index, Collection) did not return true");
376         
377         // Since PyObjectList has some specific handling when it detects
378
// addAll on a PySequenceList, make sure the general case works.
379
b = newInstance(null);
380         b.addAll(new ArrayList JavaDoc(defaultList()));
381         TestSupport.assertEquals(defaultList(), b,
382                 "addAll(index, <ArrayList>) failed equals test");
383     }
384
385     
386     /** Tests hashCode() */
387     public void test_hashCode() {
388         List JavaDoc a = defaultList();
389         TestSupport.assertThat(a.hashCode() == defaultList().hashCode(),
390                 "Instances with same internal state have different hashcode");
391         TestSupport.assertThat(a.hashCode() != newInstance(null).hashCode(),
392                 "Instances with different internal state have the same hashcode");
393         
394         if (isReadOnly() == false) {
395             List JavaDoc b = newInstance(null);
396             b.addAll(a);
397             b.remove(0);
398             TestSupport.assertThat(a.hashCode()!= b.hashCode(),
399                     "Instances with different internal state have the same hashcode");
400         }
401         
402     }
403     
404     /** Tests clear() */
405     public void test_clear() {
406         List JavaDoc a = defaultList();
407         a.clear();
408         TestSupport.assertThat(a.size() == 0,
409                 "clear()-ed list did not have size of 0");
410     }
411     
412     /** Tests subList(int fromIndex, int toIndex) */
413     public void test_subList() {
414         List JavaDoc a = defaultList();
415         TestSupport.assertThat((a.subList(0, a.size()) != a),
416                 "subList() returned the same instance");
417         TestSupport.assertEquals(a.subList(0, a.size()), a,
418                 "Complete subList() did not equal original List");
419         TestSupport.assertThat(a.subList(0,0).size() == 0,
420                 "empty subList had non-zero size");
421         
422         try {
423             a.subList(-1,1);
424             TestSupport.assertThat(false, "Expected IndexOutOfBoundsException");
425         } catch (IndexOutOfBoundsException JavaDoc e) {}
426         
427         try {
428             a.subList(1,0);
429             TestSupport.assertThat(false, "Expected IllegalArgumentException");
430         } catch (IllegalArgumentException JavaDoc e) {}
431         
432         try {
433             a.subList(0,a.size() + 1);
434             TestSupport.assertThat(false, "Expected IndexOutOfBoundsException");
435         } catch (IndexOutOfBoundsException JavaDoc e) {}
436         
437         if (!isReadOnly()) {
438             
439             a.subList(0, a.size()).clear();
440             TestSupport.assertThat(a.size() == 0,
441                     "clear()-ed sublist did not have zero size");
442             List JavaDoc c = newInstance(null);
443             c.addAll(defaultList());
444             List JavaDoc d = c.subList(1,3);
445             TestSupport.assertThat(d.size() == 2,
446                     "Expected subList to have size of 2");
447             TestSupport.assertThat(c.set(1,"canned").equals(new Integer JavaDoc(1)),
448                     "subList.set() did not return Integer(1) from index 1" +
449                     " of defaultList");
450             TestSupport.assertThat(d.get(0).equals("canned"),
451                     "subList does not update with changes to parent");
452             d.set(0,"spam");
453             TestSupport.assertThat(c.get(1).equals("spam"),
454                     "parent does not update with changes to subList child");
455         } else {
456             List JavaDoc b = a.subList(0, a.size());
457             verifyImutability(b);
458         }
459         
460     }
461     
462     /** Tests retainAll(Collection c) */
463     public void test_retainAll() {
464         List JavaDoc a = defaultList();
465         a.retainAll(defaultList());
466         TestSupport.assertEquals(a, defaultList(),
467                 "retainAll(<equal List>) does not equal original list");
468         a = defaultList();
469         a.retainAll(newInstance(null));
470         TestSupport.assertThat(a.size() == 0,
471                 "retainAll(<empty List>))does not have size of zero");
472         
473         a = defaultList();
474         a.remove(0);
475         a.remove(0);
476         a.add(new Integer JavaDoc(4));
477         a.add(new Integer JavaDoc(5));
478         List JavaDoc b = newInstance(null);
479         b.add(new Integer JavaDoc(2));
480         b.add(new Integer JavaDoc(3));
481         a.retainAll(b);
482         TestSupport.assertEquals(a, b,
483                 "retainAll() on overlap of indices [2,3] did not return that List");
484     }
485     
486     /** Tests containsAll(Collection c) */
487     public void test_containsAll() {
488         TestSupport.assertThat(defaultList().containsAll(defaultList()),
489                 "containsAll(<identical List> was false");
490         TestSupport.assertThat(defaultList().containsAll(newInstance(null)),
491                 "containsAll(<empty List>) was false");
492         TestSupport.assertThat(newInstance(null).containsAll(defaultList()) == false,
493                 "containsAll(<disjoint List>) returned true");
494         TestSupport.assertThat(defaultList().containsAll(defaultList().subList(1,3)),
495                 "containsAll(<subList>) was false");
496     }
497     
498     /** Tests iterator() */
499     public void test_iterator() {
500         
501         TestSupport.assertThat(newInstance(null).iterator().hasNext() == false,
502                 "Iterator for empty list thinks it hasNext()");
503         try {
504             newInstance(null).iterator().next();
505             TestSupport.assertThat(false, "expected NoSuchElementException");
506         } catch (NoSuchElementException JavaDoc e) {}
507         
508         List JavaDoc a = defaultList();
509         int i = 0;
510         for (Iterator JavaDoc iter = a.iterator(); iter.hasNext(); ) {
511             TestSupport.assertThat(iter.next() == a.get(i++),
512                     "Iterator next() failed identity test");
513         }
514         TestSupport.assertThat(i == a.size(),
515                 "Iterator did not iterator over entire list");
516     }
517     
518     public void test_listIterator() {
519         
520         ListIterator JavaDoc li = newInstance(null).listIterator();
521         TestSupport.assertThat(li.hasNext() == false,
522                 "ListIterator.hasNext() is true for empty List");
523         
524         TestSupport.assertThat(li.hasPrevious() == false,
525                 "ListIterator.hasPrevious() is true for empty List");
526         
527         try {
528             li.next();
529             TestSupport.assertThat(false, "expected NoSuchElementException");
530         } catch (NoSuchElementException JavaDoc e) {}
531         
532         try {
533             li.previous();
534             TestSupport.assertThat(false, "expected NoSuchElementException");
535         } catch (NoSuchElementException JavaDoc e) {}
536         
537         // Appears bug was fixed from 1.3 to 1.4. Code it to pass either way.
538
int nextIndex = li.nextIndex();
539         TestSupport.assertThat(nextIndex == -1 || nextIndex == 0,
540                 "ListIterator.nextIndex() on empty List did not return 0 " +
541                 "(java 1.4) or did not return -1 (java 1.3)");
542         
543         // Likewise...
544
int prevIndex = li.previousIndex();
545         TestSupport.assertThat(prevIndex == -1 || prevIndex == -2,
546                 "ListIterator.previousIndex() on empty List did not return -1 " +
547                 "(java 1.4) or -2 (java 1.3)");
548         
549         List JavaDoc l = new ArrayList JavaDoc();
550         l.add(new Integer JavaDoc(1));
551         li = newInstance(l).listIterator();
552         TestSupport.assertThat(li.hasPrevious() == false,
553                 "ListIterator.hasPrevious() is true with nothing previous");
554         
555         TestSupport.assertThat(li.hasNext() == true,
556                 "ListIterator.hasNext() is false with next present");
557         TestSupport.assertThat(li.next().equals(new Integer JavaDoc(1)),
558                 "ListIterator.next() did not return expected Integer(1)");
559        
560         if (!isReadOnly()) {
561             li.remove();
562             TestSupport.assertThat(li.hasNext() == false,
563                     "ListIterator.hasNext() is true for empty List");
564             
565             TestSupport.assertThat(li.hasPrevious() == false,
566                     "ListIterator.hasPrevious() is true for empty List");
567             try {
568                 li.set(new Integer JavaDoc(42));
569                 TestSupport.assertThat(false, "expected IllegalStateException");
570             } catch (IllegalStateException JavaDoc e) {}
571             
572             try {
573                 li.remove();
574                 TestSupport.assertThat(false, "expected IllegalStateException");
575             } catch (IllegalStateException JavaDoc e) {}
576         }
577         
578         l = new ArrayList JavaDoc();
579         l.add(new Integer JavaDoc(0));
580         l.add(new Integer JavaDoc(1));
581         l.add(new Integer JavaDoc(2));
582         
583         li = newInstance(l).listIterator();
584         
585         for (int i = 0, n = l.size(); i < n; i++) {
586             TestSupport.assertThat(li.next().equals(new Integer JavaDoc(i)),
587                     "ListIterator.previous did not return expected value");
588         }
589         
590         while (!isReadOnly() && li.hasNext()) {
591             li.next();
592             li.set(new Integer JavaDoc(42));
593             TestSupport.assertThat(li.previous().equals(new Integer JavaDoc(42)),
594                     "ListIterator.previous() did not return the value that was set()");
595             li.remove();
596         }
597         
598         if(isReadOnly()) {
599             li = newInstance(null).listIterator();
600         }
601         
602         li = defaultList().listIterator(2);
603         TestSupport.assertThat(li.next().equals(new Integer JavaDoc(2)),
604                 "List.listIteraor(index) did not return expected value");
605         TestSupport.assertThat(li.next().equals(new Integer JavaDoc(3)),
606                 "List.listIteraor(index) did not return expected value");
607         TestSupport.assertThat(li.hasNext() == false,
608                 "listIterator.hasNext() at end of list returned true");
609         
610     }
611     
612     /** Tests toArray() */
613     public void test_toArray() {
614         Object JavaDoc[] intObjArray = new Integer JavaDoc[] {
615                 new Integer JavaDoc(0), new Integer JavaDoc(1), new Integer JavaDoc(2), new Integer JavaDoc(3)};
616         TestSupport.assertThat(Arrays.equals(defaultList().toArray(), intObjArray),
617                 "toArray() did not return the expected Integer[] array");
618     }
619     
620     /** Tests toArray(Object[] a) */
621     public void test_toArray_typed() {
622         Object JavaDoc[] intObjArray = new Integer JavaDoc[] {
623                 new Integer JavaDoc(0), new Integer JavaDoc(1), new Integer JavaDoc(2), new Integer JavaDoc(3)};
624         TestSupport.assertThat(Arrays.equals(
625                 defaultList().toArray(new Integer JavaDoc[] {}), intObjArray),
626                 "toArray(Integer[]) did not return the expected Integer[] array");
627     }
628     
629     // Can't test clone since it's not visible, but it is from jython.
630
// /** Tests clone() */
631
// public void test_clone() {
632
// List a = defaultList();
633
// TestSupport.assertEquals(((Object)a).clone(), a,
634
// "clone() was not equal to original");
635
// TestSupport.assertThat(a.clone() != a,
636
// "clone() returned same instance");
637
// }
638
}
639
Popular Tags