KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > iterators > TestObjectGraphIterator


1 /*
2  * Copyright 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.iterators;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.NoSuchElementException JavaDoc;
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 /**
31  * Testcase.
32  *
33  * @version $Revision: 1.1 $ $Date: 2004/03/20 00:21:08 $
34  *
35  * @author Stephen Colebourne
36  */

37 public class TestObjectGraphIterator extends AbstractTestIterator {
38
39     protected String JavaDoc[] testArray = { "One", "Two", "Three", "Four", "Five", "Six" };
40
41     protected List JavaDoc list1 = null;
42     protected List JavaDoc list2 = null;
43     protected List JavaDoc list3 = null;
44     protected List JavaDoc iteratorList = null;
45
46     public TestObjectGraphIterator(String JavaDoc testName) {
47         super(testName);
48     }
49
50     public static void main(String JavaDoc[] 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 JavaDoc();
60         list1.add("One");
61         list1.add("Two");
62         list1.add("Three");
63         list2 = new ArrayList JavaDoc();
64         list2.add("Four");
65         list3 = new ArrayList JavaDoc();
66         list3.add("Five");
67         list3.add("Six");
68         iteratorList = new ArrayList JavaDoc();
69         iteratorList.add(list1.iterator());
70         iteratorList.add(list2.iterator());
71         iteratorList.add(list3.iterator());
72     }
73
74     //-----------------------------------------------------------------------
75
public Iterator JavaDoc makeEmptyIterator() {
76         ArrayList JavaDoc list = new ArrayList JavaDoc();
77         return new ObjectGraphIterator(list.iterator(), null);
78     }
79
80     public Iterator JavaDoc makeFullIterator() {
81         return new ObjectGraphIterator(iteratorList.iterator(), null);
82     }
83
84     //-----------------------------------------------------------------------
85
public void testIteratorConstructor_null1() {
86         Iterator JavaDoc it = new ObjectGraphIterator(null);
87
88         assertEquals(false, it.hasNext());
89         try {
90             it.next();
91             fail();
92         } catch (NoSuchElementException JavaDoc ex) {
93         }
94         try {
95             it.remove();
96             fail();
97         } catch (IllegalStateException JavaDoc ex) {
98         }
99     }
100
101     public void testIteratorConstructor_null_next() {
102         Iterator JavaDoc it = new ObjectGraphIterator(null);
103         try {
104             it.next();
105             fail();
106         } catch (NoSuchElementException JavaDoc ex) {
107         }
108     }
109
110     public void testIteratorConstructor_null_remove() {
111         Iterator JavaDoc it = new ObjectGraphIterator(null);
112         try {
113             it.remove();
114             fail();
115         } catch (IllegalStateException JavaDoc ex) {
116         }
117     }
118
119     //-----------------------------------------------------------------------
120
public void testIteratorConstructorIteration_Empty() {
121         List JavaDoc iteratorList = new ArrayList JavaDoc();
122         Iterator JavaDoc it = new ObjectGraphIterator(iteratorList.iterator());
123
124         assertEquals(false, it.hasNext());
125         try {
126             it.next();
127             fail();
128         } catch (NoSuchElementException JavaDoc ex) {
129         }
130         try {
131             it.remove();
132             fail();
133         } catch (IllegalStateException JavaDoc ex) {
134         }
135     }
136
137     public void testIteratorConstructorIteration_Simple() {
138         List JavaDoc iteratorList = new ArrayList JavaDoc();
139         iteratorList.add(list1.iterator());
140         iteratorList.add(list2.iterator());
141         iteratorList.add(list3.iterator());
142         Iterator JavaDoc 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 JavaDoc ex) {
153         }
154     }
155
156     public void testIteratorConstructorIteration_SimpleNoHasNext() {
157         List JavaDoc iteratorList = new ArrayList JavaDoc();
158         iteratorList.add(list1.iterator());
159         iteratorList.add(list2.iterator());
160         iteratorList.add(list3.iterator());
161         Iterator JavaDoc 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 JavaDoc ex) {
170         }
171     }
172
173     public void testIteratorConstructorIteration_WithEmptyIterators() {
174         List JavaDoc iteratorList = new ArrayList JavaDoc();
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 JavaDoc 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 JavaDoc ex) {
193         }
194     }
195
196     public void testIteratorConstructorRemove() {
197         List JavaDoc iteratorList = new ArrayList JavaDoc();
198         iteratorList.add(list1.iterator());
199         iteratorList.add(list2.iterator());
200         iteratorList.add(list3.iterator());
201         Iterator JavaDoc 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     //-----------------------------------------------------------------------
214
public void testIteration_IteratorOfIterators() {
215         List JavaDoc iteratorList = new ArrayList JavaDoc();
216         iteratorList.add(list1.iterator());
217         iteratorList.add(list2.iterator());
218         iteratorList.add(list3.iterator());
219         Iterator JavaDoc 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 JavaDoc iteratorList = new ArrayList JavaDoc();
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 JavaDoc 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     //-----------------------------------------------------------------------
247
public void testIteration_RootNull() {
248         Iterator JavaDoc it = new ObjectGraphIterator(null, null);
249
250         assertEquals(false, it.hasNext());
251         try {
252             it.next();
253             fail();
254         } catch (NoSuchElementException JavaDoc ex) {
255         }
256         try {
257             it.remove();
258             fail();
259         } catch (IllegalStateException JavaDoc ex) {
260         }
261     }
262
263     public void testIteration_RootNoTransformer() {
264         Forest forest = new Forest();
265         Iterator JavaDoc 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 JavaDoc ex) {
274         }
275     }
276
277     public void testIteration_Transformed1() {
278         Forest forest = new Forest();
279         Leaf l1 = forest.addTree().addBranch().addLeaf();
280         Iterator JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc ex) {
361         }
362     }
363
364     //-----------------------------------------------------------------------
365
static class LeafFinder implements Transformer {
366         public Object JavaDoc transform(Object JavaDoc 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 JavaDoc();
380         }
381     }
382
383     //-----------------------------------------------------------------------
384
static class Forest {
385         List JavaDoc trees = new ArrayList JavaDoc();
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 JavaDoc treeIterator() {
397             return trees.iterator();
398         }
399     }
400
401     static class Tree {
402         List JavaDoc branches = new ArrayList JavaDoc();
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 JavaDoc branchIterator() {
414             return branches.iterator();
415         }
416     }
417
418     static class Branch {
419         List JavaDoc leaves = new ArrayList JavaDoc();
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 JavaDoc leafIterator() {
431             return leaves.iterator();
432         }
433     }
434
435     static class Leaf {
436         String JavaDoc colour;
437
438         String JavaDoc getColour() {
439             return colour;
440         }
441
442         void setColour(String JavaDoc colour) {
443             this.colour = colour;
444         }
445     }
446
447 }
448
Popular Tags