ListIteratorTest.java revision f6c387128427e121477c1b32ad35cdcaa5101ba3
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 org.apache.harmony.luni.tests.java.util;
18
19import dalvik.annotation.TestTargets;
20import dalvik.annotation.TestLevel;
21import dalvik.annotation.TestTargetNew;
22import dalvik.annotation.TestTargetClass;
23
24import junit.framework.TestCase;
25
26import java.util.LinkedList;
27import java.util.ListIterator;
28import java.util.NoSuchElementException;
29
30@TestTargetClass(ListIterator.class)
31public class ListIteratorTest extends TestCase {
32
33    ListIterator<Integer> l = null;
34
35    static Object[] objArray;
36    {
37        objArray = new Object[100];
38        for (int i = 0; i < objArray.length; i++)
39            objArray[i] = new Integer(i);
40    }
41
42    @TestTargetNew(
43        level = TestLevel.COMPLETE,
44        notes = "",
45        method = "hasNext",
46        args = {}
47    )
48    public void testHasNext() {
49        for (int i = 0; i < objArray.length; i++) {
50            assertTrue(l.hasNext());
51            l.next();
52        }
53        assertFalse(l.hasNext());
54    }
55
56    @TestTargetNew(
57        level = TestLevel.COMPLETE,
58        notes = "",
59        method = "next",
60        args = {}
61    )
62    public void testNext() {
63        for (int i = 0; i < objArray.length; i++) {
64            assertTrue(objArray[i].equals(l.next()));
65        }
66
67        try {
68            l.next();
69            fail("NoSuchElementException expected");
70        } catch (NoSuchElementException e) {
71            //expected
72        }
73    }
74
75    class Mock_ListIterator implements ListIterator {
76        public void add(Object o) {
77            if(((String) o).equals("Wrong element")) throw new IllegalArgumentException();
78            if(o.getClass() == Double.class) throw new ClassCastException();
79            throw new UnsupportedOperationException();
80        }
81
82        public boolean hasNext() {
83            return false;
84        }
85
86        public boolean hasPrevious() {
87            return false;
88        }
89
90        public Object next() {
91            return null;
92        }
93
94        public int nextIndex() {
95            return 0;
96        }
97
98        public Object previous() {
99            return null;
100        }
101
102        public int previousIndex() {
103            return 0;
104        }
105
106        public void remove() {
107            throw new UnsupportedOperationException();
108        }
109
110        public void set(Object o) {
111            if(((String) o).equals("Wrong element")) throw new IllegalArgumentException();
112            if(o.getClass() == Double.class) throw new ClassCastException();
113            throw new UnsupportedOperationException();
114        }
115    }
116
117    @TestTargetNew(
118        level = TestLevel.COMPLETE,
119        notes = "",
120        method = "remove",
121        args = {}
122    )
123    public void testRemove() {
124        try {
125            l.remove();
126            fail("IllegalStateException expected");
127        } catch (IllegalStateException e) {
128            //expected
129        }
130        for (int i = 0; i < objArray.length; i++) {
131            l.next();
132            l.remove();
133            assertFalse(l.hasPrevious());
134        }
135
136        try {
137            l.remove();
138            fail("IllegalStateException expected");
139        } catch (IllegalStateException e) {
140            //expected
141        }
142
143        Mock_ListIterator ml = new Mock_ListIterator();
144        try {
145            ml.remove();
146            fail("UnsupportedOperationException expected");
147        } catch (UnsupportedOperationException e) {
148            //expected
149        }
150    }
151
152    @TestTargetNew(
153        level = TestLevel.COMPLETE,
154        notes = "",
155        method = "hasPrevious",
156        args = {}
157    )
158    public void testHasPrevious() {
159        assertFalse(l.hasPrevious());
160        for (int i = 0; i < objArray.length; i++) {
161            l.next();
162            assertTrue(l.hasPrevious());
163        }
164    }
165
166    @TestTargetNew(
167        level = TestLevel.COMPLETE,
168        notes = "",
169        method = "previous",
170        args = {}
171    )
172    public void testPrevious() {
173        try {
174            l.previous();
175            fail("NoSuchElementException expected");
176        } catch (NoSuchElementException e) {
177            //expected
178        }
179        while(l.hasNext()) {
180            l.next();
181        }
182
183        for (int i = objArray.length - 1; i > -1 ; i--) {
184            assertTrue(objArray[i].equals(l.previous()));
185        }
186
187        try {
188            l.previous();
189            fail("NoSuchElementException expected");
190        } catch (NoSuchElementException e) {
191            //expected
192        }
193    }
194
195    @TestTargetNew(
196        level = TestLevel.COMPLETE,
197        notes = "",
198        method = "nextIndex",
199        args = {}
200    )
201    public void testNextIndex() {
202        for (int i = 0; i < objArray.length; i++) {
203            assertTrue(objArray[i].equals(l.nextIndex()));
204            l.next();
205        }
206    }
207
208    @TestTargetNew(
209        level = TestLevel.COMPLETE,
210        notes = "",
211        method = "previousIndex",
212        args = {}
213    )
214    public void testPreviousIndex() {
215        for (int i = 0; i < objArray.length; i++) {
216            assertTrue(objArray[i].equals(l.previousIndex() + 1));
217            l.next();
218        }
219    }
220
221    @TestTargetNew(
222        level = TestLevel.COMPLETE,
223        notes = "",
224        method = "set",
225        args = {java.lang.Object.class}
226    )
227    public void testSet() {
228        try {
229            l.set(new Integer(1));
230            fail("IllegalStateException expected");
231        } catch (IllegalStateException e) {
232            //expected
233        }
234
235        for (int i = 0; i < objArray.length; i++) {
236            l.next();
237            l.set((Integer)objArray[objArray.length - i - 1]);
238        }
239
240        l.remove();
241        try {
242            l.set(new Integer(1));
243            fail("IllegalStateException expected");
244        } catch (IllegalStateException e) {
245            //expected
246        }
247
248        Mock_ListIterator ml = new Mock_ListIterator();
249        ml.next();
250        try {
251            ml.set("Wrong element");
252            fail("IllegalArgumentException expected");
253        } catch (IllegalArgumentException e) {
254            //expected
255        }
256
257        try {
258            ml.set(new Double("3.14"));
259            fail("ClassCastException expected");
260        } catch (ClassCastException e) {
261            //expected
262        }
263
264        try {
265            ml.set("");
266            fail("UnsupportedOperationException expected");
267        } catch (UnsupportedOperationException e) {
268            //expected
269        }
270    }
271
272    @TestTargetNew(
273        level = TestLevel.COMPLETE,
274        notes = "",
275        method = "add",
276        args = {java.lang.Object.class}
277    )
278    public void testAdd() {
279        l.add(new Integer(33));
280
281        Mock_ListIterator ml = new Mock_ListIterator();
282        ml.next();
283        try {
284            ml.add("Wrong element");
285            fail("IllegalArgumentException expected");
286        } catch (IllegalArgumentException e) {
287            //expected
288        }
289
290        try {
291            ml.add(new Double("3.14"));
292            fail("ClassCastException expected");
293        } catch (ClassCastException e) {
294            //expected
295        }
296
297        try {
298            ml.add("");
299            fail("UnsupportedOperationException expected");
300        } catch (UnsupportedOperationException e) {
301            //expected
302        }
303    }
304
305    protected void setUp() throws Exception {
306        super.setUp();
307        LinkedList ll = new LinkedList();
308        for (int i = 0; i < objArray.length; i++) {
309            ll.add(objArray[i]);
310        }
311        l = ll.listIterator();
312    }
313}
314