KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 LoopingListIterator class.
29  *
30  * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
31  *
32  * @author Eric Crampton <ccesc@eonomine.com>
33  */

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

47     public void testConstructorEx() throws Exception JavaDoc {
48         try {
49             new LoopingListIterator(null);
50             fail();
51         } catch (NullPointerException JavaDoc ex) {
52         }
53     }
54
55     /**
56      * Tests whether an empty looping list iterator works.
57      */

58     public void testLooping0() throws Exception JavaDoc {
59         List JavaDoc list = new ArrayList JavaDoc();
60         LoopingListIterator loop = new LoopingListIterator(list);
61         assertFalse(loop.hasNext());
62         assertFalse(loop.hasPrevious());
63         
64         try {
65             loop.next();
66             fail();
67         } catch (NoSuchElementException JavaDoc ex) {
68         }
69
70         try {
71             loop.previous();
72             fail();
73         } catch (NoSuchElementException JavaDoc ex) {
74         }
75     }
76
77     /**
78      * Tests whether a looping list iterator works on a list with only
79      * one element.
80      */

81     public void testLooping1() throws Exception JavaDoc {
82         List JavaDoc list = new ArrayList JavaDoc(Arrays.asList(new String JavaDoc[] { "a" }));
83         LoopingListIterator loop = new LoopingListIterator(list); // <a>
84

85         assertTrue(loop.hasNext());
86         assertEquals("a", loop.next()); // <a>
87

88         assertTrue(loop.hasNext());
89         assertEquals("a", loop.next()); // <a>
90

91         assertTrue(loop.hasNext());
92         assertEquals("a", loop.next()); // <a>
93

94         assertTrue(loop.hasPrevious());
95         assertEquals("a", loop.previous()); // <a>
96

97         assertTrue(loop.hasPrevious());
98         assertEquals("a", loop.previous()); // <a>
99

100         assertTrue(loop.hasPrevious());
101         assertEquals("a", loop.previous()); // <a>
102
}
103
104     /**
105      * Tests whether a looping list iterator works on a list with two
106      * elements.
107      */

108     public void testLooping2() throws Exception JavaDoc {
109         List JavaDoc list = new ArrayList JavaDoc(Arrays.asList(new String JavaDoc[] { "a", "b" }));
110         LoopingListIterator loop = new LoopingListIterator(list); // <a> b
111

112         assertTrue(loop.hasNext());
113         assertEquals("a", loop.next()); // a <b>
114

115         assertTrue(loop.hasNext());
116         assertEquals("b", loop.next()); // <a> b
117

118         assertTrue(loop.hasNext());
119         assertEquals("a", loop.next()); // a <b>
120

121         // Reset the iterator and try using previous.
122
loop.reset(); // <a> b
123

124         assertTrue(loop.hasPrevious());
125         assertEquals("b", loop.previous()); // a <b>
126

127         assertTrue(loop.hasPrevious());
128         assertEquals("a", loop.previous()); // <a> b
129

130         assertTrue(loop.hasPrevious());
131         assertEquals("b", loop.previous()); // a <b>
132
}
133
134     /**
135      * Tests jogging back and forth between two elements, but not over
136      * the begin/end boundary of the list.
137      */

138     public void testJoggingNotOverBoundary() {
139         List JavaDoc list = new ArrayList JavaDoc(Arrays.asList(new String JavaDoc[] { "a", "b" }));
140         LoopingListIterator loop = new LoopingListIterator(list); // <a> b
141

142         // Try jogging back and forth between the elements, but not
143
// over the begin/end boundary.
144
loop.reset();
145         assertEquals("a", loop.next()); // a <b>
146
assertEquals("a", loop.previous()); // <a> b
147
assertEquals("a", loop.next()); // a <b>
148

149         assertEquals("b", loop.next()); // <a> b
150
assertEquals("b", loop.previous()); // a <b>
151
assertEquals("b", loop.next()); // <a> b
152
}
153
154     /**
155      * Tests jogging back and forth between two elements over the
156      * begin/end boundary of the list.
157      */

