001: /* Licensed to the Apache Software Foundation (ASF) under one or more
002: * contributor license agreements. See the NOTICE file distributed with
003: * this work for additional information regarding copyright ownership.
004: * The ASF licenses this file to You under the Apache License, Version 2.0
005: * (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.harmony.luni.tests.java.util;
017:
018: import java.util.AbstractQueue;
019: import java.util.ArrayList;
020: import java.util.Arrays;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.NoSuchElementException;
024:
025: import junit.framework.TestCase;
026:
027: public class AbstractQueueTest extends TestCase {
028:
029: private MockAbstractQueue<Object> queue;
030:
031: private class MockAbstractQueue<E> extends AbstractQueue<E> {
032:
033: static final int CAPACITY = 10;
034:
035: private int size = 0;
036:
037: private Object[] elements = new Object[CAPACITY];
038:
039: public Iterator<E> iterator() {
040: return new Iterator<E>() {
041:
042: private int currentIndex = -1;
043:
044: public boolean hasNext() {
045: return size > 0 && currentIndex < size;
046: }
047:
048: public E next() {
049: if (!hasNext()) {
050: throw new NoSuchElementException();
051: }
052: currentIndex++;
053: return (E) elements[currentIndex];
054: }
055:
056: public void remove() {
057: if (-1 == currentIndex) {
058: throw new IllegalStateException();
059: }
060: for (int i = currentIndex; i < size - 1; i++) {
061: elements[i] = elements[i + 1];
062: }
063: size--;
064: }
065: };
066: }
067:
068: public int size() {
069: return size;
070: }
071:
072: public boolean offer(E o) {
073: if (null == o) {
074: throw new NullPointerException();
075: }
076:
077: if (size >= CAPACITY) {
078: return false;
079: }
080:
081: elements[size++] = o;
082: return true;
083: }
084:
085: public E poll() {
086: if (isEmpty()) {
087: return null;
088: }
089: E e = (E) elements[0];
090: for (int i = 0; i < size - 1; i++) {
091: elements[i] = elements[i + 1];
092: }
093: size--;
094: return e;
095: }
096:
097: public E peek() {
098: if (isEmpty()) {
099: 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(
172: 123.456));
173: try {
174: queue.addAll(list);
175: fail("should throw NullPointerException");
176: } catch (NullPointerException e) {
177: // expected
178: }
179: }
180:
181: /**
182: * @tests java.util.AbstractQueue#addAll(E)
183: */
184: public void test_addAllLE_full() {
185: List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
186: try {
187: queue.addAll(list);
188: fail("should throw IllegalStateException");
189: } catch (IllegalStateException e) {
190: // expected
191: }
192: }
193:
194: /**
195: * @tests java.util.AbstractQueue#addAll(E)
196: */
197: public void test_addAllLE_empty() {
198: // Regression test for HARMONY-1178
199: List list = new ArrayList<Object>(0);
200: assertFalse("Non modification to queue should return false",
201: queue.addAll(list));
202: }
203:
204: /**
205: * @tests 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: /**
217: * @tests java.util.AbstractQueue#clear()
218: */
219: public void test_clear_empty() {
220: queue.clear();
221: assertTrue(queue.isEmpty());
222: assertNull(queue.peek());
223: }
224:
225: /**
226: * @tests java.util.AbstractQueue#clear()
227: */
228: public void test_clear() {
229: List list = Arrays.asList(123.456, "MYTESTSTRING",
230: new Object(), 'c');
231: queue.addAll(list);
232: queue.clear();
233: assertTrue(queue.isEmpty());
234: assertNull(queue.peek());
235: }
236:
237: /**
238: * @tests java.util.AbstractQueue#AbstractQueue()
239: */
240: public void test_Constructor() {
241: MockAbstractQueue queue = new MockAbstractQueue();
242: assertNotNull(queue);
243: }
244:
245: /**
246: * @tests java.util.AbstractQueue#remove()
247: */
248: public void test_remove_null() {
249: try {
250: queue.remove();
251: fail("should throw NoSuchElementException");
252: } catch (NoSuchElementException e) {
253: // expected
254: }
255:
256: }
257:
258: /**
259: * @tests java.util.AbstractQueue#remove()
260: */
261: public void test_remove() {
262: char c = 'a';
263: queue.add(c);
264: c = 'b';
265: queue.add(c);
266: assertEquals('a', queue.remove());
267: assertEquals('b', queue.remove());
268: try {
269: queue.remove();
270: fail("should throw NoSuchElementException");
271: } catch (NoSuchElementException e) {
272: // expected
273: }
274: }
275:
276: /**
277: * @tests java.util.AbstractQueue#element()
278: */
279: public void test_element_empty() {
280: try {
281: queue.element();
282: fail("should throw NoSuchElementException");
283: } catch (NoSuchElementException e) {
284: // expected
285: }
286: }
287:
288: /**
289: * @tests java.util.AbstractQueue#element()
290: */
291: public void test_element() {
292: String s = "MYTESTSTRING_ONE";
293: queue.add(s);
294: s = "MYTESTSTRING_TWO";
295: queue.add(s);
296: assertEquals("MYTESTSTRING_ONE", queue.element());
297: // still the first element
298: assertEquals("MYTESTSTRING_ONE", queue.element());
299: }
300:
301: protected void setUp() throws Exception {
302: super .setUp();
303: queue = new MockAbstractQueue<Object>();
304: }
305:
306: protected void tearDown() throws Exception {
307: super.tearDown();
308: queue = null;
309: }
310: }
|