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 |
|
*/ |
750 |
|
unexpectedException(); |
751 |
|
} |
752 |
|
} |
753 |
+ |
|
754 |
+ |
|
755 |
+ |
/** |
756 |
+ |
* toArray(null) throws NPE |
757 |
+ |
*/ |
758 |
+ |
public void testToArray_BadArg() { |
759 |
+ |
try { |
760 |
+ |
DelayQueue q = populatedQueue(SIZE); |
761 |
+ |
Object o[] = q.toArray(null); |
762 |
+ |
shouldThrow(); |
763 |
+ |
} catch(NullPointerException success){} |
764 |
+ |
} |
765 |
+ |
|
766 |
+ |
/** |
767 |
+ |
* toArray with incompatible array type throws CCE |
768 |
+ |
*/ |
769 |
+ |
public void testToArray1_BadArg() { |
770 |
+ |
try { |
771 |
+ |
DelayQueue q = populatedQueue(SIZE); |
772 |
+ |
Object o[] = q.toArray(new String[10] ); |
773 |
+ |
shouldThrow(); |
774 |
+ |
} catch(ArrayStoreException success){} |
775 |
+ |
} |
776 |
|
|
777 |
|
/** |
778 |
|
* iterator iterates through all elements |
853 |
|
|
854 |
|
|
855 |
|
/** |
856 |
< |
* Dekayed actions do not occur until their delay elapses |
856 |
> |
* Delayed actions do not occur until their delay elapses |
857 |
|
*/ |
858 |
|
public void testDelay() { |
859 |
|
DelayQueue q = new DelayQueue(); |
881 |
|
} |
882 |
|
} |
883 |
|
|
884 |
+ |
|
885 |
+ |
/** |
886 |
+ |
* drainTo(null) throws NPE |
887 |
+ |
*/ |
888 |
+ |
public void testDrainToNull() { |
889 |
+ |
DelayQueue q = populatedQueue(SIZE); |
890 |
+ |
try { |
891 |
+ |
q.drainTo(null); |
892 |
+ |
shouldThrow(); |
893 |
+ |
} catch(NullPointerException success) { |
894 |
+ |
} |
895 |
+ |
} |
896 |
+ |
|
897 |
+ |
/** |
898 |
+ |
* drainTo(this) throws IAE |
899 |
+ |
*/ |
900 |
+ |
public void testDrainToSelf() { |
901 |
+ |
DelayQueue q = populatedQueue(SIZE); |
902 |
+ |
try { |
903 |
+ |
q.drainTo(q); |
904 |
+ |
shouldThrow(); |
905 |
+ |
} catch(IllegalArgumentException success) { |
906 |
+ |
} |
907 |
+ |
} |
908 |
+ |
|
909 |
+ |
/** |
910 |
+ |
* drainTo(c) empties queue into another collection c |
911 |
+ |
*/ |
912 |
+ |
public void testDrainTo() { |
913 |
+ |
DelayQueue q = populatedQueue(SIZE); |
914 |
+ |
ArrayList l = new ArrayList(); |
915 |
+ |
q.drainTo(l); |
916 |
+ |
assertEquals(q.size(), 0); |
917 |
+ |
assertEquals(l.size(), SIZE); |
918 |
+ |
} |
919 |
+ |
|
920 |
+ |
/** |
921 |
+ |
* drainTo empties queue |
922 |
+ |
*/ |
923 |
+ |
public void testDrainToWithActivePut() { |
924 |
+ |
final DelayQueue q = populatedQueue(SIZE); |
925 |
+ |
Thread t = new Thread(new Runnable() { |
926 |
+ |
public void run() { |
927 |
+ |
q.put(new PDelay(SIZE+1)); |
928 |
+ |
} |
929 |
+ |
}); |
930 |
+ |
try { |
931 |
+ |
t.start(); |
932 |
+ |
ArrayList l = new ArrayList(); |
933 |
+ |
q.drainTo(l); |
934 |
+ |
assertTrue(l.size() >= SIZE); |
935 |
+ |
t.join(); |
936 |
+ |
assertTrue(q.size() + l.size() >= SIZE); |
937 |
+ |
} catch(Exception e){ |
938 |
+ |
unexpectedException(); |
939 |
+ |
} |
940 |
+ |
} |
941 |
+ |
|
942 |
+ |
/** |
943 |
+ |
* drainTo(null, n) throws NPE |
944 |
+ |
*/ |
945 |
+ |
public void testDrainToNullN() { |
946 |
+ |
DelayQueue q = populatedQueue(SIZE); |
947 |
+ |
try { |
948 |
+ |
q.drainTo(null, 0); |
949 |
+ |
shouldThrow(); |
950 |
+ |
} catch(NullPointerException success) { |
951 |
+ |
} |
952 |
+ |
} |
953 |
+ |
|
954 |
+ |
/** |
955 |
+ |
* drainTo(this, n) throws IAE |
956 |
+ |
*/ |
957 |
+ |
public void testDrainToSelfN() { |
958 |
+ |
DelayQueue q = populatedQueue(SIZE); |
959 |
+ |
try { |
960 |
+ |
q.drainTo(q, 0); |
961 |
+ |
shouldThrow(); |
962 |
+ |
} catch(IllegalArgumentException success) { |
963 |
+ |
} |
964 |
+ |
} |
965 |
+ |
|
966 |
+ |
/** |
967 |
+ |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
968 |
+ |
*/ |
969 |
+ |
public void testDrainToN() { |
970 |
+ |
for (int i = 0; i < SIZE + 2; ++i) { |
971 |
+ |
DelayQueue q = populatedQueue(SIZE); |
972 |
+ |
ArrayList l = new ArrayList(); |
973 |
+ |
q.drainTo(l, i); |
974 |
+ |
int k = (i < SIZE)? i : SIZE; |
975 |
+ |
assertEquals(q.size(), SIZE-k); |
976 |
+ |
assertEquals(l.size(), k); |
977 |
+ |
} |
978 |
+ |
} |
979 |
+ |
|
980 |
+ |
|
981 |
|
} |