74 |
|
import java.util.List; |
75 |
|
import java.util.NoSuchElementException; |
76 |
|
import java.util.PropertyPermission; |
77 |
+ |
import java.util.Queue; |
78 |
|
import java.util.Set; |
79 |
|
import java.util.concurrent.BlockingQueue; |
80 |
|
import java.util.concurrent.Callable; |
151 |
|
* but even so, if there is ever any doubt, they can all be increased |
152 |
|
* in one spot to rerun tests on slower platforms. |
153 |
|
* |
154 |
+ |
* Class Item is used for elements of collections and related |
155 |
+ |
* purposes. Many tests rely on themir keys being equal to ints. To |
156 |
+ |
* check these, methods mustEqual, mustContain, etc adapt the JUnit |
157 |
+ |
* assert methods to intercept ints. |
158 |
+ |
* |
159 |
|
* <li>All threads generated must be joined inside each test case |
160 |
|
* method (or {@code fail} to do so) before returning from the |
161 |
|
* method. The {@code joinPool} method can be used to do this when |
710 |
|
/** |
711 |
|
* Returns a random element from given choices. |
712 |
|
*/ |
713 |
+ |
<T> T chooseRandomly(List<T> choices) { |
714 |
+ |
return choices.get(ThreadLocalRandom.current().nextInt(choices.size())); |
715 |
+ |
} |
716 |
+ |
|
717 |
+ |
/** |
718 |
+ |
* Returns a random element from given choices. |
719 |
+ |
*/ |
720 |
+ |
@SuppressWarnings("unchecked") |
721 |
|
<T> T chooseRandomly(T... choices) { |
722 |
|
return choices[ThreadLocalRandom.current().nextInt(choices.length)]; |
723 |
|
} |
1192 |
|
* Checks that future.get times out, with the default timeout of |
1193 |
|
* {@code timeoutMillis()}. |
1194 |
|
*/ |
1195 |
< |
void assertFutureTimesOut(Future future) { |
1195 |
> |
void assertFutureTimesOut(Future<?> future) { |
1196 |
|
assertFutureTimesOut(future, timeoutMillis()); |
1197 |
|
} |
1198 |
|
|
1199 |
|
/** |
1200 |
|
* Checks that future.get times out, with the given millisecond timeout. |
1201 |
|
*/ |
1202 |
< |
void assertFutureTimesOut(Future future, long timeoutMillis) { |
1202 |
> |
void assertFutureTimesOut(Future<?> future, long timeoutMillis) { |
1203 |
|
long startTime = System.nanoTime(); |
1204 |
|
try { |
1205 |
|
future.get(timeoutMillis, MILLISECONDS); |
1207 |
|
} catch (TimeoutException success) { |
1208 |
|
} catch (Exception fail) { |
1209 |
|
threadUnexpectedException(fail); |
1210 |
< |
} finally { future.cancel(true); } |
1210 |
> |
} |
1211 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
1212 |
+ |
assertFalse(future.isDone()); |
1213 |
|
} |
1214 |
|
|
1215 |
|
/** |
1234 |
|
|
1235 |
|
/** |
1236 |
|
* The number of elements to place in collections, arrays, etc. |
1237 |
+ |
* Must be at least ten; |
1238 |
|
*/ |
1239 |
< |
public static final int SIZE = 20; |
1224 |
< |
|
1225 |
< |
// Some convenient Integer constants |
1239 |
> |
public static final int SIZE = 32; |
1240 |
|
|
1241 |
< |
public static final Integer zero = new Integer(0); |
1242 |
< |
public static final Integer one = new Integer(1); |
1243 |
< |
public static final Integer two = new Integer(2); |
1244 |
< |
public static final Integer three = new Integer(3); |
1245 |
< |
public static final Integer four = new Integer(4); |
1246 |
< |
public static final Integer five = new Integer(5); |
1247 |
< |
public static final Integer six = new Integer(6); |
1248 |
< |
public static final Integer seven = new Integer(7); |
1249 |
< |
public static final Integer eight = new Integer(8); |
1250 |
< |
public static final Integer nine = new Integer(9); |
1251 |
< |
public static final Integer m1 = new Integer(-1); |
1252 |
< |
public static final Integer m2 = new Integer(-2); |
1253 |
< |
public static final Integer m3 = new Integer(-3); |
1254 |
< |
public static final Integer m4 = new Integer(-4); |
1255 |
< |
public static final Integer m5 = new Integer(-5); |
1256 |
< |
public static final Integer m6 = new Integer(-6); |
1257 |
< |
public static final Integer m10 = new Integer(-10); |
1241 |
> |
static Item[] seqItems(int size) { |
1242 |
> |
Item[] s = new Item[size]; |
1243 |
> |
for (int i = 0; i < size; ++i) |
1244 |
> |
s[i] = new Item(i); |
1245 |
> |
return s; |
1246 |
> |
} |
1247 |
> |
static Item[] negativeSeqItems(int size) { |
1248 |
> |
Item[] s = new Item[size]; |
1249 |
> |
for (int i = 0; i < size; ++i) |
1250 |
> |
s[i] = new Item(-i); |
1251 |
> |
return s; |
1252 |
> |
} |
1253 |
> |
|
1254 |
> |
// Many tests rely on defaultItems all being sequential nonnegative |
1255 |
> |
public static final Item[] defaultItems = seqItems(SIZE); |
1256 |
> |
|
1257 |
> |
static Item itemFor(int i) { // check cache for defaultItems |
1258 |
> |
Item[] items = defaultItems; |
1259 |
> |
return (i >= 0 && i < items.length) ? items[i] : new Item(i); |
1260 |
> |
} |
1261 |
> |
|
1262 |
> |
public static final Item zero = defaultItems[0]; |
1263 |
> |
public static final Item one = defaultItems[1]; |
1264 |
> |
public static final Item two = defaultItems[2]; |
1265 |
> |
public static final Item three = defaultItems[3]; |
1266 |
> |
public static final Item four = defaultItems[4]; |
1267 |
> |
public static final Item five = defaultItems[5]; |
1268 |
> |
public static final Item six = defaultItems[6]; |
1269 |
> |
public static final Item seven = defaultItems[7]; |
1270 |
> |
public static final Item eight = defaultItems[8]; |
1271 |
> |
public static final Item nine = defaultItems[9]; |
1272 |
> |
public static final Item ten = defaultItems[10]; |
1273 |
> |
|
1274 |
> |
public static final Item[] negativeItems = negativeSeqItems(SIZE); |
1275 |
> |
|
1276 |
> |
public static final Item minusOne = negativeItems[1]; |
1277 |
> |
public static final Item minusTwo = negativeItems[2]; |
1278 |
> |
public static final Item minusThree = negativeItems[3]; |
1279 |
> |
public static final Item minusFour = negativeItems[4]; |
1280 |
> |
public static final Item minusFive = negativeItems[5]; |
1281 |
> |
public static final Item minusSix = negativeItems[6]; |
1282 |
> |
public static final Item minusSeven = negativeItems[7]; |
1283 |
> |
public static final Item minusEight = negativeItems[8]; |
1284 |
> |
public static final Item minusNone = negativeItems[9]; |
1285 |
> |
public static final Item minusTen = negativeItems[10]; |
1286 |
> |
|
1287 |
> |
// elements expected to be missing |
1288 |
> |
public static final Item fortytwo = new Item(42); |
1289 |
> |
public static final Item eightysix = new Item(86); |
1290 |
> |
public static final Item ninetynine = new Item(99); |
1291 |
> |
|
1292 |
> |
// Interop across Item, int |
1293 |
> |
|
1294 |
> |
static void mustEqual(Item x, Item y) { |
1295 |
> |
if (x != y) |
1296 |
> |
assertEquals(x.value, y.value); |
1297 |
> |
} |
1298 |
> |
static void mustEqual(Item x, int y) { |
1299 |
> |
assertEquals(x.value, y); |
1300 |
> |
} |
1301 |
> |
static void mustEqual(int x, Item y) { |
1302 |
> |
assertEquals(x, y.value); |
1303 |
> |
} |
1304 |
> |
static void mustEqual(int x, int y) { |
1305 |
> |
assertEquals(x, y); |
1306 |
> |
} |
1307 |
> |
static void mustEqual(Object x, Object y) { |
1308 |
> |
if (x != y) |
1309 |
> |
assertEquals(x, y); |
1310 |
> |
} |
1311 |
> |
static void mustEqual(int x, Object y) { |
1312 |
> |
if (y instanceof Item) |
1313 |
> |
assertEquals(x, ((Item)y).value); |
1314 |
> |
else fail(); |
1315 |
> |
} |
1316 |
> |
static void mustEqual(Object x, int y) { |
1317 |
> |
if (x instanceof Item) |
1318 |
> |
assertEquals(((Item)x).value, y); |
1319 |
> |
else fail(); |
1320 |
> |
} |
1321 |
> |
static void mustEqual(boolean x, boolean y) { |
1322 |
> |
assertEquals(x, y); |
1323 |
> |
} |
1324 |
> |
static void mustEqual(long x, long y) { |
1325 |
> |
assertEquals(x, y); |
1326 |
> |
} |
1327 |
> |
static void mustEqual(double x, double y) { |
1328 |
> |
assertEquals(x, y); |
1329 |
> |
} |
1330 |
> |
static void mustContain(Collection<Item> c, int i) { |
1331 |
> |
assertTrue(c.contains(itemFor(i))); |
1332 |
> |
} |
1333 |
> |
static void mustContain(Collection<Item> c, Item i) { |
1334 |
> |
assertTrue(c.contains(i)); |
1335 |
> |
} |
1336 |
> |
static void mustNotContain(Collection<Item> c, int i) { |
1337 |
> |
assertFalse(c.contains(itemFor(i))); |
1338 |
> |
} |
1339 |
> |
static void mustNotContain(Collection<Item> c, Item i) { |
1340 |
> |
assertFalse(c.contains(i)); |
1341 |
> |
} |
1342 |
> |
static void mustRemove(Collection<Item> c, int i) { |
1343 |
> |
assertTrue(c.remove(itemFor(i))); |
1344 |
> |
} |
1345 |
> |
static void mustRemove(Collection<Item> c, Item i) { |
1346 |
> |
assertTrue(c.remove(i)); |
1347 |
> |
} |
1348 |
> |
static void mustNotRemove(Collection<Item> c, int i) { |
1349 |
> |
Item[] items = defaultItems; |
1350 |
> |
Item x = (i >= 0 && i < items.length) ? items[i] : new Item(i); |
1351 |
> |
assertFalse(c.remove(x)); |
1352 |
> |
} |
1353 |
> |
static void mustNotRemove(Collection<Item> c, Item i) { |
1354 |
> |
assertFalse(c.remove(i)); |
1355 |
> |
} |
1356 |
> |
static void mustAdd(Collection<Item> c, int i) { |
1357 |
> |
assertTrue(c.add(itemFor(i))); |
1358 |
> |
} |
1359 |
> |
static void mustAdd(Collection<Item> c, Item i) { |
1360 |
> |
assertTrue(c.add(i)); |
1361 |
> |
} |
1362 |
> |
static void mustOffer(Queue<Item> c, int i) { |
1363 |
> |
assertTrue(c.offer(itemFor(i))); |
1364 |
> |
} |
1365 |
> |
static void mustOffer(Queue<Item> c, Item i) { |
1366 |
> |
assertTrue(c.offer(i)); |
1367 |
> |
} |
1368 |
|
|
1369 |
|
/** |
1370 |
|
* Runs Runnable r with a security policy that permits precisely |
1712 |
|
public void run() {} |
1713 |
|
} |
1714 |
|
|
1715 |
< |
public static class NoOpCallable implements Callable { |
1715 |
> |
public static class NoOpCallable implements Callable<Object> { |
1716 |
|
public Object call() { return Boolean.TRUE; } |
1717 |
|
} |
1718 |
|
|
1896 |
|
|
1897 |
|
public int await() { |
1898 |
|
try { |
1899 |
< |
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1899 |
> |
return super.await(LONGER_DELAY_MS, MILLISECONDS); |
1900 |
|
} catch (TimeoutException timedOut) { |
1901 |
|
throw new AssertionError("timed out"); |
1902 |
|
} catch (Exception fail) { |
1905 |
|
} |
1906 |
|
} |
1907 |
|
|
1908 |
< |
void checkEmpty(BlockingQueue q) { |
1908 |
> |
void checkEmpty(BlockingQueue<?> q) { |
1909 |
|
try { |
1910 |
|
assertTrue(q.isEmpty()); |
1911 |
|
assertEquals(0, q.size()); |
1952 |
|
} |
1953 |
|
} |
1954 |
|
|
1955 |
+ |
@SuppressWarnings("unchecked") |
1956 |
|
void assertImmutable(Object o) { |
1957 |
|
if (o instanceof Collection) { |
1958 |
|
assertThrows( |
2102 |
|
shouldThrow(); |
2103 |
|
} catch (NullPointerException success) {} |
2104 |
|
try { |
2105 |
< |
es.submit((Callable) null); |
2105 |
> |
es.submit((Callable<?>) null); |
2106 |
|
shouldThrow(); |
2107 |
|
} catch (NullPointerException success) {} |
2108 |
|
|
2114 |
|
shouldThrow(); |
2115 |
|
} catch (NullPointerException success) {} |
2116 |
|
try { |
2117 |
< |
ses.schedule((Callable) null, |
2117 |
> |
ses.schedule((Callable<?>) null, |
2118 |
|
randomTimeout(), randomTimeUnit()); |
2119 |
|
shouldThrow(); |
2120 |
|
} catch (NullPointerException success) {} |
2273 |
|
else { |
2274 |
|
assertEquals(x.isEmpty(), y.isEmpty()); |
2275 |
|
assertEquals(x.size(), y.size()); |
2276 |
< |
assertEquals(new HashSet(x), new HashSet(y)); |
2276 |
> |
assertEquals(new HashSet<Object>(x), new HashSet<Object>(y)); |
2277 |
|
if (x instanceof Deque) { |
2278 |
|
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
2279 |
|
assertTrue(Arrays.equals(x.toArray(new Object[0]), |