1 16 package org.apache.commons.collections.iterators; 17 18 import java.util.ArrayList ; 19 import java.util.Arrays ; 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 import junit.textui.TestRunner; 27 28 import org.apache.commons.collections.ResettableListIterator; 29 30 35 public class TestReverseListIterator extends AbstractTestListIterator { 36 37 protected String [] testArray = { "One", "Two", "Three", "Four" }; 38 39 public static void main(String args[]) { 40 String [] testCaseName = { TestReverseListIterator.class.getName() }; 41 TestRunner.main(testCaseName); 42 } 43 44 public static Test suite() { 45 return new TestSuite(TestReverseListIterator.class); 46 } 47 48 public TestReverseListIterator(String testName) { 49 super(testName); 50 } 51 52 public ListIterator makeEmptyListIterator() { 53 List list = new ArrayList (); 54 return new ReverseListIterator(list); 55 } 56 57 public ListIterator makeFullListIterator() { 58 List list = new ArrayList (Arrays.asList(testArray)); 59 return new ReverseListIterator(list); 60 } 61 62 public void testEmptyListIteratorIsIndeedEmpty() { 65 ListIterator it = makeEmptyListIterator(); 66 67 assertEquals(false, it.hasNext()); 68 assertEquals(-1, it.nextIndex()); assertEquals(false, it.hasPrevious()); 70 assertEquals(0, it.previousIndex()); 72 try { 74 it.next(); 75 fail("NoSuchElementException must be thrown from empty ListIterator"); 76 } catch (NoSuchElementException e) { 77 } 78 79 try { 81 it.previous(); 82 fail("NoSuchElementException must be thrown from empty ListIterator"); 83 } catch (NoSuchElementException e) { 84 } 85 } 86 87 public void testWalkForwardAndBack() { 88 ArrayList list = new ArrayList (); 89 ListIterator it = makeFullListIterator(); 90 while (it.hasNext()) { 91 list.add(it.next()); 92 } 93 94 assertEquals(false, it.hasNext()); 96 assertEquals(true, it.hasPrevious()); 97 98 107 for (int i = list.size() - 1; i >= 0; i--) { 109 assertEquals("" + i, list.size() - i - 2, it.nextIndex()); assertEquals(list.size() - i - 1, it.previousIndex()); 112 Object obj = list.get(i); 113 assertEquals(obj, it.previous()); 114 } 115 116 assertEquals(true, it.hasNext()); 118 assertEquals(false, it.hasPrevious()); 119 try { 120 it.previous(); 121 fail("NoSuchElementException must be thrown from previous at start of ListIterator"); 122 } catch (NoSuchElementException e) { 123 } 124 } 125 126 public void testReverse() { 128 ListIterator it = makeFullListIterator(); 129 assertEquals(true, it.hasNext()); 130 assertEquals(3, it.nextIndex()); 131 assertEquals(false, it.hasPrevious()); 132 assertEquals(4, it.previousIndex()); 133 assertEquals("Four", it.next()); 134 assertEquals(2, it.nextIndex()); 135 assertEquals(true, it.hasNext()); 136 assertEquals(3, it.previousIndex()); 137 assertEquals(true, it.hasPrevious()); 138 assertEquals("Three", it.next()); 139 assertEquals(true, it.hasNext()); 140 assertEquals(1, it.nextIndex()); 141 assertEquals(true, it.hasPrevious()); 142 assertEquals(2, it.previousIndex()); 143 assertEquals("Two", it.next()); 144 assertEquals(true, it.hasNext()); 145 assertEquals(0, it.nextIndex()); 146 assertEquals(true, it.hasPrevious()); 147 assertEquals(1, it.previousIndex()); 148 assertEquals("One", it.next()); 149 assertEquals(false, it.hasNext()); 150 assertEquals(-1, it.nextIndex()); 151 assertEquals(true, it.hasPrevious()); 152 assertEquals(0, it.previousIndex()); 153 assertEquals("One", it.previous()); 154 assertEquals("Two", it.previous()); 155 assertEquals("Three", it.previous()); 156 assertEquals("Four", it.previous()); 157 } 158 159 public void testReset() { 160 ResettableListIterator it = (ResettableListIterator) makeFullListIterator(); 161 assertEquals("Four", it.next()); 162 it.reset(); 163 assertEquals("Four", it.next()); 164 it.next(); 165 it.next(); 166 it.reset(); 167 assertEquals("Four", it.next()); 168 } 169 170 } 171 | Popular Tags |