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 |
|
*/ |
638 |
|
assertEquals(SIZE, q.remainingCapacity()); |
639 |
|
q.add(one); |
640 |
|
assertFalse(q.isEmpty()); |
641 |
+ |
assertTrue(q.contains(one)); |
642 |
|
q.clear(); |
643 |
|
assertTrue(q.isEmpty()); |
644 |
|
} |
720 |
|
unexpectedException(); |
721 |
|
} |
722 |
|
} |
723 |
+ |
|
724 |
+ |
/** |
725 |
+ |
* toArray(null) throws NPE |
726 |
+ |
*/ |
727 |
+ |
public void testToArray_BadArg() { |
728 |
+ |
try { |
729 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
730 |
+ |
Object o[] = q.toArray(null); |
731 |
+ |
shouldThrow(); |
732 |
+ |
} catch(NullPointerException success){} |
733 |
+ |
} |
734 |
+ |
|
735 |
+ |
/** |
736 |
+ |
* toArray with incompatible array type throws CCE |
737 |
+ |
*/ |
738 |
+ |
public void testToArray1_BadArg() { |
739 |
+ |
try { |
740 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
741 |
+ |
Object o[] = q.toArray(new String[10] ); |
742 |
+ |
shouldThrow(); |
743 |
+ |
} catch(ArrayStoreException success){} |
744 |
+ |
} |
745 |
+ |
|
746 |
|
|
747 |
|
/** |
748 |
|
* iterator iterates through all elements |
922 |
|
} |
923 |
|
} |
924 |
|
|
925 |
+ |
/** |
926 |
+ |
* drainTo(null) throws NPE |
927 |
+ |
*/ |
928 |
+ |
public void testDrainToNull() { |
929 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
930 |
+ |
try { |
931 |
+ |
q.drainTo(null); |
932 |
+ |
shouldThrow(); |
933 |
+ |
} catch(NullPointerException success) { |
934 |
+ |
} |
935 |
+ |
} |
936 |
+ |
|
937 |
+ |
/** |
938 |
+ |
* drainTo(this) throws IAE |
939 |
+ |
*/ |
940 |
+ |
public void testDrainToSelf() { |
941 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
942 |
+ |
try { |
943 |
+ |
q.drainTo(q); |
944 |
+ |
shouldThrow(); |
945 |
+ |
} catch(IllegalArgumentException success) { |
946 |
+ |
} |
947 |
+ |
} |
948 |
+ |
|
949 |
+ |
/** |
950 |
+ |
* drainTo(c) empties queue into another collection c |
951 |
+ |
*/ |
952 |
+ |
public void testDrainTo() { |
953 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
954 |
+ |
ArrayList l = new ArrayList(); |
955 |
+ |
q.drainTo(l); |
956 |
+ |
assertEquals(q.size(), 0); |
957 |
+ |
assertEquals(l.size(), SIZE); |
958 |
+ |
for (int i = 0; i < SIZE; ++i) |
959 |
+ |
assertEquals(l.get(i), new Integer(i)); |
960 |
+ |
q.add(zero); |
961 |
+ |
q.add(one); |
962 |
+ |
assertFalse(q.isEmpty()); |
963 |
+ |
assertTrue(q.contains(zero)); |
964 |
+ |
assertTrue(q.contains(one)); |
965 |
+ |
l.clear(); |
966 |
+ |
q.drainTo(l); |
967 |
+ |
assertEquals(q.size(), 0); |
968 |
+ |
assertEquals(l.size(), 2); |
969 |
+ |
for (int i = 0; i < 2; ++i) |
970 |
+ |
assertEquals(l.get(i), new Integer(i)); |
971 |
+ |
} |
972 |
+ |
|
973 |
+ |
/** |
974 |
+ |
* drainTo empties full queue, unblocking a waiting put. |
975 |
+ |
*/ |
976 |
+ |
public void testDrainToWithActivePut() { |
977 |
+ |
final LinkedBlockingQueue q = populatedQueue(SIZE); |
978 |
+ |
Thread t = new Thread(new Runnable() { |
979 |
+ |
public void run() { |
980 |
+ |
try { |
981 |
+ |
q.put(new Integer(SIZE+1)); |
982 |
+ |
} catch (InterruptedException ie){ |
983 |
+ |
threadUnexpectedException(); |
984 |
+ |
} |
985 |
+ |
} |
986 |
+ |
}); |
987 |
+ |
try { |
988 |
+ |
t.start(); |
989 |
+ |
ArrayList l = new ArrayList(); |
990 |
+ |
q.drainTo(l); |
991 |
+ |
assertTrue(l.size() >= SIZE); |
992 |
+ |
for (int i = 0; i < SIZE; ++i) |
993 |
+ |
assertEquals(l.get(i), new Integer(i)); |
994 |
+ |
t.join(); |
995 |
+ |
assertTrue(q.size() + l.size() >= SIZE); |
996 |
+ |
} catch(Exception e){ |
997 |
+ |
unexpectedException(); |
998 |
+ |
} |
999 |
+ |
} |
1000 |
+ |
|
1001 |
+ |
/** |
1002 |
+ |
* drainTo(null, n) throws NPE |
1003 |
+ |
*/ |
1004 |
+ |
public void testDrainToNullN() { |
1005 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
1006 |
+ |
try { |
1007 |
+ |
q.drainTo(null, 0); |
1008 |
+ |
shouldThrow(); |
1009 |
+ |
} catch(NullPointerException success) { |
1010 |
+ |
} |
1011 |
+ |
} |
1012 |
+ |
|
1013 |
+ |
/** |
1014 |
+ |
* drainTo(this, n) throws IAE |
1015 |
+ |
*/ |
1016 |
+ |
public void testDrainToSelfN() { |
1017 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
1018 |
+ |
try { |
1019 |
+ |
q.drainTo(q, 0); |
1020 |
+ |
shouldThrow(); |
1021 |
+ |
} catch(IllegalArgumentException success) { |
1022 |
+ |
} |
1023 |
+ |
} |
1024 |
+ |
|
1025 |
+ |
/** |
1026 |
+ |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
1027 |
+ |
*/ |
1028 |
+ |
public void testDrainToN() { |
1029 |
+ |
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
1030 |
+ |
for (int i = 0; i < SIZE + 2; ++i) { |
1031 |
+ |
for(int j = 0; j < SIZE; j++) |
1032 |
+ |
assertTrue(q.offer(new Integer(j))); |
1033 |
+ |
ArrayList l = new ArrayList(); |
1034 |
+ |
q.drainTo(l, i); |
1035 |
+ |
int k = (i < SIZE)? i : SIZE; |
1036 |
+ |
assertEquals(l.size(), k); |
1037 |
+ |
assertEquals(q.size(), SIZE-k); |
1038 |
+ |
for (int j = 0; j < k; ++j) |
1039 |
+ |
assertEquals(l.get(j), new Integer(j)); |
1040 |
+ |
while (q.poll() != null) ; |
1041 |
+ |
} |
1042 |
+ |
} |
1043 |
+ |
|
1044 |
|
} |