KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-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
26 /**
27  * Tests the IteratorChain class.
28  *
29  * @version $Revision: 1.9 $ $Date: 2004/02/18 01:20:33 $
30  *
31  * @author James Strachan
32  * @author Mauricio S. Moura
33  * @author Morgan Delagrange
34  */

35 public class TestIteratorChain extends AbstractTestIterator {
36
37     protected String JavaDoc[] testArray = {
38         "One", "Two", "Three", "Four", "Five", "Six"
39     };
40
41     protected List JavaDoc list1 = null;
42     protected List JavaDoc list2 = null;
43     protected List JavaDoc list3 = null;
44
45     public static Test suite() {
46         return new TestSuite(TestIteratorChain.class);
47     }
48
49     public TestIteratorChain(String JavaDoc testName) {
50         super(testName);
51     }
52
53     public void setUp() {
54         list1 = new ArrayList JavaDoc();
55         list1.add("One");
56         list1.add("Two");
57         list1.add("Three");
58         list2 = new ArrayList JavaDoc();
59         list2.add("Four");
60         list3 = new ArrayList JavaDoc();
61         list3.add("Five");
62         list3.add("Six");
63     }
64
65     public Iterator JavaDoc makeEmptyIterator() {
66         ArrayList JavaDoc list = new ArrayList JavaDoc();
67         return new IteratorChain(list.iterator());
68     }
69
70     public Iterator JavaDoc makeFullIterator() {
71         IteratorChain chain = new IteratorChain();
72
73         chain.addIterator(list1.iterator());
74         chain.addIterator(list2.iterator());
75         chain.addIterator(list3.iterator());
76         return chain;
77     }
78
79     public void testIterator() {
80         Iterator JavaDoc iter = (Iterator JavaDoc) makeFullIterator();
81         for ( int i = 0; i < testArray.length; i++ ) {
82             Object JavaDoc testValue = testArray[i];
83             Object JavaDoc iterValue = iter.next();
84
85             assertEquals( "Iteration value is correct", testValue, iterValue );
86         }
87
88         assertTrue("Iterator should now be empty", ! iter.hasNext() );
89
90         try {
91             Object JavaDoc testValue = iter.next();
92         } catch (Exception JavaDoc e) {
93             assertTrue("NoSuchElementException must be thrown",
94                        e.getClass().equals((new NoSuchElementException JavaDoc()).getClass()));
95         }
96     }
97
98     public void testRemove() {
99         Iterator JavaDoc iter = (Iterator JavaDoc) makeFullIterator();
100
101         try {
102             iter.remove();
103             fail("Calling remove before the first call to next() should throw an exception");
104         } catch (IllegalStateException JavaDoc e) {
105
106         }
107
108         for ( int i = 0; i < testArray.length; i++ ) {
109             Object JavaDoc testValue = testArray[i];
110             Object JavaDoc iterValue = iter.next();
111
112             assertEquals( "Iteration value is correct", testValue, iterValue );
113
114             if (! iterValue.equals("Four")) {
115                 iter.remove();
116             }
117         }
118
119         assertTrue("List is empty",list1.size() == 0);
120         assertTrue("List is empty",list2.size() == 1);
121         assertTrue("List is empty",list3.size() == 0);
122     }
123
124     public void testFirstIteratorIsEmptyBug() {
125         List JavaDoc empty = new ArrayList JavaDoc();
126         List JavaDoc notEmpty = new ArrayList JavaDoc();
127         notEmpty.add("A");
128         notEmpty.add("B");
129         notEmpty.add("C");
130         IteratorChain chain = new IteratorChain();
131         chain.addIterator(empty.iterator());
132         chain.addIterator(notEmpty.iterator());
133         assertTrue("should have next",chain.hasNext());
134         assertEquals("A",chain.next());
135         assertTrue("should have next",chain.hasNext());
136         assertEquals("B",chain.next());
137         assertTrue("should have next",chain.hasNext());
138         assertEquals("C",chain.next());
139         assertTrue("should not have next",!chain.hasNext());
140     }
141     
142     public void testEmptyChain() {
143         IteratorChain chain = new IteratorChain();
144         assertEquals(false, chain.hasNext());
145         try {
146             chain.next();
147             fail();
148         } catch (NoSuchElementException JavaDoc ex) {}
149         try {
150             chain.remove();
151             fail();
152         } catch (IllegalStateException JavaDoc ex) {}
153     }
154         
155 }
156
Popular Tags