158     public void testJoggingOverBoundary() {
159         List JavaDoc list = new ArrayList JavaDoc(Arrays.asList(new String JavaDoc[] { "a", "b" }));
160         LoopingListIterator loop = new LoopingListIterator(list); // <a> b
161

162         // Try jogging back and forth between the elements, but not
163
// over the begin/end boundary.
164
assertEquals("b", loop.previous()); // a <b>
165
assertEquals("b", loop.next()); // <a> b
166
assertEquals("b", loop.previous()); // a <b>
167

168         assertEquals("a", loop.previous()); // <a> b
169
assertEquals("a", loop.next()); // a <b>
170
assertEquals("a", loop.previous()); // <a> b
171
}
172
173     /**
174      * Tests removing an element from a wrapped ArrayList.
175      */

176     public void testRemovingElementsAndIteratingForward() {
177         List JavaDoc list = new ArrayList JavaDoc(Arrays.asList(new String JavaDoc[] { "a", "b", "c" }));
178         LoopingListIterator loop = new LoopingListIterator(list); // <a> b c
179

180         assertTrue(loop.hasNext());
181         assertEquals("a", loop.next()); // a <b> c
182
loop.remove(); // <b> c
183
assertEquals(2, list.size());
184
185         assertTrue(loop.hasNext());
186         assertEquals("b", loop.next()); // b <c>
187
loop.remove(); // <c>
188
assertEquals(1, list.size());
189
190         assertTrue(loop.hasNext());
191         assertEquals("c", loop.next()); // <c>
192
loop.remove(); // ---
193
assertEquals(0, list.size());
194
195         assertFalse(loop.hasNext());
196         try {
197             loop.next();
198             fail();
199         } catch (NoSuchElementException JavaDoc ex) {
200         }
201     }
202
203     /**
204      * Tests removing an element from a wrapped ArrayList.
205      */

206     public void testRemovingElementsAndIteratingBackwards() {
207         List JavaDoc list = new ArrayList JavaDoc(Arrays.asList(new String JavaDoc[] { "a", "b", "c" }));
208         LoopingListIterator loop = new LoopingListIterator(list); // <a> b c
209

210         assertTrue(loop.hasPrevious());
211         assertEquals("c", loop.previous()); // a b <c>
212
loop.remove(); // <a> b
213
assertEquals(2, list.size());
214
215         assertTrue(loop.hasPrevious());
216         assertEquals("b", loop.previous()); // a <b>
217
loop.remove(); // <a>
218
assertEquals(1, list.size());
219
220         assertTrue(loop.hasPrevious());
221         assertEquals("a", loop.previous()); // <a>
222
loop.remove(); // ---
223
assertEquals(0, list.size());
224
225         assertFalse(loop.hasPrevious());
226         try {
227             loop.previous();
228             fail();
229         } catch (NoSuchElementException JavaDoc ex) {
230         }
231     }
232
233     /**
234      * Tests the reset method.
235      */

236     public void testReset() {
237         List JavaDoc list = new ArrayList JavaDoc(Arrays.asList(new String JavaDoc[] { "a", "b", "c" }));
238         LoopingListIterator loop = new LoopingListIterator(list); // <a> b c
239

240         assertEquals("a", loop.next()); // a <b> c
241
assertEquals("b", loop.next()); // a b <c>
242
loop.reset(); // <a> b c
243
assertEquals("a", loop.next()); // a <b> c
244
loop.reset(); // <a> b c
245
assertEquals("a", loop.next()); // a <b> c
246
assertEquals("b", loop.next()); // a b <c>
247
assertEquals("c", loop.next()); // <a> b c
248
loop.reset(); // <a> b c
249

250         assertEquals("c", loop.previous()); // a b <c>
251
assertEquals("b", loop.previous()); // a <b> c
252
loop.reset(); // <a> b c
253
assertEquals("c", loop.previous()); // a b <c>
254
loop.reset(); // <a> b c
255
assertEquals("c", loop.previous()); // a b <c>
256
assertEquals("b", loop.previous()); // a <b> c
257
assertEquals("a", loop.previous()); // <a> b c
258
}
259
260     /**
261      * Tests the add method.
262      */

