KickJava   Java API By Example, From Geeks To Geeks.

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


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.Arrays JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.ListIterator JavaDoc;
23 import java.util.NoSuchElementException JavaDoc;
24
25 import junit.framework.Test;
26
27 import org.apache.commons.collections.iterators.EmptyIterator;
28 import org.apache.commons.collections.iterators.EmptyListIterator;
29 import org.apache.commons.collections.iterators.EmptyMapIterator;
30 import org.apache.commons.collections.iterators.EmptyOrderedIterator;
31 import org.apache.commons.collections.iterators.EmptyOrderedMapIterator;
32
33 /**
34  * Tests for IteratorUtils.
35  *
36  * @version $Revision: 1.16 $ $Date: 2004/05/26 21:56:45 $
37  *
38  * @author Unknown
39  */

40 public class TestIteratorUtils extends BulkTest {
41
42     public TestIteratorUtils(String JavaDoc name) {
43         super(name);
44     }
45
46     public static void main(String JavaDoc args[]) {
47         junit.textui.TestRunner.run(suite());
48     }
49
50     public static Test suite() {
51         return BulkTest.makeSuite(TestIteratorUtils.class);
52     }
53
54     public void testToList() {
55         List JavaDoc list = new ArrayList JavaDoc();
56         list.add(new Integer JavaDoc(1));
57         list.add("Two");
58         list.add(null);
59         List JavaDoc result = IteratorUtils.toList(list.iterator());
60         assertEquals(list, result);
61     }
62
63     public void testToArray() {
64         List JavaDoc list = new ArrayList JavaDoc();
65         list.add(new Integer JavaDoc(1));
66         list.add("Two");
67         list.add(null);
68         Object JavaDoc[] result = IteratorUtils.toArray(list.iterator());
69         assertEquals(list, Arrays.asList(result));
70     }
71
72     public void testToArray2() {
73         List JavaDoc list = new ArrayList JavaDoc();
74         list.add("One");
75         list.add("Two");
76         list.add(null);
77         String JavaDoc[] result = (String JavaDoc[]) IteratorUtils.toArray(list.iterator(), String JavaDoc.class);
78         assertEquals(list, Arrays.asList(result));
79     }
80     
81     public void testArrayIterator() {
82         Object JavaDoc[] objArray = {"a", "b", "c"};
83         ResettableIterator iterator = IteratorUtils.arrayIterator(objArray);
84         assertTrue(iterator.next().equals("a"));
85         assertTrue(iterator.next().equals("b"));
86         iterator.reset();
87         assertTrue(iterator.next().equals("a"));
88         
89         try {
90             iterator = IteratorUtils.arrayIterator(new Integer JavaDoc(0));
91             fail("Expecting IllegalArgumentException");
92         } catch (IllegalArgumentException JavaDoc ex) {
93                 // expected
94
}
95         
96         try {
97             iterator = IteratorUtils.arrayIterator(null);
98             fail("Expecting NullPointerException");
99         } catch (NullPointerException JavaDoc ex) {
100                 // expected
101
}
102         
103         iterator = IteratorUtils.arrayIterator(objArray, 1);
104         assertTrue(iterator.next().equals("b"));
105         
106         try {
107             iterator = IteratorUtils.arrayIterator(objArray, -1);
108             fail("Expecting IndexOutOfBoundsException");
109         } catch (IndexOutOfBoundsException JavaDoc ex) {
110             // expected
111
}
112         
113         iterator = IteratorUtils.arrayIterator(objArray, 3);
114         assertTrue(!iterator.hasNext());
115         iterator.reset();
116         
117         try {
118             iterator = IteratorUtils.arrayIterator(objArray, 4);
119             fail("Expecting IndexOutOfBoundsException");
120         } catch (IndexOutOfBoundsException JavaDoc ex) {
121             // expected
122
}
123         
124         iterator = IteratorUtils.arrayIterator(objArray, 2, 3);
125         assertTrue(iterator.next().equals("c"));
126         
127         try {
128             iterator = IteratorUtils.arrayIterator(objArray, 2, 4);
129             fail("Expecting IndexOutOfBoundsException");
130         } catch (IndexOutOfBoundsException JavaDoc ex) {
131             // expected
132
}
133         
134         try {
135             iterator = IteratorUtils.arrayIterator(objArray, -1, 1);
136             fail("Expecting IndexOutOfBoundsException");
137         } catch (IndexOutOfBoundsException JavaDoc ex) {
138             // expected
139
}
140         
141         try {
142             iterator = IteratorUtils.arrayIterator(objArray, 2, 1);
143             fail("Expecting IllegalArgumentException");
144         } catch (IllegalArgumentException JavaDoc ex) {
145             // expected
146
}
147         
148         int[] intArray = {0, 1, 2};
149         iterator = IteratorUtils.arrayIterator(intArray);
150         assertTrue(iterator.next().equals(new Integer JavaDoc(0)));
151         assertTrue(iterator.next().equals(new Integer JavaDoc(1)));
152         iterator.reset();
153         assertTrue(iterator.next().equals(new Integer JavaDoc(0)));
154         
155         iterator = IteratorUtils.arrayIterator(intArray, 1);
156         assertTrue(iterator.next().equals(new Integer JavaDoc(1)));
157         
158         try {
159             iterator = IteratorUtils.arrayIterator(intArray, -1);
160             fail("Expecting IndexOutOfBoundsException");
161         } catch (IndexOutOfBoundsException JavaDoc ex) {
162             // expected
163
}
164         
165         iterator = IteratorUtils.arrayIterator(intArray, 3);
166         assertTrue(!iterator.hasNext());
167         iterator.reset();
168         
169         try {
170             iterator = IteratorUtils.arrayIterator(intArray, 4);
171             fail("Expecting IndexOutOfBoundsException");
172         } catch (IndexOutOfBoundsException JavaDoc ex) {
173             // expected
174
}
175         
176         iterator = IteratorUtils.arrayIterator(intArray, 2, 3);
177         assertTrue(iterator.next().equals(new Integer JavaDoc(2)));
178         
179         try {
180             iterator = IteratorUtils.arrayIterator(intArray, 2, 4);
181             fail("Expecting IndexOutOfBoundsException");
182         } catch (IndexOutOfBoundsException JavaDoc ex) {
183             // expected
184
}
185         
186         try {
187             iterator = IteratorUtils.arrayIterator(intArray, -1, 1);
188             fail("Expecting IndexOutOfBoundsException");
189         } catch (IndexOutOfBoundsException JavaDoc ex) {
190             // expected
191
}
192         
193         try {
194             iterator = IteratorUtils.arrayIterator(intArray, 2, 1);
195             fail("Expecting IllegalArgumentException");
196         } catch (IllegalArgumentException JavaDoc ex) {
197             // expected
198
}
199     }
200     
201     public void testArrayListIterator() {
202         Object JavaDoc[] objArray = {"a", "b", "c", "d"};
203         ResettableListIterator iterator = IteratorUtils.arrayListIterator(objArray);
204         assertTrue(!iterator.hasPrevious());
205         assertTrue(iterator.previousIndex() == -1);
206         assertTrue(iterator.nextIndex() == 0);
207         assertTrue(iterator.next().equals("a"));
208         assertTrue(iterator.previous().equals("a"));
209         assertTrue(iterator.next().equals("a"));
210         assertTrue(iterator.previousIndex() == 0);
211         assertTrue(iterator.nextIndex() == 1);
212         assertTrue(iterator.next().equals("b"));
213         assertTrue(iterator.next().equals("c"));
214         assertTrue(iterator.next().equals("d"));
215         assertTrue(iterator.nextIndex() == 4); // size of list
216
assertTrue(iterator.previousIndex() == 3);
217         
218         try {
219             iterator = IteratorUtils.arrayListIterator(new Integer JavaDoc(0));
220             fail("Expecting IllegalArgumentException");
221         } catch (IllegalArgumentException JavaDoc ex) {
222                 // expected
223
}
224         
225         try {
226             iterator = IteratorUtils.arrayListIterator(null);
227             fail("Expecting NullPointerException");
228         } catch (NullPointerException JavaDoc ex) {
229                 // expected
230
}
231         
232         iterator = IteratorUtils.arrayListIterator(objArray, 1);
233         assertTrue(iterator.previousIndex() == -1);
234         assertTrue(!iterator.hasPrevious());
235         assertTrue(iterator.nextIndex() == 0);
236         assertTrue(iterator.next().equals("b"));
237         assertTrue(iterator.previousIndex() == 0);
238         
239         try {
240             iterator = IteratorUtils.arrayListIterator(objArray, -1);
241             fail("Expecting IndexOutOfBoundsException.");
242         } catch (IndexOutOfBoundsException JavaDoc ex) {
243             // expected
244
}
245         
246         iterator = IteratorUtils.arrayListIterator(objArray, 3);
247         assertTrue(iterator.hasNext());
248         try {
249             Object JavaDoc x = iterator.previous();
250             fail("Expecting NoSuchElementException.");
251         } catch (NoSuchElementException JavaDoc ex) {
252             // expected
253
}
254         
255         try {
256             iterator = IteratorUtils.arrayListIterator(objArray, 5);
257             fail("Expecting IndexOutOfBoundsException.");
258         } catch (IndexOutOfBoundsException JavaDoc ex) {
259             // expected
260
}
261         
262         iterator = IteratorUtils.arrayListIterator(objArray, 2, 3);
263         assertTrue(iterator.next().equals("c"));
264         
265         try {
266             iterator = IteratorUtils.arrayListIterator(objArray, 2, 5);
267             fail("Expecting IndexOutOfBoundsException");
268         } catch (IndexOutOfBoundsException JavaDoc ex) {
269             // expected
270
}
271         
272         try {
273             iterator = IteratorUtils.arrayListIterator(objArray, -1, 1);
274             fail("Expecting IndexOutOfBoundsException");
275         } catch (IndexOutOfBoundsException JavaDoc ex) {
276             // expected
277
}
278         
279         try {
280             iterator = IteratorUtils.arrayListIterator(objArray, 2, 1);
281             fail("Expecting IllegalArgumentException");
282         } catch (IllegalArgumentException JavaDoc ex) {
283             // expected
284
}
285         
286         int[] intArray = {0, 1, 2};
287         iterator = IteratorUtils.arrayListIterator(intArray);
288         assertTrue(iterator.previousIndex() == -1);
289         assertTrue(!iterator.hasPrevious());
290         assertTrue(iterator.nextIndex() == 0);
291         assertTrue(iterator.next().equals(new Integer JavaDoc(0)));
292         assertTrue(iterator.previousIndex() == 0);
293         assertTrue(iterator.nextIndex() == 1);
294         assertTrue(iterator.next().equals(new Integer JavaDoc(1)));
295         assertTrue(iterator.previousIndex() == 1);
296         assertTrue(iterator.nextIndex() == 2);
297         assertTrue(iterator.previous().equals(new Integer JavaDoc(1)));
298         assertTrue(iterator.next().equals(new Integer JavaDoc(1)));
299         
300         iterator = IteratorUtils.arrayListIterator(intArray, 1);
301         assertTrue(iterator.previousIndex() == -1);
302         assertTrue(!iterator.hasPrevious());
303         assertTrue(iterator.nextIndex() == 0);
304         assertTrue(iterator.next().equals(new Integer JavaDoc(1)));
305         assertTrue(iterator.previous().equals(new Integer JavaDoc(1)));
306         assertTrue(iterator.next().equals(new Integer JavaDoc(1)));
307         assertTrue(iterator.previousIndex() == 0);
308         assertTrue(iterator.nextIndex() == 1);
309         assertTrue(iterator.next().equals(new Integer JavaDoc(2)));
310         assertTrue(iterator.previousIndex() == 1);
311         assertTrue(iterator.nextIndex() == 2);
312         assertTrue(iterator.previous().equals(new Integer JavaDoc(2)));
313         assertTrue(iterator.previousIndex() == 0);
314         assertTrue(iterator.nextIndex() == 1);
315         
316         try {
317             iterator = IteratorUtils.arrayListIterator(intArray, -1);
318             fail("Expecting IndexOutOfBoundsException");
319         } catch (IndexOutOfBoundsException JavaDoc ex) {
320             // expected
321
}
322         
323         iterator = IteratorUtils.arrayListIterator(intArray, 3);
324         assertTrue(!iterator.hasNext());
325      
326         try {
327             iterator = IteratorUtils.arrayListIterator(intArray, 4);
328             fail("Expecting IndexOutOfBoundsException");
329         } catch (IndexOutOfBoundsException JavaDoc ex) {
330             // expected
331
}
332         
333         iterator = IteratorUtils.arrayListIterator(intArray, 2, 3);
334         assertTrue(!iterator.hasPrevious());
335         assertTrue(iterator.previousIndex() == -1);
336         assertTrue(iterator.next().equals(new Integer JavaDoc(2)));
337         assertTrue(iterator.hasPrevious());
338         assertTrue(!iterator.hasNext());
339         
340         
341         try {
342             iterator = IteratorUtils.arrayListIterator(intArray, 2, 4);
343             fail("Expecting IndexOutOfBoundsException");
344         } catch (IndexOutOfBoundsException JavaDoc ex) {
345             // expected
346
}
347         
348         try {
349             iterator = IteratorUtils.arrayListIterator(intArray, -1, 1);
350             fail("Expecting IndexOutOfBoundsException");
351         } catch (IndexOutOfBoundsException JavaDoc ex) {
352             // expected
353
}
354         
355         try {
356             iterator = IteratorUtils.arrayListIterator(intArray, 2, 1);
357             fail("Expecting IllegalArgumentException");
358         } catch (IllegalArgumentException JavaDoc ex) {
359             // expected
360
}
361     }
362         
363
364     /**
365      * Gets an immutable Iterator operating on the elements ["a", "b", "c", "d"].
366      */

367     private Iterator JavaDoc getImmutableIterator() {
368         List JavaDoc list = new ArrayList JavaDoc();
369         list.add("a");
370         list.add("b");
371         list.add("c");
372         list.add("d");
373         return IteratorUtils.unmodifiableIterator(list.iterator());
374     }
375
376     /**
377      * Gets an immutable ListIterator operating on the elements ["a", "b", "c", "d"].
378      */

379     private ListIterator JavaDoc getImmutableListIterator() {
380         List JavaDoc list = new ArrayList JavaDoc();
381         list.add("a");
382         list.add("b");
383         list.add("c");
384         list.add("d");
385         return IteratorUtils.unmodifiableListIterator(list.listIterator());
386     }
387
388     //-----------------------------------------------------------------------
389
/**
390      * Test empty iterator
391      */

392     public void testEmptyIterator() {
393         assertSame(EmptyIterator.INSTANCE, IteratorUtils.EMPTY_ITERATOR);
394         assertSame(EmptyIterator.RESETTABLE_INSTANCE, IteratorUtils.EMPTY_ITERATOR);
395         assertEquals(true, IteratorUtils.EMPTY_ITERATOR instanceof Iterator JavaDoc);
396         assertEquals(true, IteratorUtils.EMPTY_ITERATOR instanceof ResettableIterator);
397         assertEquals(false, IteratorUtils.EMPTY_ITERATOR instanceof OrderedIterator);
398         assertEquals(false, IteratorUtils.EMPTY_ITERATOR instanceof ListIterator JavaDoc);
399         assertEquals(false, IteratorUtils.EMPTY_ITERATOR instanceof MapIterator);
400         assertEquals(false, IteratorUtils.EMPTY_ITERATOR.hasNext());
401         IteratorUtils.EMPTY_ITERATOR.reset();
402         assertSame(IteratorUtils.EMPTY_ITERATOR, IteratorUtils.EMPTY_ITERATOR);
403         assertSame(IteratorUtils.EMPTY_ITERATOR, IteratorUtils.emptyIterator());
404         try {
405             IteratorUtils.EMPTY_ITERATOR.next();
406             fail();
407         } catch (NoSuchElementException JavaDoc ex) {}
408         try {
409             IteratorUtils.EMPTY_ITERATOR.remove();
410             fail();
411         } catch (IllegalStateException JavaDoc ex) {}
412     }
413     
414     //-----------------------------------------------------------------------
415
/**
416      * Test empty list iterator
417      */

418     public void testEmptyListIterator() {
419         assertSame(EmptyListIterator.INSTANCE, IteratorUtils.EMPTY_LIST_ITERATOR);
420         assertSame(EmptyListIterator.RESETTABLE_INSTANCE, IteratorUtils.EMPTY_LIST_ITERATOR);
421         assertEquals(true, IteratorUtils.EMPTY_LIST_ITERATOR instanceof Iterator JavaDoc);
422         assertEquals(true, IteratorUtils.EMPTY_LIST_ITERATOR instanceof ListIterator JavaDoc);
423         assertEquals(true, IteratorUtils.EMPTY_LIST_ITERATOR instanceof ResettableIterator);
424         assertEquals(true, IteratorUtils.EMPTY_LIST_ITERATOR instanceof ResettableListIterator);
425         assertEquals(false, IteratorUtils.EMPTY_LIST_ITERATOR instanceof MapIterator);
426         assertEquals(false, IteratorUtils.EMPTY_LIST_ITERATOR.hasNext());
427         assertEquals(0, IteratorUtils.EMPTY_LIST_ITERATOR.nextIndex());
428         assertEquals(-1, IteratorUtils.EMPTY_LIST_ITERATOR.previousIndex());
429         IteratorUtils.EMPTY_LIST_ITERATOR.reset();
430         assertSame(IteratorUtils.EMPTY_LIST_ITERATOR, IteratorUtils.EMPTY_LIST_ITERATOR);
431         assertSame(IteratorUtils.EMPTY_LIST_ITERATOR, IteratorUtils.emptyListIterator());
432         try {
433             IteratorUtils.EMPTY_LIST_ITERATOR.next();
434             fail();
435         } catch (NoSuchElementException JavaDoc ex) {}
436         try {
437             IteratorUtils.EMPTY_LIST_ITERATOR.previous();
438             fail();
439         } catch (NoSuchElementException JavaDoc ex) {}
440         try {
441             IteratorUtils.EMPTY_LIST_ITERATOR.remove();
442             fail();
443         } catch (IllegalStateException JavaDoc ex) {}
444         try {
445             IteratorUtils.EMPTY_LIST_ITERATOR.set(null);
446             fail();
447         } catch (IllegalStateException JavaDoc ex) {}
448         try {
449             IteratorUtils.EMPTY_LIST_ITERATOR.add(null);
450             fail();
451         } catch (UnsupportedOperationException JavaDoc ex) {}
452     }
453     
454     //-----------------------------------------------------------------------
455
/**
456      * Test empty map iterator
457      */

458     public void testEmptyMapIterator() {
459         assertSame(EmptyMapIterator.INSTANCE, IteratorUtils.EMPTY_MAP_ITERATOR);
460         assertEquals(true, IteratorUtils.EMPTY_MAP_ITERATOR instanceof Iterator JavaDoc);
461         assertEquals(true, IteratorUtils.EMPTY_MAP_ITERATOR instanceof MapIterator);
462         assertEquals(true, IteratorUtils.EMPTY_MAP_ITERATOR instanceof ResettableIterator);
463         assertEquals(false, IteratorUtils.EMPTY_MAP_ITERATOR instanceof ListIterator JavaDoc);
464         assertEquals(false, IteratorUtils.EMPTY_MAP_ITERATOR instanceof OrderedIterator);
465         assertEquals(false, IteratorUtils.EMPTY_MAP_ITERATOR instanceof OrderedMapIterator);
466         assertEquals(false, IteratorUtils.EMPTY_MAP_ITERATOR.hasNext());
467         ((ResettableIterator) IteratorUtils.EMPTY_MAP_ITERATOR).reset();
468         assertSame(IteratorUtils.EMPTY_MAP_ITERATOR, IteratorUtils.EMPTY_MAP_ITERATOR);
469         assertSame(IteratorUtils.EMPTY_MAP_ITERATOR, IteratorUtils.emptyMapIterator());
470         try {
471             IteratorUtils.EMPTY_MAP_ITERATOR.next();
472             fail();
473         } catch (NoSuchElementException JavaDoc ex) {}
474         try {
475             IteratorUtils.EMPTY_MAP_ITERATOR.remove();
476             fail();
477         } catch (IllegalStateException JavaDoc ex) {}
478         try {
479             IteratorUtils.EMPTY_MAP_ITERATOR.getKey();
480             fail();
481         } catch (IllegalStateException JavaDoc ex) {}
482         try {
483             IteratorUtils.EMPTY_MAP_ITERATOR.getValue();
484             fail();
485         } catch (IllegalStateException JavaDoc ex) {}
486         try {
487             IteratorUtils.EMPTY_MAP_ITERATOR.setValue(null);
488             fail();
489         } catch (IllegalStateException JavaDoc ex) {}
490     }
491     
492     //-----------------------------------------------------------------------
493
/**
494      * Test empty map iterator
495      */

496     public void testEmptyOrderedIterator() {
497         assertSame(EmptyOrderedIterator.INSTANCE, IteratorUtils.EMPTY_ORDERED_ITERATOR);
498         assertEquals(true, IteratorUtils.EMPTY_ORDERED_ITERATOR instanceof Iterator JavaDoc);
499         assertEquals(true, IteratorUtils.EMPTY_ORDERED_ITERATOR instanceof OrderedIterator);
500         assertEquals(true, IteratorUtils.EMPTY_ORDERED_ITERATOR instanceof ResettableIterator);
501         assertEquals(false, IteratorUtils.EMPTY_ORDERED_ITERATOR instanceof ListIterator JavaDoc);
502         assertEquals(false, IteratorUtils.EMPTY_ORDERED_ITERATOR instanceof MapIterator);
503         assertEquals(false, IteratorUtils.EMPTY_ORDERED_ITERATOR.hasNext());
504         assertEquals(false, IteratorUtils.EMPTY_ORDERED_ITERATOR.hasPrevious());
505         ((ResettableIterator) IteratorUtils.EMPTY_ORDERED_ITERATOR).reset();
506         assertSame(IteratorUtils.EMPTY_ORDERED_ITERATOR, IteratorUtils.EMPTY_ORDERED_ITERATOR);
507         assertSame(IteratorUtils.EMPTY_ORDERED_ITERATOR, IteratorUtils.emptyOrderedIterator());
508         try {
509             IteratorUtils.EMPTY_ORDERED_ITERATOR.next();
510             fail();
511         } catch (NoSuchElementException JavaDoc ex) {}
512         try {
513             IteratorUtils.EMPTY_ORDERED_ITERATOR.previous();
514             fail();
515         } catch (NoSuchElementException JavaDoc ex) {}
516         try {
517             IteratorUtils.EMPTY_ORDERED_ITERATOR.remove();
518             fail();
519         } catch (IllegalStateException JavaDoc ex) {}
520     }
521     
522     //-----------------------------------------------------------------------
523
/**
524      * Test empty map iterator
525      */

526     public void testEmptyOrderedMapIterator() {
527         assertSame(EmptyOrderedMapIterator.INSTANCE, IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR);
528         assertEquals(true, IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR instanceof Iterator JavaDoc);
529         assertEquals(true, IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR instanceof MapIterator);
530         assertEquals(true, IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR instanceof OrderedMapIterator);
531         assertEquals(true, IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR instanceof ResettableIterator);
532         assertEquals(false, IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR instanceof ListIterator JavaDoc);
533         assertEquals(false, IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.hasNext());
534         assertEquals(false, IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.hasPrevious());
535         ((ResettableIterator) IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR).reset();
536         assertSame(IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR, IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR);
537         assertSame(IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR, IteratorUtils.emptyOrderedMapIterator());
538         try {
539             IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.next();
540             fail();
541         } catch (NoSuchElementException JavaDoc ex) {}
542         try {
543             IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.previous();
544             fail();
545         } catch (NoSuchElementException JavaDoc ex) {}
546         try {
547             IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.remove();
548             fail();
549         } catch (IllegalStateException JavaDoc ex) {}
550         try {
551             IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.getKey();
552             fail();
553         } catch (IllegalStateException JavaDoc ex) {}
554         try {
555             IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.getValue();
556             fail();
557         } catch (IllegalStateException JavaDoc ex) {}
558         try {
559             IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.setValue(null);
560             fail();
561         } catch (IllegalStateException JavaDoc ex) {}
562     }
563     
564     //-----------------------------------------------------------------------
565
/**
566      * Test next() and hasNext() for an immutable Iterator.
567      */

568     public void testUnmodifiableIteratorIteration() {
569         Iterator JavaDoc iterator = getImmutableIterator();
570
571         assertTrue(iterator.hasNext());
572
573         assertEquals("a", iterator.next());
574
575         assertTrue(iterator.hasNext());
576
577         assertEquals("b", iterator.next());
578
579         assertTrue(iterator.hasNext());
580
581         assertEquals("c", iterator.next());
582
583         assertTrue(iterator.hasNext());
584
585         assertEquals("d", iterator.next());
586
587         assertTrue(!iterator.hasNext());
588     }
589
590     /**
591      * Test next(), hasNext(), previous() and hasPrevious() for an immutable
592      * ListIterator.
593      */

594     public void testUnmodifiableListIteratorIteration() {
595         ListIterator JavaDoc listIterator = getImmutableListIterator();
596
597         assertTrue(!listIterator.hasPrevious());
598         assertTrue(listIterator.hasNext());
599
600         assertEquals("a", listIterator.next());
601
602         assertTrue(listIterator.hasPrevious());
603         assertTrue(listIterator.hasNext());
604
605         assertEquals("b", listIterator.next());
606
607         assertTrue(listIterator.hasPrevious());
608         assertTrue(listIterator.hasNext());
609
610         assertEquals("c", listIterator.next());
611
612         assertTrue(listIterator.hasPrevious());
613         assertTrue(listIterator.hasNext());
614
615         assertEquals("d", listIterator.next());
616
617         assertTrue(listIterator.hasPrevious());
618         assertTrue(!listIterator.hasNext());
619
620         assertEquals("d", listIterator.previous());
621
622         assertTrue(listIterator.hasPrevious());
623         assertTrue(listIterator.hasNext());
624
625         assertEquals("c", listIterator.previous());
626
627         assertTrue(listIterator.hasPrevious());
628         assertTrue(listIterator.hasNext());
629
630         assertEquals("b", listIterator.previous());
631
632         assertTrue(listIterator.hasPrevious());
633         assertTrue(listIterator.hasNext());
634
635         assertEquals("a", listIterator.previous());
636
637         assertTrue(!listIterator.hasPrevious());
638         assertTrue(listIterator.hasNext());
639     }
640
641     /**
642      * Test remove() for an immutable Iterator.
643      */

644     public void testUnmodifiableIteratorImmutability() {
645         Iterator JavaDoc iterator = getImmutableIterator();
646
647         try {
648             iterator.remove();
649             // We shouldn't get to here.
650
fail("remove() should throw an UnsupportedOperationException");
651         } catch (UnsupportedOperationException JavaDoc e) {
652             // This is correct; ignore the exception.
653
}
654
655         iterator.next();
656
657         try {
658             iterator.remove();
659             // We shouldn't get to here.
660
fail("remove() should throw an UnsupportedOperationException");
661         } catch (UnsupportedOperationException JavaDoc e) {
662             // This is correct; ignore the exception.
663
}
664
665     }
666
667     /**
668      * Test remove() for an immutable ListIterator.
669      */

670     public void testUnmodifiableListIteratorImmutability() {
671         ListIterator JavaDoc listIterator = getImmutableListIterator();
672
673         try {
674             listIterator.remove();
675             // We shouldn't get to here.
676
fail("remove() should throw an UnsupportedOperationException");
677         } catch (UnsupportedOperationException JavaDoc e) {
678             // This is correct; ignore the exception.
679
}
680
681         try {
682             listIterator.set("a");
683             // We shouldn't get to here.
684
fail("set(Object) should throw an UnsupportedOperationException");
685         } catch (UnsupportedOperationException JavaDoc e) {
686             // This is correct; ignore the exception.
687
}
688
689         try {
690             listIterator.add("a");
691             // We shouldn't get to here.
692
fail("add(Object) should throw an UnsupportedOperationException");
693         } catch (UnsupportedOperationException JavaDoc e) {
694             // This is correct; ignore the exception.
695
}
696
697         listIterator.next();
698
699         try {
700             listIterator.remove();
701             // We shouldn't get to here.
702
fail("remove() should throw an UnsupportedOperationException");
703         } catch (UnsupportedOperationException JavaDoc e) {
704             // This is correct; ignore the exception.
705
}
706
707         try {
708             listIterator.set("a");
709             // We shouldn't get to here.
710
fail("set(Object) should throw an UnsupportedOperationException");
711         } catch (UnsupportedOperationException JavaDoc e) {
712             // This is correct; ignore the exception.
713
}
714
715         try {
716             listIterator.add("a");
717             // We shouldn't get to here.
718
fail("add(Object) should throw an UnsupportedOperationException");
719         } catch (UnsupportedOperationException JavaDoc e) {
720             // This is correct; ignore the exception.
721
}
722     }
723 }
724
Popular Tags