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.NoSuchElementException ; 22 23 import junit.framework.Test; 24 import junit.framework.TestCase; 25 import junit.framework.TestSuite; 26 27 34 public class TestLoopingListIterator extends TestCase { 35 36 public TestLoopingListIterator(String testName) { 37 super(testName); 38 } 39 40 public static Test suite() { 41 return new TestSuite(TestLoopingListIterator.class); 42 } 43 44 47 public void testConstructorEx() throws Exception { 48 try { 49 new LoopingListIterator(null); 50 fail(); 51 } catch (NullPointerException ex) { 52 } 53 } 54 55 58 public void testLooping0() throws Exception { 59 List list = new ArrayList (); 60 LoopingListIterator loop = new LoopingListIterator(list); 61 assertFalse(loop.hasNext()); 62 assertFalse(loop.hasPrevious()); 63 64 try { 65 loop.next(); 66 fail(); 67 } catch (NoSuchElementException ex) { 68 } 69 70 try { 71 loop.previous(); 72 fail(); 73 } catch (NoSuchElementException ex) { 74 } 75 } 76 77 81 public void testLooping1() throws Exception { 82 List list = new ArrayList (Arrays.asList(new String [] { "a" })); 83 LoopingListIterator loop = new LoopingListIterator(list); 85 assertTrue(loop.hasNext()); 86 assertEquals("a", loop.next()); 88 assertTrue(loop.hasNext()); 89 assertEquals("a", loop.next()); 91 assertTrue(loop.hasNext()); 92 assertEquals("a", loop.next()); 94 assertTrue(loop.hasPrevious()); 95 assertEquals("a", loop.previous()); 97 assertTrue(loop.hasPrevious()); 98 assertEquals("a", loop.previous()); 100 assertTrue(loop.hasPrevious()); 101 assertEquals("a", loop.previous()); } 103 104 108 public void testLooping2() throws Exception { 109 List list = new ArrayList (Arrays.asList(new String [] { "a", "b" })); 110 LoopingListIterator loop = new LoopingListIterator(list); 112 assertTrue(loop.hasNext()); 113 assertEquals("a", loop.next()); 115 assertTrue(loop.hasNext()); 116 assertEquals("b", loop.next()); 118 assertTrue(loop.hasNext()); 119 assertEquals("a", loop.next()); 121 loop.reset(); 124 assertTrue(loop.hasPrevious()); 125 assertEquals("b", loop.previous()); 127 assertTrue(loop.hasPrevious()); 128 assertEquals("a", loop.previous()); 130 assertTrue(loop.hasPrevious()); 131 assertEquals("b", loop.previous()); } 133 134 138 public void testJoggingNotOverBoundary() { 139 List list = new ArrayList (Arrays.asList(new String [] { "a", "b" })); 140 LoopingListIterator loop = new LoopingListIterator(list); 142 loop.reset(); 145 assertEquals("a", loop.next()); assertEquals("a", loop.previous()); assertEquals("a", loop.next()); 149 assertEquals("b", loop.next()); assertEquals("b", loop.previous()); assertEquals("b", loop.next()); } 153 154 158 public void testJoggingOverBoundary() { 159 List list = new ArrayList (Arrays.asList(new String [] { "a", "b" })); 160 LoopingListIterator loop = new LoopingListIterator(list); 162 assertEquals("b", loop.previous()); assertEquals("b", loop.next()); assertEquals("b", loop.previous()); 168 assertEquals("a", loop.previous()); assertEquals("a", loop.next()); assertEquals("a", loop.previous()); } 172 173 176 public void testRemovingElementsAndIteratingForward() { 177 List list = new ArrayList (Arrays.asList(new String [] { "a", "b", "c" })); 178 LoopingListIterator loop = new LoopingListIterator(list); 180 assertTrue(loop.hasNext()); 181 assertEquals("a", loop.next()); loop.remove(); assertEquals(2, list.size()); 184 185 assertTrue(loop.hasNext()); 186 assertEquals("b", loop.next()); loop.remove(); assertEquals(1, list.size()); 189 190 assertTrue(loop.hasNext()); 191 assertEquals("c", loop.next()); loop.remove(); assertEquals(0, list.size()); 194 195 assertFalse(loop.hasNext()); 196 try { 197 loop.next(); 198 fail(); 199 } catch (NoSuchElementException ex) { 200 } 201 } 202 203 206 public void testRemovingElementsAndIteratingBackwards() { 207 List list = new ArrayList (Arrays.asList(new String [] { "a", "b", "c" })); 208 LoopingListIterator loop = new LoopingListIterator(list); 210 assertTrue(loop.hasPrevious()); 211 assertEquals("c", loop.previous()); loop.remove(); assertEquals(2, list.size()); 214 215 assertTrue(loop.hasPrevious()); 216 assertEquals("b", loop.previous()); loop.remove(); assertEquals(1, list.size()); 219 220 assertTrue(loop.hasPrevious()); 221 assertEquals("a", loop.previous()); loop.remove(); assertEquals(0, list.size()); 224 225 assertFalse(loop.hasPrevious()); 226 try { 227 loop.previous(); 228 fail(); 229 } catch (NoSuchElementException ex) { 230 } 231 } 232 233 236 public void testReset() { 237 List list = new ArrayList (Arrays.asList(new String [] { "a", "b", "c" })); 238 LoopingListIterator loop = new LoopingListIterator(list); 240 assertEquals("a", loop.next()); assertEquals("b", loop.next()); loop.reset(); assertEquals("a", loop.next()); loop.reset(); assertEquals("a", loop.next()); assertEquals("b", loop.next()); assertEquals("c", loop.next()); loop.reset(); 250 assertEquals("c", loop.previous()); assertEquals("b", loop.previous()); loop.reset(); assertEquals("c", loop.previous()); loop.reset(); assertEquals("c", loop.previous()); assertEquals("b", loop.previous()); assertEquals("a", loop.previous()); } 259 260 263 public void testAdd() { 264 List list = new ArrayList (Arrays.asList(new String [] { "b", "e", "f" })); 265 LoopingListIterator loop = new LoopingListIterator(list); 267 loop.add("a"); assertEquals("b", loop.next()); loop.reset(); assertEquals("a", loop.next()); assertEquals("b", loop.next()); 273 loop.add("c"); assertEquals("e", loop.next()); assertEquals("e", loop.previous()); assertEquals("c", loop.previous()); assertEquals("c", loop.next()); 279 loop.add("d"); loop.reset(); assertEquals("a", loop.next()); assertEquals("b", loop.next()); assertEquals("c", loop.next()); assertEquals("d", loop.next()); assertEquals("e", loop.next()); assertEquals("f", loop.next()); assertEquals("a", loop.next()); 289 list = new ArrayList (Arrays.asList(new String [] { "b", "e", "f" })); 290 loop = new LoopingListIterator(list); 292 loop.add("a"); assertEquals("a", loop.previous()); loop.reset(); assertEquals("f", loop.previous()); assertEquals("e", loop.previous()); 298 loop.add("d"); assertEquals("d", loop.previous()); 301 loop.add("c"); assertEquals("c", loop.previous()); 304 loop.reset(); 305 assertEquals("a", loop.next()); assertEquals("b", loop.next()); assertEquals("c", loop.next()); assertEquals("d", loop.next()); assertEquals("e", loop.next()); assertEquals("f", loop.next()); assertEquals("a", loop.next()); } 313 314 317 public void testNextAndPreviousIndex() { 318 List list = new ArrayList (Arrays.asList(new String [] { "a", "b", "c" })); 319 LoopingListIterator loop = new LoopingListIterator(list); 321 assertEquals(0, loop.nextIndex()); 322 assertEquals(2, loop.previousIndex()); 323 324 assertEquals("a", loop.next()); assertEquals(1, loop.nextIndex()); 326 assertEquals(0, loop.previousIndex()); 327 328 assertEquals("a", loop.previous()); assertEquals(0, loop.nextIndex()); 330 assertEquals(2, loop.previousIndex()); 331 332 assertEquals("c", loop.previous()); assertEquals(2, loop.nextIndex()); 334 assertEquals(1, loop.previousIndex()); 335 336 assertEquals("b", loop.previous()); assertEquals(1, loop.nextIndex()); 338 assertEquals(0, loop.previousIndex()); 339 340 assertEquals("a", loop.previous()); assertEquals(0, loop.nextIndex()); 342 assertEquals(2, loop.previousIndex()); 343 } 344 345 348 public void testSet() { 349 List list = new ArrayList (Arrays.asList(new String [] { "q", "r", "z" })); 350 LoopingListIterator loop = new LoopingListIterator(list); 352 assertEquals("z", loop.previous()); loop.set("c"); 355 loop.reset(); assertEquals("q", loop.next()); loop.set("a"); 359 assertEquals("r", loop.next()); loop.set("b"); 362 loop.reset(); assertEquals("a", loop.next()); assertEquals("b", loop.next()); assertEquals("c", loop.next()); } 367 368 } 369 | Popular Tags |