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; |
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)" |
385 |
|
return suite; |
386 |
|
} |
387 |
|
|
388 |
+ |
/** Returns list of junit-style test method names in given class. */ |
389 |
+ |
public static ArrayList<String> testMethodNames(Class<?> testClass) { |
390 |
+ |
Method[] methods = testClass.getDeclaredMethods(); |
391 |
+ |
ArrayList<String> names = new ArrayList<String>(methods.length); |
392 |
+ |
for (Method method : methods) { |
393 |
+ |
if (method.getName().startsWith("test") |
394 |
+ |
&& Modifier.isPublic(method.getModifiers()) |
395 |
+ |
// method.getParameterCount() requires jdk8+ |
396 |
+ |
&& method.getParameterTypes().length == 0) { |
397 |
+ |
names.add(method.getName()); |
398 |
+ |
} |
399 |
+ |
} |
400 |
+ |
return names; |
401 |
+ |
} |
402 |
+ |
|
403 |
+ |
/** |
404 |
+ |
* Returns junit-style testSuite for the given test class, but |
405 |
+ |
* parameterized by passing extra data to each test. |
406 |
+ |
*/ |
407 |
+ |
public static <ExtraData> Test parameterizedTestSuite |
408 |
+ |
(Class<? extends JSR166TestCase> testClass, |
409 |
+ |
Class<ExtraData> dataClass, |
410 |
+ |
ExtraData data) { |
411 |
+ |
try { |
412 |
+ |
TestSuite suite = new TestSuite(); |
413 |
+ |
Constructor c = |
414 |
+ |
testClass.getDeclaredConstructor(dataClass, String.class); |
415 |
+ |
for (String methodName : testMethodNames(testClass)) |
416 |
+ |
suite.addTest((Test) c.newInstance(data, methodName)); |
417 |
+ |
return suite; |
418 |
+ |
} catch (Exception e) { |
419 |
+ |
throw new Error(e); |
420 |
+ |
} |
421 |
+ |
} |
422 |
+ |
|
423 |
+ |
/** |
424 |
+ |
* Returns junit-style testSuite for the jdk8 extension of the |
425 |
+ |
* given test class, but parameterized by passing extra data to |
426 |
+ |
* each test. Uses reflection to allow compilation in jdk7. |
427 |
+ |
*/ |
428 |
+ |
public static <ExtraData> Test jdk8ParameterizedTestSuite |
429 |
+ |
(Class<? extends JSR166TestCase> testClass, |
430 |
+ |
Class<ExtraData> dataClass, |
431 |
+ |
ExtraData data) { |
432 |
+ |
if (atLeastJava8()) { |
433 |
+ |
String name = testClass.getName(); |
434 |
+ |
String name8 = name.replaceAll("Test$", "8Test"); |
435 |
+ |
if (name.equals(name8)) throw new Error(name); |
436 |
+ |
try { |
437 |
+ |
return (Test) |
438 |
+ |
Class.forName(name8) |
439 |
+ |
.getMethod("testSuite", new Class[] { dataClass }) |
440 |
+ |
.invoke(null, data); |
441 |
+ |
} catch (Exception e) { |
442 |
+ |
throw new Error(e); |
443 |
+ |
} |
444 |
+ |
} else { |
445 |
+ |
return new TestSuite(); |
446 |
+ |
} |
447 |
+ |
|
448 |
+ |
} |
449 |
+ |
|
450 |
|
// Delays for timing-dependent tests, in milliseconds. |
451 |
|
|
452 |
|
public static long SHORT_DELAY_MS; |
481 |
|
} |
482 |
|
|
483 |
|
/** |
484 |
< |
* Returns a new Date instance representing a time delayMillis |
485 |
< |
* milliseconds in the future. |
484 |
> |
* Returns a new Date instance representing a time at least |
485 |
> |
* delayMillis milliseconds in the future. |
486 |
|
*/ |
487 |
|
Date delayedDate(long delayMillis) { |
488 |
< |
return new Date(System.currentTimeMillis() + delayMillis); |
488 |
> |
// Add 1 because currentTimeMillis is known to round into the past. |
489 |
> |
return new Date(System.currentTimeMillis() + delayMillis + 1); |
490 |
|
} |
491 |
|
|
492 |
|
/** |
554 |
|
// give thread some time to terminate |
555 |
|
thread.join(LONG_DELAY_MS); |
556 |
|
if (!thread.isAlive()) continue; |
489 |
– |
thread.stop(); |
557 |
|
throw new AssertionFailedError |
558 |
|
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
559 |
|
toString(), name)); |