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.ListIterator ; 22 import java.util.NoSuchElementException ; 23 24 import junit.framework.Test; 25 import junit.framework.TestSuite; 26 27 35 public class TestListIteratorWrapper extends AbstractTestIterator { 36 37 protected String [] testArray = { 38 "One", "Two", "Three", "Four", "Five", "Six" 39 }; 40 41 protected List list1 = null; 42 43 public static Test suite() { 44 return new TestSuite(TestListIteratorWrapper.class); 45 } 46 47 public TestListIteratorWrapper(String testName) { 48 super(testName); 49 } 50 51 public void setUp() { 52 list1 = new ArrayList (); 53 list1.add("One"); 54 list1.add("Two"); 55 list1.add("Three"); 56 list1.add("Four"); 57 list1.add("Five"); 58 list1.add("Six"); 59 } 60 61 public Iterator makeEmptyIterator() { 62 ArrayList list = new ArrayList (); 63 return new ListIteratorWrapper(list.iterator()); 64 } 65 66 public Iterator makeFullIterator() { 67 Iterator i = list1.iterator(); 68 69 return new ListIteratorWrapper(i); 70 } 71 72 public void testIterator() { 73 ListIterator iter = (ListIterator ) makeFullIterator(); 74 for ( int i = 0; i < testArray.length; i++ ) { 75 Object testValue = testArray[i]; 76 Object iterValue = iter.next(); 77 78 assertEquals( "Iteration value is correct", testValue, iterValue ); 79 } 80 81 assertTrue("Iterator should now be empty", ! iter.hasNext() ); 82 83 try { 84 Object testValue = iter.next(); 85 } catch (Exception e) { 86 assertTrue("NoSuchElementException must be thrown", 87 e.getClass().equals((new NoSuchElementException ()).getClass())); 88 } 89 90 for (int i = testArray.length - 1; i > -1; --i) { 92 Object testValue = testArray[i]; 93 Object iterValue = iter.previous(); 94 95 assertEquals( "Iteration value is correct", testValue, iterValue ); 96 } 97 98 try { 99 Object testValue = iter.previous(); 100 } catch (Exception e) { 101 assertTrue("NoSuchElementException must be thrown", 102 e.getClass().equals((new NoSuchElementException ()).getClass())); 103 } 104 105 for ( int i = 0; i < testArray.length; i++ ) { 107 Object testValue = testArray[i]; 108 Object iterValue = iter.next(); 109 110 assertEquals( "Iteration value is correct", testValue, iterValue ); 111 } 112 113 } 114 115 public void testRemove() { 116 Iterator iter = (Iterator ) makeFullIterator(); 117 118 try { 119 iter.remove(); 120 fail("FilterIterator does not support the remove() method"); 121 } catch (UnsupportedOperationException e) { 122 123 } 124 125 } 126 127 } 128 129 | Popular Tags |