1/*
2 * Copyright (C) 2008 The Android Open Source Project
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
17package libcore.java.util;
18
19import junit.framework.TestCase;
20
21import java.util.LinkedList;
22import java.util.ListIterator;
23import java.util.NoSuchElementException;
24
25public class OldListIteratorTest extends TestCase {
26
27    ListIterator<Integer> l = null;
28
29    static Object[] objArray;
30    {
31        objArray = new Object[100];
32        for (int i = 0; i < objArray.length; i++)
33            objArray[i] = new Integer(i);
34    }
35
36    public void testHasNext() {
37        for (int i = 0; i < objArray.length; i++) {
38            assertTrue(l.hasNext());
39            l.next();
40        }
41        assertFalse(l.hasNext());
42    }
43
44    public void testNext() {
45        for (int i = 0; i < objArray.length; i++) {
46            assertTrue(objArray[i].equals(l.next()));
47        }
48
49        try {
50            l.next();
51            fail("NoSuchElementException expected");
52        } catch (NoSuchElementException e) {
53            //expected
54        }
55    }
56
57    class Mock_ListIterator implements ListIterator {
58        public void add(Object o) {
59            if(((String) o).equals("Wrong element")) throw new IllegalArgumentException();
60            if(o.getClass() == Double.class) throw new ClassCastException();
61            throw new UnsupportedOperationException();
62        }
63
64        public boolean hasNext() {
65            return false;
66        }
67
68        public boolean hasPrevious() {
69            return false;
70        }
71
72        public Object next() {
73            return null;
74        }
75
76        public int nextIndex() {
77            return 0;
78        }
79
80        public Object previous() {
81            return null;
82        }
83
84        public int previousIndex() {
85            return 0;
86        }
87
88        public void remove() {
89            throw new UnsupportedOperationException();
90        }
91
92        public void set(Object o) {
93            if(((String) o).equals("Wrong element")) throw new IllegalArgumentException();
94            if(o.getClass() == Double.class) throw new ClassCastException();
95            throw new UnsupportedOperationException();
96        }
97    }
98
99    public void testRemove() {
100        try {
101            l.remove();
102            fail("IllegalStateException expected");
103        } catch (IllegalStateException e) {
104            //expected
105        }
106        for (int i = 0; i < objArray.length; i++) {
107            l.next();
108            l.remove();
109            assertFalse(l.hasPrevious());
110        }
111
112        try {
113            l.remove();
114            fail("IllegalStateException expected");
115        } catch (IllegalStateException e) {
116            //expected
117        }
118
119        Mock_ListIterator ml = new Mock_ListIterator();
120        try {
121            ml.remove();
122            fail("UnsupportedOperationException expected");
123        } catch (UnsupportedOperationException e) {
124            //expected
125        }
126    }
127
128    public void testHasPrevious() {
129        assertFalse(l.hasPrevious());
130        for (int i = 0; i < objArray.length; i++) {
131            l.next();
132            assertTrue(l.hasPrevious());
133        }
134    }
135
136    public void testPrevious() {
137        try {
138            l.previous();
139            fail("NoSuchElementException expected");
140        } catch (NoSuchElementException e) {
141            //expected
142        }
143        while(l.hasNext()) {
144            l.next();
145        }
146
147        for (int i = objArray.length - 1; i > -1 ; i--) {
148            assertTrue(objArray[i].equals(l.previous()));
149        }
150
151        try {
152            l.previous();
153            fail("NoSuchElementException expected");
154        } catch (NoSuchElementException e) {
155            //expected
156        }
157    }
158
159    public void testNextIndex() {
160        for (int i = 0; i < objArray.length; i++) {
161            assertTrue(objArray[i].equals(l.nextIndex()));
162            l.next();
163        }
164    }
165
166    public void testPreviousIndex() {
167        for (int i = 0; i < objArray.length; i++) {
168            assertTrue(objArray[i].equals(l.previousIndex() + 1));
169            l.next();
170        }
171    }
172
173    public void testSet() {
174        try {
175            l.set(new Integer(1));
176            fail("IllegalStateException expected");
177        } catch (IllegalStateException e) {
178            //expected
179        }
180
181        for (int i = 0; i < objArray.length; i++) {
182            l.next();
183            l.set((Integer)objArray[objArray.length - i - 1]);
184        }
185
186        l.remove();
187        try {
188            l.set(new Integer(1));
189            fail("IllegalStateException expected");
190        } catch (IllegalStateException e) {
191            //expected
192        }
193
194        Mock_ListIterator ml = new Mock_ListIterator();
195        ml.next();
196        try {
197            ml.set("Wrong element");
198            fail("IllegalArgumentException expected");
199        } catch (IllegalArgumentException e) {
200            //expected
201        }
202
203        try {
204            ml.set(new Double("3.14"));
205            fail("ClassCastException expected");
206        } catch (ClassCastException e) {
207            //expected
208        }
209
210        try {
211            ml.set("");
212            fail("UnsupportedOperationException expected");
213        } catch (UnsupportedOperationException e) {
214            //expected
215        }
216    }
217
218    public void testAdd() {
219        l.add(new Integer(33));
220
221        Mock_ListIterator ml = new Mock_ListIterator();
222        ml.next();
223        try {
224            ml.add("Wrong element");
225            fail("IllegalArgumentException expected");
226        } catch (IllegalArgumentException e) {
227            //expected
228        }
229
230        try {
231            ml.add(new Double("3.14"));
232            fail("ClassCastException expected");
233        } catch (ClassCastException e) {
234            //expected
235        }
236
237        try {
238            ml.add("");
239            fail("UnsupportedOperationException expected");
240        } catch (UnsupportedOperationException e) {
241            //expected
242        }
243    }
244
245    protected void setUp() throws Exception {
246        super.setUp();
247        LinkedList ll = new LinkedList();
248        for (int i = 0; i < objArray.length; i++) {
249            ll.add(objArray[i]);
250        }
251        l = ll.listIterator();
252    }
253}
254