AbstractQueueTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  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 */
16package org.apache.harmony.luni.tests.java.util;
17
18import java.util.AbstractQueue;
19import java.util.ArrayList;
20import java.util.Arrays;
21import java.util.Iterator;
22import java.util.List;
23import java.util.NoSuchElementException;
24
25import junit.framework.TestCase;
26
27public class AbstractQueueTest extends TestCase {
28
29    private MockAbstractQueue<Object> queue;
30
31    private class MockAbstractQueue<E> extends AbstractQueue<E> {
32
33        static final int CAPACITY = 10;
34
35        private int size = 0;
36
37        private Object[] elements = new Object[CAPACITY];
38
39        public Iterator<E> iterator() {
40            return new Iterator<E>() {
41
42                private int currentIndex = -1;
43
44                public boolean hasNext() {
45                    return size > 0 && currentIndex < size;
46                }
47
48                public E next() {
49                    if (!hasNext()) {
50                        throw new NoSuchElementException();
51                    }
52                    currentIndex++;
53                    return (E) elements[currentIndex];
54                }
55
56                public void remove() {
57                    if (-1 == currentIndex) {
58                        throw new IllegalStateException();
59                    }
60                    for (int i = currentIndex; i < size - 1; i++) {
61                        elements[i] = elements[i + 1];
62                    }
63                    size--;
64                }
65            };
66        }
67
68        public int size() {
69            return size;
70        }
71
72        public boolean offer(E o) {
73            if (null == o) {
74                throw new NullPointerException();
75            }
76
77            if (size >= CAPACITY) {
78                return false;
79            }
80
81            elements[size++] = o;
82            return true;
83        }
84
85        public E poll() {
86            if (isEmpty()) {
87                return null;
88            }
89            E e = (E) elements[0];
90            for (int i = 0; i < size - 1; i++) {
91                elements[i] = elements[i + 1];
92            }
93            size--;
94            return e;
95        }
96
97        public E peek() {
98            if (isEmpty()) {
99                return null;
100            }
101            return (E) elements[0];
102        }
103
104    }
105
106    /**
107     * @tests java.util.AbstractQueue.add(E)
108     */
109    public void test_addLE_null() {
110        try {
111            queue.add(null);
112            fail("should throw NullPointerException");
113        } catch (NullPointerException e) {
114            // expected
115        }
116    }
117
118    /**
119     * @tests java.util.AbstractQueue.add(E)
120     */
121    public void test_addLE_Full() {
122        Object o = new Object();
123
124        for(int i = 0; i < MockAbstractQueue.CAPACITY; i++ ) {
125            queue.add(o);
126        }
127
128        try {
129            queue.add(o);
130            fail("should throw IllegalStateException");
131        } catch (IllegalStateException e) {
132            //expected
133        }
134    }
135
136    /**
137     * @tests java.util.AbstractQueue#add(E)
138     */
139    public void test_addLE() {
140        Object o = new Object();
141        final int LAST_INDEX = 4;
142        for (int i = 0; i < LAST_INDEX; i++) {
143            queue.add(o);
144        }
145        Integer I = new Integer(123456);
146        queue.add(I);
147        assertTrue(queue.contains(I));
148        Iterator iter = queue.iterator();
149        for (int i = 0; i < LAST_INDEX; i++) {
150            iter.next();
151        }
152        assertTrue(I == iter.next());
153    }
154
155    /**
156     * @tests java.util.AbstractQueue#addAll(E)
157     */
158    public void test_addAllLE_null() {
159        try {
160            queue.addAll(null);
161            fail("should throw NullPointerException");
162        } catch (NullPointerException e) {
163            // expected
164        }
165    }
166
167    /**
168     * @tests java.util.AbstractQueue#addAll(E)
169     */
170    public void test_addAllLE_with_null() {
171        List list = Arrays.asList("MYTESTSTRING", null, new Float(123.456));
172        try {
173            queue.addAll(list);
174            fail("should throw NullPointerException");
175        } catch (NullPointerException e) {
176            // expected
177        }
178    }
179
180    /**
181     * @tests java.util.AbstractQueue#addAll(E)
182     */
183    public void test_addAllLE_full() {
184        List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
185        try {
186            queue.addAll(list);
187            fail("should throw IllegalStateException");
188        } catch (IllegalStateException e) {
189            // expected
190        }
191    }
192
193    /**
194     * @tests java.util.AbstractQueue#addAll(E)
195     */
196    public void test_addAllLE_empty() {
197        // Regression test for HARMONY-1178
198        List list = new ArrayList<Object>(0);
199        assertFalse("Non modification to queue should return false", queue.addAll(list));
200    }
201
202    /**
203     * @tests java.util.AbstractQueue#addAll(E)
204     */
205    public void test_addAllLE_this() {
206        try {
207            queue.addAll(queue);
208            fail("should throw IllegalArgumentException ");
209        } catch (IllegalArgumentException e) {
210            // expected
211        }
212    }
213
214    /**
215     * @tests java.util.AbstractQueue#clear()
216     */
217    public void test_clear_empty() {
218        queue.clear();
219        assertTrue(queue.isEmpty());
220        assertNull(queue.peek());
221    }
222
223    /**
224     * @tests java.util.AbstractQueue#clear()
225     */
226    public void test_clear() {
227        List list = Arrays.asList(123.456, "MYTESTSTRING", new Object(), 'c');
228        queue.addAll(list);
229        queue.clear();
230        assertTrue(queue.isEmpty());
231        assertNull(queue.peek());
232    }
233
234    /**
235     * @tests java.util.AbstractQueue#AbstractQueue()
236     */
237    public void test_Constructor() {
238        MockAbstractQueue queue = new MockAbstractQueue();
239        assertNotNull(queue);
240    }
241
242    /**
243     * @tests java.util.AbstractQueue#remove()
244     */
245    public void test_remove_null() {
246        try {
247            queue.remove();
248            fail("should throw NoSuchElementException");
249        } catch (NoSuchElementException e) {
250            // expected
251        }
252
253    }
254
255    /**
256     * @tests java.util.AbstractQueue#remove()
257     */
258    public void test_remove() {
259        char c = 'a';
260        queue.add(c);
261        c = 'b';
262        queue.add(c);
263        assertEquals('a', queue.remove());
264        assertEquals('b', queue.remove());
265        try {
266            queue.remove();
267            fail("should throw NoSuchElementException");
268        } catch (NoSuchElementException e) {
269            // expected
270        }
271    }
272
273    /**
274     * @tests java.util.AbstractQueue#element()
275     */
276    public void test_element_empty() {
277        try {
278            queue.element();
279            fail("should throw NoSuchElementException");
280        } catch (NoSuchElementException e) {
281            // expected
282        }
283    }
284
285    /**
286     * @tests java.util.AbstractQueue#element()
287     */
288    public void test_element() {
289        String s = "MYTESTSTRING_ONE";
290        queue.add(s);
291        s = "MYTESTSTRING_TWO";
292        queue.add(s);
293        assertEquals("MYTESTSTRING_ONE", queue.element());
294        // still the first element
295        assertEquals("MYTESTSTRING_ONE", queue.element());
296    }
297
298    protected void setUp() throws Exception {
299        super.setUp();
300        queue = new MockAbstractQueue<Object>();
301    }
302
303    protected void tearDown() throws Exception {
304        super.tearDown();
305        queue = null;
306    }
307}
308