15 |
|
import java.io.ObjectOutputStream; |
16 |
|
import java.lang.management.ManagementFactory; |
17 |
|
import java.lang.management.ThreadInfo; |
18 |
+ |
import java.lang.reflect.Constructor; |
19 |
|
import java.lang.reflect.Method; |
20 |
+ |
import java.lang.reflect.Modifier; |
21 |
|
import java.security.CodeSource; |
22 |
|
import java.security.Permission; |
23 |
|
import java.security.PermissionCollection; |
52 |
|
import junit.framework.AssertionFailedError; |
53 |
|
import junit.framework.Test; |
54 |
|
import junit.framework.TestCase; |
55 |
+ |
import junit.framework.TestResult; |
56 |
|
import junit.framework.TestSuite; |
57 |
|
|
58 |
|
/** |
163 |
|
Integer.getInteger("jsr166.runsPerTest", 1); |
164 |
|
|
165 |
|
/** |
166 |
+ |
* The number of repetitions of the test suite (for finding leaks?). |
167 |
+ |
*/ |
168 |
+ |
private static final int suiteRuns = |
169 |
+ |
Integer.getInteger("jsr166.suiteRuns", 1); |
170 |
+ |
|
171 |
+ |
public JSR166TestCase() { super(); } |
172 |
+ |
public JSR166TestCase(String name) { super(name); } |
173 |
+ |
|
174 |
+ |
/** |
175 |
|
* A filter for tests to run, matching strings of the form |
176 |
|
* methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)" |
177 |
|
* Usefully combined with jsr166.runsPerTest. |
210 |
|
|
211 |
|
/** |
212 |
|
* Runs all JSR166 unit tests using junit.textui.TestRunner. |
201 |
– |
* Optional command line arg provides the number of iterations to |
202 |
– |
* repeat running the tests. |
213 |
|
*/ |
214 |
|
public static void main(String[] args) { |
215 |
+ |
main(suite(), args); |
216 |
+ |
} |
217 |
+ |
|
218 |
+ |
/** |
219 |
+ |
* Runs all unit tests in the given test suite. |
220 |
+ |
* Actual behavior influenced by jsr166.* system properties. |
221 |
+ |
*/ |
222 |
+ |
static void main(Test suite, String[] args) { |
223 |
|
if (useSecurityManager) { |
224 |
|
System.err.println("Setting a permissive security manager"); |
225 |
|
Policy.setPolicy(permissivePolicy()); |
226 |
|
System.setSecurityManager(new SecurityManager()); |
227 |
|
} |
228 |
< |
int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]); |
229 |
< |
|
230 |
< |
Test s = suite(); |
231 |
< |
for (int i = 0; i < iters; ++i) { |
214 |
< |
junit.textui.TestRunner.run(s); |
228 |
> |
for (int i = 0; i < suiteRuns; i++) { |
229 |
> |
TestResult result = junit.textui.TestRunner.run(suite); |
230 |
> |
if (!result.wasSuccessful()) |
231 |
> |
System.exit(1); |
232 |
|
System.gc(); |
233 |
|
System.runFinalization(); |
234 |
|
} |
218 |
– |
System.exit(0); |
235 |
|
} |
236 |
|
|
237 |
|
public static TestSuite newTestSuite(Object... suiteOrClasses) { |
281 |
|
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
282 |
|
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
283 |
|
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
284 |
< |
public static boolean atLeastJava9() { return JAVA_CLASS_VERSION >= 53.0; } |
284 |
> |
public static boolean atLeastJava9() { |
285 |
> |
return JAVA_CLASS_VERSION >= 53.0 |
286 |
> |
// As of 2015-09, java9 still uses 52.0 class file version |
287 |
> |
|| JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?(9|[0-9][0-9])$"); |
288 |
> |
} |
289 |
> |
public static boolean atLeastJava10() { |
290 |
> |
return JAVA_CLASS_VERSION >= 54.0 |
291 |
> |
|| JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?[0-9][0-9]$"); |
292 |
> |
} |
293 |
|
|
294 |
|
/** |
295 |
|
* Collects all JSR166 unit tests as one suite. |
393 |
|
return suite; |
394 |
|
} |
395 |
|
|
396 |
+ |
/** Returns list of junit-style test method names in given class. */ |
397 |
+ |
public static ArrayList<String> testMethodNames(Class<?> testClass) { |
398 |
+ |
Method[] methods = testClass.getDeclaredMethods(); |
399 |
+ |
ArrayList<String> names = new ArrayList<String>(methods.length); |
400 |
+ |
for (Method method : methods) { |
401 |
+ |
if (method.getName().startsWith("test") |
402 |
+ |
&& Modifier.isPublic(method.getModifiers()) |
403 |
+ |
// method.getParameterCount() requires jdk8+ |
404 |
+ |
&& method.getParameterTypes().length == 0) { |
405 |
+ |
names.add(method.getName()); |
406 |
+ |
} |
407 |
+ |
} |
408 |
+ |
return names; |
409 |
+ |
} |
410 |
+ |
|
411 |
+ |
/** |
412 |
+ |
* Returns junit-style testSuite for the given test class, but |
413 |
+ |
* parameterized by passing extra data to each test. |
414 |
+ |
*/ |
415 |
+ |
public static <ExtraData> Test parameterizedTestSuite |
416 |
+ |
(Class<? extends JSR166TestCase> testClass, |
417 |
+ |
Class<ExtraData> dataClass, |
418 |
+ |
ExtraData data) { |
419 |
+ |
try { |
420 |
+ |
TestSuite suite = new TestSuite(); |
421 |
+ |
Constructor c = |
422 |
+ |
testClass.getDeclaredConstructor(dataClass, String.class); |
423 |
+ |
for (String methodName : testMethodNames(testClass)) |
424 |
+ |
suite.addTest((Test) c.newInstance(data, methodName)); |
425 |
+ |
return suite; |
426 |
+ |
} catch (Exception e) { |
427 |
+ |
throw new Error(e); |
428 |
+ |
} |
429 |
+ |
} |
430 |
+ |
|
431 |
+ |
/** |
432 |
+ |
* Returns junit-style testSuite for the jdk8 extension of the |
433 |
+ |
* given test class, but parameterized by passing extra data to |
434 |
+ |
* each test. Uses reflection to allow compilation in jdk7. |
435 |
+ |
*/ |
436 |
+ |
public static <ExtraData> Test jdk8ParameterizedTestSuite |
437 |
+ |
(Class<? extends JSR166TestCase> testClass, |
438 |
+ |
Class<ExtraData> dataClass, |
439 |
+ |
ExtraData data) { |
440 |
+ |
if (atLeastJava8()) { |
441 |
+ |
String name = testClass.getName(); |
442 |
+ |
String name8 = name.replaceAll("Test$", "8Test"); |
443 |
+ |
if (name.equals(name8)) throw new Error(name); |
444 |
+ |
try { |
445 |
+ |
return (Test) |
446 |
+ |
Class.forName(name8) |
447 |
+ |
.getMethod("testSuite", new Class[] { dataClass }) |
448 |
+ |
.invoke(null, data); |
449 |
+ |
} catch (Exception e) { |
450 |
+ |
throw new Error(e); |
451 |
+ |
} |
452 |
+ |
} else { |
453 |
+ |
return new TestSuite(); |
454 |
+ |
} |
455 |
+ |
|
456 |
+ |
} |
457 |
+ |
|
458 |
|
// Delays for timing-dependent tests, in milliseconds. |
459 |
|
|
460 |
|
public static long SHORT_DELAY_MS; |
489 |
|
} |
490 |
|
|
491 |
|
/** |
492 |
< |
* Returns a new Date instance representing a time delayMillis |
493 |
< |
* milliseconds in the future. |
492 |
> |
* Returns a new Date instance representing a time at least |
493 |
> |
* delayMillis milliseconds in the future. |
494 |
|
*/ |
495 |
|
Date delayedDate(long delayMillis) { |
496 |
< |
return new Date(System.currentTimeMillis() + delayMillis); |
496 |
> |
// Add 1 because currentTimeMillis is known to round into the past. |
497 |
> |
return new Date(System.currentTimeMillis() + delayMillis + 1); |
498 |
|
} |
499 |
|
|
500 |
|
/** |
562 |
|
// give thread some time to terminate |
563 |
|
thread.join(LONG_DELAY_MS); |
564 |
|
if (!thread.isAlive()) continue; |
478 |
– |
thread.stop(); |
565 |
|
throw new AssertionFailedError |
566 |
|
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
567 |
|
toString(), name)); |