KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2006 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.ListIterator JavaDoc;
22 import java.util.NoSuchElementException JavaDoc;
23
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26 import junit.textui.TestRunner;
27
28 import org.apache.commons.collections.ResettableListIterator;
29
30 /**
31  * Tests the ReverseListIterator.
32  *
33  * @version $Revision: $ $Date: 2006-05-06 17:10:31 +0100 (Sat, 06 May 2006) $
34  */

35 public class TestReverseListIterator extends AbstractTestListIterator {
36
37     protected String JavaDoc[] testArray = { "One", "Two", "Three", "Four" };
38
39     public static void main(String JavaDoc args[]) {
40         String JavaDoc[] testCaseName = { TestReverseListIterator.class.getName() };
41         TestRunner.main(testCaseName);
42     }
43
44     public static Test suite() {
45         return new TestSuite(TestReverseListIterator.class);
46     }
47
48     public TestReverseListIterator(String JavaDoc testName) {
49         super(testName);
50     }
51
52     public ListIterator JavaDoc makeEmptyListIterator() {
53         List JavaDoc list = new ArrayList JavaDoc();
54         return new ReverseListIterator(list);
55     }
56
57     public ListIterator JavaDoc makeFullListIterator() {
58         List JavaDoc list = new ArrayList JavaDoc(Arrays.asList(testArray));
59         return new ReverseListIterator(list);
60     }
61
62     // overrides
63
//-----------------------------------------------------------------------
64
public void testEmptyListIteratorIsIndeedEmpty() {
65         ListIterator JavaDoc it = makeEmptyListIterator();
66         
67         assertEquals(false, it.hasNext());
68         assertEquals(-1, it.nextIndex()); // reversed index
69
assertEquals(false, it.hasPrevious());
70         assertEquals(0, it.previousIndex()); // reversed index
71

72         // next() should throw a NoSuchElementException
73
try {
74             it.next();
75             fail("NoSuchElementException must be thrown from empty ListIterator");
76         } catch (NoSuchElementException JavaDoc e) {
77         }
78         
79         // previous() should throw a NoSuchElementException
80
try {
81             it.previous();
82             fail("NoSuchElementException must be thrown from empty ListIterator");
83         } catch (NoSuchElementException JavaDoc e) {
84         }
85     }
86
87     public void testWalkForwardAndBack() {
88         ArrayList JavaDoc list = new ArrayList JavaDoc();
89         ListIterator JavaDoc it = makeFullListIterator();
90         while (it.hasNext()) {
91             list.add(it.next());
92         }
93         
94         // check state at end
95
assertEquals(false, it.hasNext());
96         assertEquals(true, it.hasPrevious());
97         
98         // this had to be commented out, as there is a bug in the JDK before JDK1.5
99
// where calling previous at the start of an iterator would push the cursor
100
// back to an invalid negative value
101
// try {
102
// it.next();
103
// fail("NoSuchElementException must be thrown from next at end of ListIterator");
104
// } catch (NoSuchElementException e) {
105
// }
106

107         // loop back through comparing
108
for (int i = list.size() - 1; i >= 0; i--) {
109             assertEquals("" + i, list.size() - i - 2, it.nextIndex()); // reversed index
110
assertEquals(list.size() - i - 1, it.previousIndex()); // reversed index
111

112             Object JavaDoc obj = list.get(i);
113             assertEquals(obj, it.previous());
114         }
115         
116         // check state at start
117
assertEquals(true, it.hasNext());
118         assertEquals(false, it.hasPrevious());
119         try {
120             it.previous();
121             fail("NoSuchElementException must be thrown from previous at start of ListIterator");
122         } catch (NoSuchElementException JavaDoc e) {
123         }
124     }
125
126     //-----------------------------------------------------------------------
127
public void testReverse() {
128         ListIterator JavaDoc it = makeFullListIterator();
129         assertEquals(true, it.hasNext());
130         assertEquals(3, it.nextIndex());
131         assertEquals(false, it.hasPrevious());
132         assertEquals(4, it.previousIndex());
133         assertEquals("Four", it.next());
134         assertEquals(2, it.nextIndex());
135         assertEquals(true, it.hasNext());
136         assertEquals(3, it.previousIndex());
137         assertEquals(true, it.hasPrevious());
138         assertEquals("Three", it.next());
139         assertEquals(true, it.hasNext());
140         assertEquals(1, it.nextIndex());
141         assertEquals(true, it.hasPrevious());
142         assertEquals(2, it.previousIndex());
143         assertEquals("Two", it.next());
144         assertEquals(true, it.hasNext());
145         assertEquals(0, it.nextIndex());
146         assertEquals(true, it.hasPrevious());
147         assertEquals(1, it.previousIndex());
148         assertEquals("One", it.next());
149         assertEquals(false, it.hasNext());
150         assertEquals(-1, it.nextIndex());
151         assertEquals(true, it.hasPrevious());
152         assertEquals(0, it.previousIndex());
153         assertEquals("One", it.previous());
154         assertEquals("Two", it.previous());
155         assertEquals("Three", it.previous());
156         assertEquals("Four", it.previous());
157     }
158
159     public void testReset() {
160         ResettableListIterator it = (ResettableListIterator) makeFullListIterator();
161         assertEquals("Four", it.next());
162         it.reset();
163         assertEquals("Four", it.next());
164         it.next();
165         it.next();
166         it.reset();
167         assertEquals("Four", it.next());
168     }
169
170 }
171
Popular Tags