001: /*
002: * Copyright 2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.iterators;
017:
018: import java.util.ArrayList;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.NoSuchElementException;
022:
023: import junit.framework.Test;
024: import junit.framework.TestSuite;
025: import junit.textui.TestRunner;
026:
027: import org.apache.commons.collections.IteratorUtils;
028: import org.apache.commons.collections.Transformer;
029:
030: /**
031: * Testcase.
032: *
033: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
034: *
035: * @author Stephen Colebourne
036: */
037: public class TestObjectGraphIterator extends AbstractTestIterator {
038:
039: protected String[] testArray = { "One", "Two", "Three", "Four",
040: "Five", "Six" };
041:
042: protected List list1 = null;
043: protected List list2 = null;
044: protected List list3 = null;
045: protected List iteratorList = null;
046:
047: public TestObjectGraphIterator(String testName) {
048: super (testName);
049: }
050:
051: public static void main(String[] args) {
052: TestRunner.run(suite());
053: }
054:
055: public static Test suite() {
056: return new TestSuite(TestObjectGraphIterator.class);
057: }
058:
059: public void setUp() {
060: list1 = new ArrayList();
061: list1.add("One");
062: list1.add("Two");
063: list1.add("Three");
064: list2 = new ArrayList();
065: list2.add("Four");
066: list3 = new ArrayList();
067: list3.add("Five");
068: list3.add("Six");
069: iteratorList = new ArrayList();
070: iteratorList.add(list1.iterator());
071: iteratorList.add(list2.iterator());
072: iteratorList.add(list3.iterator());
073: }
074:
075: //-----------------------------------------------------------------------
076: public Iterator makeEmptyIterator() {
077: ArrayList list = new ArrayList();
078: return new ObjectGraphIterator(list.iterator(), null);
079: }
080:
081: public Iterator makeFullIterator() {
082: return new ObjectGraphIterator(iteratorList.iterator(), null);
083: }
084:
085: //-----------------------------------------------------------------------
086: public void testIteratorConstructor_null1() {
087: Iterator it = new ObjectGraphIterator(null);
088:
089: assertEquals(false, it.hasNext());
090: try {
091: it.next();
092: fail();
093: } catch (NoSuchElementException ex) {
094: }
095: try {
096: it.remove();
097: fail();
098: } catch (IllegalStateException ex) {
099: }
100: }
101:
102: public void testIteratorConstructor_null_next() {
103: Iterator it = new ObjectGraphIterator(null);
104: try {
105: it.next();
106: fail();
107: } catch (NoSuchElementException ex) {
108: }
109: }
110:
111: public void testIteratorConstructor_null_remove() {
112: Iterator it = new ObjectGraphIterator(null);
113: try {
114: it.remove();
115: fail();
116: } catch (IllegalStateException ex) {
117: }
118: }
119:
120: //-----------------------------------------------------------------------
121: public void testIteratorConstructorIteration_Empty() {
122: List iteratorList = new ArrayList();
123: Iterator it = new ObjectGraphIterator(iteratorList.iterator());
124:
125: assertEquals(false, it.hasNext());
126: try {
127: it.next();
128: fail();
129: } catch (NoSuchElementException ex) {
130: }
131: try {
132: it.remove();
133: fail();
134: } catch (IllegalStateException ex) {
135: }
136: }
137:
138: public void testIteratorConstructorIteration_Simple() {
139: List iteratorList = new ArrayList();
140: iteratorList.add(list1.iterator());
141: iteratorList.add(list2.iterator());
142: iteratorList.add(list3.iterator());
143: Iterator it = new ObjectGraphIterator(iteratorList.iterator());
144:
145: for (int i = 0; i < 6; i++) {
146: assertEquals(true, it.hasNext());
147: assertEquals(testArray[i], it.next());
148: }
149: assertEquals(false, it.hasNext());
150: try {
151: it.next();
152: fail();
153: } catch (NoSuchElementException ex) {
154: }
155: }
156:
157: public void testIteratorConstructorIteration_SimpleNoHasNext() {
158: List iteratorList = new ArrayList();
159: iteratorList.add(list1.iterator());
160: iteratorList.add(list2.iterator());
161: iteratorList.add(list3.iterator());
162: Iterator it = new ObjectGraphIterator(iteratorList.iterator());
163:
164: for (int i = 0; i < 6; i++) {
165: assertEquals(testArray[i], it.next());
166: }
167: try {
168: it.next();
169: fail();
170: } catch (NoSuchElementException ex) {
171: }
172: }
173:
174: public void testIteratorConstructorIteration_WithEmptyIterators() {
175: List iteratorList = new ArrayList();
176: iteratorList.add(IteratorUtils.EMPTY_ITERATOR);
177: iteratorList.add(list1.iterator());
178: iteratorList.add(IteratorUtils.EMPTY_ITERATOR);
179: iteratorList.add(list2.iterator());
180: iteratorList.add(IteratorUtils.EMPTY_ITERATOR);
181: iteratorList.add(list3.iterator());
182: iteratorList.add(IteratorUtils.EMPTY_ITERATOR);
183: Iterator it = new ObjectGraphIterator(iteratorList.iterator());
184:
185: for (int i = 0; i < 6; i++) {
186: assertEquals(true, it.hasNext());
187: assertEquals(testArray[i], it.next());
188: }
189: assertEquals(false, it.hasNext());
190: try {
191: it.next();
192: fail();
193: } catch (NoSuchElementException ex) {
194: }
195: }
196:
197: public void testIteratorConstructorRemove() {
198: List iteratorList = new ArrayList();
199: iteratorList.add(list1.iterator());
200: iteratorList.add(list2.iterator());
201: iteratorList.add(list3.iterator());
202: Iterator it = new ObjectGraphIterator(iteratorList.iterator());
203:
204: for (int i = 0; i < 6; i++) {
205: assertEquals(testArray[i], it.next());
206: it.remove();
207: }
208: assertEquals(false, it.hasNext());
209: assertEquals(0, list1.size());
210: assertEquals(0, list2.size());
211: assertEquals(0, list3.size());
212: }
213:
214: //-----------------------------------------------------------------------
215: public void testIteration_IteratorOfIterators() {
216: List iteratorList = new ArrayList();
217: iteratorList.add(list1.iterator());
218: iteratorList.add(list2.iterator());
219: iteratorList.add(list3.iterator());
220: Iterator it = new ObjectGraphIterator(iteratorList.iterator(),
221: null);
222:
223: for (int i = 0; i < 6; i++) {
224: assertEquals(true, it.hasNext());
225: assertEquals(testArray[i], it.next());
226: }
227: assertEquals(false, it.hasNext());
228: }
229:
230: public void testIteration_IteratorOfIteratorsWithEmptyIterators() {
231: List iteratorList = new ArrayList();
232: iteratorList.add(IteratorUtils.EMPTY_ITERATOR);
233: iteratorList.add(list1.iterator());
234: iteratorList.add(IteratorUtils.EMPTY_ITERATOR);
235: iteratorList.add(list2.iterator());
236: iteratorList.add(IteratorUtils.EMPTY_ITERATOR);
237: iteratorList.add(list3.iterator());
238: iteratorList.add(IteratorUtils.EMPTY_ITERATOR);
239: Iterator it = new ObjectGraphIterator(iteratorList.iterator(),
240: null);
241:
242: for (int i = 0; i < 6; i++) {
243: assertEquals(true, it.hasNext());
244: assertEquals(testArray[i], it.next());
245: }
246: assertEquals(false, it.hasNext());
247: }
248:
249: //-----------------------------------------------------------------------
250: public void testIteration_RootNull() {
251: Iterator it = new ObjectGraphIterator(null, null);
252:
253: assertEquals(false, it.hasNext());
254: try {
255: it.next();
256: fail();
257: } catch (NoSuchElementException ex) {
258: }
259: try {
260: it.remove();
261: fail();
262: } catch (IllegalStateException ex) {
263: }
264: }
265:
266: public void testIteration_RootNoTransformer() {
267: Forest forest = new Forest();
268: Iterator it = new ObjectGraphIterator(forest, null);
269:
270: assertEquals(true, it.hasNext());
271: assertSame(forest, it.next());
272: assertEquals(false, it.hasNext());
273: try {
274: it.next();
275: fail();
276: } catch (NoSuchElementException ex) {
277: }
278: }
279:
280: public void testIteration_Transformed1() {
281: Forest forest = new Forest();
282: Leaf l1 = forest.addTree().addBranch().addLeaf();
283: Iterator it = new ObjectGraphIterator(forest, new LeafFinder());
284:
285: assertEquals(true, it.hasNext());
286: assertSame(l1, it.next());
287: assertEquals(false, it.hasNext());
288: try {
289: it.next();
290: fail();
291: } catch (NoSuchElementException ex) {
292: }
293: }
294:
295: public void testIteration_Transformed2() {
296: Forest forest = new Forest();
297: forest.addTree();
298: forest.addTree();
299: forest.addTree();
300: Branch b1 = forest.getTree(0).addBranch();
301: Branch b2 = forest.getTree(0).addBranch();
302: Branch b3 = forest.getTree(2).addBranch();
303: Branch b4 = forest.getTree(2).addBranch();
304: Branch b5 = forest.getTree(2).addBranch();
305: Leaf l1 = b1.addLeaf();
306: Leaf l2 = b1.addLeaf();
307: Leaf l3 = b2.addLeaf();
308: Leaf l4 = b3.addLeaf();
309: Leaf l5 = b5.addLeaf();
310:
311: Iterator it = new ObjectGraphIterator(forest, new LeafFinder());
312:
313: assertEquals(true, it.hasNext());
314: assertSame(l1, it.next());
315: assertEquals(true, it.hasNext());
316: assertSame(l2, it.next());
317: assertEquals(true, it.hasNext());
318: assertSame(l3, it.next());
319: assertEquals(true, it.hasNext());
320: assertSame(l4, it.next());
321: assertEquals(true, it.hasNext());
322: assertSame(l5, it.next());
323: assertEquals(false, it.hasNext());
324: try {
325: it.next();
326: fail();
327: } catch (NoSuchElementException ex) {
328: }
329: }
330:
331: public void testIteration_Transformed3() {
332: Forest forest = new Forest();
333: forest.addTree();
334: forest.addTree();
335: forest.addTree();
336: Branch b1 = forest.getTree(1).addBranch();
337: Branch b2 = forest.getTree(1).addBranch();
338: Branch b3 = forest.getTree(2).addBranch();
339: Branch b4 = forest.getTree(2).addBranch();
340: Branch b5 = forest.getTree(2).addBranch();
341: Leaf l1 = b1.addLeaf();
342: Leaf l2 = b1.addLeaf();
343: Leaf l3 = b2.addLeaf();
344: Leaf l4 = b3.addLeaf();
345: Leaf l5 = b4.addLeaf();
346:
347: Iterator it = new ObjectGraphIterator(forest, new LeafFinder());
348:
349: assertEquals(true, it.hasNext());
350: assertSame(l1, it.next());
351: assertEquals(true, it.hasNext());
352: assertSame(l2, it.next());
353: assertEquals(true, it.hasNext());
354: assertSame(l3, it.next());
355: assertEquals(true, it.hasNext());
356: assertSame(l4, it.next());
357: assertEquals(true, it.hasNext());
358: assertSame(l5, it.next());
359: assertEquals(false, it.hasNext());
360: try {
361: it.next();
362: fail();
363: } catch (NoSuchElementException ex) {
364: }
365: }
366:
367: //-----------------------------------------------------------------------
368: static class LeafFinder implements Transformer {
369: public Object transform(Object input) {
370: if (input instanceof Forest) {
371: return ((Forest) input).treeIterator();
372: }
373: if (input instanceof Tree) {
374: return ((Tree) input).branchIterator();
375: }
376: if (input instanceof Branch) {
377: return ((Branch) input).leafIterator();
378: }
379: if (input instanceof Leaf) {
380: return input;
381: }
382: throw new ClassCastException();
383: }
384: }
385:
386: //-----------------------------------------------------------------------
387: static class Forest {
388: List trees = new ArrayList();
389:
390: Tree addTree() {
391: trees.add(new Tree());
392: return getTree(trees.size() - 1);
393: }
394:
395: Tree getTree(int index) {
396: return (Tree) trees.get(index);
397: }
398:
399: Iterator treeIterator() {
400: return trees.iterator();
401: }
402: }
403:
404: static class Tree {
405: List branches = new ArrayList();
406:
407: Branch addBranch() {
408: branches.add(new Branch());
409: return getBranch(branches.size() - 1);
410: }
411:
412: Branch getBranch(int index) {
413: return (Branch) branches.get(index);
414: }
415:
416: Iterator branchIterator() {
417: return branches.iterator();
418: }
419: }
420:
421: static class Branch {
422: List leaves = new ArrayList();
423:
424: Leaf addLeaf() {
425: leaves.add(new Leaf());
426: return getLeaf(leaves.size() - 1);
427: }
428:
429: Leaf getLeaf(int index) {
430: return (Leaf) leaves.get(index);
431: }
432:
433: Iterator leafIterator() {
434: return leaves.iterator();
435: }
436: }
437:
438: static class Leaf {
439: String colour;
440:
441: String getColour() {
442: return colour;
443: }
444:
445: void setColour(String colour) {
446: this.colour = colour;
447: }
448: }
449:
450: }
|