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 33 public class TestObjectArrayListIterator extends TestObjectArrayIterator { 34 35 public TestObjectArrayListIterator(String testName) { 36 super(testName); 37 } 38 39 public static Test suite() { 40 return new TestSuite(TestObjectArrayListIterator.class); 41 } 42 43 public Iterator makeEmptyIterator() { 44 return new ObjectArrayListIterator(new Object [0]); 45 } 46 47 public Iterator makeFullIterator() { 48 return new ObjectArrayListIterator(testArray); 49 } 50 51 public ListIterator makeArrayListIterator(Object [] array) { 52 return new ObjectArrayListIterator(array); 53 } 54 55 59 public void testListIterator() { 60 ListIterator iter = (ListIterator ) makeFullIterator(); 61 62 65 while (iter.hasNext()) { 67 iter.next(); 68 } 69 70 for (int x = testArray.length - 1; x >= 0; x--) { 71 Object testValue = testArray[x]; 72 Object iterValue = iter.previous(); 73 74 assertEquals("Iteration value is correct", testValue, iterValue); 75 } 76 77 assertTrue("Iterator should now be empty", !iter.hasPrevious()); 78 79 try { 80 Object testValue = iter.previous(); 81 } catch (Exception e) { 82 assertTrue( 83 "NoSuchElementException must be thrown", 84 e.getClass().equals((new NoSuchElementException ()).getClass())); 85 } 86 87 } 88 89 92 public void testListIteratorSet() { 93 String [] testData = new String [] { "a", "b", "c" }; 94 95 String [] result = new String [] { "0", "1", "2" }; 96 97 ListIterator iter = (ListIterator ) makeArrayListIterator(testData); 98 int x = 0; 99 100 while (iter.hasNext()) { 101 iter.next(); 102 iter.set(Integer.toString(x)); 103 x++; 104 } 105 106 assertTrue("The two arrays should have the same value, i.e. {0,1,2}", Arrays.equals(testData, result)); 107 108 iter = makeArrayListIterator(testArray); 110 111 try { 112 iter.set("should fail"); 113 fail("ListIterator#set should fail if next() or previous() have not yet been called."); 114 } catch (IllegalStateException e) { 115 } catch (Throwable t) { fail(t.toString()); 118 } 119 120 } 121 122 } 123 | Popular Tags |