1 16 package org.apache.commons.collections.iterators; 17 18 import java.util.ArrayList ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.NoSuchElementException ; 22 23 import junit.framework.Test; 24 import junit.framework.TestSuite; 25 import junit.textui.TestRunner; 26 27 import org.apache.commons.collections.IteratorUtils; 28 import org.apache.commons.collections.Transformer; 29 30 37 public class TestObjectGraphIterator extends AbstractTestIterator { 38 39 protected String [] testArray = { "One", "Two", "Three", "Four", "Five", "Six" }; 40 41 protected List list1 = null; 42 protected List list2 = null; 43 protected List list3 = null; 44 protected List iteratorList = null; 45 46 public TestObjectGraphIterator(String testName) { 47 super(testName); 48 } 49 50 public static void main(String [] args) { 51 TestRunner.run(suite()); 52 } 53 54 public static Test suite() { 55 return new TestSuite(TestObjectGraphIterator.class); 56 } 57 58 public void setUp() { 59 list1 = new ArrayList (); 60 list1.add("One"); 61 list1.add("Two"); 62 list1.add("Three"); 63 list2 = new ArrayList (); 64 list2.add("Four"); 65 list3 = new ArrayList (); 66 list3.add("Five"); 67 list3.add("Six"); 68 iteratorList = new ArrayList (); 69 iteratorList.add(list1.iterator()); 70 iteratorList.add(list2.iterator()); 71 iteratorList.add(list3.iterator()); 72 } 73 74 public Iterator makeEmptyIterator() { 76 ArrayList list = new ArrayList (); 77 return new ObjectGraphIterator(list.iterator(), null); 78 } 79 80 public Iterator makeFullIterator() { 81 return new ObjectGraphIterator(iteratorList.iterator(), null); 82 } 83 84 public void testIteratorConstructor_null1() { 86 Iterator it = new ObjectGraphIterator(null); 87 88 assertEquals(false, it.hasNext()); 89 try { 90 it.next(); 91 fail(); 92 } catch (NoSuchElementException ex) { 93 } 94 try { 95 it.remove(); 96 fail(); 97 } catch (IllegalStateException ex) { 98 } 99 } 100 101 public void testIteratorConstructor_null_next() { 102 Iterator it = new ObjectGraphIterator(null); 103 try { 104 it.next(); 105 fail(); 106 } catch (NoSuchElementException ex) { 107 } 108 } 109 110 public void testIteratorConstructor_null_remove() { 111 Iterator it = new ObjectGraphIterator(null); 112 try { 113 it.remove(); 114 fail(); 115 } catch (IllegalStateException ex) { 116 } 117 } 118 119 public void testIteratorConstructorIteration_Empty() { 121 List iteratorList = new ArrayList (); 122 Iterator it = new ObjectGraphIterator(iteratorList.iterator()); 123 124 assertEquals(false, it.hasNext()); 125 try { 126 it.next(); 127 fail(); 128 } catch (NoSuchElementException ex) { 129 } 130 try { 131 it.remove(); 132 fail(); 133 } catch (IllegalStateException ex) { 134 } 135 } 136 137 public void testIteratorConstructorIteration_Simple() { 138 List iteratorList = new ArrayList (); 139 iteratorList.add(list1.iterator()); 140 iteratorList.add(list2.iterator()); 141 iteratorList.add(list3.iterator()); 142 Iterator it = new ObjectGraphIterator(iteratorList.iterator()); 143 144 for (int i = 0; i < 6; i++) { 145 assertEquals(true, it.hasNext()); 146 assertEquals(testArray[i], it.next()); 147 } 148 assertEquals(false, it.hasNext()); 149 try { 150 it.next(); 151 fail(); 152 } catch (NoSuchElementException ex) { 153 } 154 } 155 156 public void testIteratorConstructorIteration_SimpleNoHasNext() { 157 List iteratorList = new ArrayList (); 158 iteratorList.add(list1.iterator()); 159 iteratorList.add(list2.iterator()); 160 iteratorList.add(list3.iterator()); 161 Iterator it = new ObjectGraphIterator(iteratorList.iterator()); 162 163 for (int i = 0; i < 6; i++) { 164 assertEquals(testArray[i], it.next()); 165 } 166 try { 167 it.next(); 168 fail(); 169 } catch (NoSuchElementException ex) { 170 } 171 } 172 173 public void testIteratorConstructorIteration_WithEmptyIterators() { 174 List iteratorList = new ArrayList (); 175 iteratorList.add(IteratorUtils.EMPTY_ITERATOR); 176 iteratorList.add(list1.iterator()); 177 iteratorList.add(IteratorUtils.EMPTY_ITERATOR); 178 iteratorList.add(list2.iterator()); 179 iteratorList.add(IteratorUtils.EMPTY_ITERATOR); 180 iteratorList.add(list3.iterator()); 181 iteratorList.add(IteratorUtils.EMPTY_ITERATOR); 182 Iterator it = new ObjectGraphIterator(iteratorList.iterator()); 183 184 for (int i = 0; i < 6; i++) { 185 assertEquals(true, it.hasNext()); 186 assertEquals(testArray[i], it.next()); 187 } 188 assertEquals(false, it.hasNext()); 189 try { 190 it.next(); 191 fail(); 192 } catch (NoSuchElementException ex) { 193 } 194 } 195 196 public void testIteratorConstructorRemove() { 197 List iteratorList = new ArrayList (); 198 iteratorList.add(list1.iterator()); 199 iteratorList.add(list2.iterator()); 200 iteratorList.add(list3.iterator()); 201 Iterator it = new ObjectGraphIterator(iteratorList.iterator()); 202 203 for (int i = 0; i < 6; i++) { 204 assertEquals(testArray[i], it.next()); 205 it.remove(); 206 } 207 assertEquals(false, it.hasNext()); 208 assertEquals(0, list1.size()); 209 assertEquals(0, list2.size()); 210 assertEquals(0, list3.size()); 211 } 212 213 public void testIteration_IteratorOfIterators() { 215 List iteratorList = new ArrayList (); 216 iteratorList.add(list1.iterator()); 217 iteratorList.add(list2.iterator()); 218 iteratorList.add(list3.iterator()); 219 Iterator it = new ObjectGraphIterator(iteratorList.iterator(), null); 220 221 for (int i = 0; i < 6; i++) { 222 assertEquals(true, it.hasNext()); 223 assertEquals(testArray[i], it.next()); 224 } 225 assertEquals(false, it.hasNext()); 226 } 227 228 public void testIteration_IteratorOfIteratorsWithEmptyIterators() { 229 List iteratorList = new ArrayList (); 230 iteratorList.add(IteratorUtils.EMPTY_ITERATOR); 231 iteratorList.add(list1.iterator()); 232 iteratorList.add(IteratorUtils.EMPTY_ITERATOR); 233 iteratorList.add(list2.iterator()); 234 iteratorList.add(IteratorUtils.EMPTY_ITERATOR); 235 iteratorList.add(list3.iterator()); 236 iteratorList.add(IteratorUtils.EMPTY_ITERATOR); 237 Iterator it = new ObjectGraphIterator(iteratorList.iterator(), null); 238 239 for (int i = 0; i < 6; i++) { 240 assertEquals(true, it.hasNext()); 241 assertEquals(testArray[i], it.next()); 242 } 243 assertEquals(false, it.hasNext()); 244 } 245 246 public void testIteration_RootNull() { 248 Iterator it = new ObjectGraphIterator(null, null); 249 250 assertEquals(false, it.hasNext()); 251 try { 252 it.next(); 253 fail(); 254 } catch (NoSuchElementException ex) { 255 } 256 try { 257 it.remove(); 258 fail(); 259 } catch (IllegalStateException ex) { 260 } 261 } 262 263 public void testIteration_RootNoTransformer() { 264 Forest forest = new Forest(); 265 Iterator it = new ObjectGraphIterator(forest, null); 266 267 assertEquals(true, it.hasNext()); 268 assertSame(forest, it.next()); 269 assertEquals(false, it.hasNext()); 270 try { 271 it.next(); 272 fail(); 273 } catch (NoSuchElementException ex) { 274 } 275 } 276 277 public void testIteration_Transformed1() { 278 Forest forest = new Forest(); 279 Leaf l1 = forest.addTree().addBranch().addLeaf(); 280 Iterator it = new ObjectGraphIterator(forest, new LeafFinder()); 281 282 assertEquals(true, it.hasNext()); 283 assertSame(l1, it.next()); 284 assertEquals(false, it.hasNext()); 285 try { 286 it.next(); 287 fail(); 288 } catch (NoSuchElementException ex) { 289 } 290 } 291 292 public void testIteration_Transformed2() { 293 Forest forest = new Forest(); 294 forest.addTree(); 295 forest.addTree(); 296 forest.addTree(); 297 Branch b1 = forest.getTree(0).addBranch(); 298 Branch b2 = forest.getTree(0).addBranch(); 299 Branch b3 = forest.getTree(2).addBranch(); 300 Branch b4 = forest.getTree(2).addBranch(); 301 Branch b5 = forest.getTree(2).addBranch(); 302 Leaf l1 = b1.addLeaf(); 303 Leaf l2 = b1.addLeaf(); 304 Leaf l3 = b2.addLeaf(); 305 Leaf l4 = b3.addLeaf(); 306 Leaf l5 = b5.addLeaf(); 307 308 Iterator it = new ObjectGraphIterator(forest, new LeafFinder()); 309 310 assertEquals(true, it.hasNext()); 311 assertSame(l1, it.next()); 312 assertEquals(true, it.hasNext()); 313 assertSame(l2, it.next()); 314 assertEquals(true, it.hasNext()); 315 assertSame(l3, it.next()); 316 assertEquals(true, it.hasNext()); 317 assertSame(l4, it.next()); 318 assertEquals(true, it.hasNext()); 319 assertSame(l5, it.next()); 320 assertEquals(false, it.hasNext()); 321 try { 322 it.next(); 323 fail(); 324 } catch (NoSuchElementException ex) { 325 } 326 } 327 328 public void testIteration_Transformed3() { 329 Forest forest = new Forest(); 330 forest.addTree(); 331 forest.addTree(); 332 forest.addTree(); 333 Branch b1 = forest.getTree(1).addBranch(); 334 Branch b2 = forest.getTree(1).addBranch(); 335 Branch b3 = forest.getTree(2).addBranch(); 336 Branch b4 = forest.getTree(2).addBranch(); 337 Branch b5 = forest.getTree(2).addBranch(); 338 Leaf l1 = b1.addLeaf(); 339 Leaf l2 = b1.addLeaf(); 340 Leaf l3 = b2.addLeaf(); 341 Leaf l4 = b3.addLeaf(); 342 Leaf l5 = b4.addLeaf(); 343 344 Iterator it = new ObjectGraphIterator(forest, new LeafFinder()); 345 346 assertEquals(true, it.hasNext()); 347 assertSame(l1, it.next()); 348 assertEquals(true, it.hasNext()); 349 assertSame(l2, it.next()); 350 assertEquals(true, it.hasNext()); 351 assertSame(l3, it.next()); 352 assertEquals(true, it.hasNext()); 353 assertSame(l4, it.next()); 354 assertEquals(true, it.hasNext()); 355 assertSame(l5, it.next()); 356 assertEquals(false, it.hasNext()); 357 try { 358 it.next(); 359 fail(); 360 } catch (NoSuchElementException ex) { 361 } 362 } 363 364 static class LeafFinder implements Transformer { 366 public Object transform(Object input) { 367 if (input instanceof Forest) { 368 return ((Forest) input).treeIterator(); 369 } 370 if (input instanceof Tree) { 371 return ((Tree) input).branchIterator(); 372 } 373 if (input instanceof Branch) { 374 return ((Branch) input).leafIterator(); 375 } 376 if (input instanceof Leaf) { 377 return input; 378 } 379 throw new ClassCastException (); 380 } 381 } 382 383 static class Forest { 385 List trees = new ArrayList (); 386 387 Tree addTree() { 388 trees.add(new Tree()); 389 return getTree(trees.size() - 1); 390 } 391 392 Tree getTree(int index) { 393 return (Tree) trees.get(index); 394 } 395 396 Iterator treeIterator() { 397 return trees.iterator(); 398 } 399 } 400 401 static class Tree { 402 List branches = new ArrayList (); 403 404 Branch addBranch() { 405 branches.add(new Branch()); 406 return getBranch(branches.size() - 1); 407 } 408 409 Branch getBranch(int index) { 410 return (Branch) branches.get(index); 411 } 412 413 Iterator branchIterator() { 414 return branches.iterator(); 415 } 416 } 417 418 static class Branch { 419 List leaves = new ArrayList (); 420 421 Leaf addLeaf() { 422 leaves.add(new Leaf()); 423 return getLeaf(leaves.size() - 1); 424 } 425 426 Leaf getLeaf(int index) { 427 return (Leaf) leaves.get(index); 428 } 429 430 Iterator leafIterator() { 431 return leaves.iterator(); 432 } 433 } 434 435 static class Leaf { 436 String colour; 437 438 String getColour() { 439 return colour; 440 } 441 442 void setColour(String colour) { 443 this.colour = colour; 444 } 445 } 446 447 } 448 | Popular Tags |