KickJava   Java API By Example, From Geeks To Geeks.

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


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.Arrays JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.NoSuchElementException JavaDoc;
22
23 import junit.framework.Test;
24 import junit.framework.TestCase;
25 import junit.framework.TestSuite;
26
27 /**
28  * Tests the LoopingIterator class.
29  *
30  * @version $Revision: 1.5 $ $Date: 2004/02/18 01:20:33 $
31  *
32  * @author Jonathan Carlson
33  * @author Stephen Colebourne
34  */

35 public class TestLoopingIterator extends TestCase {
36
37     public TestLoopingIterator(String JavaDoc testName) {
38         super(testName);
39     }
40
41     public static Test suite() {
42         return new TestSuite(TestLoopingIterator.class);
43     }
44
45     /**
46      * Tests constructor exception.
47      */

48     public void testConstructorEx() throws Exception JavaDoc {
49         try {
50             new LoopingIterator(null);
51             fail();
52         } catch (NullPointerException JavaDoc ex) {
53         }
54     }
55     
56     /**
57      * Tests whether an empty looping iterator works as designed.
58      * @throws Exception If something unexpected occurs.
59      */

60     public void testLooping0() throws Exception JavaDoc {
61         List JavaDoc list = new ArrayList JavaDoc();
62         LoopingIterator loop = new LoopingIterator(list);
63         assertTrue("hasNext should return false", loop.hasNext() == false);
64
65         try {
66             loop.next();
67             fail("NoSuchElementException was not thrown during next() call.");
68         } catch (NoSuchElementException JavaDoc ex) {
69         }
70     }
71
72     /**
73      * Tests whether a populated looping iterator works as designed.
74      * @throws Exception If something unexpected occurs.
75      */

76     public void testLooping1() throws Exception JavaDoc {
77         List JavaDoc list = new ArrayList JavaDoc(Arrays.asList(new String JavaDoc[] { "a" }));
78         LoopingIterator loop = new LoopingIterator(list);
79
80         assertTrue("1st hasNext should return true", loop.hasNext());
81         assertEquals("a", loop.next());
82
83         assertTrue("2nd hasNext should return true", loop.hasNext());
84         assertEquals("a", loop.next());
85
86         assertTrue("3rd hasNext should return true", loop.hasNext());
87         assertEquals("a", loop.next());
88
89     }
90
91     /**
92      * Tests whether a populated looping iterator works as designed.
93      * @throws Exception If something unexpected occurs.
94      */

95     public void testLooping2() throws Exception JavaDoc {
96         List JavaDoc list = new ArrayList JavaDoc(Arrays.asList(new String JavaDoc[] { "a", "b" }));
97         LoopingIterator loop = new LoopingIterator(list);
98
99         assertTrue("1st hasNext should return true", loop.hasNext());
100         assertEquals("a", loop.next());
101
102         assertTrue("2nd hasNext should return true", loop.hasNext());
103         assertEquals("b", loop.next());
104
105         assertTrue("3rd hasNext should return true", loop.hasNext());
106         assertEquals("a", loop.next());
107
108     }
109
110     /**
111      * Tests whether a populated looping iterator works as designed.
112      * @throws Exception If something unexpected occurs.
113      */

114     public void testLooping3() throws Exception JavaDoc {
115         List JavaDoc list = new ArrayList JavaDoc(Arrays.asList(new String JavaDoc[] { "a", "b", "c" }));
116         LoopingIterator loop = new LoopingIterator(list);
117
118         assertTrue("1st hasNext should return true", loop.hasNext());
119         assertEquals("a", loop.next());
120
121         assertTrue("2nd hasNext should return true", loop.hasNext());
122         assertEquals("b", loop.next());
123
124         assertTrue("3rd hasNext should return true", loop.hasNext());
125         assertEquals("c", loop.next());
126
127         assertTrue("4th hasNext should return true", loop.hasNext());
128         assertEquals("a", loop.next());
129
130     }
131
132     /**
133      * Tests the remove() method on a LoopingIterator wrapped ArrayList.
134      * @throws Exception If something unexpected occurs.
135      */

136     public void testRemoving1() throws Exception JavaDoc {
137         List JavaDoc list = new ArrayList JavaDoc(Arrays.asList(new String JavaDoc[] { "a", "b", "c" }));
138         LoopingIterator loop = new LoopingIterator(list);
139         assertEquals("list should have 3 elements.", 3, list.size());
140
141         assertTrue("1st hasNext should return true", loop.hasNext());
142         assertEquals("a", loop.next());
143         loop.remove(); // removes a
144
assertEquals("list should have 2 elements.", 2, list.size());
145
146         assertTrue("2nd hasNext should return true", loop.hasNext());
147         assertEquals("b", loop.next());
148         loop.remove(); // removes b
149
assertEquals("list should have 1 elements.", 1, list.size());
150
151         assertTrue("3rd hasNext should return true", loop.hasNext());
152         assertEquals("c", loop.next());
153         loop.remove(); // removes c
154
assertEquals("list should have 0 elements.", 0, list.size());
155
156         assertTrue("4th hasNext should return false", loop.hasNext() == false);
157         try {
158             loop.next();
159             fail("Expected NoSuchElementException to be thrown.");
160         } catch (NoSuchElementException JavaDoc ex) {
161         }
162     }
163
164     /**
165      * Tests the reset() method on a LoopingIterator wrapped ArrayList.
166      * @throws Exception If something unexpected occurs.
167      */

168     public void testReset() throws Exception JavaDoc {
169         List JavaDoc list = new ArrayList JavaDoc(Arrays.asList(new String JavaDoc[] { "a", "b", "c" }));
170         LoopingIterator loop = new LoopingIterator(list);
171
172         assertEquals("a", loop.next());
173         assertEquals("b", loop.next());
174         loop.reset();
175         assertEquals("a", loop.next());
176         loop.reset();
177         assertEquals("a", loop.next());
178         assertEquals("b", loop.next());
179         assertEquals("c", loop.next());
180         loop.reset();
181         assertEquals("a", loop.next());
182         assertEquals("b", loop.next());
183         assertEquals("c", loop.next());
184     }
185     
186     /**
187      * Tests the size() method on a LoopingIterator wrapped ArrayList.
188      * @throws Exception If something unexpected occurs.
189      */

190     public void testSize() throws Exception JavaDoc {
191         List JavaDoc list = new ArrayList JavaDoc(Arrays.asList(new String JavaDoc[] { "a", "b", "c" }));
192         LoopingIterator loop = new LoopingIterator(list);
193
194         assertEquals(3, loop.size());
195         loop.next();
196         loop.next();
197         assertEquals(3, loop.size());
198         loop.reset();
199         assertEquals(3, loop.size());
200         loop.next();
201         loop.remove();
202         assertEquals(2, loop.size());
203     }
204
205 }
206
Popular Tags