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 35 public class TestLoopingIterator extends TestCase { 36 37 public TestLoopingIterator(String testName) { 38 super(testName); 39 } 40 41 public static Test suite() { 42 return new TestSuite(TestLoopingIterator.class); 43 } 44 45 48 public void testConstructorEx() throws Exception { 49 try { 50 new LoopingIterator(null); 51 fail(); 52 } catch (NullPointerException ex) { 53 } 54 } 55 56 60 public void testLooping0() throws Exception { 61 List list = new ArrayList (); 62 LoopingIterator loop = new LoopingIterator(list); 63 assertTrue("hasNext should return false", loop.hasNext() == false); 64 65 try { 66 loop.next(); 67 fail("NoSuchElementException was not thrown during next() call."); 68 } catch (NoSuchElementException ex) { 69 } 70 } 71 72 76 public void testLooping1() throws Exception { 77 List list = new ArrayList (Arrays.asList(new String [] { "a" })); 78 LoopingIterator loop = new LoopingIterator(list); 79 80 assertTrue("1st hasNext should return true", loop.hasNext()); 81 assertEquals("a", loop.next()); 82 83 assertTrue("2nd hasNext should return true", loop.hasNext()); 84 assertEquals("a", loop.next()); 85 86 assertTrue("3rd hasNext should return true", loop.hasNext()); 87 assertEquals("a", loop.next()); 88 89 } 90 91 95 public void testLooping2() throws Exception { 96 List list = new ArrayList (Arrays.asList(new String [] { "a", "b" })); 97 LoopingIterator loop = new LoopingIterator(list); 98 99 assertTrue("1st hasNext should return true", loop.hasNext()); 100 assertEquals("a", loop.next()); 101 102 assertTrue("2nd hasNext should return true", loop.hasNext()); 103 assertEquals("b", loop.next()); 104 105 assertTrue("3rd hasNext should return true", loop.hasNext()); 106 assertEquals("a", loop.next()); 107 108 } 109 110 114 public void testLooping3() throws Exception { 115 List list = new ArrayList (Arrays.asList(new String [] { "a", "b", "c" })); 116 LoopingIterator loop = new LoopingIterator(list); 117 118 assertTrue("1st hasNext should return true", loop.hasNext()); 119 assertEquals("a", loop.next()); 120 121 assertTrue("2nd hasNext should return true", loop.hasNext()); 122 assertEquals("b", loop.next()); 123 124 assertTrue("3rd hasNext should return true", loop.hasNext()); 125 assertEquals("c", loop.next()); 126 127 assertTrue("4th hasNext should return true", loop.hasNext()); 128 assertEquals("a", loop.next()); 129 130 } 131 132 136 public void testRemoving1() throws Exception { 137 List list = new ArrayList (Arrays.asList(new String [] { "a", "b", "c" })); 138 LoopingIterator loop = new LoopingIterator(list); 139 assertEquals("list should have 3 elements.", 3, list.size()); 140 141 assertTrue("1st hasNext should return true", loop.hasNext()); 142 assertEquals("a", loop.next()); 143 loop.remove(); assertEquals("list should have 2 elements.", 2, list.size()); 145 146 assertTrue("2nd hasNext should return true", loop.hasNext()); 147 assertEquals("b", loop.next()); 148 loop.remove(); assertEquals("list should have 1 elements.", 1, list.size()); 150 151 assertTrue("3rd hasNext should return true", loop.hasNext()); 152 assertEquals("c", loop.next()); 153 loop.remove(); assertEquals("list should have 0 elements.", 0, list.size()); 155 156 assertTrue("4th hasNext should return false", loop.hasNext() == false); 157 try { 158 loop.next(); 159 fail("Expected NoSuchElementException to be thrown."); 160 } catch (NoSuchElementException ex) { 161 } 162 } 163 164 168 public void testReset() throws Exception { 169 List list = new ArrayList (Arrays.asList(new String [] { "a", "b", "c" })); 170 LoopingIterator loop = new LoopingIterator(list); 171 172 assertEquals("a", loop.next()); 173 assertEquals("b", loop.next()); 174 loop.reset(); 175 assertEquals("a", loop.next()); 176 loop.reset(); 177 assertEquals("a", loop.next()); 178 assertEquals("b", loop.next()); 179 assertEquals("c", loop.next()); 180 loop.reset(); 181 assertEquals("a", loop.next()); 182 assertEquals("b", loop.next()); 183 assertEquals("c", loop.next()); 184 } 185 186 190 public void testSize() throws Exception { 191 List list = new ArrayList (Arrays.asList(new String [] { "a", "b", "c" })); 192 LoopingIterator loop = new LoopingIterator(list); 193 194 assertEquals(3, loop.size()); 195 loop.next(); 196 loop.next(); 197 assertEquals(3, loop.size()); 198 loop.reset(); 199 assertEquals(3, loop.size()); 200 loop.next(); 201 loop.remove(); 202 assertEquals(2, loop.size()); 203 } 204 205 } 206 | Popular Tags |