7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.*; |
11 |
< |
import java.util.concurrent.*; |
10 |
> |
import java.util.Arrays; |
11 |
> |
import java.util.ArrayList; |
12 |
> |
import java.util.Collection; |
13 |
> |
import java.util.Iterator; |
14 |
> |
import java.util.NoSuchElementException; |
15 |
> |
import java.util.Queue; |
16 |
> |
import java.util.concurrent.ArrayBlockingQueue; |
17 |
> |
import java.util.concurrent.BlockingQueue; |
18 |
> |
import java.util.concurrent.CountDownLatch; |
19 |
> |
import java.util.concurrent.Executors; |
20 |
> |
import java.util.concurrent.ExecutorService; |
21 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
– |
import java.io.*; |
22 |
|
|
23 |
|
public class ArrayBlockingQueueTest extends JSR166TestCase { |
24 |
|
|
25 |
|
public static class Fair extends BlockingQueueTest { |
26 |
|
protected BlockingQueue emptyCollection() { |
27 |
< |
return new ArrayBlockingQueue(20, true); |
27 |
> |
return new ArrayBlockingQueue(SIZE, true); |
28 |
|
} |
29 |
|
} |
30 |
|
|
31 |
|
public static class NonFair extends BlockingQueueTest { |
32 |
|
protected BlockingQueue emptyCollection() { |
33 |
< |
return new ArrayBlockingQueue(20, false); |
33 |
> |
return new ArrayBlockingQueue(SIZE, false); |
34 |
|
} |
35 |
|
} |
36 |
|
|
71 |
|
*/ |
72 |
|
public void testConstructor2() { |
73 |
|
try { |
74 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(0); |
74 |
> |
new ArrayBlockingQueue(0); |
75 |
|
shouldThrow(); |
76 |
|
} catch (IllegalArgumentException success) {} |
77 |
|
} |
81 |
|
*/ |
82 |
|
public void testConstructor3() { |
83 |
|
try { |
84 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null); |
84 |
> |
new ArrayBlockingQueue(1, true, null); |
85 |
|
shouldThrow(); |
86 |
|
} catch (NullPointerException success) {} |
87 |
|
} |
90 |
|
* Initializing from Collection of null elements throws NPE |
91 |
|
*/ |
92 |
|
public void testConstructor4() { |
93 |
+ |
Collection<Integer> elements = Arrays.asList(new Integer[SIZE]); |
94 |
|
try { |
95 |
< |
Integer[] ints = new Integer[SIZE]; |
87 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
95 |
> |
new ArrayBlockingQueue(SIZE, false, elements); |
96 |
|
shouldThrow(); |
97 |
|
} catch (NullPointerException success) {} |
98 |
|
} |
101 |
|
* Initializing from Collection with some null elements throws NPE |
102 |
|
*/ |
103 |
|
public void testConstructor5() { |
104 |
+ |
Integer[] ints = new Integer[SIZE]; |
105 |
+ |
for (int i = 0; i < SIZE-1; ++i) |
106 |
+ |
ints[i] = i; |
107 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
108 |
|
try { |
109 |
< |
Integer[] ints = new Integer[SIZE]; |
98 |
< |
for (int i = 0; i < SIZE-1; ++i) |
99 |
< |
ints[i] = new Integer(i); |
100 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
109 |
> |
new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
110 |
|
shouldThrow(); |
111 |
|
} catch (NullPointerException success) {} |
112 |
|
} |
115 |
|
* Initializing from too large collection throws IAE |
116 |
|
*/ |
117 |
|
public void testConstructor6() { |
118 |
+ |
Integer[] ints = new Integer[SIZE]; |
119 |
+ |
for (int i = 0; i < SIZE; ++i) |
120 |
+ |
ints[i] = i; |
121 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
122 |
|
try { |
123 |
< |
Integer[] ints = new Integer[SIZE]; |
111 |
< |
for (int i = 0; i < SIZE; ++i) |
112 |
< |
ints[i] = new Integer(i); |
113 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints)); |
123 |
> |
new ArrayBlockingQueue(SIZE - 1, false, elements); |
124 |
|
shouldThrow(); |
125 |
|
} catch (IllegalArgumentException success) {} |
126 |
|
} |
131 |
|
public void testConstructor7() { |
132 |
|
Integer[] ints = new Integer[SIZE]; |
133 |
|
for (int i = 0; i < SIZE; ++i) |
134 |
< |
ints[i] = new Integer(i); |
135 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints)); |
134 |
> |
ints[i] = i; |
135 |
> |
Collection<Integer> elements = Arrays.asList(ints); |
136 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, elements); |
137 |
|
for (int i = 0; i < SIZE; ++i) |
138 |
|
assertEquals(ints[i], q.poll()); |
139 |
|
} |
171 |
|
} |
172 |
|
|
173 |
|
/** |
163 |
– |
* offer(null) throws NPE |
164 |
– |
*/ |
165 |
– |
public void testOfferNull() { |
166 |
– |
try { |
167 |
– |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
168 |
– |
q.offer(null); |
169 |
– |
shouldThrow(); |
170 |
– |
} catch (NullPointerException success) {} |
171 |
– |
} |
172 |
– |
|
173 |
– |
/** |
174 |
– |
* add(null) throws NPE |
175 |
– |
*/ |
176 |
– |
public void testAddNull() { |
177 |
– |
try { |
178 |
– |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
179 |
– |
q.add(null); |
180 |
– |
shouldThrow(); |
181 |
– |
} catch (NullPointerException success) {} |
182 |
– |
} |
183 |
– |
|
184 |
– |
/** |
174 |
|
* Offer succeeds if not full; fails if full |
175 |
|
*/ |
176 |
|
public void testOffer() { |
195 |
|
} |
196 |
|
|
197 |
|
/** |
209 |
– |
* addAll(null) throws NPE |
210 |
– |
*/ |
211 |
– |
public void testAddAll1() { |
212 |
– |
try { |
213 |
– |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
214 |
– |
q.addAll(null); |
215 |
– |
shouldThrow(); |
216 |
– |
} catch (NullPointerException success) {} |
217 |
– |
} |
218 |
– |
|
219 |
– |
/** |
198 |
|
* addAll(this) throws IAE |
199 |
|
*/ |
200 |
|
public void testAddAllSelf() { |
206 |
|
} |
207 |
|
|
208 |
|
/** |
231 |
– |
* addAll of a collection with null elements throws NPE |
232 |
– |
*/ |
233 |
– |
public void testAddAll2() { |
234 |
– |
try { |
235 |
– |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
236 |
– |
Integer[] ints = new Integer[SIZE]; |
237 |
– |
q.addAll(Arrays.asList(ints)); |
238 |
– |
shouldThrow(); |
239 |
– |
} catch (NullPointerException success) {} |
240 |
– |
} |
241 |
– |
|
242 |
– |
/** |
209 |
|
* addAll of a collection with any null elements throws NPE after |
210 |
|
* possibly adding some elements |
211 |
|
*/ |
250 |
|
} |
251 |
|
|
252 |
|
/** |
287 |
– |
* put(null) throws NPE |
288 |
– |
*/ |
289 |
– |
public void testPutNull() throws InterruptedException { |
290 |
– |
try { |
291 |
– |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
292 |
– |
q.put(null); |
293 |
– |
shouldThrow(); |
294 |
– |
} catch (NullPointerException success) {} |
295 |
– |
} |
296 |
– |
|
297 |
– |
/** |
253 |
|
* all elements successfully put are contained |
254 |
|
*/ |
255 |
|
public void testPut() throws InterruptedException { |
515 |
|
} |
516 |
|
|
517 |
|
/** |
563 |
– |
* remove(x) removes x and returns true if present |
564 |
– |
*/ |
565 |
– |
public void testRemoveElement() { |
566 |
– |
ArrayBlockingQueue q = populatedQueue(SIZE); |
567 |
– |
for (int i = 1; i < SIZE; i+=2) { |
568 |
– |
assertTrue(q.remove(new Integer(i))); |
569 |
– |
} |
570 |
– |
for (int i = 0; i < SIZE; i+=2) { |
571 |
– |
assertTrue(q.remove(new Integer(i))); |
572 |
– |
assertFalse(q.remove(new Integer(i+1))); |
573 |
– |
} |
574 |
– |
assertTrue(q.isEmpty()); |
575 |
– |
} |
576 |
– |
|
577 |
– |
/** |
518 |
|
* contains(x) reports true when elements added but not yet removed |
519 |
|
*/ |
520 |
|
public void testContains() { |
614 |
|
} |
615 |
|
|
616 |
|
/** |
677 |
– |
* toArray(null) throws NullPointerException |
678 |
– |
*/ |
679 |
– |
public void testToArray_NullArg() { |
680 |
– |
ArrayBlockingQueue q = populatedQueue(SIZE); |
681 |
– |
try { |
682 |
– |
q.toArray(null); |
683 |
– |
shouldThrow(); |
684 |
– |
} catch (NullPointerException success) {} |
685 |
– |
} |
686 |
– |
|
687 |
– |
/** |
617 |
|
* toArray(incompatible array type) throws ArrayStoreException |
618 |
|
*/ |
619 |
|
public void testToArray1_BadArg() { |
753 |
|
* A deserialized serialized queue has same elements in same order |
754 |
|
*/ |
755 |
|
public void testSerialization() throws Exception { |
756 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
757 |
< |
|
829 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
830 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
831 |
< |
out.writeObject(q); |
832 |
< |
out.close(); |
833 |
< |
|
834 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
835 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
836 |
< |
ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject(); |
837 |
< |
assertEquals(q.size(), r.size()); |
838 |
< |
while (!q.isEmpty()) |
839 |
< |
assertEquals(q.remove(), r.remove()); |
840 |
< |
} |
841 |
< |
|
842 |
< |
/** |
843 |
< |
* drainTo(null) throws NPE |
844 |
< |
*/ |
845 |
< |
public void testDrainToNull() { |
846 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
847 |
< |
try { |
848 |
< |
q.drainTo(null); |
849 |
< |
shouldThrow(); |
850 |
< |
} catch (NullPointerException success) {} |
851 |
< |
} |
756 |
> |
Queue x = populatedQueue(SIZE); |
757 |
> |
Queue y = serialClone(x); |
758 |
|
|
759 |
< |
/** |
760 |
< |
* drainTo(this) throws IAE |
761 |
< |
*/ |
762 |
< |
public void testDrainToSelf() { |
763 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
764 |
< |
try { |
765 |
< |
q.drainTo(q); |
766 |
< |
shouldThrow(); |
767 |
< |
} catch (IllegalArgumentException success) {} |
759 |
> |
assertTrue(x != y); |
760 |
> |
assertEquals(x.size(), y.size()); |
761 |
> |
assertEquals(x.toString(), y.toString()); |
762 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
763 |
> |
while (!x.isEmpty()) { |
764 |
> |
assertFalse(y.isEmpty()); |
765 |
> |
assertEquals(x.remove(), y.remove()); |
766 |
> |
} |
767 |
> |
assertTrue(y.isEmpty()); |
768 |
|
} |
769 |
|
|
770 |
|
/** |
812 |
|
} |
813 |
|
|
814 |
|
/** |
909 |
– |
* drainTo(null, n) throws NPE |
910 |
– |
*/ |
911 |
– |
public void testDrainToNullN() { |
912 |
– |
ArrayBlockingQueue q = populatedQueue(SIZE); |
913 |
– |
try { |
914 |
– |
q.drainTo(null, 0); |
915 |
– |
shouldThrow(); |
916 |
– |
} catch (NullPointerException success) {} |
917 |
– |
} |
918 |
– |
|
919 |
– |
/** |
920 |
– |
* drainTo(this, n) throws IAE |
921 |
– |
*/ |
922 |
– |
public void testDrainToSelfN() { |
923 |
– |
ArrayBlockingQueue q = populatedQueue(SIZE); |
924 |
– |
try { |
925 |
– |
q.drainTo(q, 0); |
926 |
– |
shouldThrow(); |
927 |
– |
} catch (IllegalArgumentException success) {} |
928 |
– |
} |
929 |
– |
|
930 |
– |
/** |
815 |
|
* drainTo(c, n) empties first min(n, size) elements of queue into c |
816 |
|
*/ |
817 |
|
public void testDrainToN() { |