001: /*
002: * Copyright 2001-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections;
017:
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.ListIterator;
023: import java.util.NoSuchElementException;
024:
025: import junit.framework.Test;
026:
027: import org.apache.commons.collections.iterators.EmptyIterator;
028: import org.apache.commons.collections.iterators.EmptyListIterator;
029: import org.apache.commons.collections.iterators.EmptyMapIterator;
030: import org.apache.commons.collections.iterators.EmptyOrderedIterator;
031: import org.apache.commons.collections.iterators.EmptyOrderedMapIterator;
032:
033: /**
034: * Tests for IteratorUtils.
035: *
036: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
037: *
038: * @author Unknown
039: */
040: public class TestIteratorUtils extends BulkTest {
041:
042: public TestIteratorUtils(String name) {
043: super (name);
044: }
045:
046: public static void main(String args[]) {
047: junit.textui.TestRunner.run(suite());
048: }
049:
050: public static Test suite() {
051: return BulkTest.makeSuite(TestIteratorUtils.class);
052: }
053:
054: public void testToList() {
055: List list = new ArrayList();
056: list.add(new Integer(1));
057: list.add("Two");
058: list.add(null);
059: List result = IteratorUtils.toList(list.iterator());
060: assertEquals(list, result);
061: }
062:
063: public void testToArray() {
064: List list = new ArrayList();
065: list.add(new Integer(1));
066: list.add("Two");
067: list.add(null);
068: Object[] result = IteratorUtils.toArray(list.iterator());
069: assertEquals(list, Arrays.asList(result));
070: }
071:
072: public void testToArray2() {
073: List list = new ArrayList();
074: list.add("One");
075: list.add("Two");
076: list.add(null);
077: String[] result = (String[]) IteratorUtils.toArray(list
078: .iterator(), String.class);
079: assertEquals(list, Arrays.asList(result));
080: }
081:
082: public void testArrayIterator() {
083: Object[] objArray = { "a", "b", "c" };
084: ResettableIterator iterator = IteratorUtils
085: .arrayIterator(objArray);
086: assertTrue(iterator.next().equals("a"));
087: assertTrue(iterator.next().equals("b"));
088: iterator.reset();
089: assertTrue(iterator.next().equals("a"));
090:
091: try {
092: iterator = IteratorUtils.arrayIterator(new Integer(0));
093: fail("Expecting IllegalArgumentException");
094: } catch (IllegalArgumentException ex) {
095: // expected
096: }
097:
098: try {
099: iterator = IteratorUtils.arrayIterator(null);
100: fail("Expecting NullPointerException");
101: } catch (NullPointerException ex) {
102: // expected
103: }
104:
105: iterator = IteratorUtils.arrayIterator(objArray, 1);
106: assertTrue(iterator.next().equals("b"));
107:
108: try {
109: iterator = IteratorUtils.arrayIterator(objArray, -1);
110: fail("Expecting IndexOutOfBoundsException");
111: } catch (IndexOutOfBoundsException ex) {
112: // expected
113: }
114:
115: iterator = IteratorUtils.arrayIterator(objArray, 3);
116: assertTrue(!iterator.hasNext());
117: iterator.reset();
118:
119: try {
120: iterator = IteratorUtils.arrayIterator(objArray, 4);
121: fail("Expecting IndexOutOfBoundsException");
122: } catch (IndexOutOfBoundsException ex) {
123: // expected
124: }
125:
126: iterator = IteratorUtils.arrayIterator(objArray, 2, 3);
127: assertTrue(iterator.next().equals("c"));
128:
129: try {
130: iterator = IteratorUtils.arrayIterator(objArray, 2, 4);
131: fail("Expecting IndexOutOfBoundsException");
132: } catch (IndexOutOfBoundsException ex) {
133: // expected
134: }
135:
136: try {
137: iterator = IteratorUtils.arrayIterator(objArray, -1, 1);
138: fail("Expecting IndexOutOfBoundsException");
139: } catch (IndexOutOfBoundsException ex) {
140: // expected
141: }
142:
143: try {
144: iterator = IteratorUtils.arrayIterator(objArray, 2, 1);
145: fail("Expecting IllegalArgumentException");
146: } catch (IllegalArgumentException ex) {
147: // expected
148: }
149:
150: int[] intArray = { 0, 1, 2 };
151: iterator = IteratorUtils.arrayIterator(intArray);
152: assertTrue(iterator.next().equals(new Integer(0)));
153: assertTrue(iterator.next().equals(new Integer(1)));
154: iterator.reset();
155: assertTrue(iterator.next().equals(new Integer(0)));
156:
157: iterator = IteratorUtils.arrayIterator(intArray, 1);
158: assertTrue(iterator.next().equals(new Integer(1)));
159:
160: try {
161: iterator = IteratorUtils.arrayIterator(intArray, -1);
162: fail("Expecting IndexOutOfBoundsException");
163: } catch (IndexOutOfBoundsException ex) {
164: // expected
165: }
166:
167: iterator = IteratorUtils.arrayIterator(intArray, 3);
168: assertTrue(!iterator.hasNext());
169: iterator.reset();
170:
171: try {
172: iterator = IteratorUtils.arrayIterator(intArray, 4);
173: fail("Expecting IndexOutOfBoundsException");
174: } catch (IndexOutOfBoundsException ex) {
175: // expected
176: }
177:
178: iterator = IteratorUtils.arrayIterator(intArray, 2, 3);
179: assertTrue(iterator.next().equals(new Integer(2)));
180:
181: try {
182: iterator = IteratorUtils.arrayIterator(intArray, 2, 4);
183: fail("Expecting IndexOutOfBoundsException");
184: } catch (IndexOutOfBoundsException ex) {
185: // expected
186: }
187:
188: try {
189: iterator = IteratorUtils.arrayIterator(intArray, -1, 1);
190: fail("Expecting IndexOutOfBoundsException");
191: } catch (IndexOutOfBoundsException ex) {
192: // expected
193: }
194:
195: try {
196: iterator = IteratorUtils.arrayIterator(intArray, 2, 1);
197: fail("Expecting IllegalArgumentException");
198: } catch (IllegalArgumentException ex) {
199: // expected
200: }
201: }
202:
203: public void testArrayListIterator() {
204: Object[] objArray = { "a", "b", "c", "d" };
205: ResettableListIterator iterator = IteratorUtils
206: .arrayListIterator(objArray);
207: assertTrue(!iterator.hasPrevious());
208: assertTrue(iterator.previousIndex() == -1);
209: assertTrue(iterator.nextIndex() == 0);
210: assertTrue(iterator.next().equals("a"));
211: assertTrue(iterator.previous().equals("a"));
212: assertTrue(iterator.next().equals("a"));
213: assertTrue(iterator.previousIndex() == 0);
214: assertTrue(iterator.nextIndex() == 1);
215: assertTrue(iterator.next().equals("b"));
216: assertTrue(iterator.next().equals("c"));
217: assertTrue(iterator.next().equals("d"));
218: assertTrue(iterator.nextIndex() == 4); // size of list
219: assertTrue(iterator.previousIndex() == 3);
220:
221: try {
222: iterator = IteratorUtils.arrayListIterator(new Integer(0));
223: fail("Expecting IllegalArgumentException");
224: } catch (IllegalArgumentException ex) {
225: // expected
226: }
227:
228: try {
229: iterator = IteratorUtils.arrayListIterator(null);
230: fail("Expecting NullPointerException");
231: } catch (NullPointerException ex) {
232: // expected
233: }
234:
235: iterator = IteratorUtils.arrayListIterator(objArray, 1);
236: assertTrue(iterator.previousIndex() == -1);
237: assertTrue(!iterator.hasPrevious());
238: assertTrue(iterator.nextIndex() == 0);
239: assertTrue(iterator.next().equals("b"));
240: assertTrue(iterator.previousIndex() == 0);
241:
242: try {
243: iterator = IteratorUtils.arrayListIterator(objArray, -1);
244: fail("Expecting IndexOutOfBoundsException.");
245: } catch (IndexOutOfBoundsException ex) {
246: // expected
247: }
248:
249: iterator = IteratorUtils.arrayListIterator(objArray, 3);
250: assertTrue(iterator.hasNext());
251: try {
252: Object x = iterator.previous();
253: fail("Expecting NoSuchElementException.");
254: } catch (NoSuchElementException ex) {
255: // expected
256: }
257:
258: try {
259: iterator = IteratorUtils.arrayListIterator(objArray, 5);
260: fail("Expecting IndexOutOfBoundsException.");
261: } catch (IndexOutOfBoundsException ex) {
262: // expected
263: }
264:
265: iterator = IteratorUtils.arrayListIterator(objArray, 2, 3);
266: assertTrue(iterator.next().equals("c"));
267:
268: try {
269: iterator = IteratorUtils.arrayListIterator(objArray, 2, 5);
270: fail("Expecting IndexOutOfBoundsException");
271: } catch (IndexOutOfBoundsException ex) {
272: // expected
273: }
274:
275: try {
276: iterator = IteratorUtils.arrayListIterator(objArray, -1, 1);
277: fail("Expecting IndexOutOfBoundsException");
278: } catch (IndexOutOfBoundsException ex) {
279: // expected
280: }
281:
282: try {
283: iterator = IteratorUtils.arrayListIterator(objArray, 2, 1);
284: fail("Expecting IllegalArgumentException");
285: } catch (IllegalArgumentException ex) {
286: // expected
287: }
288:
289: int[] intArray = { 0, 1, 2 };
290: iterator = IteratorUtils.arrayListIterator(intArray);
291: assertTrue(iterator.previousIndex() == -1);
292: assertTrue(!iterator.hasPrevious());
293: assertTrue(iterator.nextIndex() == 0);
294: assertTrue(iterator.next().equals(new Integer(0)));
295: assertTrue(iterator.previousIndex() == 0);
296: assertTrue(iterator.nextIndex() == 1);
297: assertTrue(iterator.next().equals(new Integer(1)));
298: assertTrue(iterator.previousIndex() == 1);
299: assertTrue(iterator.nextIndex() == 2);
300: assertTrue(iterator.previous().equals(new Integer(1)));
301: assertTrue(iterator.next().equals(new Integer(1)));
302:
303: iterator = IteratorUtils.arrayListIterator(intArray, 1);
304: assertTrue(iterator.previousIndex() == -1);
305: assertTrue(!iterator.hasPrevious());
306: assertTrue(iterator.nextIndex() == 0);
307: assertTrue(iterator.next().equals(new Integer(1)));
308: assertTrue(iterator.previous().equals(new Integer(1)));
309: assertTrue(iterator.next().equals(new Integer(1)));
310: assertTrue(iterator.previousIndex() == 0);
311: assertTrue(iterator.nextIndex() == 1);
312: assertTrue(iterator.next().equals(new Integer(2)));
313: assertTrue(iterator.previousIndex() == 1);
314: assertTrue(iterator.nextIndex() == 2);
315: assertTrue(iterator.previous().equals(new Integer(2)));
316: assertTrue(iterator.previousIndex() == 0);
317: assertTrue(iterator.nextIndex() == 1);
318:
319: try {
320: iterator = IteratorUtils.arrayListIterator(intArray, -1);
321: fail("Expecting IndexOutOfBoundsException");
322: } catch (IndexOutOfBoundsException ex) {
323: // expected
324: }
325:
326: iterator = IteratorUtils.arrayListIterator(intArray, 3);
327: assertTrue(!iterator.hasNext());
328:
329: try {
330: iterator = IteratorUtils.arrayListIterator(intArray, 4);
331: fail("Expecting IndexOutOfBoundsException");
332: } catch (IndexOutOfBoundsException ex) {
333: // expected
334: }
335:
336: iterator = IteratorUtils.arrayListIterator(intArray, 2, 3);
337: assertTrue(!iterator.hasPrevious());
338: assertTrue(iterator.previousIndex() == -1);
339: assertTrue(iterator.next().equals(new Integer(2)));
340: assertTrue(iterator.hasPrevious());
341: assertTrue(!iterator.hasNext());
342:
343: try {
344: iterator = IteratorUtils.arrayListIterator(intArray, 2, 4);
345: fail("Expecting IndexOutOfBoundsException");
346: } catch (IndexOutOfBoundsException ex) {
347: // expected
348: }
349:
350: try {
351: iterator = IteratorUtils.arrayListIterator(intArray, -1, 1);
352: fail("Expecting IndexOutOfBoundsException");
353: } catch (IndexOutOfBoundsException ex) {
354: // expected
355: }
356:
357: try {
358: iterator = IteratorUtils.arrayListIterator(intArray, 2, 1);
359: fail("Expecting IllegalArgumentException");
360: } catch (IllegalArgumentException ex) {
361: // expected
362: }
363: }
364:
365: /**
366: * Gets an immutable Iterator operating on the elements ["a", "b", "c", "d"].
367: */
368: private Iterator getImmutableIterator() {
369: List list = new ArrayList();
370: list.add("a");
371: list.add("b");
372: list.add("c");
373: list.add("d");
374: return IteratorUtils.unmodifiableIterator(list.iterator());
375: }
376:
377: /**
378: * Gets an immutable ListIterator operating on the elements ["a", "b", "c", "d"].
379: */
380: private ListIterator getImmutableListIterator() {
381: List list = new ArrayList();
382: list.add("a");
383: list.add("b");
384: list.add("c");
385: list.add("d");
386: return IteratorUtils.unmodifiableListIterator(list
387: .listIterator());
388: }
389:
390: //-----------------------------------------------------------------------
391: /**
392: * Test empty iterator
393: */
394: public void testEmptyIterator() {
395: assertSame(EmptyIterator.INSTANCE, IteratorUtils.EMPTY_ITERATOR);
396: assertSame(EmptyIterator.RESETTABLE_INSTANCE,
397: IteratorUtils.EMPTY_ITERATOR);
398: assertEquals(true,
399: IteratorUtils.EMPTY_ITERATOR instanceof Iterator);
400: assertEquals(
401: true,
402: IteratorUtils.EMPTY_ITERATOR instanceof ResettableIterator);
403: assertEquals(false,
404: IteratorUtils.EMPTY_ITERATOR instanceof OrderedIterator);
405: assertEquals(false,
406: IteratorUtils.EMPTY_ITERATOR instanceof ListIterator);
407: assertEquals(false,
408: IteratorUtils.EMPTY_ITERATOR instanceof MapIterator);
409: assertEquals(false, IteratorUtils.EMPTY_ITERATOR.hasNext());
410: IteratorUtils.EMPTY_ITERATOR.reset();
411: assertSame(IteratorUtils.EMPTY_ITERATOR,
412: IteratorUtils.EMPTY_ITERATOR);
413: assertSame(IteratorUtils.EMPTY_ITERATOR, IteratorUtils
414: .emptyIterator());
415: try {
416: IteratorUtils.EMPTY_ITERATOR.next();
417: fail();
418: } catch (NoSuchElementException ex) {
419: }
420: try {
421: IteratorUtils.EMPTY_ITERATOR.remove();
422: fail();
423: } catch (IllegalStateException ex) {
424: }
425: }
426:
427: //-----------------------------------------------------------------------
428: /**
429: * Test empty list iterator
430: */
431: public void testEmptyListIterator() {
432: assertSame(EmptyListIterator.INSTANCE,
433: IteratorUtils.EMPTY_LIST_ITERATOR);
434: assertSame(EmptyListIterator.RESETTABLE_INSTANCE,
435: IteratorUtils.EMPTY_LIST_ITERATOR);
436: assertEquals(true,
437: IteratorUtils.EMPTY_LIST_ITERATOR instanceof Iterator);
438: assertEquals(
439: true,
440: IteratorUtils.EMPTY_LIST_ITERATOR instanceof ListIterator);
441: assertEquals(
442: true,
443: IteratorUtils.EMPTY_LIST_ITERATOR instanceof ResettableIterator);
444: assertEquals(
445: true,
446: IteratorUtils.EMPTY_LIST_ITERATOR instanceof ResettableListIterator);
447: assertEquals(
448: false,
449: IteratorUtils.EMPTY_LIST_ITERATOR instanceof MapIterator);
450: assertEquals(false, IteratorUtils.EMPTY_LIST_ITERATOR.hasNext());
451: assertEquals(0, IteratorUtils.EMPTY_LIST_ITERATOR.nextIndex());
452: assertEquals(-1, IteratorUtils.EMPTY_LIST_ITERATOR
453: .previousIndex());
454: IteratorUtils.EMPTY_LIST_ITERATOR.reset();
455: assertSame(IteratorUtils.EMPTY_LIST_ITERATOR,
456: IteratorUtils.EMPTY_LIST_ITERATOR);
457: assertSame(IteratorUtils.EMPTY_LIST_ITERATOR, IteratorUtils
458: .emptyListIterator());
459: try {
460: IteratorUtils.EMPTY_LIST_ITERATOR.next();
461: fail();
462: } catch (NoSuchElementException ex) {
463: }
464: try {
465: IteratorUtils.EMPTY_LIST_ITERATOR.previous();
466: fail();
467: } catch (NoSuchElementException ex) {
468: }
469: try {
470: IteratorUtils.EMPTY_LIST_ITERATOR.remove();
471: fail();
472: } catch (IllegalStateException ex) {
473: }
474: try {
475: IteratorUtils.EMPTY_LIST_ITERATOR.set(null);
476: fail();
477: } catch (IllegalStateException ex) {
478: }
479: try {
480: IteratorUtils.EMPTY_LIST_ITERATOR.add(null);
481: fail();
482: } catch (UnsupportedOperationException ex) {
483: }
484: }
485:
486: //-----------------------------------------------------------------------
487: /**
488: * Test empty map iterator
489: */
490: public void testEmptyMapIterator() {
491: assertSame(EmptyMapIterator.INSTANCE,
492: IteratorUtils.EMPTY_MAP_ITERATOR);
493: assertEquals(true,
494: IteratorUtils.EMPTY_MAP_ITERATOR instanceof Iterator);
495: assertEquals(true,
496: IteratorUtils.EMPTY_MAP_ITERATOR instanceof MapIterator);
497: assertEquals(
498: true,
499: IteratorUtils.EMPTY_MAP_ITERATOR instanceof ResettableIterator);
500: assertEquals(
501: false,
502: IteratorUtils.EMPTY_MAP_ITERATOR instanceof ListIterator);
503: assertEquals(
504: false,
505: IteratorUtils.EMPTY_MAP_ITERATOR instanceof OrderedIterator);
506: assertEquals(
507: false,
508: IteratorUtils.EMPTY_MAP_ITERATOR instanceof OrderedMapIterator);
509: assertEquals(false, IteratorUtils.EMPTY_MAP_ITERATOR.hasNext());
510: ((ResettableIterator) IteratorUtils.EMPTY_MAP_ITERATOR).reset();
511: assertSame(IteratorUtils.EMPTY_MAP_ITERATOR,
512: IteratorUtils.EMPTY_MAP_ITERATOR);
513: assertSame(IteratorUtils.EMPTY_MAP_ITERATOR, IteratorUtils
514: .emptyMapIterator());
515: try {
516: IteratorUtils.EMPTY_MAP_ITERATOR.next();
517: fail();
518: } catch (NoSuchElementException ex) {
519: }
520: try {
521: IteratorUtils.EMPTY_MAP_ITERATOR.remove();
522: fail();
523: } catch (IllegalStateException ex) {
524: }
525: try {
526: IteratorUtils.EMPTY_MAP_ITERATOR.getKey();
527: fail();
528: } catch (IllegalStateException ex) {
529: }
530: try {
531: IteratorUtils.EMPTY_MAP_ITERATOR.getValue();
532: fail();
533: } catch (IllegalStateException ex) {
534: }
535: try {
536: IteratorUtils.EMPTY_MAP_ITERATOR.setValue(null);
537: fail();
538: } catch (IllegalStateException ex) {
539: }
540: }
541:
542: //-----------------------------------------------------------------------
543: /**
544: * Test empty map iterator
545: */
546: public void testEmptyOrderedIterator() {
547: assertSame(EmptyOrderedIterator.INSTANCE,
548: IteratorUtils.EMPTY_ORDERED_ITERATOR);
549: assertEquals(
550: true,
551: IteratorUtils.EMPTY_ORDERED_ITERATOR instanceof Iterator);
552: assertEquals(
553: true,
554: IteratorUtils.EMPTY_ORDERED_ITERATOR instanceof OrderedIterator);
555: assertEquals(
556: true,
557: IteratorUtils.EMPTY_ORDERED_ITERATOR instanceof ResettableIterator);
558: assertEquals(
559: false,
560: IteratorUtils.EMPTY_ORDERED_ITERATOR instanceof ListIterator);
561: assertEquals(
562: false,
563: IteratorUtils.EMPTY_ORDERED_ITERATOR instanceof MapIterator);
564: assertEquals(false, IteratorUtils.EMPTY_ORDERED_ITERATOR
565: .hasNext());
566: assertEquals(false, IteratorUtils.EMPTY_ORDERED_ITERATOR
567: .hasPrevious());
568: ((ResettableIterator) IteratorUtils.EMPTY_ORDERED_ITERATOR)
569: .reset();
570: assertSame(IteratorUtils.EMPTY_ORDERED_ITERATOR,
571: IteratorUtils.EMPTY_ORDERED_ITERATOR);
572: assertSame(IteratorUtils.EMPTY_ORDERED_ITERATOR, IteratorUtils
573: .emptyOrderedIterator());
574: try {
575: IteratorUtils.EMPTY_ORDERED_ITERATOR.next();
576: fail();
577: } catch (NoSuchElementException ex) {
578: }
579: try {
580: IteratorUtils.EMPTY_ORDERED_ITERATOR.previous();
581: fail();
582: } catch (NoSuchElementException ex) {
583: }
584: try {
585: IteratorUtils.EMPTY_ORDERED_ITERATOR.remove();
586: fail();
587: } catch (IllegalStateException ex) {
588: }
589: }
590:
591: //-----------------------------------------------------------------------
592: /**
593: * Test empty map iterator
594: */
595: public void testEmptyOrderedMapIterator() {
596: assertSame(EmptyOrderedMapIterator.INSTANCE,
597: IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR);
598: assertEquals(
599: true,
600: IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR instanceof Iterator);
601: assertEquals(
602: true,
603: IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR instanceof MapIterator);
604: assertEquals(
605: true,
606: IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR instanceof OrderedMapIterator);
607: assertEquals(
608: true,
609: IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR instanceof ResettableIterator);
610: assertEquals(
611: false,
612: IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR instanceof ListIterator);
613: assertEquals(false, IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR
614: .hasNext());
615: assertEquals(false, IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR
616: .hasPrevious());
617: ((ResettableIterator) IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR)
618: .reset();
619: assertSame(IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR,
620: IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR);
621: assertSame(IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR,
622: IteratorUtils.emptyOrderedMapIterator());
623: try {
624: IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.next();
625: fail();
626: } catch (NoSuchElementException ex) {
627: }
628: try {
629: IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.previous();
630: fail();
631: } catch (NoSuchElementException ex) {
632: }
633: try {
634: IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.remove();
635: fail();
636: } catch (IllegalStateException ex) {
637: }
638: try {
639: IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.getKey();
640: fail();
641: } catch (IllegalStateException ex) {
642: }
643: try {
644: IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.getValue();
645: fail();
646: } catch (IllegalStateException ex) {
647: }
648: try {
649: IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.setValue(null);
650: fail();
651: } catch (IllegalStateException ex) {
652: }
653: }
654:
655: //-----------------------------------------------------------------------
656: /**
657: * Test next() and hasNext() for an immutable Iterator.
658: */
659: public void testUnmodifiableIteratorIteration() {
660: Iterator iterator = getImmutableIterator();
661:
662: assertTrue(iterator.hasNext());
663:
664: assertEquals("a", iterator.next());
665:
666: assertTrue(iterator.hasNext());
667:
668: assertEquals("b", iterator.next());
669:
670: assertTrue(iterator.hasNext());
671:
672: assertEquals("c", iterator.next());
673:
674: assertTrue(iterator.hasNext());
675:
676: assertEquals("d", iterator.next());
677:
678: assertTrue(!iterator.hasNext());
679: }
680:
681: /**
682: * Test next(), hasNext(), previous() and hasPrevious() for an immutable
683: * ListIterator.
684: */
685: public void testUnmodifiableListIteratorIteration() {
686: ListIterator listIterator = getImmutableListIterator();
687:
688: assertTrue(!listIterator.hasPrevious());
689: assertTrue(listIterator.hasNext());
690:
691: assertEquals("a", listIterator.next());
692:
693: assertTrue(listIterator.hasPrevious());
694: assertTrue(listIterator.hasNext());
695:
696: assertEquals("b", listIterator.next());
697:
698: assertTrue(listIterator.hasPrevious());
699: assertTrue(listIterator.hasNext());
700:
701: assertEquals("c", listIterator.next());
702:
703: assertTrue(listIterator.hasPrevious());
704: assertTrue(listIterator.hasNext());
705:
706: assertEquals("d", listIterator.next());
707:
708: assertTrue(listIterator.hasPrevious());
709: assertTrue(!listIterator.hasNext());
710:
711: assertEquals("d", listIterator.previous());
712:
713: assertTrue(listIterator.hasPrevious());
714: assertTrue(listIterator.hasNext());
715:
716: assertEquals("c", listIterator.previous());
717:
718: assertTrue(listIterator.hasPrevious());
719: assertTrue(listIterator.hasNext());
720:
721: assertEquals("b", listIterator.previous());
722:
723: assertTrue(listIterator.hasPrevious());
724: assertTrue(listIterator.hasNext());
725:
726: assertEquals("a", listIterator.previous());
727:
728: assertTrue(!listIterator.hasPrevious());
729: assertTrue(listIterator.hasNext());
730: }
731:
732: /**
733: * Test remove() for an immutable Iterator.
734: */
735: public void testUnmodifiableIteratorImmutability() {
736: Iterator iterator = getImmutableIterator();
737:
738: try {
739: iterator.remove();
740: // We shouldn't get to here.
741: fail("remove() should throw an UnsupportedOperationException");
742: } catch (UnsupportedOperationException e) {
743: // This is correct; ignore the exception.
744: }
745:
746: iterator.next();
747:
748: try {
749: iterator.remove();
750: // We shouldn't get to here.
751: fail("remove() should throw an UnsupportedOperationException");
752: } catch (UnsupportedOperationException e) {
753: // This is correct; ignore the exception.
754: }
755:
756: }
757:
758: /**
759: * Test remove() for an immutable ListIterator.
760: */
761: public void testUnmodifiableListIteratorImmutability() {
762: ListIterator listIterator = getImmutableListIterator();
763:
764: try {
765: listIterator.remove();
766: // We shouldn't get to here.
767: fail("remove() should throw an UnsupportedOperationException");
768: } catch (UnsupportedOperationException e) {
769: // This is correct; ignore the exception.
770: }
771:
772: try {
773: listIterator.set("a");
774: // We shouldn't get to here.
775: fail("set(Object) should throw an UnsupportedOperationException");
776: } catch (UnsupportedOperationException e) {
777: // This is correct; ignore the exception.
778: }
779:
780: try {
781: listIterator.add("a");
782: // We shouldn't get to here.
783: fail("add(Object) should throw an UnsupportedOperationException");
784: } catch (UnsupportedOperationException e) {
785: // This is correct; ignore the exception.
786: }
787:
788: listIterator.next();
789:
790: try {
791: listIterator.remove();
792: // We shouldn't get to here.
793: fail("remove() should throw an UnsupportedOperationException");
794: } catch (UnsupportedOperationException e) {
795: // This is correct; ignore the exception.
796: }
797:
798: try {
799: listIterator.set("a");
800: // We shouldn't get to here.
801: fail("set(Object) should throw an UnsupportedOperationException");
802: } catch (UnsupportedOperationException e) {
803: // This is correct; ignore the exception.
804: }
805:
806: try {
807: listIterator.add("a");
808: // We shouldn't get to here.
809: fail("add(Object) should throw an UnsupportedOperationException");
810: } catch (UnsupportedOperationException e) {
811: // This is correct; ignore the exception.
812: }
813: }
814: }
|