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  */
16 package org.apache.harmony.tests.java.util;
17 
18 import java.util.AbstractQueue;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.Collection;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.NoSuchElementException;
25 import java.util.Vector;
26 
27 import junit.framework.TestCase;
28 
29 public 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 
iterator()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 
size()70         public int size() {
71             return size;
72         }
73 
offer(E o)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 
poll()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 
peek()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      */
test_addLE_null()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      */
test_addLE_Full()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      */
test_addLE()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      */
test_addAllLE_null()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      */
test_addAllLE_with_null()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      */
test_addAllLE_full()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      */
test_addAllLE_empty()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      */
test_addAllLE_this()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 
test_addAllLjava_lang_Object()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      */
test_clear_empty()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      */
test_clear()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      */
test_Constructor()253     public void test_Constructor() {
254         MockAbstractQueue queue = new MockAbstractQueue();
255         assertNotNull(queue);
256     }
257 
258     /**
259      * java.util.AbstractQueue#remove()
260      */
test_remove_null()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      */
test_remove()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      */
test_element_empty()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      */
test_element()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 
setUp()314     protected void setUp() throws Exception {
315         super.setUp();
316         queue = new MockAbstractQueue<Object>();
317     }
318 
tearDown()319     protected void tearDown() throws Exception {
320         super.tearDown();
321         queue = null;
322     }
323 }
324