263     public void testAdd() {
264         List JavaDoc list = new ArrayList JavaDoc(Arrays.asList(new String JavaDoc[] { "b", "e", "f" }));
265         LoopingListIterator loop = new LoopingListIterator(list); // <b> e f
266

267         loop.add("a"); // <a> b e f
268
assertEquals("b", loop.next()); // a <b> e f
269
loop.reset(); // <a> b e f
270
assertEquals("a", loop.next()); // a <b> e f
271
assertEquals("b", loop.next()); // a b <e> f
272

273         loop.add("c"); // a b c <e> f
274
assertEquals("e", loop.next()); // a b c e <f>
275
assertEquals("e", loop.previous()); // a b c <e> f
276
assertEquals("c", loop.previous()); // a b <c> e f
277
assertEquals("c", loop.next()); // a b c <e> f
278

279         loop.add("d"); // a b c d <e> f
280
loop.reset(); // <a> b c d e f
281
assertEquals("a", loop.next()); // a <b> c d e f
282
assertEquals("b", loop.next()); // a b <c> d e f
283
assertEquals("c", loop.next()); // a b c <d> e f
284
assertEquals("d", loop.next()); // a b c d <e> f
285
assertEquals("e", loop.next()); // a b c d e <f>
286
assertEquals("f", loop.next()); // <a> b c d e f
287
assertEquals("a", loop.next()); // a <b> c d e f
288

289         list = new ArrayList JavaDoc(Arrays.asList(new String JavaDoc[] { "b", "e", "f" }));
290         loop = new LoopingListIterator(list); // <b> e f
291

292         loop.add("a"); // a <b> e f
293
assertEquals("a", loop.previous()); // a b e <f>
294
loop.reset(); // <a> b e f
295
assertEquals("f", loop.previous()); // a b e <f>
296
assertEquals("e", loop.previous()); // a b <e> f
297

298         loop.add("d"); // a b d <e> f
299
assertEquals("d", loop.previous()); // a b <d> e f
300

301         loop.add("c"); // a b c <d> e f
302
assertEquals("c", loop.previous()); // a b <c> d e f
303

304         loop.reset();
305         assertEquals("a", loop.next()); // a <b> c d e f
306
assertEquals("b", loop.next()); // a b <c> d e f
307
assertEquals("c", loop.next()); // a b c <d> e f
308
assertEquals("d", loop.next()); // a b c d <e> f
309
assertEquals("e", loop.next()); // a b c d e <f>
310
assertEquals("f", loop.next()); // <a> b c d e f
311
assertEquals("a", loop.next()); // a <b> c d e f
312
}
313
314     /**
315      * Tests nextIndex and previousIndex.
316      */

317     public void testNextAndPreviousIndex() {
318         List JavaDoc list = new ArrayList JavaDoc(Arrays.asList(new String JavaDoc[] { "a", "b", "c" }));
319         LoopingListIterator loop = new LoopingListIterator(list); // <a> b c
320

321         assertEquals(0, loop.nextIndex());
322         assertEquals(2, loop.previousIndex());
323
324         assertEquals("a", loop.next()); // a <b> c
325
assertEquals(1, loop.nextIndex());
326         assertEquals(0, loop.previousIndex());
327
328         assertEquals("a", loop.previous()); // <a> b c
329
assertEquals(0, loop.nextIndex());
330         assertEquals(2, loop.previousIndex());
331
332         assertEquals("c", loop.previous()); // a b <c>
333
assertEquals(2, loop.nextIndex());
334         assertEquals(1, loop.previousIndex());
335
336         assertEquals("b", loop.previous()); // a <b> c
337
assertEquals(1, loop.nextIndex());
338         assertEquals(0, loop.previousIndex());
339
340         assertEquals("a", loop.previous()); // <a> b c
341
assertEquals(0, loop.nextIndex());
342         assertEquals(2, loop.previousIndex());
343     }
344
345     /**
346      * Tests using the set method to change elements.
347      */

348     public void testSet() {
349         List JavaDoc list = new ArrayList JavaDoc(Arrays.asList(new String JavaDoc[] { "q", "r", "z" }));
350         LoopingListIterator loop = new LoopingListIterator(list); // <q> r z
351

352         assertEquals("z", loop.previous()); // q r <z>
353
loop.set("c"); // q r <c>
354

355         loop.reset(); // <q> r c
356
assertEquals("q", loop.next()); // q <r> c
357
loop.set("a"); // a <r> c
358

359         assertEquals("r", loop.next()); // a r <c>
360
loop.set("b"); // a b <c>
361

362         loop.reset(); // <a> b c
363
assertEquals("a", loop.next()); // a <b> c
364
assertEquals("b", loop.next()); // a b <c>
365
assertEquals("c", loop.next()); // <a> b c
366
}
367     
368 }
369
Popular Tags