org.junit.runners.ParentRunner Java Examples

The following examples show how to use org.junit.runners.ParentRunner. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ParallelComputer.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #2
Source File: DelegateRunNotifier.java    From buck with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #3
Source File: ClassRunner.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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);
    }
  }
}
 
Example #4
Source File: StackLocatorUtilTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #5
Source File: StackLocatorTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #6
Source File: ParentRunnerSpy.java    From burst with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
  }
}
 
Example #7
Source File: IsolatedClassLoaderSuite.java    From vanillacore with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
	}
}
 
Example #8
Source File: FilterRegistry.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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
  }
}
 
Example #9
Source File: ParallelParameterized.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
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()))));
    }
  });
}
 
Example #10
Source File: ParallelParameterized.java    From offheap-store with Apache License 2.0 5 votes vote down vote up
@Override
public void setScheduler(RunnerScheduler scheduler) {
  for (Runner child : getChildren()) {
    if (child instanceof ParentRunner<?>) {
      ((ParentRunner<?>) child).setScheduler(scheduler);
    }
  }
}
 
Example #11
Source File: JustTestLahRunner.java    From justtestlah with Apache License 2.0 4 votes vote down vote up
@Override
protected Description describeChild(ParentRunner<?> child) {
  return child.getDescription();
}
 
Example #12
Source File: ParameterizedCucumber.java    From senbot with MIT License 4 votes vote down vote up
@Override
protected Description describeChild(ParentRunner child) {
	return child.getDescription();
}
 
Example #13
Source File: NeodymiumCucumberWrapper.java    From neodymium-library with MIT License 4 votes vote down vote up
@Override
protected void runChild(ParentRunner<?> child, RunNotifier notifier)
{
    cucumber.runChild(child, notifier);
}
 
Example #14
Source File: NeodymiumCucumberWrapper.java    From neodymium-library with MIT License 4 votes vote down vote up
@Override
protected Description describeChild(ParentRunner<?> child)
{
    return cucumber.describeChild(child);
}
 
Example #15
Source File: NeodymiumCucumberWrapper.java    From neodymium-library with MIT License 4 votes vote down vote up
@Override
protected List<ParentRunner<?>> getChildren()
{
    return cucumber.getChildren();
}
 
Example #16
Source File: JustTestLahRunner.java    From justtestlah with Apache License 2.0 4 votes vote down vote up
@Override
protected void runChild(ParentRunner<?> child, RunNotifier notifier) {
  child.run(notifier);
}
 
Example #17
Source File: JustTestLahRunner.java    From justtestlah with Apache License 2.0 4 votes vote down vote up
@Override
protected List<ParentRunner<?>> getChildren() {
  return children;
}
 
Example #18
Source File: SorterUtil.java    From dsl-devkit with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * 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);
}
 
Example #19
Source File: SorterUtil.java    From dsl-devkit with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * 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);
}