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.*; |
153 |
|
} |
154 |
|
|
155 |
|
/** |
156 |
+ |
* add(null) throws NPE |
157 |
+ |
*/ |
158 |
+ |
public void testAddNull() { |
159 |
+ |
try { |
160 |
+ |
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
161 |
+ |
q.add(null); |
162 |
+ |
shouldThrow(); |
163 |
+ |
} catch (NullPointerException success) { } |
164 |
+ |
} |
165 |
+ |
|
166 |
+ |
/** |
167 |
|
* Offer succeeds if not full; fails if full |
168 |
|
*/ |
169 |
|
public void testOffer() { |
198 |
|
} |
199 |
|
catch (NullPointerException success) {} |
200 |
|
} |
201 |
+ |
|
202 |
+ |
/** |
203 |
+ |
* addAll(this) throws IAE |
204 |
+ |
*/ |
205 |
+ |
public void testAddAllSelf() { |
206 |
+ |
try { |
207 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
208 |
+ |
q.addAll(q); |
209 |
+ |
shouldThrow(); |
210 |
+ |
} |
211 |
+ |
catch (IllegalArgumentException success) {} |
212 |
+ |
} |
213 |
+ |
|
214 |
|
/** |
215 |
|
* addAll of a collection with null elements throws NPE |
216 |
|
*/ |
614 |
|
} |
615 |
|
assertTrue(q.isEmpty()); |
616 |
|
} |
617 |
+ |
|
618 |
+ |
/** |
619 |
+ |
* An add following remove(x) succeeds |
620 |
+ |
*/ |
621 |
+ |
public void testRemoveElementAndAdd() { |
622 |
+ |
try { |
623 |
+ |
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
624 |
+ |
assertTrue(q.add(new Integer(1))); |
625 |
+ |
assertTrue(q.add(new Integer(2))); |
626 |
+ |
assertTrue(q.remove(new Integer(1))); |
627 |
+ |
assertTrue(q.remove(new Integer(2))); |
628 |
+ |
assertTrue(q.add(new Integer(3))); |
629 |
+ |
assertTrue(q.take() != null); |
630 |
+ |
} catch (Exception e){ |
631 |
+ |
unexpectedException(); |
632 |
+ |
} |
633 |
+ |
} |
634 |
|
|
635 |
|
/** |
636 |
|
* contains(x) reports true when elements added but not yet removed |
655 |
|
assertEquals(SIZE, q.remainingCapacity()); |
656 |
|
q.add(one); |
657 |
|
assertFalse(q.isEmpty()); |
658 |
+ |
assertTrue(q.contains(one)); |
659 |
|
q.clear(); |
660 |
|
assertTrue(q.isEmpty()); |
661 |
|
} |
737 |
|
unexpectedException(); |
738 |
|
} |
739 |
|
} |
740 |
+ |
|
741 |
+ |
/** |
742 |
+ |
* toArray(null) throws NPE |
743 |
+ |
*/ |
744 |
+ |
public void testToArray_BadArg() { |
745 |
+ |
try { |
746 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
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 { |
757 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
758 |
+ |
Object o[] = q.toArray(new String[10] ); |
759 |
+ |
shouldThrow(); |
760 |
+ |
} catch(ArrayStoreException success){} |
761 |
+ |
} |
762 |
+ |
|
763 |
|
|
764 |
|
/** |
765 |
|
* iterator iterates through all elements |
939 |
|
} |
940 |
|
} |
941 |
|
|
942 |
+ |
/** |
943 |
+ |
* drainTo(null) throws NPE |
944 |
+ |
*/ |
945 |
+ |
public void testDrainToNull() { |
946 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
947 |
+ |
try { |
948 |
+ |
q.drainTo(null); |
949 |
+ |
shouldThrow(); |
950 |
+ |
} catch(NullPointerException success) { |
951 |
+ |
} |
952 |
+ |
} |
953 |
+ |
|
954 |
+ |
/** |
955 |
+ |
* drainTo(this) throws IAE |
956 |
+ |
*/ |
957 |
+ |
public void testDrainToSelf() { |
958 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
959 |
+ |
try { |
960 |
+ |
q.drainTo(q); |
961 |
+ |
shouldThrow(); |
962 |
+ |
} catch(IllegalArgumentException success) { |
963 |
+ |
} |
964 |
+ |
} |
965 |
+ |
|
966 |
+ |
/** |
967 |
+ |
* drainTo(c) empties queue into another collection c |
968 |
+ |
*/ |
969 |
+ |
public void testDrainTo() { |
970 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
971 |
+ |
ArrayList l = new ArrayList(); |
972 |
+ |
q.drainTo(l); |
973 |
+ |
assertEquals(q.size(), 0); |
974 |
+ |
assertEquals(l.size(), SIZE); |
975 |
+ |
for (int i = 0; i < SIZE; ++i) |
976 |
+ |
assertEquals(l.get(i), new Integer(i)); |
977 |
+ |
q.add(zero); |
978 |
+ |
q.add(one); |
979 |
+ |
assertFalse(q.isEmpty()); |
980 |
+ |
assertTrue(q.contains(zero)); |
981 |
+ |
assertTrue(q.contains(one)); |
982 |
+ |
l.clear(); |
983 |
+ |
q.drainTo(l); |
984 |
+ |
assertEquals(q.size(), 0); |
985 |
+ |
assertEquals(l.size(), 2); |
986 |
+ |
for (int i = 0; i < 2; ++i) |
987 |
+ |
assertEquals(l.get(i), new Integer(i)); |
988 |
+ |
} |
989 |
+ |
|
990 |
+ |
/** |
991 |
+ |
* drainTo empties full queue, unblocking a waiting put. |
992 |
+ |
*/ |
993 |
+ |
public void testDrainToWithActivePut() { |
994 |
+ |
final LinkedBlockingQueue q = populatedQueue(SIZE); |
995 |
+ |
Thread t = new Thread(new Runnable() { |
996 |
+ |
public void run() { |
997 |
+ |
try { |
998 |
+ |
q.put(new Integer(SIZE+1)); |
999 |
+ |
} catch (InterruptedException ie){ |
1000 |
+ |
threadUnexpectedException(); |
1001 |
+ |
} |
1002 |
+ |
} |
1003 |
+ |
}); |
1004 |
+ |
try { |
1005 |
+ |
t.start(); |
1006 |
+ |
ArrayList l = new ArrayList(); |
1007 |
+ |
q.drainTo(l); |
1008 |
+ |
assertTrue(l.size() >= SIZE); |
1009 |
+ |
for (int i = 0; i < SIZE; ++i) |
1010 |
+ |
assertEquals(l.get(i), new Integer(i)); |
1011 |
+ |
t.join(); |
1012 |
+ |
assertTrue(q.size() + l.size() >= SIZE); |
1013 |
+ |
} catch(Exception e){ |
1014 |
+ |
unexpectedException(); |
1015 |
+ |
} |
1016 |
+ |
} |
1017 |
+ |
|
1018 |
+ |
/** |
1019 |
+ |
* drainTo(null, n) throws NPE |
1020 |
+ |
*/ |
1021 |
+ |
public void testDrainToNullN() { |
1022 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
1023 |
+ |
try { |
1024 |
+ |
q.drainTo(null, 0); |
1025 |
+ |
shouldThrow(); |
1026 |
+ |
} catch(NullPointerException success) { |
1027 |
+ |
} |
1028 |
+ |
} |
1029 |
+ |
|
1030 |
+ |
/** |
1031 |
+ |
* drainTo(this, n) throws IAE |
1032 |
+ |
*/ |
1033 |
+ |
public void testDrainToSelfN() { |
1034 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
1035 |
+ |
try { |
1036 |
+ |
q.drainTo(q, 0); |
1037 |
+ |
shouldThrow(); |
1038 |
+ |
} catch(IllegalArgumentException success) { |
1039 |
+ |
} |
1040 |
+ |
} |
1041 |
+ |
|
1042 |
+ |
/** |
1043 |
+ |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
1044 |
+ |
*/ |
1045 |
+ |
public void testDrainToN() { |
1046 |
+ |
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
1047 |
+ |
for (int i = 0; i < SIZE + 2; ++i) { |
1048 |
+ |
for(int j = 0; j < SIZE; j++) |
1049 |
+ |
assertTrue(q.offer(new Integer(j))); |
1050 |
+ |
ArrayList l = new ArrayList(); |
1051 |
+ |
q.drainTo(l, i); |
1052 |
+ |
int k = (i < SIZE)? i : SIZE; |
1053 |
+ |
assertEquals(l.size(), k); |
1054 |
+ |
assertEquals(q.size(), SIZE-k); |
1055 |
+ |
for (int j = 0; j < k; ++j) |
1056 |
+ |
assertEquals(l.get(j), new Integer(j)); |
1057 |
+ |
while (q.poll() != null) ; |
1058 |
+ |
} |
1059 |
+ |
} |
1060 |
+ |
|
1061 |
|
} |