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