KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > TestCursorableLinkedList


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.ListIterator JavaDoc;
23
24 import junit.framework.Test;
25
26 import org.apache.commons.collections.list.AbstractTestList;
27
28 /**
29  * Test class.
30  *
31  * @version $Revision: 1.18 $ $Date: 2004/02/18 01:20:35 $
32  *
33  * @author Rodney Waldhoff
34  * @author Simon Kitching
35  */

36 public class TestCursorableLinkedList extends AbstractTestList {
37     public TestCursorableLinkedList(String JavaDoc testName) {
38         super(testName);
39     }
40
41     public static Test suite() {
42         return BulkTest.makeSuite(TestCursorableLinkedList.class);
43     }
44
45     public static void main(String JavaDoc args[]) {
46         String JavaDoc[] testCaseName = { TestCursorableLinkedList.class.getName() };
47         junit.textui.TestRunner.main(testCaseName);
48     }
49
50     private CursorableLinkedList list = null;
51
52     public void setUp() {
53         list = new CursorableLinkedList();
54     }
55
56     public List JavaDoc makeEmptyList() {
57         return new CursorableLinkedList();
58     }
59
60     public void testAdd() {
61         assertEquals("[]",list.toString());
62         assertTrue(list.add(new Integer JavaDoc(1)));
63         assertEquals("[1]",list.toString());
64         assertTrue(list.add(new Integer JavaDoc(2)));
65         assertEquals("[1, 2]",list.toString());
66         assertTrue(list.add(new Integer JavaDoc(3)));
67         assertEquals("[1, 2, 3]",list.toString());
68         assertTrue(list.addFirst(new Integer JavaDoc(0)));
69         assertEquals("[0, 1, 2, 3]",list.toString());
70         assertTrue(list.addLast(new Integer JavaDoc(4)));
71         assertEquals("[0, 1, 2, 3, 4]",list.toString());
72         list.add(0,new Integer JavaDoc(-2));
73         assertEquals("[-2, 0, 1, 2, 3, 4]",list.toString());
74         list.add(1,new Integer JavaDoc(-1));
75         assertEquals("[-2, -1, 0, 1, 2, 3, 4]",list.toString());
76         list.add(7,new Integer JavaDoc(5));
77         assertEquals("[-2, -1, 0, 1, 2, 3, 4, 5]",list.toString());
78
79         java.util.List JavaDoc list2 = new java.util.LinkedList JavaDoc();
80         list2.add("A");
81         list2.add("B");
82         list2.add("C");
83
84         assertTrue(list.addAll(list2));
85         assertEquals("[-2, -1, 0, 1, 2, 3, 4, 5, A, B, C]",list.toString());
86         assertTrue(list.addAll(3,list2));
87         assertEquals("[-2, -1, 0, A, B, C, 1, 2, 3, 4, 5, A, B, C]",list.toString());
88     }
89
90     public void testClear() {
91         assertEquals(0,list.size());
92         assertTrue(list.isEmpty());
93         list.clear();
94         assertEquals(0,list.size());
95         assertTrue(list.isEmpty());
96
97         list.add("element");
98         assertEquals(1,list.size());
99         assertTrue(!list.isEmpty());
100
101         list.clear();
102         assertEquals(0,list.size());
103         assertTrue(list.isEmpty());
104
105         list.add("element1");
106         list.add("element2");
107         assertEquals(2,list.size());
108         assertTrue(!list.isEmpty());
109
110         list.clear();
111         assertEquals(0,list.size());
112         assertTrue(list.isEmpty());
113
114         for(int i=0;i<1000;i++) {
115             list.add(new Integer JavaDoc(i));
116         }
117         assertEquals(1000,list.size());
118         assertTrue(!list.isEmpty());
119
120         list.clear();
121         assertEquals(0,list.size());
122         assertTrue(list.isEmpty());
123     }
124
125     public void testContains() {
126         assertTrue(!list.contains("A"));
127         assertTrue(list.add("A"));
128         assertTrue(list.contains("A"));
129         assertTrue(list.add("B"));
130         assertTrue(list.contains("A"));
131         assertTrue(list.addFirst("a"));
132         assertTrue(list.contains("A"));
133         assertTrue(list.remove("a"));
134         assertTrue(list.contains("A"));
135         assertTrue(list.remove("A"));
136         assertTrue(!list.contains("A"));
137     }
138
139     public void testContainsAll() {
140         assertTrue(list.containsAll(list));
141         java.util.List JavaDoc list2 = new java.util.LinkedList JavaDoc();
142         assertTrue(list.containsAll(list2));
143         list2.add("A");
144         assertTrue(!list.containsAll(list2));
145         list.add("B");
146         list.add("A");
147         assertTrue(list.containsAll(list2));
148         list2.add("B");
149         assertTrue(list.containsAll(list2));
150         list2.add("C");
151         assertTrue(!list.containsAll(list2));
152         list.add("C");
153         assertTrue(list.containsAll(list2));
154         list2.add("C");
155         assertTrue(list.containsAll(list2));
156         assertTrue(list.containsAll(list));
157     }
158
159     public void testCursorNavigation() {
160         list.add("1");
161         list.add("2");
162         list.add("3");
163         list.add("4");
164         list.add("5");
165         CursorableLinkedList.Cursor it = list.cursor();
166         assertTrue(it.hasNext());
167         assertTrue(!it.hasPrevious());
168         assertEquals("1",it.next());
169         assertTrue(it.hasNext());
170         assertTrue(it.hasPrevious());
171         assertEquals("1",it.previous());
172         assertTrue(it.hasNext());
173         assertTrue(!it.hasPrevious());
174         assertEquals("1",it.next());
175         assertTrue(it.hasNext());
176         assertTrue(it.hasPrevious());
177         assertEquals("2",it.next());
178         assertTrue(it.hasNext());
179         assertTrue(it.hasPrevious());
180         assertEquals("2",it.previous());
181         assertTrue(it.hasNext());
182         assertTrue(it.hasPrevious());
183         assertEquals("2",it.next());
184         assertTrue(it.hasNext());
185         assertTrue(it.hasPrevious());
186         assertEquals("3",it.next());
187         assertTrue(it.hasNext());
188         assertTrue(it.hasPrevious());
189         assertEquals("4",it.next());
190         assertTrue(it.hasNext());
191         assertTrue(it.hasPrevious());
192         assertEquals("5",it.next());
193         assertTrue(!it.hasNext());
194         assertTrue(it.hasPrevious());
195         assertEquals("5",it.previous());
196         assertTrue(it.hasNext());
197         assertTrue(it.hasPrevious());
198         assertEquals("4",it.previous());
199         assertTrue(it.hasNext());
200         assertTrue(it.hasPrevious());
201         assertEquals("3",it.previous());
202         assertTrue(it.hasNext());
203         assertTrue(it.hasPrevious());
204         assertEquals("2",it.previous());
205         assertTrue(it.hasNext());
206         assertTrue(it.hasPrevious());
207         assertEquals("1",it.previous());
208         assertTrue(it.hasNext());
209         assertTrue(!it.hasPrevious());
210         it.close();
211     }
212
213     public void testCursorSet() {
214         list.add("1");
215         list.add("2");
216         list.add("3");
217         list.add("4");
218         list.add("5");
219
220         CursorableLinkedList.Cursor it = list.cursor();
221         assertEquals("1",it.next());
222         it.set("a");
223         assertEquals("a",it.previous());
224         it.set("A");
225         assertEquals("A",it.next());
226         assertEquals("2",it.next());
227         it.set("B");
228         assertEquals("3",it.next());
229         assertEquals("4",it.next());
230         it.set("D");
231         assertEquals("5",it.next());
232         it.set("E");
233         assertEquals("[A, B, 3, D, E]",list.toString());
234         it.close();
235     }
236
237     public void testCursorRemove() {
238         list.add("1");
239         list.add("2");
240         list.add("3");
241         list.add("4");
242         list.add("5");
243
244         CursorableLinkedList.Cursor it = list.cursor();
245         try {
246             it.remove();
247         } catch(IllegalStateException JavaDoc e) {
248             // expected
249
}
250         assertEquals("1",it.next());
251         assertEquals("2",it.next());
252         assertEquals("[1, 2, 3, 4, 5]",list.toString());
253         it.remove();
254         assertEquals("[1, 3, 4, 5]",list.toString());
255         assertEquals("3",it.next());
256         assertEquals("3",it.previous());
257         assertEquals("1",it.previous());
258         it.remove();
259         assertEquals("[3, 4, 5]",list.toString());
260         assertTrue(!it.hasPrevious());
261         assertEquals("3",it.next());
262         it.remove();
263         assertEquals("[4, 5]",list.toString());
264         try {
265             it.remove();
266         } catch(IllegalStateException JavaDoc e) {
267             // expected
268
}
269         assertEquals("4",it.next());
270         assertEquals("5",it.next());
271         it.remove();
272         assertEquals("[4]",list.toString());
273         assertEquals("4",it.previous());
274         it.remove();
275         assertEquals("[]",list.toString());
276         it.close();
277     }
278
279     public void testCursorAdd() {
280         CursorableLinkedList.Cursor it = list.cursor();
281         it.add("1");
282         assertEquals("[1]",list.toString());
283         it.add("3");
284         assertEquals("[1, 3]",list.toString());
285         it.add("5");
286         assertEquals("[1, 3, 5]",list.toString());
287         assertEquals("5",it.previous());
288         it.add("4");
289         assertEquals("[1, 3, 4, 5]",list.toString());
290         assertEquals("4",it.previous());
291         assertEquals("3",it.previous());
292         it.add("2");
293         assertEquals("[1, 2, 3, 4, 5]",list.toString());
294         it.close();
295     }
296
297     public void testCursorConcurrentModification() {
298         // this test verifies that cursors remain valid when the list
299
// is modified via other means.
300
list.add("1");
301         list.add("2");
302         list.add("3");
303         list.add("5");
304         list.add("7");
305         list.add("9");
306
307         CursorableLinkedList.Cursor c1 = list.cursor();
308         CursorableLinkedList.Cursor c2 = list.cursor();
309         ListIterator JavaDoc li = list.listIterator();
310         
311         // test cursors remain valid when list modified by std ListIterator
312
// test cursors skip elements removed via ListIterator
313
assertEquals("1",li.next());
314         assertEquals("2",li.next());
315         li.remove();
316         assertEquals("3",li.next());
317         assertEquals("1",c1.next());
318         assertEquals("3",c1.next());
319         assertEquals("1",c2.next());
320         
321         // test cursor c1 can remove elements from previously modified list
322
// test cursor c2 skips elements removed via different cursor
323
c1.remove();
324         assertEquals("5",c2.next());
325         c2.add("6");
326         assertEquals("5",c1.next());
327         assertEquals("6",c1.next());
328         assertEquals("7",c1.next());
329         
330         // test cursors remain valid when list mod via CursorableLinkedList
331
// test cursor remains valid when elements inserted into list before
332
// the current position of the cursor.
333
list.add(0, "0");
334
335         // test cursor remains valid when element inserted immediately after
336
// current element of a cursor, and the element is seen on the
337
// next call to the next method of that cursor.
338
list.add(5, "8");
339
340         assertEquals("8",c1.next());
341         assertEquals("9",c1.next());
342         c1.add("10");
343         assertEquals("7",c2.next());
344         assertEquals("8",c2.next());
345         assertEquals("9",c2.next());
346         assertEquals("10",c2.next());
347         
348         boolean nosuch = false;
349         try {
350             c2.next();
351         }
352         catch (java.util.NoSuchElementException JavaDoc nse) {
353            nosuch = true; // expected
354
}
355         assertTrue(nosuch);
356         
357         boolean listIteratorInvalid = false;
358         try {
359             li.next();
360         }
361         catch(java.util.ConcurrentModificationException JavaDoc cme) {
362             listIteratorInvalid = true; // expected
363
}
364         assertTrue(listIteratorInvalid);
365         
366         c1.close(); // not necessary
367
c2.close(); // not necessary
368
}
369     
370     public void testEqualsAndHashCode() {
371         assertTrue(list.equals(list));
372         assertEquals(list.hashCode(),list.hashCode());
373         list.add("A");
374         assertTrue(list.equals(list));
375         assertEquals(list.hashCode(),list.hashCode());
376
377         CursorableLinkedList list2 = new CursorableLinkedList();
378         assertTrue(!list.equals(list2));
379         assertTrue(!list2.equals(list));
380
381         java.util.List JavaDoc list3 = new java.util.LinkedList JavaDoc();
382         assertTrue(!list.equals(list3));
383         assertTrue(!list3.equals(list));
384         assertTrue(list2.equals(list3));
385         assertTrue(list3.equals(list2));
386         assertEquals(list2.hashCode(),list3.hashCode());
387
388         list2.add("A");
389         assertTrue(list.equals(list2));
390         assertTrue(list2.equals(list));
391         assertTrue(!list2.equals(list3));
392         assertTrue(!list3.equals(list2));
393
394         list3.add("A");
395         assertTrue(list2.equals(list3));
396         assertTrue(list3.equals(list2));
397         assertEquals(list2.hashCode(),list3.hashCode());
398
399         list.add("B");
400         assertTrue(list.equals(list));
401         assertTrue(!list.equals(list2));
402         assertTrue(!list2.equals(list));
403         assertTrue(!list.equals(list3));
404         assertTrue(!list3.equals(list));
405
406         list2.add("B");
407         list3.add("B");
408         assertTrue(list.equals(list));
409         assertTrue(list.equals(list2));
410         assertTrue(list2.equals(list));
411         assertTrue(list2.equals(list3));
412         assertTrue(list3.equals(list2));
413         assertEquals(list2.hashCode(),list3.hashCode());
414
415         list.add("C");
416         list2.add("C");
417         list3.add("C");
418         assertTrue(list.equals(list));
419         assertTrue(list.equals(list2));
420         assertTrue(list2.equals(list));
421         assertTrue(list2.equals(list3));
422         assertTrue(list3.equals(list2));
423         assertEquals(list.hashCode(),list2.hashCode());
424         assertEquals(list2.hashCode(),list3.hashCode());
425
426         list.add("D");
427         list2.addFirst("D");
428         assertTrue(list.equals(list));
429         assertTrue(!list.equals(list2));
430         assertTrue(!list2.equals(list));
431     }
432
433     public void testGet() {
434         try {
435             list.get(0);
436             fail("shouldn't get here");
437         } catch(IndexOutOfBoundsException JavaDoc e) {
438             // expected
439
}
440
441         assertTrue(list.add("A"));
442         assertEquals("A",list.get(0));
443         assertTrue(list.add("B"));
444         assertEquals("A",list.get(0));
445         assertEquals("B",list.get(1));
446
447         try {
448             list.get(-1);
449             fail("shouldn't get here");
450         } catch(IndexOutOfBoundsException JavaDoc e) {
451             // expected
452
}
453
454         try {
455             list.get(2);
456             fail("shouldn't get here");
457         } catch(IndexOutOfBoundsException JavaDoc e) {
458             // expected
459
}
460     }
461
462     public void testIndexOf() {
463         assertEquals(-1,list.indexOf("A"));
464         assertEquals(-1,list.lastIndexOf("A"));
465         list.add("A");
466         assertEquals(0,list.indexOf("A"));
467         assertEquals(0,list.lastIndexOf("A"));
468         assertEquals(-1,list.indexOf("B"));
469         assertEquals(-1,list.lastIndexOf("B"));
470         list.add("B");
471         assertEquals(0,list.indexOf("A"));
472         assertEquals(0,list.lastIndexOf("A"));
473         assertEquals(1,list.indexOf("B"));
474         assertEquals(1,list.lastIndexOf("B"));
475         list.addFirst("B");
476         assertEquals(1,list.indexOf("A"));
477         assertEquals(1,list.lastIndexOf("A"));
478         assertEquals(0,list.indexOf("B"));
479         assertEquals(2,list.lastIndexOf("B"));
480     }
481
482     public void testIsEmpty() {
483         assertTrue(list.isEmpty());
484         list.add("element");
485         assertTrue(!list.isEmpty());
486         list.remove("element");
487         assertTrue(list.isEmpty());
488         list.add("element");
489         assertTrue(!list.isEmpty());
490         list.clear();
491         assertTrue(list.isEmpty());
492     }
493
494     public void testIterator() {
495         list.add("1");
496         list.add("2");
497         list.add("3");
498         list.add("4");
499         list.add("5");
500         Iterator JavaDoc it = list.iterator();
501         assertTrue(it.hasNext());
502         assertEquals("1",it.next());
503         assertTrue(it.hasNext());
504         assertEquals("2",it.next());
505         assertTrue(it.hasNext());
506         assertEquals("3",it.next());
507         assertTrue(it.hasNext());
508         assertEquals("4",it.next());
509         assertTrue(it.hasNext());
510         assertEquals("5",it.next());
511         assertTrue(!it.hasNext());
512
513         it = list.iterator();
514         assertTrue(it.hasNext());
515         assertEquals("1",it.next());
516         it.remove();
517         assertEquals("[2, 3, 4, 5]",list.toString());
518         assertTrue(it.hasNext());
519         assertEquals("2",it.next());
520         it.remove();
521         assertEquals("[3, 4, 5]",list.toString());
522         assertTrue(it.hasNext());
523         assertEquals("3",it.next());
524         it.remove();
525         assertEquals("[4, 5]",list.toString());
526         assertTrue(it.hasNext());
527         assertEquals("4",it.next());
528         it.remove();
529         assertEquals("[5]",list.toString());
530         assertTrue(it.hasNext());
531         assertEquals("5",it.next());
532         it.remove();
533         assertEquals("[]",list.toString());
534         assertTrue(!it.hasNext());
535     }
536
537     public void testListIteratorNavigation() {
538         list.add("1");
539         list.add("2");
540         list.add("3");
541         list.add("4");
542         list.add("5");
543         ListIterator JavaDoc it = list.listIterator();
544         assertTrue(it.hasNext());
545         assertTrue(!it.hasPrevious());
546         assertEquals(-1,it.previousIndex());
547         assertEquals(0,it.nextIndex());
548         assertEquals("1",it.next());
549         assertTrue(it.hasNext());
550         assertTrue(it.hasPrevious());
551         assertEquals(0,it.previousIndex());
552         assertEquals(1,it.nextIndex());
553         assertEquals("1",it.previous());
554         assertTrue(it.hasNext());
555         assertTrue(!it.hasPrevious());
556         assertEquals(-1,it.previousIndex());
557         assertEquals(0,it.nextIndex());
558         assertEquals("1",it.next());
559         assertTrue(it.hasNext());
560         assertTrue(it.hasPrevious());
561         assertEquals(0,it.previousIndex());
562         assertEquals(1,it.nextIndex());
563         assertEquals("2",it.next());
564         assertTrue(it.hasNext());
565         assertTrue(it.hasPrevious());
566         assertEquals(1,it.previousIndex());
567         assertEquals(2,it.nextIndex());
568         assertEquals("2",it.previous());
569         assertTrue(it.hasNext());
570         assertTrue(it.hasPrevious());
571         assertEquals(0,it.previousIndex());
572         assertEquals(1,it.nextIndex());
573         assertEquals("2",it.next());
574         assertTrue(it.hasNext());
575         assertTrue(it.hasPrevious());
576         assertEquals(1,it.previousIndex());
577         assertEquals(2,it.nextIndex());
578         assertEquals("3",it.next());
579         assertTrue(it.hasNext());
580         assertTrue(it.hasPrevious());
581         assertEquals(2,it.previousIndex());
582         assertEquals(3,it.nextIndex());
583         assertEquals("4",it.next());
584         assertTrue(it.hasNext());
585         assertTrue(it.hasPrevious());
586         assertEquals(3,it.previousIndex());
587         assertEquals(4,it.nextIndex());
588         assertEquals("5",it.next());
589         assertTrue(!it.hasNext());
590         assertTrue(it.hasPrevious());
591         assertEquals(4,it.previousIndex());
592         assertEquals(5,it.nextIndex());
593         assertEquals("5",it.previous());
594         assertTrue(it.hasNext());
595         assertTrue(it.hasPrevious());
596         assertEquals(3,it.previousIndex());
597         assertEquals(4,it.nextIndex());
598         assertEquals("4",it.previous());
599         assertTrue(it.hasNext());
600         assertTrue(it.hasPrevious());
601         assertEquals(2,it.previousIndex());
602         assertEquals(3,it.nextIndex());
603         assertEquals("3",it.previous());
604         assertTrue(it.hasNext());
605         assertTrue(it.hasPrevious());
606         assertEquals(1,it.previousIndex());
607         assertEquals(2,it.nextIndex());
608         assertEquals("2",it.previous());
609         assertTrue(it.hasNext());
610         assertTrue(it.hasPrevious());
611         assertEquals(0,it.previousIndex());
612         assertEquals(1,it.nextIndex());
613         assertEquals("1",it.previous());
614         assertTrue(it.hasNext());
615         assertTrue(!it.hasPrevious());
616         assertEquals(-1,it.previousIndex());
617         assertEquals(0,it.nextIndex());
618     }
619
620     public void testListIteratorSet() {
621         list.add("1");
622         list.add("2");
623         list.add("3");
624         list.add("4");
625         list.add("5");
626
627         ListIterator JavaDoc it = list.listIterator();
628         assertEquals("1",it.next());
629         it.set("a");
630         assertEquals("a",it.previous());
631         it.set("A");
632         assertEquals("A",it.next());
633         assertEquals("2",it.next());
634         it.set("B");
635         assertEquals("3",it.next());
636         assertEquals("4",it.next());
637         it.set("D");
638         assertEquals("5",it.next());
639         it.set("E");
640         assertEquals("[A, B, 3, D, E]",list.toString());
641     }
642
643     public void testListIteratorRemove() {
644         list.add("1");
645         list.add("2");
646         list.add("3");
647         list.add("4");
648         list.add("5");
649
650         ListIterator JavaDoc it = list.listIterator();
651         try {
652             it.remove();
653         } catch(IllegalStateException JavaDoc e) {
654             // expected
655
}
656         assertEquals("1",it.next());
657         assertEquals("2",it.next());
658         assertEquals("[1, 2, 3, 4, 5]",list.toString());
659         it.remove();
660         assertEquals("[1, 3, 4, 5]",list.toString());
661         assertEquals("3",it.next());
662         assertEquals("3",it.previous());
663         assertEquals("1",it.previous());
664         it.remove();
665         assertEquals("[3, 4, 5]",list.toString());
666         assertTrue(!it.hasPrevious());
667         assertEquals("3",it.next());
668         it.remove();
669         assertEquals("[4, 5]",list.toString());
670         try {
671             it.remove();
672         } catch(IllegalStateException JavaDoc e) {
673             // expected
674
}
675         assertEquals("4",it.next());
676         assertEquals("5",it.next());
677         it.remove();
678         assertEquals("[4]",list.toString());
679         assertEquals("4",it.previous());
680         it.remove();
681         assertEquals("[]",list.toString());
682     }
683
684     public void testListIteratorAdd() {
685         ListIterator JavaDoc it = list.listIterator();
686         it.add("1");
687         assertEquals("[1]",list.toString());
688         it.add("3");
689         assertEquals("[1, 3]",list.toString());
690         it.add("5");
691         assertEquals("[1, 3, 5]",list.toString());
692         assertEquals("5",it.previous());
693         it.add("4");
694         assertEquals("[1, 3, 4, 5]",list.toString());
695         assertEquals("4",it.previous());
696         assertEquals("3",it.previous());
697         it.add("2");
698         assertEquals("[1, 2, 3, 4, 5]",list.toString());
699     }
700
701     public void testRemoveAll() {
702         list.add("1");
703         list.add("2");
704         list.add("3");
705         list.add("4");
706         list.add("5");
707
708         HashSet JavaDoc set = new HashSet JavaDoc();
709         set.add("A");
710         set.add("2");
711         set.add("C");
712         set.add("4");
713         set.add("D");
714
715         assertTrue(list.removeAll(set));
716         assertEquals("[1, 3, 5]",list.toString());
717         assertTrue(!list.removeAll(set));
718     }
719
720     public void testRemoveByIndex() {
721         list.add("1");
722         list.add("2");
723         list.add("3");
724         list.add("4");
725         list.add("5");
726         assertEquals("[1, 2, 3, 4, 5]",list.toString());
727         assertEquals("1",list.remove(0));
728         assertEquals("[2, 3, 4, 5]",list.toString());
729         assertEquals("3",list.remove(1));
730         assertEquals("[2, 4, 5]",list.toString());
731         assertEquals("4",list.remove(1));
732         assertEquals("[2, 5]",list.toString());
733         assertEquals("5",list.remove(1));
734         assertEquals("[2]",list.toString());
735         assertEquals("2",list.remove(0));
736         assertEquals("[]",list.toString());
737     }
738
739     public void testRemove() {
740         list.add("1");
741         list.add("1");
742         list.add("2");
743         list.add("3");
744         list.add("4");
745         list.add("5");
746         list.add("2");
747         list.add("3");
748         list.add("4");
749         list.add("5");
750         assertEquals("[1, 1, 2, 3, 4, 5, 2, 3, 4, 5]",list.toString());
751         assertTrue(!list.remove("6"));
752         assertTrue(list.remove("5"));
753         assertEquals("[1, 1, 2, 3, 4, 2, 3, 4, 5]",list.toString());
754         assertTrue(list.remove("5"));
755         assertEquals("[1, 1, 2, 3, 4, 2, 3, 4]",list.toString());
756         assertTrue(!list.remove("5"));
757         assertTrue(list.remove("1"));
758         assertEquals("[1, 2, 3, 4, 2, 3, 4]",list.toString());
759         assertTrue(list.remove("1"));
760         assertEquals("[2, 3, 4, 2, 3, 4]",list.toString());
761         assertTrue(list.remove("2"));
762         assertEquals("[3, 4, 2, 3, 4]",list.toString());
763         assertTrue(list.remove("2"));
764         assertEquals("[3, 4, 3, 4]",list.toString());
765         assertTrue(list.remove("3"));
766         assertEquals("[4, 3, 4]",list.toString());
767         assertTrue(list.remove("3"));
768         assertEquals("[4, 4]",list.toString());
769         assertTrue(list.remove("4"));
770         assertEquals("[4]",list.toString());
771         assertTrue(list.remove("4"));
772         assertEquals("[]",list.toString());
773     }
774
775     public void testRetainAll() {
776         list.add("1");
777         list.add("1");
778         list.add("2");
779         list.add("2");
780         list.add("3");
781         list.add("3");
782         list.add("4");
783         list.add("4");
784         list.add("5");
785         list.add("5");
786
787         HashSet JavaDoc set = new HashSet JavaDoc();
788         set.add("A");
789         set.add("2");
790         set.add("C");
791         set.add("4");
792         set.add("D");
793
794         assertTrue(list.retainAll(set));
795         assertEquals("[2, 2, 4, 4]",list.toString());
796         assertTrue(!list.retainAll(set));
797     }
798
799     public void testSet() {
800         list.add("1");
801         list.add("2");
802         list.add("3");
803         list.add("4");
804         list.add("5");
805         assertEquals("[1, 2, 3, 4, 5]",list.toString());
806         list.set(0,"A");
807         assertEquals("[A, 2, 3, 4, 5]",list.toString());
808         list.set(1,"B");
809         assertEquals("[A, B, 3, 4, 5]",list.toString());
810         list.set(2,"C");
811         assertEquals("[A, B, C, 4, 5]",list.toString());
812         list.set(3,"D");
813         assertEquals("[A, B, C, D, 5]",list.toString());
814         list.set(4,"E");
815         assertEquals("[A, B, C, D, E]",list.toString());
816     }
817
818     public void testSubList() {
819         list.add("A");
820         list.add("B");
821         list.add("C");
822         list.add("D");
823         list.add("E");
824
825         assertEquals("[A, B, C, D, E]",list.toString());
826         assertEquals("[A, B, C, D, E]",list.subList(0,5).toString());
827         assertEquals("[B, C, D, E]",list.subList(1,5).toString());
828         assertEquals("[C, D, E]",list.subList(2,5).toString());
829         assertEquals("[D, E]",list.subList(3,5).toString());
830         assertEquals("[E]",list.subList(4,5).toString());
831         assertEquals("[]",list.subList(5,5).toString());
832     }
833
834     public void testSubListAddEnd() {
835         list.add("A");
836         list.add("B");
837         list.add("C");
838         list.add("D");
839         list.add("E");
840
841         List JavaDoc sublist = list.subList(5,5);
842         sublist.add("F");
843         assertEquals("[A, B, C, D, E, F]",list.toString());
844         assertEquals("[F]",sublist.toString());
845         sublist.add("G");
846         assertEquals("[A, B, C, D, E, F, G]",list.toString());
847         assertEquals("[F, G]",sublist.toString());
848     }
849
850     public void testSubListAddBegin() {
851         list.add("A");
852         list.add("B");
853         list.add("C");
854         list.add("D");
855         list.add("E");
856
857         List JavaDoc sublist = list.subList(0,0);
858         sublist.add("a");
859         assertEquals("[a, A, B, C, D, E]",list.toString());
860         assertEquals("[a]",sublist.toString());
861         sublist.add("b");
862         assertEquals("[a, b, A, B, C, D, E]",list.toString());
863         assertEquals("[a, b]",sublist.toString());
864     }
865
866     public void testSubListAddMiddle() {
867         list.add("A");
868         list.add("B");
869         list.add("C");
870         list.add("D");
871         list.add("E");
872
873         List JavaDoc sublist = list.subList(1,3);
874         sublist.add("a");
875         assertEquals("[A, B, C, a, D, E]",list.toString());
876         assertEquals("[B, C, a]",sublist.toString());
877         sublist.add("b");
878         assertEquals("[A, B, C, a, b, D, E]",list.toString());
879         assertEquals("[B, C, a, b]",sublist.toString());
880     }
881
882     public void testSubListRemove() {
883         list.add("A");
884         list.add("B");
885         list.add("C");
886         list.add("D");
887         list.add("E");
888
889         List JavaDoc sublist = list.subList(1,4);
890         assertEquals("[B, C, D]",sublist.toString());
891         assertEquals("[A, B, C, D, E]",list.toString());
892         sublist.remove("C");
893         assertEquals("[B, D]",sublist.toString());
894         assertEquals("[A, B, D, E]",list.toString());
895         sublist.remove(1);
896         assertEquals("[B]",sublist.toString());
897         assertEquals("[A, B, E]",list.toString());
898         sublist.clear();
899         assertEquals("[]",sublist.toString());
900         assertEquals("[A, E]",list.toString());
901     }
902
903     public void testToArray() {
904         list.add("1");
905         list.add("2");
906         list.add("3");
907         list.add("4");
908         list.add("5");
909
910         Object JavaDoc[] elts = list.toArray();
911         assertEquals("1",elts[0]);
912         assertEquals("2",elts[1]);
913         assertEquals("3",elts[2]);
914         assertEquals("4",elts[3]);
915         assertEquals("5",elts[4]);
916         assertEquals(5,elts.length);
917
918         String JavaDoc[] elts2 = (String JavaDoc[])(list.toArray(new String JavaDoc[0]));
919         assertEquals("1",elts2[0]);
920         assertEquals("2",elts2[1]);
921         assertEquals("3",elts2[2]);
922         assertEquals("4",elts2[3]);
923         assertEquals("5",elts2[4]);
924         assertEquals(5,elts2.length);
925
926         String JavaDoc[] elts3 = new String JavaDoc[5];
927         assertSame(elts3,list.toArray(elts3));
928         assertEquals("1",elts3[0]);
929         assertEquals("2",elts3[1]);
930         assertEquals("3",elts3[2]);
931         assertEquals("4",elts3[3]);
932         assertEquals("5",elts3[4]);
933         assertEquals(5,elts3.length);
934
935         String JavaDoc[] elts4 = new String JavaDoc[3];
936         String JavaDoc[] elts4b = (String JavaDoc[])(list.toArray(elts4));
937         assertTrue(elts4 != elts4b);
938         assertEquals("1",elts4b[0]);
939         assertEquals("2",elts4b[1]);
940         assertEquals("3",elts4b[2]);
941         assertEquals("4",elts4b[3]);
942         assertEquals("5",elts4b[4]);
943         assertEquals(5,elts4b.length);
944     }
945
946     public void testSerialization() throws Exception JavaDoc {
947         list.add("A");
948         list.add("B");
949         list.add("C");
950         list.add("D");
951         list.add("E");
952
953         java.io.ByteArrayOutputStream JavaDoc buf = new java.io.ByteArrayOutputStream JavaDoc();
954         java.io.ObjectOutputStream JavaDoc out = new java.io.ObjectOutputStream JavaDoc(buf);
955         out.writeObject(list);
956         out.flush();
957         out.close();
958
959         java.io.ByteArrayInputStream JavaDoc bufin = new java.io.ByteArrayInputStream JavaDoc(buf.toByteArray());
960         java.io.ObjectInputStream JavaDoc in = new java.io.ObjectInputStream JavaDoc(bufin);
961         Object JavaDoc list2 = in.readObject();
962
963         assertTrue(list != list2);
964         assertTrue(list2.equals(list));
965         assertTrue(list.equals(list2));
966     }
967
968     public void testSerializationWithOpenCursor() throws Exception JavaDoc {
969         list.add("A");
970         list.add("B");
971         list.add("C");
972         list.add("D");
973         list.add("E");
974         CursorableLinkedList.Cursor cursor = list.cursor();
975
976         java.io.ByteArrayOutputStream JavaDoc buf = new java.io.ByteArrayOutputStream JavaDoc();
977         java.io.ObjectOutputStream JavaDoc out = new java.io.ObjectOutputStream JavaDoc(buf);
978         out.writeObject(list);
979         out.flush();
980         out.close();
981
982         java.io.ByteArrayInputStream JavaDoc bufin = new java.io.ByteArrayInputStream JavaDoc(buf.toByteArray());
983         java.io.ObjectInputStream JavaDoc in = new java.io.ObjectInputStream JavaDoc(bufin);
984         Object JavaDoc list2 = in.readObject();
985
986         assertTrue(list != list2);
987         assertTrue(list2.equals(list));
988         assertTrue(list.equals(list2));
989     }
990
991     public void testLongSerialization() throws Exception JavaDoc {
992         // recursive serialization will cause a stack
993
// overflow exception with long lists
994
for(int i=0;i<10000;i++) {
995             list.add(new Integer JavaDoc(i));
996         }
997
998         java.io.ByteArrayOutputStream JavaDoc buf = new java.io.ByteArrayOutputStream JavaDoc();
999         java.io.ObjectOutputStream JavaDoc out = new java.io.ObjectOutputStream JavaDoc(buf);
1000        out.writeObject(list);
1001        out.flush();
1002        out.close();
1003
1004        java.io.ByteArrayInputStream JavaDoc bufin = new java.io.ByteArrayInputStream JavaDoc(buf.toByteArray());
1005        java.io.ObjectInputStream JavaDoc in = new java.io.ObjectInputStream JavaDoc(bufin);
1006        Object JavaDoc list2 = in.readObject();
1007
1008        assertTrue(list != list2);
1009        assertTrue(list2.equals(list));
1010        assertTrue(list.equals(list2));
1011    }
1012
1013
1014    /**
1015     * Ignore the serialization tests for sublists and sub-sublists.
1016     *
1017     * @return an array of sublist serialization test names
1018     */

1019    public String JavaDoc[] ignoredTests() {
1020        ArrayList JavaDoc list = new ArrayList JavaDoc();
1021        String JavaDoc prefix = "TestCursorableLinkedList";
1022        String JavaDoc bulk = ".bulkTestSubList";
1023        String JavaDoc[] ignored = new String JavaDoc[] {
1024          ".testEmptyListSerialization",
1025          ".testFullListSerialization",
1026          ".testEmptyListCompatibility",
1027          ".testFullListCompatibility",
1028          ".testSimpleSerialization",
1029          ".testCanonicalEmptyCollectionExists",
1030          ".testCanonicalFullCollectionExists",
1031          ".testSerializeDeserializeThenCompare"
1032        };
1033        for (int i = 0; i < ignored.length; i++) {
1034            list.add(prefix + bulk + ignored[i]);
1035            list.add(prefix + bulk + bulk + ignored[i]);
1036        }
1037        return (String JavaDoc[])list.toArray(new String JavaDoc[0]);
1038    }
1039
1040}
1041
Popular Tags