KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > list > 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.list;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.ConcurrentModificationException JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.ListIterator JavaDoc;
24 import java.util.NoSuchElementException JavaDoc;
25
26 import junit.framework.Test;
27
28 import org.apache.commons.collections.BulkTest;
29
30 /**
31  * Test class.
32  *
33  * @version $Revision: 1.3 $ $Date: 2004/02/18 01:20:34 $
34  *
35  * @author Rodney Waldhoff
36  * @author Simon Kitching
37  */

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

1097    public String JavaDoc[] ignoredTests() {
1098        ArrayList JavaDoc list = new ArrayList JavaDoc();
1099        String JavaDoc prefix = "TestCursorableLinkedList";
1100        String JavaDoc bulk = ".bulkTestSubList";
1101        String JavaDoc[] ignored = new String JavaDoc[] {
1102          ".testEmptyListSerialization",
1103          ".testFullListSerialization",
1104          ".testEmptyListCompatibility",
1105          ".testFullListCompatibility",
1106          ".testSimpleSerialization",
1107          ".testCanonicalEmptyCollectionExists",
1108          ".testCanonicalFullCollectionExists",
1109          ".testSerializeDeserializeThenCompare"
1110        };
1111        for (int i = 0; i < ignored.length; i++) {
1112            list.add(prefix + bulk + ignored[i]);
1113            list.add(prefix + bulk + bulk + ignored[i]);
1114        }
1115        return (String JavaDoc[])list.toArray(new String JavaDoc[0]);
1116    }
1117
1118}
1119
Popular Tags