14 |
|
public class LinkedBlockingQueueTest extends JSR166TestCase { |
15 |
|
|
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run (suite()); |
18 |
|
} |
19 |
|
|
20 |
|
public static Test suite() { |
21 |
< |
return new TestSuite(LinkedBlockingQueueTest.class); |
21 |
> |
return new TestSuite(LinkedBlockingQueueTest.class); |
22 |
|
} |
23 |
|
|
24 |
|
|
29 |
|
private LinkedBlockingQueue populatedQueue(int n) { |
30 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(n); |
31 |
|
assertTrue(q.isEmpty()); |
32 |
< |
for (int i = 0; i < n; i++) |
33 |
< |
assertTrue(q.offer(new Integer(i))); |
32 |
> |
for (int i = 0; i < n; i++) |
33 |
> |
assertTrue(q.offer(new Integer(i))); |
34 |
|
assertFalse(q.isEmpty()); |
35 |
|
assertEquals(0, q.remainingCapacity()); |
36 |
< |
assertEquals(n, q.size()); |
36 |
> |
assertEquals(n, q.size()); |
37 |
|
return q; |
38 |
|
} |
39 |
|
|
145 |
|
* offer(null) throws NPE |
146 |
|
*/ |
147 |
|
public void testOfferNull() { |
148 |
< |
try { |
148 |
> |
try { |
149 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
150 |
|
q.offer(null); |
151 |
|
shouldThrow(); |
156 |
|
* add(null) throws NPE |
157 |
|
*/ |
158 |
|
public void testAddNull() { |
159 |
< |
try { |
159 |
> |
try { |
160 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
161 |
|
q.add(null); |
162 |
|
shouldThrow(); |
176 |
|
* add succeeds if not full; throws ISE if full |
177 |
|
*/ |
178 |
|
public void testAdd() { |
179 |
< |
try { |
179 |
> |
try { |
180 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
181 |
|
for (int i = 0; i < SIZE; ++i) { |
182 |
|
assertTrue(q.add(new Integer(i))); |
184 |
|
assertEquals(0, q.remainingCapacity()); |
185 |
|
q.add(new Integer(SIZE)); |
186 |
|
} catch (IllegalStateException success) { |
187 |
< |
} |
187 |
> |
} |
188 |
|
} |
189 |
|
|
190 |
|
/** |
274 |
|
* put(null) throws NPE |
275 |
|
*/ |
276 |
|
public void testPutNull() { |
277 |
< |
try { |
277 |
> |
try { |
278 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
279 |
|
q.put(null); |
280 |
|
shouldThrow(); |
281 |
|
} |
282 |
|
catch (NullPointerException success) { |
283 |
< |
} |
283 |
> |
} |
284 |
|
catch (InterruptedException ie) { |
285 |
< |
unexpectedException(); |
285 |
> |
unexpectedException(); |
286 |
|
} |
287 |
|
} |
288 |
|
|
300 |
|
assertEquals(0, q.remainingCapacity()); |
301 |
|
} |
302 |
|
catch (InterruptedException ie) { |
303 |
< |
unexpectedException(); |
303 |
> |
unexpectedException(); |
304 |
|
} |
305 |
|
} |
306 |
|
|
330 |
|
t.join(); |
331 |
|
} |
332 |
|
catch (InterruptedException ie) { |
333 |
< |
unexpectedException(); |
333 |
> |
unexpectedException(); |
334 |
|
} |
335 |
|
} |
336 |
|
|
351 |
|
++added; |
352 |
|
q.put(new Object()); |
353 |
|
++added; |
354 |
< |
threadShouldThrow(); |
354 |
> |
threadShouldThrow(); |
355 |
|
} catch (InterruptedException e) { |
356 |
|
threadAssertTrue(added >= 2); |
357 |
|
} |
380 |
|
q.put(new Object()); |
381 |
|
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
382 |
|
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
383 |
< |
threadShouldThrow(); |
383 |
> |
threadShouldThrow(); |
384 |
|
} catch (InterruptedException success) {} |
385 |
|
} |
386 |
|
}); |
399 |
|
* take retrieves elements in FIFO order |
400 |
|
*/ |
401 |
|
public void testTake() { |
402 |
< |
try { |
402 |
> |
try { |
403 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
404 |
|
for (int i = 0; i < SIZE; ++i) { |
405 |
|
assertEquals(i, ((Integer)q.take()).intValue()); |
406 |
|
} |
407 |
|
} catch (InterruptedException e) { |
408 |
< |
unexpectedException(); |
409 |
< |
} |
408 |
> |
unexpectedException(); |
409 |
> |
} |
410 |
|
} |
411 |
|
|
412 |
|
/** |
418 |
|
public void run() { |
419 |
|
try { |
420 |
|
q.take(); |
421 |
< |
threadShouldThrow(); |
421 |
> |
threadShouldThrow(); |
422 |
|
} catch (InterruptedException success) { } |
423 |
|
} |
424 |
|
}); |
455 |
|
t.join(); |
456 |
|
} |
457 |
|
catch (InterruptedException ie) { |
458 |
< |
unexpectedException(); |
458 |
> |
unexpectedException(); |
459 |
|
} |
460 |
|
} |
461 |
|
|
468 |
|
for (int i = 0; i < SIZE; ++i) { |
469 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
470 |
|
} |
471 |
< |
assertNull(q.poll()); |
471 |
> |
assertNull(q.poll()); |
472 |
|
} |
473 |
|
|
474 |
|
/** |
482 |
|
} |
483 |
|
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
484 |
|
} catch (InterruptedException e) { |
485 |
< |
unexpectedException(); |
486 |
< |
} |
485 |
> |
unexpectedException(); |
486 |
> |
} |
487 |
|
} |
488 |
|
|
489 |
|
/** |
497 |
|
} |
498 |
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
499 |
|
} catch (InterruptedException e) { |
500 |
< |
unexpectedException(); |
501 |
< |
} |
500 |
> |
unexpectedException(); |
501 |
> |
} |
502 |
|
} |
503 |
|
|
504 |
|
/** |
524 |
|
t.join(); |
525 |
|
} |
526 |
|
catch (InterruptedException ie) { |
527 |
< |
unexpectedException(); |
527 |
> |
unexpectedException(); |
528 |
|
} |
529 |
|
} |
530 |
|
|
540 |
|
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
541 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
542 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
543 |
< |
threadShouldThrow(); |
543 |
> |
threadShouldThrow(); |
544 |
|
} catch (InterruptedException success) { } |
545 |
|
} |
546 |
|
}); |
566 |
|
assertTrue(q.peek() == null || |
567 |
|
i != ((Integer)q.peek()).intValue()); |
568 |
|
} |
569 |
< |
assertNull(q.peek()); |
569 |
> |
assertNull(q.peek()); |
570 |
|
} |
571 |
|
|
572 |
|
/** |
597 |
|
q.remove(); |
598 |
|
shouldThrow(); |
599 |
|
} catch (NoSuchElementException success) { |
600 |
< |
} |
600 |
> |
} |
601 |
|
} |
602 |
|
|
603 |
|
/** |
714 |
|
*/ |
715 |
|
public void testToArray() { |
716 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
717 |
< |
Object[] o = q.toArray(); |
718 |
< |
try { |
719 |
< |
for (int i = 0; i < o.length; i++) |
720 |
< |
assertEquals(o[i], q.take()); |
721 |
< |
} catch (InterruptedException e) { |
722 |
< |
unexpectedException(); |
723 |
< |
} |
717 |
> |
Object[] o = q.toArray(); |
718 |
> |
try { |
719 |
> |
for (int i = 0; i < o.length; i++) |
720 |
> |
assertEquals(o[i], q.take()); |
721 |
> |
} catch (InterruptedException e) { |
722 |
> |
unexpectedException(); |
723 |
> |
} |
724 |
|
} |
725 |
|
|
726 |
|
/** |
728 |
|
*/ |
729 |
|
public void testToArray2() { |
730 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
731 |
< |
Integer[] ints = new Integer[SIZE]; |
732 |
< |
ints = (Integer[])q.toArray(ints); |
733 |
< |
try { |
734 |
< |
for (int i = 0; i < ints.length; i++) |
735 |
< |
assertEquals(ints[i], q.take()); |
736 |
< |
} catch (InterruptedException e) { |
737 |
< |
unexpectedException(); |
738 |
< |
} |
731 |
> |
Integer[] ints = new Integer[SIZE]; |
732 |
> |
ints = (Integer[])q.toArray(ints); |
733 |
> |
try { |
734 |
> |
for (int i = 0; i < ints.length; i++) |
735 |
> |
assertEquals(ints[i], q.take()); |
736 |
> |
} catch (InterruptedException e) { |
737 |
> |
unexpectedException(); |
738 |
> |
} |
739 |
|
} |
740 |
|
|
741 |
|
/** |
742 |
|
* toArray(null) throws NPE |
743 |
|
*/ |
744 |
|
public void testToArray_BadArg() { |
745 |
< |
try { |
745 |
> |
try { |
746 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
747 |
< |
Object o[] = q.toArray(null); |
748 |
< |
shouldThrow(); |
749 |
< |
} catch (NullPointerException success) {} |
747 |
> |
Object o[] = q.toArray(null); |
748 |
> |
shouldThrow(); |
749 |
> |
} catch (NullPointerException success) {} |
750 |
|
} |
751 |
|
|
752 |
|
/** |
753 |
|
* toArray with incompatible array type throws CCE |
754 |
|
*/ |
755 |
|
public void testToArray1_BadArg() { |
756 |
< |
try { |
756 |
> |
try { |
757 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
758 |
< |
Object o[] = q.toArray(new String[10] ); |
759 |
< |
shouldThrow(); |
760 |
< |
} catch (ArrayStoreException success) {} |
758 |
> |
Object o[] = q.toArray(new String[10] ); |
759 |
> |
shouldThrow(); |
760 |
> |
} catch (ArrayStoreException success) {} |
761 |
|
} |
762 |
|
|
763 |
|
|
766 |
|
*/ |
767 |
|
public void testIterator() { |
768 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
769 |
< |
Iterator it = q.iterator(); |
770 |
< |
try { |
771 |
< |
while (it.hasNext()) { |
772 |
< |
assertEquals(it.next(), q.take()); |
773 |
< |
} |
774 |
< |
} catch (InterruptedException e) { |
775 |
< |
unexpectedException(); |
776 |
< |
} |
769 |
> |
Iterator it = q.iterator(); |
770 |
> |
try { |
771 |
> |
while (it.hasNext()) { |
772 |
> |
assertEquals(it.next(), q.take()); |
773 |
> |
} |
774 |
> |
} catch (InterruptedException e) { |
775 |
> |
unexpectedException(); |
776 |
> |
} |
777 |
|
} |
778 |
|
|
779 |
|
/** |