14 |
|
|
15 |
|
public class ArrayBlockingQueueTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run (suite()); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
< |
return new TestSuite(ArrayBlockingQueueTest.class); |
20 |
> |
return new TestSuite(ArrayBlockingQueueTest.class); |
21 |
|
} |
22 |
|
|
23 |
|
/** |
27 |
|
private ArrayBlockingQueue populatedQueue(int n) { |
28 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(n); |
29 |
|
assertTrue(q.isEmpty()); |
30 |
< |
for (int i = 0; i < n; i++) |
31 |
< |
assertTrue(q.offer(new Integer(i))); |
30 |
> |
for (int i = 0; i < n; i++) |
31 |
> |
assertTrue(q.offer(new Integer(i))); |
32 |
|
assertFalse(q.isEmpty()); |
33 |
|
assertEquals(0, q.remainingCapacity()); |
34 |
< |
assertEquals(n, q.size()); |
34 |
> |
assertEquals(n, q.size()); |
35 |
|
return q; |
36 |
|
} |
37 |
|
|
155 |
|
* offer(null) throws NPE |
156 |
|
*/ |
157 |
|
public void testOfferNull() { |
158 |
< |
try { |
158 |
> |
try { |
159 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
160 |
|
q.offer(null); |
161 |
|
shouldThrow(); |
166 |
|
* add(null) throws NPE |
167 |
|
*/ |
168 |
|
public void testAddNull() { |
169 |
< |
try { |
169 |
> |
try { |
170 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
171 |
|
q.add(null); |
172 |
|
shouldThrow(); |
186 |
|
* add succeeds if not full; throws ISE if full |
187 |
|
*/ |
188 |
|
public void testAdd() { |
189 |
< |
try { |
189 |
> |
try { |
190 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
191 |
|
for (int i = 0; i < SIZE; ++i) { |
192 |
|
assertTrue(q.add(new Integer(i))); |
194 |
|
assertEquals(0, q.remainingCapacity()); |
195 |
|
q.add(new Integer(SIZE)); |
196 |
|
} catch (IllegalStateException success) { |
197 |
< |
} |
197 |
> |
} |
198 |
|
} |
199 |
|
|
200 |
|
/** |
285 |
|
* put(null) throws NPE |
286 |
|
*/ |
287 |
|
public void testPutNull() { |
288 |
< |
try { |
288 |
> |
try { |
289 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
290 |
|
q.put(null); |
291 |
|
shouldThrow(); |
292 |
|
} |
293 |
|
catch (NullPointerException success) { |
294 |
< |
} |
294 |
> |
} |
295 |
|
catch (InterruptedException ie) { |
296 |
< |
unexpectedException(); |
296 |
> |
unexpectedException(); |
297 |
|
} |
298 |
|
} |
299 |
|
|
311 |
|
assertEquals(0, q.remainingCapacity()); |
312 |
|
} |
313 |
|
catch (InterruptedException ie) { |
314 |
< |
unexpectedException(); |
314 |
> |
unexpectedException(); |
315 |
|
} |
316 |
|
} |
317 |
|
|
341 |
|
t.join(); |
342 |
|
} |
343 |
|
catch (InterruptedException ie) { |
344 |
< |
unexpectedException(); |
344 |
> |
unexpectedException(); |
345 |
|
} |
346 |
|
} |
347 |
|
|
362 |
|
++added; |
363 |
|
q.put(new Object()); |
364 |
|
++added; |
365 |
< |
threadShouldThrow(); |
365 |
> |
threadShouldThrow(); |
366 |
|
} catch (InterruptedException e) { |
367 |
|
threadAssertTrue(added >= 2); |
368 |
|
} |
391 |
|
q.put(new Object()); |
392 |
|
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS)); |
393 |
|
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
394 |
< |
threadShouldThrow(); |
394 |
> |
threadShouldThrow(); |
395 |
|
} catch (InterruptedException success) {} |
396 |
|
} |
397 |
|
}); |
410 |
|
* take retrieves elements in FIFO order |
411 |
|
*/ |
412 |
|
public void testTake() { |
413 |
< |
try { |
413 |
> |
try { |
414 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
415 |
|
for (int i = 0; i < SIZE; ++i) { |
416 |
|
assertEquals(i, ((Integer)q.take()).intValue()); |
417 |
|
} |
418 |
|
} catch (InterruptedException e) { |
419 |
< |
unexpectedException(); |
420 |
< |
} |
419 |
> |
unexpectedException(); |
420 |
> |
} |
421 |
|
} |
422 |
|
|
423 |
|
/** |
429 |
|
public void run() { |
430 |
|
try { |
431 |
|
q.take(); |
432 |
< |
threadShouldThrow(); |
432 |
> |
threadShouldThrow(); |
433 |
|
} catch (InterruptedException success) { } |
434 |
|
} |
435 |
|
}); |
466 |
|
t.join(); |
467 |
|
} |
468 |
|
catch (InterruptedException ie) { |
469 |
< |
unexpectedException(); |
469 |
> |
unexpectedException(); |
470 |
|
} |
471 |
|
} |
472 |
|
|
479 |
|
for (int i = 0; i < SIZE; ++i) { |
480 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
481 |
|
} |
482 |
< |
assertNull(q.poll()); |
482 |
> |
assertNull(q.poll()); |
483 |
|
} |
484 |
|
|
485 |
|
/** |
493 |
|
} |
494 |
|
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
495 |
|
} catch (InterruptedException e) { |
496 |
< |
unexpectedException(); |
497 |
< |
} |
496 |
> |
unexpectedException(); |
497 |
> |
} |
498 |
|
} |
499 |
|
|
500 |
|
/** |
508 |
|
} |
509 |
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
510 |
|
} catch (InterruptedException e) { |
511 |
< |
unexpectedException(); |
512 |
< |
} |
511 |
> |
unexpectedException(); |
512 |
> |
} |
513 |
|
} |
514 |
|
|
515 |
|
/** |
535 |
|
t.join(); |
536 |
|
} |
537 |
|
catch (InterruptedException ie) { |
538 |
< |
unexpectedException(); |
538 |
> |
unexpectedException(); |
539 |
|
} |
540 |
|
} |
541 |
|
|
551 |
|
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
552 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
553 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
554 |
< |
threadShouldThrow(); |
554 |
> |
threadShouldThrow(); |
555 |
|
} catch (InterruptedException success) { } |
556 |
|
} |
557 |
|
}); |
578 |
|
assertTrue(q.peek() == null || |
579 |
|
i != ((Integer)q.peek()).intValue()); |
580 |
|
} |
581 |
< |
assertNull(q.peek()); |
581 |
> |
assertNull(q.peek()); |
582 |
|
} |
583 |
|
|
584 |
|
/** |
609 |
|
q.remove(); |
610 |
|
shouldThrow(); |
611 |
|
} catch (NoSuchElementException success) { |
612 |
< |
} |
612 |
> |
} |
613 |
|
} |
614 |
|
|
615 |
|
/** |
709 |
|
*/ |
710 |
|
public void testToArray() { |
711 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
712 |
< |
Object[] o = q.toArray(); |
713 |
< |
try { |
714 |
< |
for (int i = 0; i < o.length; i++) |
715 |
< |
assertEquals(o[i], q.take()); |
716 |
< |
} catch (InterruptedException e) { |
717 |
< |
unexpectedException(); |
718 |
< |
} |
712 |
> |
Object[] o = q.toArray(); |
713 |
> |
try { |
714 |
> |
for (int i = 0; i < o.length; i++) |
715 |
> |
assertEquals(o[i], q.take()); |
716 |
> |
} catch (InterruptedException e) { |
717 |
> |
unexpectedException(); |
718 |
> |
} |
719 |
|
} |
720 |
|
|
721 |
|
/** |
723 |
|
*/ |
724 |
|
public void testToArray2() { |
725 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
726 |
< |
Integer[] ints = new Integer[SIZE]; |
727 |
< |
ints = (Integer[])q.toArray(ints); |
728 |
< |
try { |
729 |
< |
for (int i = 0; i < ints.length; i++) |
730 |
< |
assertEquals(ints[i], q.take()); |
731 |
< |
} catch (InterruptedException e) { |
732 |
< |
unexpectedException(); |
733 |
< |
} |
726 |
> |
Integer[] ints = new Integer[SIZE]; |
727 |
> |
ints = (Integer[])q.toArray(ints); |
728 |
> |
try { |
729 |
> |
for (int i = 0; i < ints.length; i++) |
730 |
> |
assertEquals(ints[i], q.take()); |
731 |
> |
} catch (InterruptedException e) { |
732 |
> |
unexpectedException(); |
733 |
> |
} |
734 |
|
} |
735 |
|
|
736 |
|
/** |
737 |
|
* toArray(null) throws NPE |
738 |
|
*/ |
739 |
|
public void testToArray_BadArg() { |
740 |
< |
try { |
740 |
> |
try { |
741 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
742 |
< |
Object o[] = q.toArray(null); |
743 |
< |
shouldThrow(); |
744 |
< |
} catch (NullPointerException success) {} |
742 |
> |
Object o[] = q.toArray(null); |
743 |
> |
shouldThrow(); |
744 |
> |
} catch (NullPointerException success) {} |
745 |
|
} |
746 |
|
|
747 |
|
/** |
748 |
|
* toArray with incompatible array type throws CCE |
749 |
|
*/ |
750 |
|
public void testToArray1_BadArg() { |
751 |
< |
try { |
751 |
> |
try { |
752 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
753 |
< |
Object o[] = q.toArray(new String[10] ); |
754 |
< |
shouldThrow(); |
755 |
< |
} catch (ArrayStoreException success) {} |
753 |
> |
Object o[] = q.toArray(new String[10] ); |
754 |
> |
shouldThrow(); |
755 |
> |
} catch (ArrayStoreException success) {} |
756 |
|
} |
757 |
|
|
758 |
|
|
761 |
|
*/ |
762 |
|
public void testIterator() { |
763 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
764 |
< |
Iterator it = q.iterator(); |
765 |
< |
try { |
766 |
< |
while (it.hasNext()) { |
767 |
< |
assertEquals(it.next(), q.take()); |
768 |
< |
} |
769 |
< |
} catch (InterruptedException e) { |
770 |
< |
unexpectedException(); |
771 |
< |
} |
764 |
> |
Iterator it = q.iterator(); |
765 |
> |
try { |
766 |
> |
while (it.hasNext()) { |
767 |
> |
assertEquals(it.next(), q.take()); |
768 |
> |
} |
769 |
> |
} catch (InterruptedException e) { |
770 |
> |
unexpectedException(); |
771 |
> |
} |
772 |
|
} |
773 |
|
|
774 |
|
/** |