下面列出了怎么用org.junit.runners.ParentRunner的API类实例代码及写法,或者点击链接到github查看源代码。
private static Runner parallelize(Runner runner) {
int nThreads = Integer.getInteger(Constants.NTHREADS, Runtime.getRuntime().availableProcessors());
LOGGER.info("Using " + nThreads + " threads.");
if (runner instanceof ParentRunner) {
((ParentRunner<?>) runner).setScheduler(new RunnerScheduler() {
private final ExecutorService fService = Executors.newFixedThreadPool(nThreads);
@Override
public void schedule(Runnable childStatement) {
fService.submit(childStatement);
}
@Override
public void finished() {
try {
fService.shutdown();
fService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
e.printStackTrace(System.err);
}
}
});
}
return runner;
}
/**
* Initializes this runner by initializing {@link #expectedMethods} with the list of methods which are expected to be called. This is then also checked by
* {@link #methodBlock(FrameworkMethod)} and allows identifying the first and last methods correctly.
*/
private void ensureInitialized() {
if (expectedMethods == null) {
try {
final Method getChildrenMethod = ParentRunner.class.getDeclaredMethod("getFilteredChildren"); //$NON-NLS-1$
getChildrenMethod.setAccessible(true);
@SuppressWarnings("unchecked")
final Collection<FrameworkMethod> testMethods = (Collection<FrameworkMethod>) getChildrenMethod.invoke(this);
expectedMethods = ImmutableList.copyOf(Iterables.filter(testMethods, new Predicate<FrameworkMethod>() {
@Override
public boolean apply(final FrameworkMethod input) {
return input.getAnnotation(Ignore.class) == null;
}
}));
currentMethodIndex = 0;
// CHECKSTYLE:OFF
} catch (Exception e) {
// CHECKSTYLE:ON
throw new IllegalStateException(e);
}
}
}
boolean hasJunitTimeout(Description description) {
// Do not do apply the default timeout if the test has its own @Test(timeout).
Test testAnnotation = description.getAnnotation(Test.class);
if (testAnnotation != null && testAnnotation.timeout() > 0) {
return true;
}
// Do not do apply the default timeout if the test has its own @Rule Timeout.
if (runner instanceof ParentRunner) {
return BuckBlockJUnit4ClassRunner.hasTimeoutRule(((ParentRunner) runner).getTestClass());
}
Class<?> clazz = description.getTestClass();
while (clazz != null) {
for (Field field : clazz.getFields()) {
if (field.getAnnotationsByType(Rule.class).length > 0
&& field.getType().equals(Timeout.class)) {
return true;
}
}
clazz = clazz.getSuperclass();
}
return false;
}
/**
* Sets the thread's context class loader to the class loader for the test
* class.
*/
@Override
protected void runChild(Runner runner, RunNotifier notifier) {
ParentRunner<?> pr = (ParentRunner<?>) runner; // test class runner
ClassLoader cl = null;
try {
cl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(
pr.getTestClass().getJavaClass().getClassLoader());
super.runChild(runner, notifier);
} finally {
Thread.currentThread().setContextClassLoader(cl);
}
}
/**
* Initializes the test filter.
*
* @param parentRunner
* the {@link ParentRunner} to initialize, must not be {@code null}
*/
public static void initializeFilter(final ParentRunner<?> parentRunner) {
try {
parentRunner.filter(INSTANCE);
} catch (NoTestsRemainException e) {
// we ignore the case where no children are left
}
}
@Override
public void setScheduler(RunnerScheduler scheduler) {
for (Runner child : getChildren()) {
if (child instanceof ParentRunner<?>) {
((ParentRunner<?>) child).setScheduler(scheduler);
}
}
}
public ParallelParameterized(Class<?> klass) throws Throwable {
super(klass);
setScheduler(new ExecutorScheduler(() -> newCachedThreadPool(r -> new Thread(r, "TestRunner-Thread-" + klass))));
getChildren().forEach(child -> {
if (child instanceof ParentRunner<?>) {
((ParentRunner) child).setScheduler(new ExecutorScheduler(() -> newCachedThreadPool(r -> new Thread(r, "TestRunner-Thread-" + r.toString()))));
}
});
}
/**
* Reflectively invokes a {@link ParentRunner}'s getFilteredChildren method. Manipulating this
* list lets us control which tests will be run.
*/
static <T> List<T> getFilteredChildren(ParentRunner<T> parentRunner) {
try {
//noinspection unchecked
return new ArrayList<>((Collection<T>) getFilteredChildrenMethod.invoke(parentRunner));
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException("Failed to invoke getFilteredChildren()", e);
}
}
@Test
public void testGetCallerClassViaAnchorClass() throws Exception {
final Class<?> expected = BlockJUnit4ClassRunner.class;
final Class<?> actual = stackLocator.getCallerClass(ParentRunner.class);
// if this test fails in the future, it's probably because of a JUnit upgrade; check the new stack trace and
// update this test accordingly
assertSame(expected, actual);
}
@Test
public void testGetCallerClassViaAnchorClass() throws Exception {
final Class<?> expected = BlockJUnit4ClassRunner.class;
final Class<?> actual = StackLocatorUtil.getCallerClass(ParentRunner.class);
// if this test fails in the future, it's probably because of a JUnit upgrade; check the new stack trace and
// update this test accordingly
assertSame(expected, actual);
}
@Override
protected List<ParentRunner<?>> getChildren() {
return children;
}
@Override
protected Description describeChild(ParentRunner<?> child) {
return child.getDescription();
}
@Override
protected void runChild(ParentRunner<?> child, RunNotifier notifier) {
child.run(notifier);
}
@Override
protected List<ParentRunner<?>> getChildren()
{
return cucumber.getChildren();
}
@Override
protected Description describeChild(ParentRunner<?> child)
{
return cucumber.describeChild(child);
}
@Override
protected void runChild(ParentRunner<?> child, RunNotifier notifier)
{
cucumber.runChild(child, notifier);
}
@Override
protected Description describeChild(ParentRunner child) {
return child.getDescription();
}
/**
* Initializes the test sorter.
*
* @param parentRunner
* the {@link ParentRunner} to initialize, must not be {@code null}
*/
public void initializeSorter(final ParentRunner<?> parentRunner) {
parentRunner.sort(sorter);
}
/**
* Initializes the test sorter.
*
* @param parentRunner
* the {@link ParentRunner} to initialize, must not be {@code null}
*/
public void initializeSorter(final ParentRunner<?> parentRunner) {
parentRunner.sort(sorter);
}