5 |
|
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
6 |
|
*/ |
7 |
|
|
8 |
+ |
|
9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
12 |
|
import java.io.*; |
13 |
|
|
14 |
|
public class ArrayBlockingQueueTest extends JSR166TestCase { |
14 |
– |
|
15 |
|
public static void main(String[] args) { |
16 |
|
junit.textui.TestRunner.run (suite()); |
17 |
|
} |
18 |
– |
|
18 |
|
public static Test suite() { |
19 |
|
return new TestSuite(ArrayBlockingQueueTest.class); |
20 |
|
} |
34 |
|
return q; |
35 |
|
} |
36 |
|
|
37 |
< |
public void testConstructor1(){ |
37 |
> |
/** |
38 |
> |
* |
39 |
> |
*/ |
40 |
> |
public void testConstructor1() { |
41 |
|
assertEquals(SIZE, new ArrayBlockingQueue(SIZE).remainingCapacity()); |
42 |
|
} |
43 |
|
|
44 |
< |
public void testConstructor2(){ |
44 |
> |
/** |
45 |
> |
* |
46 |
> |
*/ |
47 |
> |
public void testConstructor2() { |
48 |
|
try { |
49 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(0); |
50 |
< |
fail("Cannot make zero-sized"); |
50 |
> |
shouldThrow(); |
51 |
|
} |
52 |
|
catch (IllegalArgumentException success) {} |
53 |
|
} |
54 |
|
|
55 |
< |
public void testConstructor3(){ |
55 |
> |
/** |
56 |
> |
* |
57 |
> |
*/ |
58 |
> |
public void testConstructor3() { |
59 |
|
|
60 |
|
try { |
61 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null); |
62 |
< |
fail("Cannot make from null collection"); |
62 |
> |
shouldThrow(); |
63 |
|
} |
64 |
|
catch (NullPointerException success) {} |
65 |
|
} |
66 |
|
|
67 |
< |
public void testConstructor4(){ |
67 |
> |
/** |
68 |
> |
* |
69 |
> |
*/ |
70 |
> |
public void testConstructor4() { |
71 |
|
try { |
72 |
|
Integer[] ints = new Integer[SIZE]; |
73 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
74 |
< |
fail("Cannot make with null elements"); |
74 |
> |
shouldThrow(); |
75 |
|
} |
76 |
|
catch (NullPointerException success) {} |
77 |
|
} |
78 |
|
|
79 |
< |
public void testConstructor5(){ |
79 |
> |
/** |
80 |
> |
* |
81 |
> |
*/ |
82 |
> |
public void testConstructor5() { |
83 |
|
try { |
84 |
|
Integer[] ints = new Integer[SIZE]; |
85 |
|
for (int i = 0; i < SIZE-1; ++i) |
86 |
|
ints[i] = new Integer(i); |
87 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
88 |
< |
fail("Cannot make with null elements"); |
88 |
> |
shouldThrow(); |
89 |
|
} |
90 |
|
catch (NullPointerException success) {} |
91 |
|
} |
92 |
|
|
93 |
< |
public void testConstructor6(){ |
93 |
> |
/** |
94 |
> |
* |
95 |
> |
*/ |
96 |
> |
public void testConstructor6() { |
97 |
|
try { |
98 |
|
Integer[] ints = new Integer[SIZE]; |
99 |
|
for (int i = 0; i < SIZE; ++i) |
100 |
|
ints[i] = new Integer(i); |
101 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints)); |
102 |
< |
fail("Cannot make with insufficient capacity"); |
102 |
> |
shouldThrow(); |
103 |
|
} |
104 |
|
catch (IllegalArgumentException success) {} |
105 |
|
} |
106 |
|
|
107 |
< |
public void testConstructor7(){ |
107 |
> |
/** |
108 |
> |
* |
109 |
> |
*/ |
110 |
> |
public void testConstructor7() { |
111 |
|
try { |
112 |
|
Integer[] ints = new Integer[SIZE]; |
113 |
|
for (int i = 0; i < SIZE; ++i) |
119 |
|
finally {} |
120 |
|
} |
121 |
|
|
122 |
+ |
/** |
123 |
+ |
* |
124 |
+ |
*/ |
125 |
|
public void testEmptyFull() { |
126 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
127 |
|
assertTrue(q.isEmpty()); |
128 |
< |
assertEquals("should have room for 2", 2, q.remainingCapacity()); |
128 |
> |
assertEquals(2, q.remainingCapacity()); |
129 |
|
q.add(one); |
130 |
|
assertFalse(q.isEmpty()); |
131 |
|
q.add(two); |
132 |
|
assertFalse(q.isEmpty()); |
133 |
< |
assertEquals("queue should be full", 0, q.remainingCapacity()); |
134 |
< |
assertFalse("offer should be rejected", q.offer(three)); |
133 |
> |
assertEquals(0, q.remainingCapacity()); |
134 |
> |
assertFalse(q.offer(three)); |
135 |
|
} |
136 |
|
|
137 |
< |
public void testRemainingCapacity(){ |
137 |
> |
/** |
138 |
> |
* |
139 |
> |
*/ |
140 |
> |
public void testRemainingCapacity() { |
141 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
142 |
|
for (int i = 0; i < SIZE; ++i) { |
143 |
|
assertEquals(i, q.remainingCapacity()); |
151 |
|
} |
152 |
|
} |
153 |
|
|
154 |
< |
public void testOfferNull(){ |
154 |
> |
/** |
155 |
> |
* |
156 |
> |
*/ |
157 |
> |
public void testOfferNull() { |
158 |
|
try { |
159 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
160 |
|
q.offer(null); |
161 |
< |
fail("should throw NPE"); |
161 |
> |
shouldThrow(); |
162 |
|
} catch (NullPointerException success) { } |
163 |
|
} |
164 |
|
|
165 |
< |
public void testOffer(){ |
165 |
> |
/** |
166 |
> |
* |
167 |
> |
*/ |
168 |
> |
public void testOffer() { |
169 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
170 |
|
assertTrue(q.offer(zero)); |
171 |
|
assertFalse(q.offer(one)); |
172 |
|
} |
173 |
|
|
174 |
< |
public void testAdd(){ |
174 |
> |
/** |
175 |
> |
* |
176 |
> |
*/ |
177 |
> |
public void testAdd() { |
178 |
|
try { |
179 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
180 |
|
for (int i = 0; i < SIZE; ++i) { |
186 |
|
} |
187 |
|
} |
188 |
|
|
189 |
< |
public void testAddAll1(){ |
189 |
> |
/** |
190 |
> |
* |
191 |
> |
*/ |
192 |
> |
public void testAddAll1() { |
193 |
|
try { |
194 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
195 |
|
q.addAll(null); |
196 |
< |
fail("Cannot add null collection"); |
196 |
> |
shouldThrow(); |
197 |
|
} |
198 |
|
catch (NullPointerException success) {} |
199 |
|
} |
200 |
< |
public void testAddAll2(){ |
200 |
> |
/** |
201 |
> |
* |
202 |
> |
*/ |
203 |
> |
public void testAddAll2() { |
204 |
|
try { |
205 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
206 |
|
Integer[] ints = new Integer[SIZE]; |
207 |
|
q.addAll(Arrays.asList(ints)); |
208 |
< |
fail("Cannot add null elements"); |
208 |
> |
shouldThrow(); |
209 |
|
} |
210 |
|
catch (NullPointerException success) {} |
211 |
|
} |
212 |
< |
public void testAddAll3(){ |
212 |
> |
/** |
213 |
> |
* |
214 |
> |
*/ |
215 |
> |
public void testAddAll3() { |
216 |
|
try { |
217 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
218 |
|
Integer[] ints = new Integer[SIZE]; |
219 |
|
for (int i = 0; i < SIZE-1; ++i) |
220 |
|
ints[i] = new Integer(i); |
221 |
|
q.addAll(Arrays.asList(ints)); |
222 |
< |
fail("Cannot add null elements"); |
222 |
> |
shouldThrow(); |
223 |
|
} |
224 |
|
catch (NullPointerException success) {} |
225 |
|
} |
226 |
< |
public void testAddAll4(){ |
226 |
> |
/** |
227 |
> |
* |
228 |
> |
*/ |
229 |
> |
public void testAddAll4() { |
230 |
|
try { |
231 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
232 |
|
Integer[] ints = new Integer[SIZE]; |
233 |
|
for (int i = 0; i < SIZE; ++i) |
234 |
|
ints[i] = new Integer(i); |
235 |
|
q.addAll(Arrays.asList(ints)); |
236 |
< |
fail("Cannot add with insufficient capacity"); |
236 |
> |
shouldThrow(); |
237 |
|
} |
238 |
|
catch (IllegalStateException success) {} |
239 |
|
} |
240 |
< |
public void testAddAll5(){ |
240 |
> |
/** |
241 |
> |
* |
242 |
> |
*/ |
243 |
> |
public void testAddAll5() { |
244 |
|
try { |
245 |
|
Integer[] empty = new Integer[0]; |
246 |
|
Integer[] ints = new Integer[SIZE]; |
255 |
|
finally {} |
256 |
|
} |
257 |
|
|
258 |
+ |
/** |
259 |
+ |
* |
260 |
+ |
*/ |
261 |
|
public void testPutNull() { |
262 |
|
try { |
263 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
264 |
|
q.put(null); |
265 |
< |
fail("put should throw NPE"); |
265 |
> |
shouldThrow(); |
266 |
|
} |
267 |
|
catch (NullPointerException success){ |
268 |
|
} |
269 |
|
catch (InterruptedException ie) { |
270 |
< |
fail("Unexpected exception"); |
270 |
> |
unexpectedException(); |
271 |
|
} |
272 |
|
} |
273 |
|
|
274 |
+ |
/** |
275 |
+ |
* |
276 |
+ |
*/ |
277 |
|
public void testPut() { |
278 |
|
try { |
279 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
285 |
|
assertEquals(0, q.remainingCapacity()); |
286 |
|
} |
287 |
|
catch (InterruptedException ie) { |
288 |
< |
fail("Unexpected exception"); |
288 |
> |
unexpectedException(); |
289 |
|
} |
290 |
|
} |
291 |
|
|
292 |
< |
public void testBlockingPut(){ |
292 |
> |
/** |
293 |
> |
* |
294 |
> |
*/ |
295 |
> |
public void testBlockingPut() { |
296 |
|
Thread t = new Thread(new Runnable() { |
297 |
|
public void run() { |
298 |
|
int added = 0; |
303 |
|
++added; |
304 |
|
} |
305 |
|
q.put(new Integer(SIZE)); |
306 |
< |
threadFail("put should block"); |
306 |
> |
threadShouldThrow(); |
307 |
|
} catch (InterruptedException ie){ |
308 |
|
threadAssertEquals(added, SIZE); |
309 |
|
} |
315 |
|
t.join(); |
316 |
|
} |
317 |
|
catch (InterruptedException ie) { |
318 |
< |
fail("Unexpected exception"); |
318 |
> |
unexpectedException(); |
319 |
|
} |
320 |
|
} |
321 |
|
|
322 |
+ |
/** |
323 |
+ |
* |
324 |
+ |
*/ |
325 |
|
public void testPutWithTake() { |
326 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
327 |
|
Thread t = new Thread(new Runnable() { |
328 |
< |
public void run(){ |
328 |
> |
public void run() { |
329 |
|
int added = 0; |
330 |
|
try { |
331 |
|
q.put(new Object()); |
336 |
|
++added; |
337 |
|
q.put(new Object()); |
338 |
|
++added; |
339 |
< |
threadFail("Should block"); |
339 |
> |
threadShouldThrow(); |
340 |
|
} catch (InterruptedException e){ |
341 |
|
threadAssertTrue(added >= 2); |
342 |
|
} |
349 |
|
t.interrupt(); |
350 |
|
t.join(); |
351 |
|
} catch (Exception e){ |
352 |
< |
fail("Unexpected exception"); |
352 |
> |
unexpectedException(); |
353 |
|
} |
354 |
|
} |
355 |
|
|
356 |
+ |
/** |
357 |
+ |
* |
358 |
+ |
*/ |
359 |
|
public void testTimedOffer() { |
360 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
361 |
|
Thread t = new Thread(new Runnable() { |
362 |
< |
public void run(){ |
362 |
> |
public void run() { |
363 |
|
try { |
364 |
|
q.put(new Object()); |
365 |
|
q.put(new Object()); |
366 |
|
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS)); |
367 |
|
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
368 |
< |
threadFail("Should block"); |
368 |
> |
threadShouldThrow(); |
369 |
|
} catch (InterruptedException success){} |
370 |
|
} |
371 |
|
}); |
376 |
|
t.interrupt(); |
377 |
|
t.join(); |
378 |
|
} catch (Exception e){ |
379 |
< |
fail("Unexpected exception"); |
379 |
> |
unexpectedException(); |
380 |
|
} |
381 |
|
} |
382 |
|
|
383 |
< |
public void testTake(){ |
383 |
> |
/** |
384 |
> |
* |
385 |
> |
*/ |
386 |
> |
public void testTake() { |
387 |
|
try { |
388 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
389 |
|
for (int i = 0; i < SIZE; ++i) { |
390 |
|
assertEquals(i, ((Integer)q.take()).intValue()); |
391 |
|
} |
392 |
|
} catch (InterruptedException e){ |
393 |
< |
fail("Unexpected exception"); |
393 |
> |
unexpectedException(); |
394 |
|
} |
395 |
|
} |
396 |
|
|
397 |
+ |
/** |
398 |
+ |
* |
399 |
+ |
*/ |
400 |
|
public void testTakeFromEmpty() { |
401 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
402 |
|
Thread t = new Thread(new Runnable() { |
403 |
< |
public void run(){ |
403 |
> |
public void run() { |
404 |
|
try { |
405 |
|
q.take(); |
406 |
< |
threadFail("Should block"); |
406 |
> |
threadShouldThrow(); |
407 |
|
} catch (InterruptedException success){ } |
408 |
|
} |
409 |
|
}); |
413 |
|
t.interrupt(); |
414 |
|
t.join(); |
415 |
|
} catch (Exception e){ |
416 |
< |
fail("Unexpected exception"); |
416 |
> |
unexpectedException(); |
417 |
|
} |
418 |
|
} |
419 |
|
|
420 |
< |
public void testBlockingTake(){ |
420 |
> |
/** |
421 |
> |
* |
422 |
> |
*/ |
423 |
> |
public void testBlockingTake() { |
424 |
|
Thread t = new Thread(new Runnable() { |
425 |
|
public void run() { |
426 |
|
try { |
429 |
|
threadAssertEquals(i, ((Integer)q.take()).intValue()); |
430 |
|
} |
431 |
|
q.take(); |
432 |
< |
threadFail("take should block"); |
432 |
> |
threadShouldThrow(); |
433 |
|
} catch (InterruptedException success){ |
434 |
|
} |
435 |
|
}}); |
440 |
|
t.join(); |
441 |
|
} |
442 |
|
catch (InterruptedException ie) { |
443 |
< |
fail("Unexpected exception"); |
443 |
> |
unexpectedException(); |
444 |
|
} |
445 |
|
} |
446 |
|
|
447 |
|
|
448 |
< |
public void testPoll(){ |
448 |
> |
/** |
449 |
> |
* |
450 |
> |
*/ |
451 |
> |
public void testPoll() { |
452 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
453 |
|
for (int i = 0; i < SIZE; ++i) { |
454 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
456 |
|
assertNull(q.poll()); |
457 |
|
} |
458 |
|
|
459 |
+ |
/** |
460 |
+ |
* |
461 |
+ |
*/ |
462 |
|
public void testTimedPoll0() { |
463 |
|
try { |
464 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
467 |
|
} |
468 |
|
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
469 |
|
} catch (InterruptedException e){ |
470 |
< |
fail("Unexpected exception"); |
470 |
> |
unexpectedException(); |
471 |
|
} |
472 |
|
} |
473 |
|
|
474 |
+ |
/** |
475 |
+ |
* |
476 |
+ |
*/ |
477 |
|
public void testTimedPoll() { |
478 |
|
try { |
479 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
482 |
|
} |
483 |
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
484 |
|
} catch (InterruptedException e){ |
485 |
< |
fail("Unexpected exception"); |
485 |
> |
unexpectedException(); |
486 |
|
} |
487 |
|
} |
488 |
|
|
489 |
< |
public void testInterruptedTimedPoll(){ |
489 |
> |
/** |
490 |
> |
* |
491 |
> |
*/ |
492 |
> |
public void testInterruptedTimedPoll() { |
493 |
|
Thread t = new Thread(new Runnable() { |
494 |
|
public void run() { |
495 |
|
try { |
508 |
|
t.join(); |
509 |
|
} |
510 |
|
catch (InterruptedException ie) { |
511 |
< |
fail("Unexpected exception"); |
511 |
> |
unexpectedException(); |
512 |
|
} |
513 |
|
} |
514 |
|
|
515 |
< |
public void testTimedPollWithOffer(){ |
515 |
> |
/** |
516 |
> |
* |
517 |
> |
*/ |
518 |
> |
public void testTimedPollWithOffer() { |
519 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
520 |
|
Thread t = new Thread(new Runnable() { |
521 |
< |
public void run(){ |
521 |
> |
public void run() { |
522 |
|
try { |
523 |
|
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
524 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
525 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
526 |
< |
threadFail("Should block"); |
526 |
> |
threadShouldThrow(); |
527 |
|
} catch (InterruptedException success) { } |
528 |
|
} |
529 |
|
}); |
534 |
|
t.interrupt(); |
535 |
|
t.join(); |
536 |
|
} catch (Exception e){ |
537 |
< |
fail("Unexpected exception"); |
537 |
> |
unexpectedException(); |
538 |
|
} |
539 |
|
} |
540 |
|
|
541 |
|
|
542 |
< |
public void testPeek(){ |
542 |
> |
/** |
543 |
> |
* |
544 |
> |
*/ |
545 |
> |
public void testPeek() { |
546 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
547 |
|
for (int i = 0; i < SIZE; ++i) { |
548 |
|
assertEquals(i, ((Integer)q.peek()).intValue()); |
553 |
|
assertNull(q.peek()); |
554 |
|
} |
555 |
|
|
556 |
< |
public void testElement(){ |
556 |
> |
/** |
557 |
> |
* |
558 |
> |
*/ |
559 |
> |
public void testElement() { |
560 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
561 |
|
for (int i = 0; i < SIZE; ++i) { |
562 |
|
assertEquals(i, ((Integer)q.element()).intValue()); |
564 |
|
} |
565 |
|
try { |
566 |
|
q.element(); |
567 |
< |
fail("no such element"); |
567 |
> |
shouldThrow(); |
568 |
|
} |
569 |
|
catch (NoSuchElementException success) {} |
570 |
|
} |
571 |
|
|
572 |
< |
public void testRemove(){ |
572 |
> |
/** |
573 |
> |
* |
574 |
> |
*/ |
575 |
> |
public void testRemove() { |
576 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
577 |
|
for (int i = 0; i < SIZE; ++i) { |
578 |
|
assertEquals(i, ((Integer)q.remove()).intValue()); |
579 |
|
} |
580 |
|
try { |
581 |
|
q.remove(); |
582 |
< |
fail("remove should throw"); |
582 |
> |
shouldThrow(); |
583 |
|
} catch (NoSuchElementException success){ |
584 |
|
} |
585 |
|
} |
586 |
|
|
587 |
< |
public void testRemoveElement(){ |
587 |
> |
/** |
588 |
> |
* |
589 |
> |
*/ |
590 |
> |
public void testRemoveElement() { |
591 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
592 |
|
for (int i = 1; i < SIZE; i+=2) { |
593 |
|
assertTrue(q.remove(new Integer(i))); |
599 |
|
assertTrue(q.isEmpty()); |
600 |
|
} |
601 |
|
|
602 |
< |
public void testContains(){ |
602 |
> |
/** |
603 |
> |
* |
604 |
> |
*/ |
605 |
> |
public void testContains() { |
606 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
607 |
|
for (int i = 0; i < SIZE; ++i) { |
608 |
|
assertTrue(q.contains(new Integer(i))); |
611 |
|
} |
612 |
|
} |
613 |
|
|
614 |
< |
public void testClear(){ |
614 |
> |
/** |
615 |
> |
* |
616 |
> |
*/ |
617 |
> |
public void testClear() { |
618 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
619 |
|
q.clear(); |
620 |
|
assertTrue(q.isEmpty()); |
626 |
|
assertTrue(q.isEmpty()); |
627 |
|
} |
628 |
|
|
629 |
< |
public void testContainsAll(){ |
629 |
> |
/** |
630 |
> |
* |
631 |
> |
*/ |
632 |
> |
public void testContainsAll() { |
633 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
634 |
|
ArrayBlockingQueue p = new ArrayBlockingQueue(SIZE); |
635 |
|
for (int i = 0; i < SIZE; ++i) { |
640 |
|
assertTrue(p.containsAll(q)); |
641 |
|
} |
642 |
|
|
643 |
< |
public void testRetainAll(){ |
643 |
> |
/** |
644 |
> |
* |
645 |
> |
*/ |
646 |
> |
public void testRetainAll() { |
647 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
648 |
|
ArrayBlockingQueue p = populatedQueue(SIZE); |
649 |
|
for (int i = 0; i < SIZE; ++i) { |
659 |
|
} |
660 |
|
} |
661 |
|
|
662 |
< |
public void testRemoveAll(){ |
662 |
> |
/** |
663 |
> |
* |
664 |
> |
*/ |
665 |
> |
public void testRemoveAll() { |
666 |
|
for (int i = 1; i < SIZE; ++i) { |
667 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
668 |
|
ArrayBlockingQueue p = populatedQueue(i); |
676 |
|
} |
677 |
|
|
678 |
|
|
679 |
< |
public void testToArray(){ |
679 |
> |
/** |
680 |
> |
* |
681 |
> |
*/ |
682 |
> |
public void testToArray() { |
683 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
684 |
|
Object[] o = q.toArray(); |
685 |
|
try { |
686 |
|
for(int i = 0; i < o.length; i++) |
687 |
|
assertEquals(o[i], q.take()); |
688 |
|
} catch (InterruptedException e){ |
689 |
< |
fail("Unexpected exception"); |
689 |
> |
unexpectedException(); |
690 |
|
} |
691 |
|
} |
692 |
|
|
693 |
< |
public void testToArray2(){ |
693 |
> |
/** |
694 |
> |
* |
695 |
> |
*/ |
696 |
> |
public void testToArray2() { |
697 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
698 |
|
Integer[] ints = new Integer[SIZE]; |
699 |
|
ints = (Integer[])q.toArray(ints); |
701 |
|
for(int i = 0; i < ints.length; i++) |
702 |
|
assertEquals(ints[i], q.take()); |
703 |
|
} catch (InterruptedException e){ |
704 |
< |
fail("Unexpected exception"); |
704 |
> |
unexpectedException(); |
705 |
|
} |
706 |
|
} |
707 |
|
|
708 |
< |
public void testIterator(){ |
708 |
> |
/** |
709 |
> |
* |
710 |
> |
*/ |
711 |
> |
public void testIterator() { |
712 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
713 |
|
Iterator it = q.iterator(); |
714 |
|
try { |
716 |
|
assertEquals(it.next(), q.take()); |
717 |
|
} |
718 |
|
} catch (InterruptedException e){ |
719 |
< |
fail("Unexpected exception"); |
719 |
> |
unexpectedException(); |
720 |
|
} |
721 |
|
} |
722 |
|
|
723 |
+ |
/** |
724 |
+ |
* |
725 |
+ |
*/ |
726 |
|
public void testIteratorOrdering() { |
599 |
– |
|
727 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
601 |
– |
|
728 |
|
q.add(one); |
729 |
|
q.add(two); |
730 |
|
q.add(three); |
734 |
|
int k = 0; |
735 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
736 |
|
int i = ((Integer)(it.next())).intValue(); |
737 |
< |
assertEquals("items should come out in order", ++k, i); |
737 |
> |
assertEquals(++k, i); |
738 |
|
} |
739 |
< |
|
614 |
< |
assertEquals("should go through 3 elements", 3, k); |
739 |
> |
assertEquals(3, k); |
740 |
|
} |
741 |
|
|
742 |
+ |
/** |
743 |
+ |
* |
744 |
+ |
*/ |
745 |
|
public void testWeaklyConsistentIteration () { |
618 |
– |
|
746 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
620 |
– |
|
747 |
|
q.add(one); |
748 |
|
q.add(two); |
749 |
|
q.add(three); |
624 |
– |
|
750 |
|
try { |
751 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
752 |
|
q.remove(); |
754 |
|
} |
755 |
|
} |
756 |
|
catch (ConcurrentModificationException e) { |
757 |
< |
fail("weakly consistent iterator; should not get CME"); |
757 |
> |
unexpectedException(); |
758 |
|
} |
759 |
|
|
760 |
< |
assertEquals("queue should be empty again", 0, q.size()); |
760 |
> |
assertEquals(0, q.size()); |
761 |
|
} |
762 |
|
|
763 |
|
|
764 |
< |
public void testToString(){ |
764 |
> |
/** |
765 |
> |
* |
766 |
> |
*/ |
767 |
> |
public void testToString() { |
768 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
769 |
|
String s = q.toString(); |
770 |
|
for (int i = 0; i < SIZE; ++i) { |
773 |
|
} |
774 |
|
|
775 |
|
|
776 |
+ |
/** |
777 |
+ |
* |
778 |
+ |
*/ |
779 |
|
public void testOfferInExecutor() { |
780 |
|
|
781 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
793 |
|
threadAssertEquals(0, q.remainingCapacity()); |
794 |
|
} |
795 |
|
catch (InterruptedException e) { |
796 |
< |
threadFail("should not be interrupted"); |
796 |
> |
threadUnexpectedException(); |
797 |
|
} |
798 |
|
} |
799 |
|
}); |
805 |
|
threadAssertEquals(one, q.take()); |
806 |
|
} |
807 |
|
catch (InterruptedException e) { |
808 |
< |
threadFail("should not be interrupted"); |
808 |
> |
threadUnexpectedException(); |
809 |
|
} |
810 |
|
} |
811 |
|
}); |
814 |
|
|
815 |
|
} |
816 |
|
|
817 |
+ |
/** |
818 |
+ |
* |
819 |
+ |
*/ |
820 |
|
public void testPollInExecutor() { |
821 |
|
|
822 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
831 |
|
threadAssertTrue(q.isEmpty()); |
832 |
|
} |
833 |
|
catch (InterruptedException e) { |
834 |
< |
threadFail("should not be interrupted"); |
834 |
> |
threadUnexpectedException(); |
835 |
|
} |
836 |
|
} |
837 |
|
}); |
843 |
|
q.put(one); |
844 |
|
} |
845 |
|
catch (InterruptedException e) { |
846 |
< |
threadFail("should not be interrupted"); |
846 |
> |
threadUnexpectedException(); |
847 |
|
} |
848 |
|
} |
849 |
|
}); |
852 |
|
|
853 |
|
} |
854 |
|
|
855 |
+ |
/** |
856 |
+ |
* |
857 |
+ |
*/ |
858 |
|
public void testSerialization() { |
859 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
860 |
|
|
871 |
|
while (!q.isEmpty()) |
872 |
|
assertEquals(q.remove(), r.remove()); |
873 |
|
} catch(Exception e){ |
874 |
< |
fail("unexpected exception"); |
874 |
> |
unexpectedException(); |
875 |
|
} |
876 |
|
} |
877 |
|
|