1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This code is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11 * version 2 for more details (a copy is included in the LICENSE file that
12 * accompanied this code).
13 *
14 * You should have received a copy of the GNU General Public License version
15 * 2 along with this work; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19 * or visit www.oracle.com if you need additional information or have any
20 * questions.
21 */
22
23/*
24 * This file is available under and governed by the GNU General Public
25 * License version 2 only, as published by the Free Software Foundation.
26 * However, the following notice accompanied the original version of this
27 * file:
28 *
29 * Written by Doug Lea with assistance from members of JCP JSR-166
30 * Expert Group and released to the public domain, as explained at
31 * http://creativecommons.org/publicdomain/zero/1.0/
32 * Other contributors include Andrew Wright, Jeffrey Hayes,
33 * Pat Fisher, Mike Judd.
34 */
35
36import java.util.AbstractQueue;
37import java.util.Arrays;
38import java.util.Iterator;
39import java.util.NoSuchElementException;
40
41import junit.framework.Test;
42import junit.framework.TestSuite;
43
44public class AbstractQueueTest extends JSR166TestCase {
45    public static void main(String[] args) {
46        main(suite(), args);
47    }
48    public static Test suite() {
49        return new TestSuite(AbstractQueueTest.class);
50    }
51
52    static class Succeed extends AbstractQueue<Integer> {
53        public boolean offer(Integer x) {
54            if (x == null) throw new NullPointerException();
55            return true;
56        }
57        public Integer peek() { return one; }
58        public Integer poll() { return one; }
59        public int size() { return 0; }
60        public Iterator iterator() { return null; } // not needed
61    }
62
63    static class Fail extends AbstractQueue<Integer> {
64        public boolean offer(Integer x) {
65            if (x == null) throw new NullPointerException();
66            return false;
67        }
68        public Integer peek() { return null; }
69        public Integer poll() { return null; }
70        public int size() { return 0; }
71        public Iterator iterator() { return null; } // not needed
72    }
73
74    /**
75     * add returns true if offer succeeds
76     */
77    public void testAddS() {
78        Succeed q = new Succeed();
79        assertTrue(q.add(two));
80    }
81
82    /**
83     * add throws ISE true if offer fails
84     */
85    public void testAddF() {
86        Fail q = new Fail();
87        try {
88            q.add(one);
89            shouldThrow();
90        } catch (IllegalStateException success) {}
91    }
92
93    /**
94     * add throws NPE if offer does
95     */
96    public void testAddNPE() {
97        Succeed q = new Succeed();
98        try {
99            q.add(null);
100            shouldThrow();
101        } catch (NullPointerException success) {}
102    }
103
104    /**
105     * remove returns normally if poll succeeds
106     */
107    public void testRemoveS() {
108        Succeed q = new Succeed();
109        q.remove();
110    }
111
112    /**
113     * remove throws NSEE if poll returns null
114     */
115    public void testRemoveF() {
116        Fail q = new Fail();
117        try {
118            q.remove();
119            shouldThrow();
120        } catch (NoSuchElementException success) {}
121    }
122
123    /**
124     * element returns normally if peek succeeds
125     */
126    public void testElementS() {
127        Succeed q = new Succeed();
128        q.element();
129    }
130
131    /**
132     * element throws NSEE if peek returns null
133     */
134    public void testElementF() {
135        Fail q = new Fail();
136        try {
137            q.element();
138            shouldThrow();
139        } catch (NoSuchElementException success) {}
140    }
141
142    /**
143     * addAll(null) throws NPE
144     */
145    public void testAddAll1() {
146        Succeed q = new Succeed();
147        try {
148            q.addAll(null);
149            shouldThrow();
150        } catch (NullPointerException success) {}
151    }
152
153    /**
154     * addAll(this) throws IAE
155     */
156    public void testAddAllSelf() {
157        Succeed q = new Succeed();
158        try {
159            q.addAll(q);
160            shouldThrow();
161        } catch (IllegalArgumentException success) {}
162    }
163
164    /**
165     * addAll of a collection with null elements throws NPE
166     */
167    public void testAddAll2() {
168        Succeed q = new Succeed();
169        Integer[] ints = new Integer[SIZE];
170        try {
171            q.addAll(Arrays.asList(ints));
172            shouldThrow();
173        } catch (NullPointerException success) {}
174    }
175
176    /**
177     * addAll of a collection with any null elements throws NPE after
178     * possibly adding some elements
179     */
180    public void testAddAll3() {
181        Succeed q = new Succeed();
182        Integer[] ints = new Integer[SIZE];
183        for (int i = 0; i < SIZE - 1; ++i)
184            ints[i] = new Integer(i);
185        try {
186            q.addAll(Arrays.asList(ints));
187            shouldThrow();
188        } catch (NullPointerException success) {}
189    }
190
191    /**
192     * addAll throws ISE if an add fails
193     */
194    public void testAddAll4() {
195        Fail q = new Fail();
196        Integer[] ints = new Integer[SIZE];
197        for (int i = 0; i < SIZE; ++i)
198            ints[i] = new Integer(i);
199        try {
200            q.addAll(Arrays.asList(ints));
201            shouldThrow();
202        } catch (IllegalStateException success) {}
203    }
204
205}
206