152 |
|
} |
153 |
|
|
154 |
|
/** |
155 |
+ |
* add(null) throws NPE |
156 |
+ |
*/ |
157 |
+ |
public void testAddNull() { |
158 |
+ |
try { |
159 |
+ |
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
160 |
+ |
q.add(null); |
161 |
+ |
shouldThrow(); |
162 |
+ |
} catch (NullPointerException success) { } |
163 |
+ |
} |
164 |
+ |
|
165 |
+ |
/** |
166 |
|
* Offer succeeds if not full; fails if full |
167 |
|
*/ |
168 |
|
public void testOffer() { |
197 |
|
} |
198 |
|
catch (NullPointerException success) {} |
199 |
|
} |
200 |
+ |
|
201 |
+ |
/** |
202 |
+ |
* addAll(this) throws IAE |
203 |
+ |
*/ |
204 |
+ |
public void testAddAllSelf() { |
205 |
+ |
try { |
206 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
207 |
+ |
q.addAll(q); |
208 |
+ |
shouldThrow(); |
209 |
+ |
} |
210 |
+ |
catch (IllegalArgumentException success) {} |
211 |
+ |
} |
212 |
+ |
|
213 |
|
/** |
214 |
|
* addAll of a collection with null elements throws NPE |
215 |
|
*/ |
718 |
|
unexpectedException(); |
719 |
|
} |
720 |
|
} |
721 |
+ |
|
722 |
+ |
/** |
723 |
+ |
* toArray(null) throws NPE |
724 |
+ |
*/ |
725 |
+ |
public void testToArray_BadArg() { |
726 |
+ |
try { |
727 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
728 |
+ |
Object o[] = q.toArray(null); |
729 |
+ |
shouldThrow(); |
730 |
+ |
} catch(NullPointerException success){} |
731 |
+ |
} |
732 |
+ |
|
733 |
+ |
/** |
734 |
+ |
* toArray with incompatable array type throws CCE |
735 |
+ |
*/ |
736 |
+ |
public void testToArray1_BadArg() { |
737 |
+ |
try { |
738 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
739 |
+ |
Object o[] = q.toArray(new String[10] ); |
740 |
+ |
shouldThrow(); |
741 |
+ |
} catch(ArrayStoreException success){} |
742 |
+ |
} |
743 |
+ |
|
744 |
|
|
745 |
|
/** |
746 |
|
* iterator iterates through all elements |
920 |
|
} |
921 |
|
} |
922 |
|
|
923 |
+ |
/** |
924 |
+ |
* drainTo(null) throws NPE |
925 |
+ |
*/ |
926 |
+ |
public void testDrainToNull() { |
927 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
928 |
+ |
try { |
929 |
+ |
q.drainTo(null); |
930 |
+ |
shouldThrow(); |
931 |
+ |
} catch(NullPointerException success) { |
932 |
+ |
} |
933 |
+ |
} |
934 |
+ |
|
935 |
+ |
/** |
936 |
+ |
* drainTo(this) throws IAE |
937 |
+ |
*/ |
938 |
+ |
public void testDrainToSelf() { |
939 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
940 |
+ |
try { |
941 |
+ |
q.drainTo(q); |
942 |
+ |
shouldThrow(); |
943 |
+ |
} catch(IllegalArgumentException success) { |
944 |
+ |
} |
945 |
+ |
} |
946 |
+ |
|
947 |
+ |
/** |
948 |
+ |
* drainTo(c) empties queue into another collection c |
949 |
+ |
*/ |
950 |
+ |
public void testDrainTo() { |
951 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
952 |
+ |
ArrayList l = new ArrayList(); |
953 |
+ |
q.drainTo(l); |
954 |
+ |
assertEquals(q.size(), 0); |
955 |
+ |
assertEquals(l.size(), SIZE); |
956 |
+ |
for (int i = 0; i < SIZE; ++i) |
957 |
+ |
assertEquals(l.get(i), new Integer(i)); |
958 |
+ |
} |
959 |
+ |
|
960 |
+ |
/** |
961 |
+ |
* drainTo empties full queue, unblocking a waiting put. |
962 |
+ |
*/ |
963 |
+ |
public void testDrainToWithActivePut() { |
964 |
+ |
final LinkedBlockingQueue q = populatedQueue(SIZE); |
965 |
+ |
Thread t = new Thread(new Runnable() { |
966 |
+ |
public void run() { |
967 |
+ |
try { |
968 |
+ |
q.put(new Integer(SIZE+1)); |
969 |
+ |
} catch (InterruptedException ie){ |
970 |
+ |
threadUnexpectedException(); |
971 |
+ |
} |
972 |
+ |
} |
973 |
+ |
}); |
974 |
+ |
try { |
975 |
+ |
t.start(); |
976 |
+ |
ArrayList l = new ArrayList(); |
977 |
+ |
q.drainTo(l); |
978 |
+ |
assertTrue(l.size() >= SIZE); |
979 |
+ |
for (int i = 0; i < SIZE; ++i) |
980 |
+ |
assertEquals(l.get(i), new Integer(i)); |
981 |
+ |
t.join(); |
982 |
+ |
assertTrue(q.size() + l.size() == SIZE+1); |
983 |
+ |
} catch(Exception e){ |
984 |
+ |
unexpectedException(); |
985 |
+ |
} |
986 |
+ |
} |
987 |
+ |
|
988 |
+ |
/** |
989 |
+ |
* drainTo(null, n) throws NPE |
990 |
+ |
*/ |
991 |
+ |
public void testDrainToNullN() { |
992 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
993 |
+ |
try { |
994 |
+ |
q.drainTo(null, 0); |
995 |
+ |
shouldThrow(); |
996 |
+ |
} catch(NullPointerException success) { |
997 |
+ |
} |
998 |
+ |
} |
999 |
+ |
|
1000 |
+ |
/** |
1001 |
+ |
* drainTo(this, n) throws IAE |
1002 |
+ |
*/ |
1003 |
+ |
public void testDrainToSelfN() { |
1004 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
1005 |
+ |
try { |
1006 |
+ |
q.drainTo(q, 0); |
1007 |
+ |
shouldThrow(); |
1008 |
+ |
} catch(IllegalArgumentException success) { |
1009 |
+ |
} |
1010 |
+ |
} |
1011 |
+ |
|
1012 |
+ |
/** |
1013 |
+ |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
1014 |
+ |
*/ |
1015 |
+ |
public void testDrainToN() { |
1016 |
+ |
for (int i = 0; i < SIZE + 2; ++i) { |
1017 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
1018 |
+ |
ArrayList l = new ArrayList(); |
1019 |
+ |
q.drainTo(l, i); |
1020 |
+ |
int k = (i < SIZE)? i : SIZE; |
1021 |
+ |
assertEquals(q.size(), SIZE-k); |
1022 |
+ |
assertEquals(l.size(), k); |
1023 |
+ |
for (int j = 0; j < k; ++j) |
1024 |
+ |
assertEquals(l.get(j), new Integer(j)); |
1025 |
+ |
} |
1026 |
+ |
} |
1027 |
+ |
|
1028 |
|
} |