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 |
|
|
163 |
|
} |
164 |
|
|
165 |
|
/** |
166 |
+ |
* add(null) throws NPE |
167 |
+ |
*/ |
168 |
+ |
public void testAddNull() { |
169 |
+ |
try { |
170 |
+ |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
171 |
+ |
q.add(null); |
172 |
+ |
shouldThrow(); |
173 |
+ |
} catch (NullPointerException success) { } |
174 |
+ |
} |
175 |
+ |
|
176 |
+ |
/** |
177 |
|
* Offer succeeds if not full; fails if full |
178 |
|
*/ |
179 |
|
public void testOffer() { |
208 |
|
} |
209 |
|
catch (NullPointerException success) {} |
210 |
|
} |
211 |
+ |
|
212 |
+ |
/** |
213 |
+ |
* addAll(this) throws IAE |
214 |
+ |
*/ |
215 |
+ |
public void testAddAllSelf() { |
216 |
+ |
try { |
217 |
+ |
ArrayBlockingQueue q = populatedQueue(SIZE); |
218 |
+ |
q.addAll(q); |
219 |
+ |
shouldThrow(); |
220 |
+ |
} |
221 |
+ |
catch (IllegalArgumentException success) {} |
222 |
+ |
} |
223 |
+ |
|
224 |
+ |
|
225 |
|
/** |
226 |
|
* addAll of a collection with null elements throws NPE |
227 |
|
*/ |
319 |
|
* put blocks interruptibly if full |
320 |
|
*/ |
321 |
|
public void testBlockingPut() { |
322 |
+ |
final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
323 |
|
Thread t = new Thread(new Runnable() { |
324 |
|
public void run() { |
325 |
|
int added = 0; |
326 |
|
try { |
300 |
– |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
327 |
|
for (int i = 0; i < SIZE; ++i) { |
328 |
|
q.put(new Integer(i)); |
329 |
|
++added; |
336 |
|
}}); |
337 |
|
try { |
338 |
|
t.start(); |
339 |
< |
Thread.sleep(SHORT_DELAY_MS); |
339 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
340 |
|
t.interrupt(); |
341 |
|
t.join(); |
342 |
|
} |
650 |
|
assertEquals(SIZE, q.remainingCapacity()); |
651 |
|
q.add(one); |
652 |
|
assertFalse(q.isEmpty()); |
653 |
+ |
assertTrue(q.contains(one)); |
654 |
|
q.clear(); |
655 |
|
assertTrue(q.isEmpty()); |
656 |
|
} |
732 |
|
unexpectedException(); |
733 |
|
} |
734 |
|
} |
735 |
+ |
|
736 |
+ |
/** |
737 |
+ |
* toArray(null) throws NPE |
738 |
+ |
*/ |
739 |
+ |
public void testToArray_BadArg() { |
740 |
+ |
try { |
741 |
+ |
ArrayBlockingQueue q = populatedQueue(SIZE); |
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 { |
752 |
+ |
ArrayBlockingQueue q = populatedQueue(SIZE); |
753 |
+ |
Object o[] = q.toArray(new String[10] ); |
754 |
+ |
shouldThrow(); |
755 |
+ |
} catch(ArrayStoreException success){} |
756 |
+ |
} |
757 |
+ |
|
758 |
|
|
759 |
|
/** |
760 |
|
* iterator iterates through all elements |
936 |
|
} |
937 |
|
} |
938 |
|
|
939 |
+ |
/** |
940 |
+ |
* drainTo(null) throws NPE |
941 |
+ |
*/ |
942 |
+ |
public void testDrainToNull() { |
943 |
+ |
ArrayBlockingQueue q = populatedQueue(SIZE); |
944 |
+ |
try { |
945 |
+ |
q.drainTo(null); |
946 |
+ |
shouldThrow(); |
947 |
+ |
} catch(NullPointerException success) { |
948 |
+ |
} |
949 |
+ |
} |
950 |
+ |
|
951 |
+ |
/** |
952 |
+ |
* drainTo(this) throws IAE |
953 |
+ |
*/ |
954 |
+ |
public void testDrainToSelf() { |
955 |
+ |
ArrayBlockingQueue q = populatedQueue(SIZE); |
956 |
+ |
try { |
957 |
+ |
q.drainTo(q); |
958 |
+ |
shouldThrow(); |
959 |
+ |
} catch(IllegalArgumentException success) { |
960 |
+ |
} |
961 |
+ |
} |
962 |
+ |
|
963 |
+ |
/** |
964 |
+ |
* drainTo(c) empties queue into another collection c |
965 |
+ |
*/ |
966 |
+ |
public void testDrainTo() { |
967 |
+ |
ArrayBlockingQueue q = populatedQueue(SIZE); |
968 |
+ |
ArrayList l = new ArrayList(); |
969 |
+ |
q.drainTo(l); |
970 |
+ |
assertEquals(q.size(), 0); |
971 |
+ |
assertEquals(l.size(), SIZE); |
972 |
+ |
for (int i = 0; i < SIZE; ++i) |
973 |
+ |
assertEquals(l.get(i), new Integer(i)); |
974 |
+ |
q.add(zero); |
975 |
+ |
q.add(one); |
976 |
+ |
assertFalse(q.isEmpty()); |
977 |
+ |
assertTrue(q.contains(zero)); |
978 |
+ |
assertTrue(q.contains(one)); |
979 |
+ |
l.clear(); |
980 |
+ |
q.drainTo(l); |
981 |
+ |
assertEquals(q.size(), 0); |
982 |
+ |
assertEquals(l.size(), 2); |
983 |
+ |
for (int i = 0; i < 2; ++i) |
984 |
+ |
assertEquals(l.get(i), new Integer(i)); |
985 |
+ |
} |
986 |
+ |
|
987 |
+ |
/** |
988 |
+ |
* drainTo empties full queue, unblocking a waiting put. |
989 |
+ |
*/ |
990 |
+ |
public void testDrainToWithActivePut() { |
991 |
+ |
final ArrayBlockingQueue q = populatedQueue(SIZE); |
992 |
+ |
Thread t = new Thread(new Runnable() { |
993 |
+ |
public void run() { |
994 |
+ |
try { |
995 |
+ |
q.put(new Integer(SIZE+1)); |
996 |
+ |
} catch (InterruptedException ie){ |
997 |
+ |
threadUnexpectedException(); |
998 |
+ |
} |
999 |
+ |
} |
1000 |
+ |
}); |
1001 |
+ |
try { |
1002 |
+ |
t.start(); |
1003 |
+ |
ArrayList l = new ArrayList(); |
1004 |
+ |
q.drainTo(l); |
1005 |
+ |
assertTrue(l.size() >= SIZE); |
1006 |
+ |
for (int i = 0; i < SIZE; ++i) |
1007 |
+ |
assertEquals(l.get(i), new Integer(i)); |
1008 |
+ |
t.join(); |
1009 |
+ |
assertTrue(q.size() + l.size() >= SIZE); |
1010 |
+ |
} catch(Exception e){ |
1011 |
+ |
unexpectedException(); |
1012 |
+ |
} |
1013 |
+ |
} |
1014 |
+ |
|
1015 |
+ |
/** |
1016 |
+ |
* drainTo(null, n) throws NPE |
1017 |
+ |
*/ |
1018 |
+ |
public void testDrainToNullN() { |
1019 |
+ |
ArrayBlockingQueue q = populatedQueue(SIZE); |
1020 |
+ |
try { |
1021 |
+ |
q.drainTo(null, 0); |
1022 |
+ |
shouldThrow(); |
1023 |
+ |
} catch(NullPointerException success) { |
1024 |
+ |
} |
1025 |
+ |
} |
1026 |
+ |
|
1027 |
+ |
/** |
1028 |
+ |
* drainTo(this, n) throws IAE |
1029 |
+ |
*/ |
1030 |
+ |
public void testDrainToSelfN() { |
1031 |
+ |
ArrayBlockingQueue q = populatedQueue(SIZE); |
1032 |
+ |
try { |
1033 |
+ |
q.drainTo(q, 0); |
1034 |
+ |
shouldThrow(); |
1035 |
+ |
} catch(IllegalArgumentException success) { |
1036 |
+ |
} |
1037 |
+ |
} |
1038 |
+ |
|
1039 |
+ |
/** |
1040 |
+ |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
1041 |
+ |
*/ |
1042 |
+ |
public void testDrainToN() { |
1043 |
+ |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2); |
1044 |
+ |
for (int i = 0; i < SIZE + 2; ++i) { |
1045 |
+ |
for(int j = 0; j < SIZE; j++) |
1046 |
+ |
assertTrue(q.offer(new Integer(j))); |
1047 |
+ |
ArrayList l = new ArrayList(); |
1048 |
+ |
q.drainTo(l, i); |
1049 |
+ |
int k = (i < SIZE)? i : SIZE; |
1050 |
+ |
assertEquals(l.size(), k); |
1051 |
+ |
assertEquals(q.size(), SIZE-k); |
1052 |
+ |
for (int j = 0; j < k; ++j) |
1053 |
+ |
assertEquals(l.get(j), new Integer(j)); |
1054 |
+ |
while (q.poll() != null) ; |
1055 |
+ |
} |
1056 |
+ |
} |
1057 |
+ |
|
1058 |
|
|
1059 |
|
} |