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.lang.reflect.Method; |
17 |
+ |
import java.util.ArrayList; |
18 |
|
import java.util.Arrays; |
19 |
|
import java.util.Date; |
20 |
+ |
import java.util.Enumeration; |
21 |
+ |
import java.util.List; |
22 |
|
import java.util.NoSuchElementException; |
23 |
|
import java.util.PropertyPermission; |
24 |
|
import java.util.concurrent.*; |
75 |
|
* |
76 |
|
* </ol> |
77 |
|
* |
78 |
< |
* <p> <b>Other notes</b> |
78 |
> |
* <p><b>Other notes</b> |
79 |
|
* <ul> |
80 |
|
* |
81 |
|
* <li> Usually, there is one testcase method per JSR166 method |
148 |
|
} |
149 |
|
|
150 |
|
/** |
151 |
< |
* Runs all JSR166 unit tests using junit.textui.TestRunner |
151 |
> |
* Runs all JSR166 unit tests using junit.textui.TestRunner. |
152 |
> |
* Optional command line arg provides the number of iterations to |
153 |
> |
* repeat running the tests. |
154 |
|
*/ |
155 |
|
public static void main(String[] args) { |
156 |
|
if (useSecurityManager) { |
182 |
|
return suite; |
183 |
|
} |
184 |
|
|
185 |
+ |
public static void addNamedTestClasses(TestSuite suite, |
186 |
+ |
String... testClassNames) { |
187 |
+ |
for (String testClassName : testClassNames) { |
188 |
+ |
try { |
189 |
+ |
Class<?> testClass = Class.forName(testClassName); |
190 |
+ |
Method m = testClass.getDeclaredMethod("suite", |
191 |
+ |
new Class<?>[0]); |
192 |
+ |
suite.addTest(newTestSuite((Test)m.invoke(null))); |
193 |
+ |
} catch (Exception e) { |
194 |
+ |
throw new Error("Missing test class", e); |
195 |
+ |
} |
196 |
+ |
} |
197 |
+ |
} |
198 |
+ |
|
199 |
+ |
public static final double JAVA_CLASS_VERSION; |
200 |
+ |
static { |
201 |
+ |
try { |
202 |
+ |
JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged( |
203 |
+ |
new java.security.PrivilegedAction<Double>() { |
204 |
+ |
public Double run() { |
205 |
+ |
return Double.valueOf(System.getProperty("java.class.version"));}}); |
206 |
+ |
} catch (Throwable t) { |
207 |
+ |
throw new Error(t); |
208 |
+ |
} |
209 |
+ |
} |
210 |
+ |
|
211 |
+ |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
212 |
+ |
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
213 |
+ |
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
214 |
+ |
|
215 |
|
/** |
216 |
|
* Collects all JSR166 unit tests as one suite. |
217 |
|
*/ |
218 |
|
public static Test suite() { |
219 |
< |
return newTestSuite( |
219 |
> |
// Java7+ test classes |
220 |
> |
TestSuite suite = newTestSuite( |
221 |
|
ForkJoinPoolTest.suite(), |
222 |
|
ForkJoinTaskTest.suite(), |
223 |
|
RecursiveActionTest.suite(), |
282 |
|
TreeSetTest.suite(), |
283 |
|
TreeSubMapTest.suite(), |
284 |
|
TreeSubSetTest.suite()); |
285 |
+ |
|
286 |
+ |
// Java8+ test classes |
287 |
+ |
if (atLeastJava8()) { |
288 |
+ |
String[] java8TestClassNames = { |
289 |
+ |
"StampedLockTest", |
290 |
+ |
}; |
291 |
+ |
addNamedTestClasses(suite, java8TestClassNames); |
292 |
+ |
} |
293 |
+ |
|
294 |
+ |
return suite; |
295 |
|
} |
296 |
|
|
297 |
|
|
556 |
|
} |
557 |
|
|
558 |
|
/** |
559 |
+ |
* A debugging tool to print all stack traces, as jstack does. |
560 |
+ |
*/ |
561 |
+ |
static void printAllStackTraces() { |
562 |
+ |
for (ThreadInfo info : |
563 |
+ |
ManagementFactory.getThreadMXBean() |
564 |
+ |
.dumpAllThreads(true, true)) |
565 |
+ |
System.err.print(info); |
566 |
+ |
} |
567 |
+ |
|
568 |
+ |
/** |
569 |
|
* Checks that thread does not terminate within the default |
570 |
|
* millisecond delay of {@code timeoutMillis()}. |
571 |
|
*/ |
587 |
|
} |
588 |
|
|
589 |
|
/** |
590 |
+ |
* Checks that the threads do not terminate within the default |
591 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
592 |
+ |
*/ |
593 |
+ |
void assertThreadsStayAlive(Thread... threads) { |
594 |
+ |
assertThreadsStayAlive(timeoutMillis(), threads); |
595 |
+ |
} |
596 |
+ |
|
597 |
+ |
/** |
598 |
+ |
* Checks that the threads do not terminate within the given millisecond delay. |
599 |
+ |
*/ |
600 |
+ |
void assertThreadsStayAlive(long millis, Thread... threads) { |
601 |
+ |
try { |
602 |
+ |
// No need to optimize the failing case via Thread.join. |
603 |
+ |
delay(millis); |
604 |
+ |
for (Thread thread : threads) |
605 |
+ |
assertTrue(thread.isAlive()); |
606 |
+ |
} catch (InterruptedException ie) { |
607 |
+ |
fail("Unexpected InterruptedException"); |
608 |
+ |
} |
609 |
+ |
} |
610 |
+ |
|
611 |
+ |
/** |
612 |
|
* Checks that future.get times out, with the default timeout of |
613 |
|
* {@code timeoutMillis()}. |
614 |
|
*/ |
682 |
|
SecurityManager sm = System.getSecurityManager(); |
683 |
|
if (sm == null) { |
684 |
|
r.run(); |
685 |
+ |
} |
686 |
+ |
runWithSecurityManagerWithPermissions(r, permissions); |
687 |
+ |
} |
688 |
+ |
|
689 |
+ |
/** |
690 |
+ |
* Runs Runnable r with a security policy that permits precisely |
691 |
+ |
* the specified permissions. If there is no current security |
692 |
+ |
* manager, a temporary one is set for the duration of the |
693 |
+ |
* Runnable. We require that any security manager permit |
694 |
+ |
* getPolicy/setPolicy. |
695 |
+ |
*/ |
696 |
+ |
public void runWithSecurityManagerWithPermissions(Runnable r, |
697 |
+ |
Permission... permissions) { |
698 |
+ |
SecurityManager sm = System.getSecurityManager(); |
699 |
+ |
if (sm == null) { |
700 |
|
Policy savedPolicy = Policy.getPolicy(); |
701 |
|
try { |
702 |
|
Policy.setPolicy(permissivePolicy()); |
703 |
|
System.setSecurityManager(new SecurityManager()); |
704 |
< |
runWithPermissions(r, permissions); |
704 |
> |
runWithSecurityManagerWithPermissions(r, permissions); |
705 |
|
} finally { |
706 |
|
System.setSecurityManager(null); |
707 |
|
Policy.setPolicy(savedPolicy); |
749 |
|
return perms.implies(p); |
750 |
|
} |
751 |
|
public void refresh() {} |
752 |
+ |
public String toString() { |
753 |
+ |
List<Permission> ps = new ArrayList<Permission>(); |
754 |
+ |
for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();) |
755 |
+ |
ps.add(e.nextElement()); |
756 |
+ |
return "AdjustablePolicy with permissions " + ps; |
757 |
+ |
} |
758 |
|
} |
759 |
|
|
760 |
|
/** |
792 |
|
} |
793 |
|
|
794 |
|
/** |
795 |
< |
* Waits up to the specified number of milliseconds for the given |
795 |
> |
* Spin-waits up to the specified number of milliseconds for the given |
796 |
|
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
797 |
|
*/ |
798 |
|
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
799 |
< |
long timeoutNanos = timeoutMillis * 1000L * 1000L; |
698 |
< |
long t0 = System.nanoTime(); |
799 |
> |
long startTime = System.nanoTime(); |
800 |
|
for (;;) { |
801 |
|
Thread.State s = thread.getState(); |
802 |
|
if (s == Thread.State.BLOCKED || |
805 |
|
return; |
806 |
|
else if (s == Thread.State.TERMINATED) |
807 |
|
fail("Unexpected thread termination"); |
808 |
< |
else if (System.nanoTime() - t0 > timeoutNanos) { |
808 |
> |
else if (millisElapsedSince(startTime) > timeoutMillis) { |
809 |
|
threadAssertTrue(thread.isAlive()); |
810 |
|
return; |
811 |
|
} |
1006 |
|
} |
1007 |
|
} |
1008 |
|
|
1009 |
+ |
public void await(Semaphore semaphore) { |
1010 |
+ |
try { |
1011 |
+ |
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
1012 |
+ |
} catch (Throwable t) { |
1013 |
+ |
threadUnexpectedException(t); |
1014 |
+ |
} |
1015 |
+ |
} |
1016 |
+ |
|
1017 |
|
// /** |
1018 |
|
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
1019 |
|
// */ |
1282 |
|
} |
1283 |
|
} |
1284 |
|
|
1285 |
< |
@SuppressWarnings("unchecked") |
1286 |
< |
<T> T serialClone(T o) { |
1285 |
> |
void assertSerialEquals(Object x, Object y) { |
1286 |
> |
assertTrue(Arrays.equals(serialBytes(x), serialBytes(y))); |
1287 |
> |
} |
1288 |
> |
|
1289 |
> |
void assertNotSerialEquals(Object x, Object y) { |
1290 |
> |
assertFalse(Arrays.equals(serialBytes(x), serialBytes(y))); |
1291 |
> |
} |
1292 |
> |
|
1293 |
> |
byte[] serialBytes(Object o) { |
1294 |
|
try { |
1295 |
|
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1296 |
|
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1297 |
|
oos.writeObject(o); |
1298 |
|
oos.flush(); |
1299 |
|
oos.close(); |
1300 |
< |
ByteArrayInputStream bin = |
1301 |
< |
new ByteArrayInputStream(bos.toByteArray()); |
1302 |
< |
ObjectInputStream ois = new ObjectInputStream(bin); |
1303 |
< |
return (T) ois.readObject(); |
1300 |
> |
return bos.toByteArray(); |
1301 |
> |
} catch (Throwable t) { |
1302 |
> |
threadUnexpectedException(t); |
1303 |
> |
return new byte[0]; |
1304 |
> |
} |
1305 |
> |
} |
1306 |
> |
|
1307 |
> |
@SuppressWarnings("unchecked") |
1308 |
> |
<T> T serialClone(T o) { |
1309 |
> |
try { |
1310 |
> |
ObjectInputStream ois = new ObjectInputStream |
1311 |
> |
(new ByteArrayInputStream(serialBytes(o))); |
1312 |
> |
T clone = (T) ois.readObject(); |
1313 |
> |
assertSame(o.getClass(), clone.getClass()); |
1314 |
> |
return clone; |
1315 |
|
} catch (Throwable t) { |
1316 |
|
threadUnexpectedException(t); |
1317 |
|
return null; |