1 package org.tigris.scarab.util; 2 3 48 49 import java.util.ArrayList ; 50 import java.util.Collection ; 51 import java.util.Iterator ; 52 import java.util.NoSuchElementException ; 53 54 59 public class SubsetIteratorWithSizeTest extends SubsetIteratorTest 60 { 61 protected static final String FIRST = "first element"; 62 protected static final String SECOND = "second element"; 63 protected static final String THIRD = "third element"; 64 protected static final String FOURTH = "fourth element"; 65 protected static final String LAST = "last element"; 66 67 protected static final int SIZE = 2; 68 protected static final int OFFSET = 2; 69 70 71 public SubsetIteratorWithSizeTest(String testName) 72 { 73 super(testName); 74 } 75 78 class MockIteratorWithSize implements IteratorWithSize 79 { 80 private Collection coll; 81 private Iterator it; 82 83 public MockIteratorWithSize(Collection coll) 84 { 85 this.coll = coll; 86 this.it = coll.iterator(); 87 } 88 89 92 public int size() 93 { 94 return coll.size(); 95 } 96 97 100 public void remove() 101 { 102 it.remove(); 103 } 104 105 108 public boolean hasNext() 109 { 110 return it.hasNext(); 111 } 112 113 116 public Object next() 117 { 118 return it.next(); 119 } 120 121 } 122 123 public void setUp() 124 { 125 Collection coll = new ArrayList (); 126 127 coll.add(FIRST); 128 coll.add(SECOND); 129 coll.add(THIRD); 130 coll.add(FOURTH); 131 coll.add(LAST); 132 133 subsetIterator = 135 new SubsetIteratorWithSize( 136 new MockIteratorWithSize(coll), 137 OFFSET, 138 SIZE); 139 140 subsetIteratorUntilLast = 142 new SubsetIteratorWithSize(new MockIteratorWithSize(coll), OFFSET); 143 } 144 145 public void tearDown() 146 { 147 subsetIterator = null; 148 } 149 150 public void testConstructor() 151 { 152 assertEquals( 153 "Could not fetch third element", 154 subsetIterator.next(), 155 THIRD); 156 assertEquals( 157 "SubsetIterator has wrong size", 158 SIZE, 159 ((SubsetIteratorWithSize) subsetIterator).size()); 160 161 assertTrue( 162 "Filled collection should have next element", 163 subsetIterator.hasNext()); 164 165 subsetIterator.remove(); assertEquals( 167 "Could not fetch fourth element", 168 subsetIterator.next(), 169 FOURTH); 170 assertFalse( 171 "SubsetIterator on last position should not have next element", 172 subsetIterator.hasNext()); 173 } 174 175 public void testConstructorWithoutElements() 176 { 177 assertEquals( 178 "Could not fetch third element", 179 subsetIteratorUntilLast.next(), 180 THIRD); 181 assertEquals( 182 "SubsetIterator has wrong size", 183 3, 184 ((SubsetIteratorWithSize) subsetIteratorUntilLast).size()); 185 186 assertTrue( 187 "Filled collection should have next element", 188 subsetIteratorUntilLast.hasNext()); 189 190 subsetIteratorUntilLast.remove(); assertEquals( 192 "Could not fetch fourth element", 193 subsetIteratorUntilLast.next(), 194 FOURTH); 195 assertEquals( 196 "Could not fetch last element", 197 subsetIteratorUntilLast.next(), 198 LAST); 199 assertFalse( 200 "SubsetIterator on last position should not have next element", 201 subsetIteratorUntilLast.hasNext()); 202 } 203 204 public void testConstructorWithEmptyIteratorWithSize() 205 { 206 SubsetIteratorWithSize i = 207 new SubsetIteratorWithSize(IteratorWithSize.EMPTY, 0, 0); 208 209 assertFalse( 210 "Iterator on empty should not have next element", 211 i.hasNext()); 212 213 try 214 { 215 i.next(); 216 fail("Iterator on empty should raise an exception on next()"); 217 } 218 catch (NoSuchElementException e) 219 { 220 } 222 catch (Exception e) 223 { 224 fail( 225 "Iterator on empty should raise NoSuchElementException on next() and not " 226 + e.getClass().getName()); 227 } 228 229 try 230 { 231 i.remove(); 232 fail("Iterator on empty should raise an exception on remove()"); 233 } 234 catch (IllegalStateException e) 235 { 236 } 238 catch (Exception e) 239 { 240 fail( 241 "Iterator on empty should raise IllegalStateException on remove() and not " 242 + e.getClass().getName()); 243 } 244 } 245 246 public void testConstructorWithBiggerSubsetThanTheOriginal() 247 { 248 Collection coll = new ArrayList (); 249 250 coll.add(FIRST); 251 coll.add(SECOND); 252 coll.add(THIRD); 253 coll.add(FOURTH); 254 coll.add(LAST); 255 256 subsetIterator = 258 new SubsetIteratorWithSize( 259 new MockIteratorWithSize(coll), 260 OFFSET, 261 100); 262 assertEquals( 263 "SubsetIterator has wrong size", 264 3, 265 ((SubsetIteratorWithSize) subsetIterator).size()); 266 267 assertEquals( 268 "Could not fetch third element", 269 subsetIterator.next(), 270 THIRD); 271 272 assertTrue( 273 "Filled collection should have next element", 274 subsetIterator.hasNext()); 275 assertEquals( 276 "Could not fetch fourth element", 277 subsetIterator.next(), 278 FOURTH); 279 assertEquals( 280 "Could not fetch last element", 281 subsetIterator.next(), 282 LAST); 283 assertFalse( 284 "SubsetIterator on last position should not have next element", 285 subsetIterator.hasNext()); 286 } 287 288 public void testSize() 289 { 290 assertEquals( 291 "size() didn't work", 292 SIZE, 293 ((SubsetIteratorWithSize) subsetIterator).size()); 294 } 295 296 } 297 | Popular Tags |