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