12 |
|
|
13 |
|
public class DelayQueueTest extends JSR166TestCase { |
14 |
|
public static void main(String[] args) { |
15 |
< |
junit.textui.TestRunner.run (suite()); |
15 |
> |
junit.textui.TestRunner.run (suite()); |
16 |
|
} |
17 |
|
|
18 |
|
public static Test suite() { |
19 |
< |
return new TestSuite(DelayQueueTest.class); |
19 |
> |
return new TestSuite(DelayQueueTest.class); |
20 |
|
} |
21 |
|
|
22 |
|
private static final int NOCAP = Integer.MAX_VALUE; |
119 |
|
private DelayQueue populatedQueue(int n) { |
120 |
|
DelayQueue q = new DelayQueue(); |
121 |
|
assertTrue(q.isEmpty()); |
122 |
< |
for (int i = n-1; i >= 0; i-=2) |
123 |
< |
assertTrue(q.offer(new PDelay(i))); |
124 |
< |
for (int i = (n & 1); i < n; i+=2) |
125 |
< |
assertTrue(q.offer(new PDelay(i))); |
122 |
> |
for (int i = n-1; i >= 0; i-=2) |
123 |
> |
assertTrue(q.offer(new PDelay(i))); |
124 |
> |
for (int i = (n & 1); i < n; i+=2) |
125 |
> |
assertTrue(q.offer(new PDelay(i))); |
126 |
|
assertFalse(q.isEmpty()); |
127 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
128 |
< |
assertEquals(n, q.size()); |
128 |
> |
assertEquals(n, q.size()); |
129 |
|
return q; |
130 |
|
} |
131 |
|
|
225 |
|
* offer(null) throws NPE |
226 |
|
*/ |
227 |
|
public void testOfferNull() { |
228 |
< |
try { |
228 |
> |
try { |
229 |
|
DelayQueue q = new DelayQueue(); |
230 |
|
q.offer(null); |
231 |
|
shouldThrow(); |
236 |
|
* add(null) throws NPE |
237 |
|
*/ |
238 |
|
public void testAddNull() { |
239 |
< |
try { |
239 |
> |
try { |
240 |
|
DelayQueue q = new DelayQueue(); |
241 |
|
q.add(null); |
242 |
|
shouldThrow(); |
338 |
|
* put(null) throws NPE |
339 |
|
*/ |
340 |
|
public void testPutNull() { |
341 |
< |
try { |
341 |
> |
try { |
342 |
|
DelayQueue q = new DelayQueue(); |
343 |
|
q.put(null); |
344 |
|
shouldThrow(); |
345 |
|
} |
346 |
|
catch (NullPointerException success) { |
347 |
< |
} |
347 |
> |
} |
348 |
|
} |
349 |
|
|
350 |
|
/** |
427 |
|
* take retrieves elements in priority order |
428 |
|
*/ |
429 |
|
public void testTake() { |
430 |
< |
try { |
430 |
> |
try { |
431 |
|
DelayQueue q = populatedQueue(SIZE); |
432 |
|
for (int i = 0; i < SIZE; ++i) { |
433 |
|
assertEquals(new PDelay(i), ((PDelay)q.take())); |
434 |
|
} |
435 |
|
} catch (InterruptedException e) { |
436 |
< |
unexpectedException(); |
437 |
< |
} |
436 |
> |
unexpectedException(); |
437 |
> |
} |
438 |
|
} |
439 |
|
|
440 |
|
/** |
446 |
|
public void run() { |
447 |
|
try { |
448 |
|
q.take(); |
449 |
< |
threadShouldThrow(); |
449 |
> |
threadShouldThrow(); |
450 |
|
} catch (InterruptedException success) { } |
451 |
|
} |
452 |
|
}); |
483 |
|
t.join(); |
484 |
|
} |
485 |
|
catch (InterruptedException ie) { |
486 |
< |
unexpectedException(); |
486 |
> |
unexpectedException(); |
487 |
|
} |
488 |
|
} |
489 |
|
|
496 |
|
for (int i = 0; i < SIZE; ++i) { |
497 |
|
assertEquals(new PDelay(i), ((PDelay)q.poll())); |
498 |
|
} |
499 |
< |
assertNull(q.poll()); |
499 |
> |
assertNull(q.poll()); |
500 |
|
} |
501 |
|
|
502 |
|
/** |
510 |
|
} |
511 |
|
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
512 |
|
} catch (InterruptedException e) { |
513 |
< |
unexpectedException(); |
514 |
< |
} |
513 |
> |
unexpectedException(); |
514 |
> |
} |
515 |
|
} |
516 |
|
|
517 |
|
/** |
525 |
|
} |
526 |
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
527 |
|
} catch (InterruptedException e) { |
528 |
< |
unexpectedException(); |
529 |
< |
} |
528 |
> |
unexpectedException(); |
529 |
> |
} |
530 |
|
} |
531 |
|
|
532 |
|
/** |
552 |
|
t.join(); |
553 |
|
} |
554 |
|
catch (InterruptedException ie) { |
555 |
< |
unexpectedException(); |
555 |
> |
unexpectedException(); |
556 |
|
} |
557 |
|
} |
558 |
|
|
568 |
|
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
569 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
570 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
571 |
< |
threadFail("Should block"); |
571 |
> |
threadFail("Should block"); |
572 |
|
} catch (InterruptedException success) { } |
573 |
|
} |
574 |
|
}); |
597 |
|
else |
598 |
|
assertTrue(i != ((PDelay)q.peek()).intValue()); |
599 |
|
} |
600 |
< |
assertNull(q.peek()); |
600 |
> |
assertNull(q.peek()); |
601 |
|
} |
602 |
|
|
603 |
|
/** |
628 |
|
q.remove(); |
629 |
|
shouldThrow(); |
630 |
|
} catch (NoSuchElementException success) { |
631 |
< |
} |
631 |
> |
} |
632 |
|
} |
633 |
|
|
634 |
|
/** |
729 |
|
*/ |
730 |
|
public void testToArray() { |
731 |
|
DelayQueue q = populatedQueue(SIZE); |
732 |
< |
Object[] o = q.toArray(); |
732 |
> |
Object[] o = q.toArray(); |
733 |
|
Arrays.sort(o); |
734 |
< |
try { |
735 |
< |
for (int i = 0; i < o.length; i++) |
736 |
< |
assertEquals(o[i], q.take()); |
737 |
< |
} catch (InterruptedException e) { |
738 |
< |
unexpectedException(); |
739 |
< |
} |
734 |
> |
try { |
735 |
> |
for (int i = 0; i < o.length; i++) |
736 |
> |
assertEquals(o[i], q.take()); |
737 |
> |
} catch (InterruptedException e) { |
738 |
> |
unexpectedException(); |
739 |
> |
} |
740 |
|
} |
741 |
|
|
742 |
|
/** |
744 |
|
*/ |
745 |
|
public void testToArray2() { |
746 |
|
DelayQueue q = populatedQueue(SIZE); |
747 |
< |
PDelay[] ints = new PDelay[SIZE]; |
748 |
< |
ints = (PDelay[])q.toArray(ints); |
747 |
> |
PDelay[] ints = new PDelay[SIZE]; |
748 |
> |
ints = (PDelay[])q.toArray(ints); |
749 |
|
Arrays.sort(ints); |
750 |
< |
try { |
751 |
< |
for (int i = 0; i < ints.length; i++) |
752 |
< |
assertEquals(ints[i], q.take()); |
753 |
< |
} catch (InterruptedException e) { |
754 |
< |
unexpectedException(); |
755 |
< |
} |
750 |
> |
try { |
751 |
> |
for (int i = 0; i < ints.length; i++) |
752 |
> |
assertEquals(ints[i], q.take()); |
753 |
> |
} catch (InterruptedException e) { |
754 |
> |
unexpectedException(); |
755 |
> |
} |
756 |
|
} |
757 |
|
|
758 |
|
|
760 |
|
* toArray(null) throws NPE |
761 |
|
*/ |
762 |
|
public void testToArray_BadArg() { |
763 |
< |
try { |
763 |
> |
try { |
764 |
|
DelayQueue q = populatedQueue(SIZE); |
765 |
< |
Object o[] = q.toArray(null); |
766 |
< |
shouldThrow(); |
767 |
< |
} catch (NullPointerException success) {} |
765 |
> |
Object o[] = q.toArray(null); |
766 |
> |
shouldThrow(); |
767 |
> |
} catch (NullPointerException success) {} |
768 |
|
} |
769 |
|
|
770 |
|
/** |
771 |
|
* toArray with incompatible array type throws CCE |
772 |
|
*/ |
773 |
|
public void testToArray1_BadArg() { |
774 |
< |
try { |
774 |
> |
try { |
775 |
|
DelayQueue q = populatedQueue(SIZE); |
776 |
< |
Object o[] = q.toArray(new String[10] ); |
777 |
< |
shouldThrow(); |
778 |
< |
} catch (ArrayStoreException success) {} |
776 |
> |
Object o[] = q.toArray(new String[10] ); |
777 |
> |
shouldThrow(); |
778 |
> |
} catch (ArrayStoreException success) {} |
779 |
|
} |
780 |
|
|
781 |
|
/** |
784 |
|
public void testIterator() { |
785 |
|
DelayQueue q = populatedQueue(SIZE); |
786 |
|
int i = 0; |
787 |
< |
Iterator it = q.iterator(); |
787 |
> |
Iterator it = q.iterator(); |
788 |
|
while (it.hasNext()) { |
789 |
|
assertTrue(q.contains(it.next())); |
790 |
|
++i; |