11 |
|
import java.io.ByteArrayOutputStream; |
12 |
|
import java.io.ObjectInputStream; |
13 |
|
import java.io.ObjectOutputStream; |
14 |
+ |
import java.lang.management.ManagementFactory; |
15 |
+ |
import java.lang.management.ThreadInfo; |
16 |
+ |
import java.util.ArrayList; |
17 |
|
import java.util.Arrays; |
18 |
|
import java.util.Date; |
19 |
+ |
import java.util.Enumeration; |
20 |
+ |
import java.util.List; |
21 |
|
import java.util.NoSuchElementException; |
22 |
|
import java.util.PropertyPermission; |
23 |
|
import java.util.concurrent.*; |
74 |
|
* |
75 |
|
* </ol> |
76 |
|
* |
77 |
< |
* <p> <b>Other notes</b> |
77 |
> |
* <p><b>Other notes</b> |
78 |
|
* <ul> |
79 |
|
* |
80 |
|
* <li> Usually, there is one testcase method per JSR166 method |
147 |
|
} |
148 |
|
|
149 |
|
/** |
150 |
< |
* Runs all JSR166 unit tests using junit.textui.TestRunner |
150 |
> |
* Runs all JSR166 unit tests using junit.textui.TestRunner. |
151 |
> |
* Optional command line arg provides the number of iterations to |
152 |
> |
* repeat running the tests. |
153 |
|
*/ |
154 |
|
public static void main(String[] args) { |
155 |
|
if (useSecurityManager) { |
514 |
|
} |
515 |
|
|
516 |
|
/** |
517 |
+ |
* A debugging tool to print all stack traces, as jstack does. |
518 |
+ |
*/ |
519 |
+ |
static void printAllStackTraces() { |
520 |
+ |
for (ThreadInfo info : |
521 |
+ |
ManagementFactory.getThreadMXBean() |
522 |
+ |
.dumpAllThreads(true, true)) |
523 |
+ |
System.err.print(info); |
524 |
+ |
} |
525 |
+ |
|
526 |
+ |
/** |
527 |
|
* Checks that thread does not terminate within the default |
528 |
|
* millisecond delay of {@code timeoutMillis()}. |
529 |
|
*/ |
545 |
|
} |
546 |
|
|
547 |
|
/** |
548 |
+ |
* Checks that the threads do not terminate within the default |
549 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
550 |
+ |
*/ |
551 |
+ |
void assertThreadsStayAlive(Thread... threads) { |
552 |
+ |
assertThreadsStayAlive(timeoutMillis(), threads); |
553 |
+ |
} |
554 |
+ |
|
555 |
+ |
/** |
556 |
+ |
* Checks that the threads do not terminate within the given millisecond delay. |
557 |
+ |
*/ |
558 |
+ |
void assertThreadsStayAlive(long millis, Thread... threads) { |
559 |
+ |
try { |
560 |
+ |
// No need to optimize the failing case via Thread.join. |
561 |
+ |
delay(millis); |
562 |
+ |
for (Thread thread : threads) |
563 |
+ |
assertTrue(thread.isAlive()); |
564 |
+ |
} catch (InterruptedException ie) { |
565 |
+ |
fail("Unexpected InterruptedException"); |
566 |
+ |
} |
567 |
+ |
} |
568 |
+ |
|
569 |
+ |
/** |
570 |
|
* Checks that future.get times out, with the default timeout of |
571 |
|
* {@code timeoutMillis()}. |
572 |
|
*/ |
640 |
|
SecurityManager sm = System.getSecurityManager(); |
641 |
|
if (sm == null) { |
642 |
|
r.run(); |
643 |
+ |
} |
644 |
+ |
runWithSecurityManagerWithPermissions(r, permissions); |
645 |
+ |
} |
646 |
+ |
|
647 |
+ |
/** |
648 |
+ |
* Runs Runnable r with a security policy that permits precisely |
649 |
+ |
* the specified permissions. If there is no current security |
650 |
+ |
* manager, a temporary one is set for the duration of the |
651 |
+ |
* Runnable. We require that any security manager permit |
652 |
+ |
* getPolicy/setPolicy. |
653 |
+ |
*/ |
654 |
+ |
public void runWithSecurityManagerWithPermissions(Runnable r, |
655 |
+ |
Permission... permissions) { |
656 |
+ |
SecurityManager sm = System.getSecurityManager(); |
657 |
+ |
if (sm == null) { |
658 |
|
Policy savedPolicy = Policy.getPolicy(); |
659 |
|
try { |
660 |
|
Policy.setPolicy(permissivePolicy()); |
661 |
|
System.setSecurityManager(new SecurityManager()); |
662 |
< |
runWithPermissions(r, permissions); |
662 |
> |
runWithSecurityManagerWithPermissions(r, permissions); |
663 |
|
} finally { |
664 |
|
System.setSecurityManager(null); |
665 |
|
Policy.setPolicy(savedPolicy); |
707 |
|
return perms.implies(p); |
708 |
|
} |
709 |
|
public void refresh() {} |
710 |
+ |
public String toString() { |
711 |
+ |
List<Permission> ps = new ArrayList<Permission>(); |
712 |
+ |
for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();) |
713 |
+ |
ps.add(e.nextElement()); |
714 |
+ |
return "AdjustablePolicy with permissions " + ps; |
715 |
+ |
} |
716 |
|
} |
717 |
|
|
718 |
|
/** |
964 |
|
} |
965 |
|
} |
966 |
|
|
967 |
+ |
public void await(Semaphore semaphore) { |
968 |
+ |
try { |
969 |
+ |
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
970 |
+ |
} catch (Throwable t) { |
971 |
+ |
threadUnexpectedException(t); |
972 |
+ |
} |
973 |
+ |
} |
974 |
+ |
|
975 |
|
// /** |
976 |
|
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
977 |
|
// */ |
1240 |
|
} |
1241 |
|
} |
1242 |
|
|
1243 |
< |
@SuppressWarnings("unchecked") |
1244 |
< |
<T> T serialClone(T o) { |
1243 |
> |
void assertSerialEquals(Object x, Object y) { |
1244 |
> |
assertTrue(Arrays.equals(serialBytes(x), serialBytes(y))); |
1245 |
> |
} |
1246 |
> |
|
1247 |
> |
void assertNotSerialEquals(Object x, Object y) { |
1248 |
> |
assertFalse(Arrays.equals(serialBytes(x), serialBytes(y))); |
1249 |
> |
} |
1250 |
> |
|
1251 |
> |
byte[] serialBytes(Object o) { |
1252 |
|
try { |
1253 |
|
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1254 |
|
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1255 |
|
oos.writeObject(o); |
1256 |
|
oos.flush(); |
1257 |
|
oos.close(); |
1258 |
+ |
return bos.toByteArray(); |
1259 |
+ |
} catch (Throwable t) { |
1260 |
+ |
threadUnexpectedException(t); |
1261 |
+ |
return new byte[0]; |
1262 |
+ |
} |
1263 |
+ |
} |
1264 |
+ |
|
1265 |
+ |
@SuppressWarnings("unchecked") |
1266 |
+ |
<T> T serialClone(T o) { |
1267 |
+ |
try { |
1268 |
|
ObjectInputStream ois = new ObjectInputStream |
1269 |
< |
(new ByteArrayInputStream(bos.toByteArray())); |
1269 |
> |
(new ByteArrayInputStream(serialBytes(o))); |
1270 |
|
T clone = (T) ois.readObject(); |
1271 |
|
assertSame(o.getClass(), clone.getClass()); |
1272 |
|
return clone; |