1 16 package org.apache.commons.collections; 17 18 import java.util.ArrayList ; 19 import java.util.Arrays ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.ListIterator ; 23 import java.util.NoSuchElementException ; 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 40 public class TestIteratorUtils extends BulkTest { 41 42 public TestIteratorUtils(String name) { 43 super(name); 44 } 45 46 public static void main(String 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 list = new ArrayList (); 56 list.add(new Integer (1)); 57 list.add("Two"); 58 list.add(null); 59 List result = IteratorUtils.toList(list.iterator()); 60 assertEquals(list, result); 61 } 62 63 public void testToArray() { 64 List list = new ArrayList (); 65 list.add(new Integer (1)); 66 list.add("Two"); 67 list.add(null); 68 Object [] result = IteratorUtils.toArray(list.iterator()); 69 assertEquals(list, Arrays.asList(result)); 70 } 71 72 public void testToArray2() { 73 List list = new ArrayList (); 74 list.add("One"); 75 list.add("Two"); 76 list.add(null); 77 String [] result = (String []) IteratorUtils.toArray(list.iterator(), String .class); 78 assertEquals(list, Arrays.asList(result)); 79 } 80 81 public void testArrayIterator() { 82 Object [] 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 (0)); 91 fail("Expecting IllegalArgumentException"); 92 } catch (IllegalArgumentException ex) { 93 } 95 96 try { 97 iterator = IteratorUtils.arrayIterator(null); 98 fail("Expecting NullPointerException"); 99 } catch (NullPointerException ex) { 100 } 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 ex) { 110 } 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 ex) { 121 } 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 ex) { 131 } 133 134 try { 135 iterator = IteratorUtils.arrayIterator(objArray, -1, 1); 136 fail("Expecting IndexOutOfBoundsException"); 137 } catch (IndexOutOfBoundsException ex) { 138 } 140 141 try { 142 iterator = IteratorUtils.arrayIterator(objArray, 2, 1); 143 fail("Expecting IllegalArgumentException"); 144 } catch (IllegalArgumentException ex) { 145 } 147 148 int[] intArray = {0, 1, 2}; 149 iterator = IteratorUtils.arrayIterator(intArray); 150 assertTrue(iterator.next().equals(new Integer (0))); 151 assertTrue(iterator.next().equals(new Integer (1))); 152 iterator.reset(); 153 assertTrue(iterator.next().equals(new Integer (0))); 154 155 iterator = IteratorUtils.arrayIterator(intArray, 1); 156 assertTrue(iterator.next().equals(new Integer (1))); 157 158 try { 159 iterator = IteratorUtils.arrayIterator(intArray, -1); 160 fail("Expecting IndexOutOfBoundsException"); 161 } catch (IndexOutOfBoundsException ex) { 162 } 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 ex) { 173 } 175 176 iterator = IteratorUtils.arrayIterator(intArray, 2, 3); 177 assertTrue(iterator.next().equals(new Integer (2))); 178 179 try { 180 iterator = IteratorUtils.arrayIterator(intArray, 2, 4); 181 fail("Expecting IndexOutOfBoundsException"); 182 } catch (IndexOutOfBoundsException ex) { 183 } 185 186 try { 187 iterator = IteratorUtils.arrayIterator(intArray, -1, 1); 188 fail("Expecting IndexOutOfBoundsException"); 189 } catch (IndexOutOfBoundsException ex) { 190 } 192 193 try { 194 iterator = IteratorUtils.arrayIterator(intArray, 2, 1); 195 fail("Expecting IllegalArgumentException"); 196 } catch (IllegalArgumentException ex) { 197 } 199 } 200 201 public void testArrayListIterator() { 202 Object [] 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); assertTrue(iterator.previousIndex() == 3); 217 218 try { 219 iterator = IteratorUtils.arrayListIterator(new Integer (0)); 220 fail("Expecting IllegalArgumentException"); 221 } catch (IllegalArgumentException ex) { 222 } 224 225 try { 226 iterator = IteratorUtils.arrayListIterator(null); 227 fail("Expecting NullPointerException"); 228 } catch (NullPointerException ex) { 229 } 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 ex) { 243 } 245 246 iterator = IteratorUtils.arrayListIterator(objArray, 3); 247 assertTrue(iterator.hasNext()); 248 try { 249 Object x = iterator.previous(); 250 fail("Expecting NoSuchElementException."); 251 } catch (NoSuchElementException ex) { 252 } 254 255 try { 256 iterator = IteratorUtils.arrayListIterator(objArray, 5); 257 fail("Expecting IndexOutOfBoundsException."); 258 } catch (IndexOutOfBoundsException ex) { 259 } 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 ex) { 269 } 271 272 try { 273 iterator = IteratorUtils.arrayListIterator(objArray, -1, 1); 274 fail("Expecting IndexOutOfBoundsException"); 275 } catch (IndexOutOfBoundsException ex) { 276 } 278 279 try { 280 iterator = IteratorUtils.arrayListIterator(objArray, 2, 1); 281 fail("Expecting IllegalArgumentException"); 282 } catch (IllegalArgumentException ex) { 283 } 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 (0))); 292 assertTrue(iterator.previousIndex() == 0); 293 assertTrue(iterator.nextIndex() == 1); 294 assertTrue(iterator.next().equals(new Integer (1))); 295 assertTrue(iterator.previousIndex() == 1); 296 assertTrue(iterator.nextIndex() == 2); 297 assertTrue(iterator.previous().equals(new Integer (1))); 298 assertTrue(iterator.next().equals(new Integer (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 (1))); 305 assertTrue(iterator.previous().equals(new Integer (1))); 306 assertTrue(iterator.next().equals(new Integer (1))); 307 assertTrue(iterator.previousIndex() == 0); 308 assertTrue(iterator.nextIndex() == 1); 309 assertTrue(iterator.next().equals(new Integer (2))); 310 assertTrue(iterator.previousIndex() == 1); 311 assertTrue(iterator.nextIndex() == 2); 312 assertTrue(iterator.previous().equals(new Integer (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 ex) { 320 } 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 ex) { 330 } 332 333 iterator = IteratorUtils.arrayListIterator(intArray, 2, 3); 334 assertTrue(!iterator.hasPrevious()); 335 assertTrue(iterator.previousIndex() == -1); 336 assertTrue(iterator.next().equals(new Integer (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 ex) { 345 } 347 348 try { 349 iterator = IteratorUtils.arrayListIterator(intArray, -1, 1); 350 fail("Expecting IndexOutOfBoundsException"); 351 } catch (IndexOutOfBoundsException ex) { 352 } 354 355 try { 356 iterator = IteratorUtils.arrayListIterator(intArray, 2, 1); 357 fail("Expecting IllegalArgumentException"); 358 } catch (IllegalArgumentException ex) { 359 } 361 } 362 363 364 367 private Iterator getImmutableIterator() { 368 List list = new ArrayList (); 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 379 private ListIterator getImmutableListIterator() { 380 List list = new ArrayList (); 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 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 ); 396 assertEquals(true, IteratorUtils.EMPTY_ITERATOR instanceof ResettableIterator); 397 assertEquals(false, IteratorUtils.EMPTY_ITERATOR instanceof OrderedIterator); 398 assertEquals(false, IteratorUtils.EMPTY_ITERATOR instanceof ListIterator ); 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 ex) {} 408 try { 409 IteratorUtils.EMPTY_ITERATOR.remove(); 410 fail(); 411 } catch (IllegalStateException ex) {} 412 } 413 414 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 ); 422 assertEquals(true, IteratorUtils.EMPTY_LIST_ITERATOR instanceof ListIterator ); 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 ex) {} 436 try { 437 IteratorUtils.EMPTY_LIST_ITERATOR.previous(); 438 fail(); 439 } catch (NoSuchElementException ex) {} 440 try { 441 IteratorUtils.EMPTY_LIST_ITERATOR.remove(); 442 fail(); 443 } catch (IllegalStateException ex) {} 444 try { 445 IteratorUtils.EMPTY_LIST_ITERATOR.set(null); 446 fail(); 447 } catch (IllegalStateException ex) {} 448 try { 449 IteratorUtils.EMPTY_LIST_ITERATOR.add(null); 450 fail(); 451 } catch (UnsupportedOperationException ex) {} 452 } 453 454 458 public void testEmptyMapIterator() { 459 assertSame(EmptyMapIterator.INSTANCE, IteratorUtils.EMPTY_MAP_ITERATOR); 460 assertEquals(true, IteratorUtils.EMPTY_MAP_ITERATOR instanceof Iterator ); 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 ); 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 ex) {} 474 try { 475 IteratorUtils.EMPTY_MAP_ITERATOR.remove(); 476 fail(); 477 } catch (IllegalStateException ex) {} 478 try { 479 IteratorUtils.EMPTY_MAP_ITERATOR.getKey(); 480 fail(); 481 } catch (IllegalStateException ex) {} 482 try { 483 IteratorUtils.EMPTY_MAP_ITERATOR.getValue(); 484 fail(); 485 } catch (IllegalStateException ex) {} 486 try { 487 IteratorUtils.EMPTY_MAP_ITERATOR.setValue(null); 488 fail(); 489 } catch (IllegalStateException ex) {} 490 } 491 492 496 public void testEmptyOrderedIterator() { 497 assertSame(EmptyOrderedIterator.INSTANCE, IteratorUtils.EMPTY_ORDERED_ITERATOR); 498 assertEquals(true, IteratorUtils.EMPTY_ORDERED_ITERATOR instanceof Iterator ); 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 ); 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 ex) {} 512 try { 513 IteratorUtils.EMPTY_ORDERED_ITERATOR.previous(); 514 fail(); 515 } catch (NoSuchElementException ex) {} 516 try { 517 IteratorUtils.EMPTY_ORDERED_ITERATOR.remove(); 518 fail(); 519 } catch (IllegalStateException ex) {} 520 } 521 522 526 public void testEmptyOrderedMapIterator() { 527 assertSame(EmptyOrderedMapIterator.INSTANCE, IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR); 528 assertEquals(true, IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR instanceof Iterator ); 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 ); 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 ex) {} 542 try { 543 IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.previous(); 544 fail(); 545 } catch (NoSuchElementException ex) {} 546 try { 547 IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.remove(); 548 fail(); 549 } catch (IllegalStateException ex) {} 550 try { 551 IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.getKey(); 552 fail(); 553 } catch (IllegalStateException ex) {} 554 try { 555 IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.getValue(); 556 fail(); 557 } catch (IllegalStateException ex) {} 558 try { 559 IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.setValue(null); 560 fail(); 561 } catch (IllegalStateException ex) {} 562 } 563 564 568 public void testUnmodifiableIteratorIteration() { 569 Iterator 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 594 public void testUnmodifiableListIteratorIteration() { 595 ListIterator 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 644 public void testUnmodifiableIteratorImmutability() { 645 Iterator iterator = getImmutableIterator(); 646 647 try { 648 iterator.remove(); 649 fail("remove() should throw an UnsupportedOperationException"); 651 } catch (UnsupportedOperationException e) { 652 } 654 655 iterator.next(); 656 657 try { 658 iterator.remove(); 659 fail("remove() should throw an UnsupportedOperationException"); 661 } catch (UnsupportedOperationException e) { 662 } 664 665 } 666 667 670 public void testUnmodifiableListIteratorImmutability() { 671 ListIterator listIterator = getImmutableListIterator(); 672 673 try { 674 listIterator.remove(); 675 fail("remove() should throw an UnsupportedOperationException"); 677 } catch (UnsupportedOperationException e) { 678 } 680 681 try { 682 listIterator.set("a"); 683 fail("set(Object) should throw an UnsupportedOperationException"); 685 } catch (UnsupportedOperationException e) { 686 } 688 689 try { 690 listIterator.add("a"); 691 fail("add(Object) should throw an UnsupportedOperationException"); 693 } catch (UnsupportedOperationException e) { 694 } 696 697 listIterator.next(); 698 699 try { 700 listIterator.remove(); 701 fail("remove() should throw an UnsupportedOperationException"); 703 } catch (UnsupportedOperationException e) { 704 } 706 707 try { 708 listIterator.set("a"); 709 fail("set(Object) should throw an UnsupportedOperationException"); 711 } catch (UnsupportedOperationException e) { 712 } 714 715 try { 716 listIterator.add("a"); 717 fail("add(Object) should throw an UnsupportedOperationException"); 719 } catch (UnsupportedOperationException e) { 720 } 722 } 723 } 724 | Popular Tags |