1 16 package org.apache.commons.collections.iterators; 17 18 import java.util.ArrayList ; 19 import java.util.Iterator ; 20 import java.util.ListIterator ; 21 import java.util.NoSuchElementException ; 22 23 37 public abstract class AbstractTestListIterator extends AbstractTestIterator { 38 39 44 public AbstractTestListIterator(String testName) { 45 super(testName); 46 } 47 48 54 public abstract ListIterator makeEmptyListIterator(); 55 56 61 public abstract ListIterator makeFullListIterator(); 62 63 68 public Iterator makeEmptyIterator() { 69 return makeEmptyListIterator(); 70 } 71 72 77 public Iterator makeFullIterator() { 78 return makeFullListIterator(); 79 } 80 81 87 public boolean supportsAdd() { 88 return true; 89 } 90 91 97 public boolean supportsSet() { 98 return true; 99 } 100 101 105 public Object addSetValue() { 106 return null; 107 } 108 109 113 public void testEmptyListIteratorIsIndeedEmpty() { 114 if (supportsEmptyIterator() == false) { 115 return; 116 } 117 118 ListIterator it = makeEmptyListIterator(); 119 120 assertEquals(false, it.hasNext()); 121 assertEquals(0, it.nextIndex()); 122 assertEquals(false, it.hasPrevious()); 123 assertEquals(-1, it.previousIndex()); 124 125 try { 127 it.next(); 128 fail("NoSuchElementException must be thrown from empty ListIterator"); 129 } catch (NoSuchElementException e) { 130 } 131 132 try { 134 it.previous(); 135 fail("NoSuchElementException must be thrown from empty ListIterator"); 136 } catch (NoSuchElementException e) { 137 } 138 } 139 140 143 public void testWalkForwardAndBack() { 144 ArrayList list = new ArrayList (); 145 ListIterator it = makeFullListIterator(); 146 while (it.hasNext()) { 147 list.add(it.next()); 148 } 149 150 assertEquals(false, it.hasNext()); 152 assertEquals(true, it.hasPrevious()); 153 try { 154 it.next(); 155 fail("NoSuchElementException must be thrown from next at end of ListIterator"); 156 } catch (NoSuchElementException e) { 157 } 158 159 for (int i = list.size() - 1; i >= 0; i--) { 161 assertEquals(i + 1, it.nextIndex()); 162 assertEquals(i, it.previousIndex()); 163 164 Object obj = list.get(i); 165 assertEquals(obj, it.previous()); 166 } 167 168 assertEquals(true, it.hasNext()); 170 assertEquals(false, it.hasPrevious()); 171 try { 172 it.previous(); 173 fail("NoSuchElementException must be thrown from previous at start of ListIterator"); 174 } catch (NoSuchElementException e) { 175 } 176 } 177 178 181 public void testAdd() { 182 ListIterator it = makeFullListIterator(); 183 184 Object addValue = addSetValue(); 185 if (supportsAdd() == false) { 186 try { 188 it.add(addValue); 189 } catch (UnsupportedOperationException ex) {} 190 return; 191 } 192 193 it = makeFullListIterator(); 195 it.add(addValue); 196 assertEquals(addValue, it.previous()); 197 198 it = makeFullListIterator(); 200 it.add(addValue); 201 assertTrue(addValue != it.next()); 202 203 it = makeFullListIterator(); 205 while (it.hasNext()) { 206 it.next(); 207 it.add(addValue); 208 assertEquals(addValue, it.previous()); 210 it.next(); 211 } 212 } 213 214 217 public void testSet() { 218 ListIterator it = makeFullListIterator(); 219 220 if (supportsSet() == false) { 221 try { 223 it.set(addSetValue()); 224 } catch (UnsupportedOperationException ex) {} 225 return; 226 } 227 228 try { 230 it.set(addSetValue()); 231 fail(); 232 } catch (IllegalStateException ex) {} 233 234 it.next(); 236 it.set(addSetValue()); 237 238 it.set(addSetValue()); 240 241 } 242 243 public void testRemoveThenSet() { 244 ListIterator it = makeFullListIterator(); 245 if (supportsRemove() && supportsSet()) { 246 it.next(); 247 it.remove(); 248 try { 249 it.set(addSetValue()); 250 fail("IllegalStateException must be thrown from set after remove"); 251 } catch (IllegalStateException e) { 252 } 253 } 254 } 255 256 public void testAddThenSet() { 257 ListIterator it = makeFullListIterator(); 258 if (supportsAdd() && supportsSet()) { 260 it.next(); 261 it.add(addSetValue()); 262 try { 263 it.set(addSetValue()); 264 fail("IllegalStateException must be thrown from set after add"); 265 } catch (IllegalStateException e) { 266 } 267 } 268 } 269 270 273 public void testAddThenRemove() { 274 ListIterator it = makeFullListIterator(); 275 276 if (supportsAdd() && supportsRemove()) { 278 it.next(); 279 it.add(addSetValue()); 280 try { 281 it.remove(); 282 fail("IllegalStateException must be thrown from remove after add"); 283 } catch (IllegalStateException e) { 284 } 285 } 286 } 287 288 } 289 | Popular Tags |