1 |
/* |
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/publicdomain/zero/1.0/ |
5 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
* Pat Fisher, Mike Judd. |
7 |
*/ |
8 |
|
9 |
import junit.framework.*; |
10 |
import java.util.*; |
11 |
import java.util.concurrent.*; |
12 |
import java.util.concurrent.locks.*; |
13 |
import java.io.*; |
14 |
|
15 |
public class AbstractQueueTest extends JSR166TestCase { |
16 |
public static void main(String[] args) { |
17 |
junit.textui.TestRunner.run(suite()); |
18 |
} |
19 |
public static Test suite() { |
20 |
return new TestSuite(AbstractQueueTest.class); |
21 |
} |
22 |
|
23 |
static class Succeed extends AbstractQueue<Integer> { |
24 |
public boolean offer(Integer x) { |
25 |
if (x == null) throw new NullPointerException(); |
26 |
return true; |
27 |
} |
28 |
public Integer peek() { return one; } |
29 |
public Integer poll() { return one; } |
30 |
public int size() { return 0; } |
31 |
public Iterator iterator() { return null; } // not needed |
32 |
} |
33 |
|
34 |
static class Fail extends AbstractQueue<Integer> { |
35 |
public boolean offer(Integer x) { |
36 |
if (x == null) throw new NullPointerException(); |
37 |
return false; |
38 |
} |
39 |
public Integer peek() { return null; } |
40 |
public Integer poll() { return null; } |
41 |
public int size() { return 0; } |
42 |
public Iterator iterator() { return null; } // not needed |
43 |
} |
44 |
|
45 |
/** |
46 |
* add returns true if offer succeeds |
47 |
*/ |
48 |
public void testAddS() { |
49 |
Succeed q = new Succeed(); |
50 |
assertTrue(q.add(two)); |
51 |
} |
52 |
|
53 |
/** |
54 |
* add throws ISE true if offer fails |
55 |
*/ |
56 |
public void testAddF() { |
57 |
Fail q = new Fail(); |
58 |
try { |
59 |
q.add(one); |
60 |
shouldThrow(); |
61 |
} catch (IllegalStateException success) {} |
62 |
} |
63 |
|
64 |
/** |
65 |
* add throws NPE if offer does |
66 |
*/ |
67 |
public void testAddNPE() { |
68 |
Succeed q = new Succeed(); |
69 |
try { |
70 |
q.add(null); |
71 |
shouldThrow(); |
72 |
} catch (NullPointerException success) {} |
73 |
} |
74 |
|
75 |
/** |
76 |
* remove returns normally if poll succeeds |
77 |
*/ |
78 |
public void testRemoveS() { |
79 |
Succeed q = new Succeed(); |
80 |
q.remove(); |
81 |
} |
82 |
|
83 |
/** |
84 |
* remove throws NSEE if poll returns null |
85 |
*/ |
86 |
public void testRemoveF() { |
87 |
Fail q = new Fail(); |
88 |
try { |
89 |
q.remove(); |
90 |
shouldThrow(); |
91 |
} catch (NoSuchElementException success) {} |
92 |
} |
93 |
|
94 |
/** |
95 |
* element returns normally if peek succeeds |
96 |
*/ |
97 |
public void testElementS() { |
98 |
Succeed q = new Succeed(); |
99 |
q.element(); |
100 |
} |
101 |
|
102 |
/** |
103 |
* element throws NSEE if peek returns null |
104 |
*/ |
105 |
public void testElementF() { |
106 |
Fail q = new Fail(); |
107 |
try { |
108 |
q.element(); |
109 |
shouldThrow(); |
110 |
} catch (NoSuchElementException success) {} |
111 |
} |
112 |
|
113 |
/** |
114 |
* addAll(null) throws NPE |
115 |
*/ |
116 |
public void testAddAll1() { |
117 |
try { |
118 |
Succeed q = new Succeed(); |
119 |
q.addAll(null); |
120 |
shouldThrow(); |
121 |
} catch (NullPointerException success) {} |
122 |
} |
123 |
|
124 |
/** |
125 |
* addAll(this) throws IAE |
126 |
*/ |
127 |
public void testAddAllSelf() { |
128 |
try { |
129 |
Succeed q = new Succeed(); |
130 |
q.addAll(q); |
131 |
shouldThrow(); |
132 |
} catch (IllegalArgumentException success) {} |
133 |
} |
134 |
|
135 |
/** |
136 |
* addAll of a collection with null elements throws NPE |
137 |
*/ |
138 |
public void testAddAll2() { |
139 |
try { |
140 |
Succeed q = new Succeed(); |
141 |
Integer[] ints = new Integer[SIZE]; |
142 |
q.addAll(Arrays.asList(ints)); |
143 |
shouldThrow(); |
144 |
} catch (NullPointerException success) {} |
145 |
} |
146 |
|
147 |
/** |
148 |
* addAll of a collection with any null elements throws NPE after |
149 |
* possibly adding some elements |
150 |
*/ |
151 |
public void testAddAll3() { |
152 |
try { |
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)); |
158 |
shouldThrow(); |
159 |
} catch (NullPointerException success) {} |
160 |
} |
161 |
|
162 |
/** |
163 |
* addAll throws ISE if an add fails |
164 |
*/ |
165 |
public void testAddAll4() { |
166 |
try { |
167 |
Fail q = new Fail(); |
168 |
Integer[] ints = new Integer[SIZE]; |
169 |
for (int i = 0; i < SIZE; ++i) |
170 |
ints[i] = new Integer(i); |
171 |
q.addAll(Arrays.asList(ints)); |
172 |
shouldThrow(); |
173 |
} catch (IllegalStateException success) {} |
174 |
} |
175 |
|
176 |
} |