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.*; |
22 |
|
private static final int NOCAP = Integer.MAX_VALUE; |
23 |
|
|
24 |
|
/** |
25 |
< |
* A delayed implmentation for testing. |
25 |
> |
* A delayed implementation for testing. |
26 |
|
* Most tests use Pseudodelays, where delays are all elapsed |
27 |
|
* (so, no blocking solely for delays) but are still ordered |
28 |
|
*/ |
29 |
|
static class PDelay implements Delayed { |
30 |
|
int pseudodelay; |
31 |
|
PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; } |
32 |
< |
public int compareTo(Object y) { |
32 |
> |
public int compareTo(PDelay y) { |
33 |
|
int i = pseudodelay; |
34 |
|
int j = ((PDelay)y).pseudodelay; |
35 |
|
if (i < j) return -1; |
37 |
|
return 0; |
38 |
|
} |
39 |
|
|
40 |
< |
public int compareTo(PDelay y) { |
40 |
> |
public int compareTo(Delayed y) { |
41 |
|
int i = pseudodelay; |
42 |
|
int j = ((PDelay)y).pseudodelay; |
43 |
|
if (i < j) return -1; |
74 |
|
NanoDelay(long i) { |
75 |
|
trigger = System.nanoTime() + i; |
76 |
|
} |
77 |
< |
public int compareTo(Object y) { |
77 |
> |
public int compareTo(NanoDelay y) { |
78 |
|
long i = trigger; |
79 |
|
long j = ((NanoDelay)y).trigger; |
80 |
|
if (i < j) return -1; |
82 |
|
return 0; |
83 |
|
} |
84 |
|
|
85 |
< |
public int compareTo(NanoDelay y) { |
85 |
> |
public int compareTo(Delayed y) { |
86 |
|
long i = trigger; |
87 |
|
long j = ((NanoDelay)y).trigger; |
88 |
|
if (i < j) return -1; |
233 |
|
} |
234 |
|
|
235 |
|
/** |
236 |
+ |
* add(null) throws NPE |
237 |
+ |
*/ |
238 |
+ |
public void testAddNull() { |
239 |
+ |
try { |
240 |
+ |
DelayQueue q = new DelayQueue(); |
241 |
+ |
q.add(null); |
242 |
+ |
shouldThrow(); |
243 |
+ |
} catch (NullPointerException success) { } |
244 |
+ |
} |
245 |
+ |
|
246 |
+ |
/** |
247 |
|
* offer non-null succeeds |
248 |
|
*/ |
249 |
|
public void testOffer() { |
274 |
|
} |
275 |
|
catch (NullPointerException success) {} |
276 |
|
} |
277 |
+ |
|
278 |
+ |
|
279 |
+ |
/** |
280 |
+ |
* addAll(this) throws IAE |
281 |
+ |
*/ |
282 |
+ |
public void testAddAllSelf() { |
283 |
+ |
try { |
284 |
+ |
DelayQueue q = populatedQueue(SIZE); |
285 |
+ |
q.addAll(q); |
286 |
+ |
shouldThrow(); |
287 |
+ |
} |
288 |
+ |
catch (IllegalArgumentException success) {} |
289 |
+ |
} |
290 |
+ |
|
291 |
|
/** |
292 |
|
* addAll of a collection with null elements throws NPE |
293 |
|
*/ |
665 |
|
assertTrue(q.isEmpty()); |
666 |
|
assertEquals(0, q.size()); |
667 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
668 |
< |
q.add(new PDelay(1)); |
668 |
> |
PDelay x = new PDelay(1); |
669 |
> |
q.add(x); |
670 |
|
assertFalse(q.isEmpty()); |
671 |
+ |
assertTrue(q.contains(x)); |
672 |
|
q.clear(); |
673 |
|
assertTrue(q.isEmpty()); |
674 |
|
} |
752 |
|
unexpectedException(); |
753 |
|
} |
754 |
|
} |
755 |
+ |
|
756 |
+ |
|
757 |
+ |
/** |
758 |
+ |
* toArray(null) throws NPE |
759 |
+ |
*/ |
760 |
+ |
public void testToArray_BadArg() { |
761 |
+ |
try { |
762 |
+ |
DelayQueue q = populatedQueue(SIZE); |
763 |
+ |
Object o[] = q.toArray(null); |
764 |
+ |
shouldThrow(); |
765 |
+ |
} catch(NullPointerException success){} |
766 |
+ |
} |
767 |
+ |
|
768 |
+ |
/** |
769 |
+ |
* toArray with incompatible array type throws CCE |
770 |
+ |
*/ |
771 |
+ |
public void testToArray1_BadArg() { |
772 |
+ |
try { |
773 |
+ |
DelayQueue q = populatedQueue(SIZE); |
774 |
+ |
Object o[] = q.toArray(new String[10] ); |
775 |
+ |
shouldThrow(); |
776 |
+ |
} catch(ArrayStoreException success){} |
777 |
+ |
} |
778 |
|
|
779 |
|
/** |
780 |
|
* iterator iterates through all elements |
855 |
|
|
856 |
|
|
857 |
|
/** |
858 |
< |
* Dekayed actions do not occur until their delay elapses |
858 |
> |
* Delayed actions do not occur until their delay elapses |
859 |
|
*/ |
860 |
|
public void testDelay() { |
861 |
|
DelayQueue q = new DelayQueue(); |
883 |
|
} |
884 |
|
} |
885 |
|
|
886 |
+ |
|
887 |
+ |
/** |
888 |
+ |
* drainTo(null) throws NPE |
889 |
+ |
*/ |
890 |
+ |
public void testDrainToNull() { |
891 |
+ |
DelayQueue q = populatedQueue(SIZE); |
892 |
+ |
try { |
893 |
+ |
q.drainTo(null); |
894 |
+ |
shouldThrow(); |
895 |
+ |
} catch(NullPointerException success) { |
896 |
+ |
} |
897 |
+ |
} |
898 |
+ |
|
899 |
+ |
/** |
900 |
+ |
* drainTo(this) throws IAE |
901 |
+ |
*/ |
902 |
+ |
public void testDrainToSelf() { |
903 |
+ |
DelayQueue q = populatedQueue(SIZE); |
904 |
+ |
try { |
905 |
+ |
q.drainTo(q); |
906 |
+ |
shouldThrow(); |
907 |
+ |
} catch(IllegalArgumentException success) { |
908 |
+ |
} |
909 |
+ |
} |
910 |
+ |
|
911 |
+ |
/** |
912 |
+ |
* drainTo(c) empties queue into another collection c |
913 |
+ |
*/ |
914 |
+ |
public void testDrainTo() { |
915 |
+ |
DelayQueue q = new DelayQueue(); |
916 |
+ |
PDelay[] elems = new PDelay[SIZE]; |
917 |
+ |
for (int i = 0; i < SIZE; ++i) { |
918 |
+ |
elems[i] = new PDelay(i); |
919 |
+ |
q.add(elems[i]); |
920 |
+ |
} |
921 |
+ |
ArrayList l = new ArrayList(); |
922 |
+ |
q.drainTo(l); |
923 |
+ |
assertEquals(q.size(), 0); |
924 |
+ |
for (int i = 0; i < SIZE; ++i) |
925 |
+ |
assertEquals(l.get(i), elems[i]); |
926 |
+ |
q.add(elems[0]); |
927 |
+ |
q.add(elems[1]); |
928 |
+ |
assertFalse(q.isEmpty()); |
929 |
+ |
assertTrue(q.contains(elems[0])); |
930 |
+ |
assertTrue(q.contains(elems[1])); |
931 |
+ |
l.clear(); |
932 |
+ |
q.drainTo(l); |
933 |
+ |
assertEquals(q.size(), 0); |
934 |
+ |
assertEquals(l.size(), 2); |
935 |
+ |
for (int i = 0; i < 2; ++i) |
936 |
+ |
assertEquals(l.get(i), elems[i]); |
937 |
+ |
} |
938 |
+ |
|
939 |
+ |
/** |
940 |
+ |
* drainTo empties queue |
941 |
+ |
*/ |
942 |
+ |
public void testDrainToWithActivePut() { |
943 |
+ |
final DelayQueue q = populatedQueue(SIZE); |
944 |
+ |
Thread t = new Thread(new Runnable() { |
945 |
+ |
public void run() { |
946 |
+ |
q.put(new PDelay(SIZE+1)); |
947 |
+ |
} |
948 |
+ |
}); |
949 |
+ |
try { |
950 |
+ |
t.start(); |
951 |
+ |
ArrayList l = new ArrayList(); |
952 |
+ |
q.drainTo(l); |
953 |
+ |
assertTrue(l.size() >= SIZE); |
954 |
+ |
t.join(); |
955 |
+ |
assertTrue(q.size() + l.size() >= SIZE); |
956 |
+ |
} catch(Exception e){ |
957 |
+ |
unexpectedException(); |
958 |
+ |
} |
959 |
+ |
} |
960 |
+ |
|
961 |
+ |
/** |
962 |
+ |
* drainTo(null, n) throws NPE |
963 |
+ |
*/ |
964 |
+ |
public void testDrainToNullN() { |
965 |
+ |
DelayQueue q = populatedQueue(SIZE); |
966 |
+ |
try { |
967 |
+ |
q.drainTo(null, 0); |
968 |
+ |
shouldThrow(); |
969 |
+ |
} catch(NullPointerException success) { |
970 |
+ |
} |
971 |
+ |
} |
972 |
+ |
|
973 |
+ |
/** |
974 |
+ |
* drainTo(this, n) throws IAE |
975 |
+ |
*/ |
976 |
+ |
public void testDrainToSelfN() { |
977 |
+ |
DelayQueue q = populatedQueue(SIZE); |
978 |
+ |
try { |
979 |
+ |
q.drainTo(q, 0); |
980 |
+ |
shouldThrow(); |
981 |
+ |
} catch(IllegalArgumentException success) { |
982 |
+ |
} |
983 |
+ |
} |
984 |
+ |
|
985 |
+ |
/** |
986 |
+ |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
987 |
+ |
*/ |
988 |
+ |
public void testDrainToN() { |
989 |
+ |
for (int i = 0; i < SIZE + 2; ++i) { |
990 |
+ |
DelayQueue q = populatedQueue(SIZE); |
991 |
+ |
ArrayList l = new ArrayList(); |
992 |
+ |
q.drainTo(l, i); |
993 |
+ |
int k = (i < SIZE)? i : SIZE; |
994 |
+ |
assertEquals(q.size(), SIZE-k); |
995 |
+ |
assertEquals(l.size(), k); |
996 |
+ |
} |
997 |
+ |
} |
998 |
+ |
|
999 |
+ |
|
1000 |
|
} |