2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/licenses/publicdomain
5 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 * Pat Fisher, Mike Judd.
9 package tests.api.java.util.concurrent; // android-added
11 import junit.framework.*;
13 import java.util.concurrent.*;
14 import java.util.concurrent.locks.*;
17 public class AbstractQueueTest extends JSR166TestCase {
18 public static Test suite() {
19 return new TestSuite(AbstractQueueTest.class);
22 static class Succeed extends AbstractQueue<Integer> {
23 public boolean offer(Integer x) {
24 if (x == null) throw new NullPointerException();
27 public Integer peek() { return one; }
28 public Integer poll() { return one; }
29 public int size() { return 0; }
30 public Iterator iterator() { return null; } // not needed
33 static class Fail extends AbstractQueue<Integer> {
34 public boolean offer(Integer x) {
35 if (x == null) throw new NullPointerException();
38 public Integer peek() { return null; }
39 public Integer poll() { return null; }
40 public int size() { return 0; }
41 public Iterator iterator() { return null; } // not needed
45 * add returns true if offer succeeds
47 public void testAddS() {
48 Succeed q = new Succeed();
49 assertTrue(q.add(two));
53 * add throws ISE true if offer fails
55 public void testAddF() {
60 } catch (IllegalStateException success) {}
64 * add throws NPE if offer does
66 public void testAddNPE() {
67 Succeed q = new Succeed();
71 } catch (NullPointerException success) {}
75 * remove returns normally if poll succeeds
77 public void testRemoveS() {
78 Succeed q = new Succeed();
83 * remove throws NSEE if poll returns null
85 public void testRemoveF() {
90 } catch (NoSuchElementException success) {}
95 * element returns normally if peek succeeds
97 public void testElementS() {
98 Succeed q = new Succeed();
103 * element throws NSEE if peek returns null
105 public void testElementF() {
110 } catch (NoSuchElementException success) {}
114 * addAll(null) throws NPE
116 public void testAddAll1() {
118 Succeed q = new Succeed();
121 } catch (NullPointerException success) {}
125 * addAll(this) throws IAE
127 public void testAddAllSelf() {
129 Succeed q = new Succeed();
132 } catch (IllegalArgumentException success) {}
137 * addAll of a collection with null elements throws NPE
139 public void testAddAll2() {
141 Succeed q = new Succeed();
142 Integer[] ints = new Integer[SIZE];
143 q.addAll(Arrays.asList(ints));
145 } catch (NullPointerException success) {}
148 * addAll of a collection with any null elements throws NPE after
149 * possibly adding some elements
151 public void testAddAll3() {
153 Succeed q = new Succeed();
154 Integer[] ints = new Integer[SIZE];
155 for (int i = 0; i < SIZE-1; ++i)
156 ints[i] = new Integer(i);
157 q.addAll(Arrays.asList(ints));
159 } catch (NullPointerException success) {}
162 * addAll throws ISE if an add fails
164 public void testAddAll4() {
167 Integer[] ints = new Integer[SIZE];
168 for (int i = 0; i < SIZE; ++i)
169 ints[i] = new Integer(i);
170 q.addAll(Arrays.asList(ints));
172 } catch (IllegalStateException success) {}