1 16 package org.apache.commons.collections.iterators; 17 18 import java.util.Arrays ; 19 import java.util.Iterator ; 20 import java.util.ListIterator ; 21 import java.util.NoSuchElementException ; 22 23 import junit.framework.Test; 24 import junit.framework.TestSuite; 25 26 32 public class TestArrayListIterator extends TestArrayIterator { 33 34 public TestArrayListIterator(String testName) { 35 super(testName); 36 } 37 38 public static Test suite() { 39 return new TestSuite(TestArrayListIterator.class); 40 } 41 42 public Iterator makeEmptyIterator() { 43 return new ArrayListIterator(new Object [0]); 44 } 45 46 public Iterator makeFullIterator() { 47 return new ArrayListIterator(testArray); 48 } 49 50 public ListIterator makeArrayListIterator(Object array) { 51 return new ArrayListIterator(array); 52 } 53 54 public boolean supportsRemove() { 55 return false; 56 } 57 58 62 public void testListIterator() { 63 ListIterator iter = (ListIterator ) makeFullIterator(); 64 65 68 while (iter.hasNext()) { 70 iter.next(); 71 } 72 73 for (int x = testArray.length - 1; x >= 0; x--) { 74 Object testValue = testArray[x]; 75 Object iterValue = iter.previous(); 76 77 assertEquals("Iteration value is correct", testValue, iterValue); 78 } 79 80 assertTrue("Iterator should now be empty", !iter.hasPrevious()); 81 82 try { 83 Object testValue = iter.previous(); 84 } catch (Exception e) { 85 assertTrue( 86 "NoSuchElementException must be thrown", 87 e.getClass().equals((new NoSuchElementException ()).getClass())); 88 } 89 90 } 91 92 95 public void testListIteratorSet() { 96 String [] testData = new String [] { "a", "b", "c" }; 97 98 String [] result = new String [] { "0", "1", "2" }; 99 100 ListIterator iter = (ListIterator ) makeArrayListIterator(testData); 101 int x = 0; 102 103 while (iter.hasNext()) { 104 iter.next(); 105 iter.set(Integer.toString(x)); 106 x++; 107 } 108 109 assertTrue("The two arrays should have the same value, i.e. {0,1,2}", Arrays.equals(testData, result)); 110 111 iter = makeArrayListIterator(testArray); 113 114 try { 115 iter.set("should fail"); 116 fail("ListIterator#set should fail if next() or previous() have not yet been called."); 117 } catch (IllegalStateException e) { 118 } catch (Throwable t) { fail(t.toString()); 121 } 122 123 } 124 125 } 126 | Popular Tags |