1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
2 |
> |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
44 |
|
} |
45 |
|
|
46 |
|
/** |
47 |
+ |
* add(null) throws NPE |
48 |
+ |
*/ |
49 |
+ |
public void testAddNull() { |
50 |
+ |
try { |
51 |
+ |
SynchronousQueue q = new SynchronousQueue(); |
52 |
+ |
q.add(null); |
53 |
+ |
shouldThrow(); |
54 |
+ |
} catch (NullPointerException success) { } |
55 |
+ |
} |
56 |
+ |
|
57 |
+ |
/** |
58 |
|
* offer fails if no active taker |
59 |
|
*/ |
60 |
|
public void testOffer() { |
86 |
|
} |
87 |
|
catch (NullPointerException success) {} |
88 |
|
} |
89 |
+ |
|
90 |
+ |
/** |
91 |
+ |
* addAll(this) throws IAE |
92 |
+ |
*/ |
93 |
+ |
public void testAddAllSelf() { |
94 |
+ |
try { |
95 |
+ |
SynchronousQueue q = new SynchronousQueue(); |
96 |
+ |
q.addAll(q); |
97 |
+ |
shouldThrow(); |
98 |
+ |
} |
99 |
+ |
catch (IllegalArgumentException success) {} |
100 |
+ |
} |
101 |
+ |
|
102 |
|
/** |
103 |
|
* addAll of a collection with null elements throws NPE |
104 |
|
*/ |
446 |
|
} |
447 |
|
|
448 |
|
/** |
449 |
+ |
* toArray(null) throws NPE |
450 |
+ |
*/ |
451 |
+ |
public void testToArray_BadArg() { |
452 |
+ |
try { |
453 |
+ |
SynchronousQueue q = new SynchronousQueue(); |
454 |
+ |
Object o[] = q.toArray(null); |
455 |
+ |
shouldThrow(); |
456 |
+ |
} catch(NullPointerException success){} |
457 |
+ |
} |
458 |
+ |
|
459 |
+ |
|
460 |
+ |
/** |
461 |
|
* iterator does not traverse any elements |
462 |
|
*/ |
463 |
|
public void testIterator() { |
587 |
|
} |
588 |
|
} |
589 |
|
|
590 |
+ |
/** |
591 |
+ |
* drainTo(null) throws NPE |
592 |
+ |
*/ |
593 |
+ |
public void testDrainToNull() { |
594 |
+ |
SynchronousQueue q = new SynchronousQueue(); |
595 |
+ |
try { |
596 |
+ |
q.drainTo(null); |
597 |
+ |
shouldThrow(); |
598 |
+ |
} catch(NullPointerException success) { |
599 |
+ |
} |
600 |
+ |
} |
601 |
+ |
|
602 |
+ |
/** |
603 |
+ |
* drainTo(this) throws IAE |
604 |
+ |
*/ |
605 |
+ |
public void testDrainToSelf() { |
606 |
+ |
SynchronousQueue q = new SynchronousQueue(); |
607 |
+ |
try { |
608 |
+ |
q.drainTo(q); |
609 |
+ |
shouldThrow(); |
610 |
+ |
} catch(IllegalArgumentException success) { |
611 |
+ |
} |
612 |
+ |
} |
613 |
+ |
|
614 |
+ |
/** |
615 |
+ |
* drainTo(c) of empty queue doesn't transfer elements |
616 |
+ |
*/ |
617 |
+ |
public void testDrainTo() { |
618 |
+ |
SynchronousQueue q = new SynchronousQueue(); |
619 |
+ |
ArrayList l = new ArrayList(); |
620 |
+ |
q.drainTo(l); |
621 |
+ |
assertEquals(q.size(), 0); |
622 |
+ |
assertEquals(l.size(), 0); |
623 |
+ |
} |
624 |
+ |
|
625 |
+ |
/** |
626 |
+ |
* drainTo empties queue, unblocking a waiting put. |
627 |
+ |
*/ |
628 |
+ |
public void testDrainToWithActivePut() { |
629 |
+ |
final SynchronousQueue q = new SynchronousQueue(); |
630 |
+ |
Thread t = new Thread(new Runnable() { |
631 |
+ |
public void run() { |
632 |
+ |
try { |
633 |
+ |
q.put(new Integer(1)); |
634 |
+ |
} catch (InterruptedException ie){ |
635 |
+ |
threadUnexpectedException(); |
636 |
+ |
} |
637 |
+ |
} |
638 |
+ |
}); |
639 |
+ |
try { |
640 |
+ |
t.start(); |
641 |
+ |
ArrayList l = new ArrayList(); |
642 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
643 |
+ |
q.drainTo(l); |
644 |
+ |
assertTrue(l.size() <= 1); |
645 |
+ |
if (l.size() > 0) |
646 |
+ |
assertEquals(l.get(0), new Integer(1)); |
647 |
+ |
t.join(); |
648 |
+ |
assertTrue(l.size() <= 1); |
649 |
+ |
} catch(Exception e){ |
650 |
+ |
unexpectedException(); |
651 |
+ |
} |
652 |
+ |
} |
653 |
+ |
|
654 |
+ |
/** |
655 |
+ |
* drainTo(null, n) throws NPE |
656 |
+ |
*/ |
657 |
+ |
public void testDrainToNullN() { |
658 |
+ |
SynchronousQueue q = new SynchronousQueue(); |
659 |
+ |
try { |
660 |
+ |
q.drainTo(null, 0); |
661 |
+ |
shouldThrow(); |
662 |
+ |
} catch(NullPointerException success) { |
663 |
+ |
} |
664 |
+ |
} |
665 |
+ |
|
666 |
+ |
/** |
667 |
+ |
* drainTo(this, n) throws IAE |
668 |
+ |
*/ |
669 |
+ |
public void testDrainToSelfN() { |
670 |
+ |
SynchronousQueue q = new SynchronousQueue(); |
671 |
+ |
try { |
672 |
+ |
q.drainTo(q, 0); |
673 |
+ |
shouldThrow(); |
674 |
+ |
} catch(IllegalArgumentException success) { |
675 |
+ |
} |
676 |
+ |
} |
677 |
+ |
|
678 |
+ |
/** |
679 |
+ |
* drainTo(c, n) empties up to n elements of queue into c |
680 |
+ |
*/ |
681 |
+ |
public void testDrainToN() { |
682 |
+ |
final SynchronousQueue q = new SynchronousQueue(); |
683 |
+ |
Thread t1 = new Thread(new Runnable() { |
684 |
+ |
public void run() { |
685 |
+ |
try { |
686 |
+ |
q.put(one); |
687 |
+ |
} catch (InterruptedException ie){ |
688 |
+ |
threadUnexpectedException(); |
689 |
+ |
} |
690 |
+ |
} |
691 |
+ |
}); |
692 |
+ |
Thread t2 = new Thread(new Runnable() { |
693 |
+ |
public void run() { |
694 |
+ |
try { |
695 |
+ |
q.put(two); |
696 |
+ |
} catch (InterruptedException ie){ |
697 |
+ |
threadUnexpectedException(); |
698 |
+ |
} |
699 |
+ |
} |
700 |
+ |
}); |
701 |
+ |
|
702 |
+ |
try { |
703 |
+ |
t1.start(); |
704 |
+ |
t2.start(); |
705 |
+ |
ArrayList l = new ArrayList(); |
706 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
707 |
+ |
q.drainTo(l, 1); |
708 |
+ |
assertTrue(l.size() == 1); |
709 |
+ |
q.drainTo(l, 1); |
710 |
+ |
assertTrue(l.size() == 2); |
711 |
+ |
assertTrue(l.contains(one)); |
712 |
+ |
assertTrue(l.contains(two)); |
713 |
+ |
t1.join(); |
714 |
+ |
t2.join(); |
715 |
+ |
} catch(Exception e){ |
716 |
+ |
unexpectedException(); |
717 |
+ |
} |
718 |
+ |
} |
719 |
+ |
|
720 |
+ |
|
721 |
|
} |