1 16 package org.apache.commons.collections.iterators; 17 18 import java.util.ArrayList ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.NoSuchElementException ; 22 23 import junit.framework.Test; 24 import junit.framework.TestSuite; 25 26 35 public class TestIteratorChain extends AbstractTestIterator { 36 37 protected String [] testArray = { 38 "One", "Two", "Three", "Four", "Five", "Six" 39 }; 40 41 protected List list1 = null; 42 protected List list2 = null; 43 protected List list3 = null; 44 45 public static Test suite() { 46 return new TestSuite(TestIteratorChain.class); 47 } 48 49 public TestIteratorChain(String testName) { 50 super(testName); 51 } 52 53 public void setUp() { 54 list1 = new ArrayList (); 55 list1.add("One"); 56 list1.add("Two"); 57 list1.add("Three"); 58 list2 = new ArrayList (); 59 list2.add("Four"); 60 list3 = new ArrayList (); 61 list3.add("Five"); 62 list3.add("Six"); 63 } 64 65 public Iterator makeEmptyIterator() { 66 ArrayList list = new ArrayList (); 67 return new IteratorChain(list.iterator()); 68 } 69 70 public Iterator makeFullIterator() { 71 IteratorChain chain = new IteratorChain(); 72 73 chain.addIterator(list1.iterator()); 74 chain.addIterator(list2.iterator()); 75 chain.addIterator(list3.iterator()); 76 return chain; 77 } 78 79 public void testIterator() { 80 Iterator iter = (Iterator ) makeFullIterator(); 81 for ( int i = 0; i < testArray.length; i++ ) { 82 Object testValue = testArray[i]; 83 Object iterValue = iter.next(); 84 85 assertEquals( "Iteration value is correct", testValue, iterValue ); 86 } 87 88 assertTrue("Iterator should now be empty", ! iter.hasNext() ); 89 90 try { 91 Object testValue = iter.next(); 92 } catch (Exception e) { 93 assertTrue("NoSuchElementException must be thrown", 94 e.getClass().equals((new NoSuchElementException ()).getClass())); 95 } 96 } 97 98 public void testRemove() { 99 Iterator iter = (Iterator ) makeFullIterator(); 100 101 try { 102 iter.remove(); 103 fail("Calling remove before the first call to next() should throw an exception"); 104 } catch (IllegalStateException e) { 105 106 } 107 108 for ( int i = 0; i < testArray.length; i++ ) { 109 Object testValue = testArray[i]; 110 Object iterValue = iter.next(); 111 112 assertEquals( "Iteration value is correct", testValue, iterValue ); 113 114 if (! iterValue.equals("Four")) { 115 iter.remove(); 116 } 117 } 118 119 assertTrue("List is empty",list1.size() == 0); 120 assertTrue("List is empty",list2.size() == 1); 121 assertTrue("List is empty",list3.size() == 0); 122 } 123 124 public void testFirstIteratorIsEmptyBug() { 125 List empty = new ArrayList (); 126 List notEmpty = new ArrayList (); 127 notEmpty.add("A"); 128 notEmpty.add("B"); 129 notEmpty.add("C"); 130 IteratorChain chain = new IteratorChain(); 131 chain.addIterator(empty.iterator()); 132 chain.addIterator(notEmpty.iterator()); 133 assertTrue("should have next",chain.hasNext()); 134 assertEquals("A",chain.next()); 135 assertTrue("should have next",chain.hasNext()); 136 assertEquals("B",chain.next()); 137 assertTrue("should have next",chain.hasNext()); 138 assertEquals("C",chain.next()); 139 assertTrue("should not have next",!chain.hasNext()); 140 } 141 142 public void testEmptyChain() { 143 IteratorChain chain = new IteratorChain(); 144 assertEquals(false, chain.hasNext()); 145 try { 146 chain.next(); 147 fail(); 148 } catch (NoSuchElementException ex) {} 149 try { 150 chain.remove(); 151 fail(); 152 } catch (IllegalStateException ex) {} 153 } 154 155 } 156 | Popular Tags |