1 16 package org.apache.commons.collections.iterators; 17 18 import java.util.ListIterator ; 19 import java.util.NoSuchElementException ; 20 21 import junit.framework.Test; 22 import junit.framework.TestSuite; 23 24 import org.apache.commons.collections.ResettableListIterator; 25 26 33 public class TestSingletonListIterator extends AbstractTestListIterator { 34 35 private static final Object testValue = "foo"; 36 37 public static Test suite() { 38 return new TestSuite(TestSingletonListIterator.class); 39 } 40 41 public TestSingletonListIterator(String testName) { 42 super(testName); 43 } 44 45 49 public ListIterator makeEmptyListIterator() { 50 SingletonListIterator iter = (SingletonListIterator)makeFullIterator(); 51 iter.next(); 52 iter.remove(); 53 iter.reset(); 54 return iter; 55 } 56 57 public ListIterator makeFullListIterator() { 58 return new SingletonListIterator( testValue ); 59 } 60 61 public boolean supportsAdd() { 62 return false; 63 } 64 65 public boolean supportsRemove() { 66 return true; 67 } 68 69 public boolean supportsEmptyIterator() { 70 return true; 71 } 72 73 public void testIterator() { 74 ListIterator iter = (ListIterator ) makeObject(); 75 assertTrue( "Iterator should have next item", iter.hasNext() ); 76 assertTrue( "Iterator should have no previous item", !iter.hasPrevious() ); 77 assertEquals( "Iteration next index", 0, iter.nextIndex() ); 78 assertEquals( "Iteration previous index", -1, iter.previousIndex() ); 79 80 Object iterValue = iter.next(); 81 assertEquals( "Iteration value is correct", testValue, iterValue ); 82 83 assertTrue( "Iterator should have no next item", !iter.hasNext() ); 84 assertTrue( "Iterator should have previous item", iter.hasPrevious() ); 85 assertEquals( "Iteration next index", 1, iter.nextIndex() ); 86 assertEquals( "Iteration previous index", 0, iter.previousIndex() ); 87 88 iterValue = iter.previous(); 89 assertEquals( "Iteration value is correct", testValue, iterValue ); 90 91 assertTrue( "Iterator should have next item", iter.hasNext() ); 92 assertTrue( "Iterator should have no previous item", !iter.hasPrevious() ); 93 assertEquals( "Iteration next index", 0, iter.nextIndex() ); 94 assertEquals( "Iteration previous index", -1, iter.previousIndex() ); 95 96 iterValue = iter.next(); 97 assertEquals( "Iteration value is correct", testValue, iterValue ); 98 99 assertTrue( "Iterator should have no next item", !iter.hasNext() ); 100 assertTrue( "Iterator should have previous item", iter.hasPrevious() ); 101 assertEquals( "Iteration next index", 1, iter.nextIndex() ); 102 assertEquals( "Iteration previous index", 0, iter.previousIndex() ); 103 104 try { 105 iter.next(); 106 } catch (Exception e) { 107 assertTrue("NoSuchElementException must be thrown", 108 e.getClass().equals((new NoSuchElementException ()).getClass())); 109 } 110 iter.previous(); 111 try { 112 iter.previous(); 113 } catch (Exception e) { 114 assertTrue("NoSuchElementException must be thrown", 115 e.getClass().equals((new NoSuchElementException ()).getClass())); 116 } 117 } 118 119 public void testReset() { 120 ResettableListIterator it = (ResettableListIterator) makeObject(); 121 122 assertEquals(true, it.hasNext()); 123 assertEquals(false, it.hasPrevious()); 124 assertEquals(testValue, it.next()); 125 assertEquals(false, it.hasNext()); 126 assertEquals(true, it.hasPrevious()); 127 128 it.reset(); 129 130 assertEquals(true, it.hasNext()); 131 assertEquals(false, it.hasPrevious()); 132 assertEquals(testValue, it.next()); 133 assertEquals(false, it.hasNext()); 134 assertEquals(true, it.hasPrevious()); 135 136 it.reset(); 137 it.reset(); 138 139 assertEquals(true, it.hasNext()); 140 } 141 142 } 143 144 | Popular Tags |