use of org.junit.runner.Runner in project xwiki-platform by xwiki.
the class XWikiExecutorSuite method getChildren.
@Override
protected List<Runner> getChildren() {
List<Runner> runners = new ArrayList<Runner>();
// Filter the test classes to run.
for (Runner runner : super.getChildren()) {
Description description = runner.getDescription();
String runnerName = description.getClassName();
if (runnerName.matches(PATTERN)) {
// If the entire test class matches, add it.
runners.add(runner);
} else {
// Otherwise, filter the test methods to run.
try {
METHOD_FILTER.apply(runner);
// If the runner still has tests remaining after the filtering, add it.
runners.add(runner);
} catch (NoTestsRemainException e) {
LOGGER.info("Skipping test class: {}", description.getClassName());
}
}
}
return runners;
}
use of org.junit.runner.Runner in project xwiki-platform by xwiki.
the class ArchiveSuite method createRunners.
/**
* Read the archive and build a list of runners for its content.
*
* @param archivePath path to the archive to use
* @return a list of test runners
* @throws InitializationError on errors
*/
private List<Runner> createRunners(String archivePath) throws InitializationError {
File archiveFile = new File(archivePath);
if (!archiveFile.exists()) {
throw new InitializationError("Not a file or directory ({" + archivePath + "])");
}
List<Runner> list = new ArrayList<Runner>();
if (archiveFile.isDirectory()) {
createRunners(archiveFile, list);
} else {
try {
final ZipFile archive = new ZipFile(archivePath);
Enumeration<? extends ZipEntry> entries = archive.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (entry.isDirectory()) {
continue;
}
Reader reader = new InputStreamReader(archive.getInputStream(entry));
addTest(list, entry.getName(), reader);
}
archive.close();
} catch (IOException exception) {
throw new InitializationError(exception);
}
}
return list;
}
use of org.junit.runner.Runner in project dspot by STAMP-project.
the class DefaultTestRunner method run.
@Override
public TestListener run(Class<?> testClass, Collection<String> testMethodNames, RunListener... additionalListeners) {
ExecutorService executor = Executors.newSingleThreadExecutor();
TestListener listener = new TestListener();
final Future<?> submit = executor.submit(() -> {
Request request = Request.aClass(testClass);
if (!testMethodNames.isEmpty()) {
request = request.filterWith(new MethodFilter(testMethodNames));
}
Runner runner = request.getRunner();
RunNotifier runNotifier = new RunNotifier();
Arrays.stream(additionalListeners).forEach(runNotifier::addListener);
runNotifier.addFirstListener(listener);
// Since we want to use our custom ClassLoader to run the tests of the project being executed by DSpot,
// and since we create a new thread for starting the JUnit Runner, we need to set the context ClassLoader
// to be our custom ClassLoader. This is so that any code in the tests or triggered by the test that uses
// the context ClassLoader will work.
// As an example if the tests call some code that uses Java's ServiceLoader then it would fail to find and
// load any provider located in our custom ClassLoader.
Thread.currentThread().setContextClassLoader(this.classLoader);
runner.run(runNotifier);
});
try {
long timeBeforeTimeOut = testMethodNames.isEmpty() ? AmplificationHelper.getTimeOutInMs() * (testClass.getMethods().length + 1) : AmplificationHelper.getTimeOutInMs() * (testMethodNames.size() + 1);
submit.get(timeBeforeTimeOut, TimeUnit.MILLISECONDS);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
submit.cancel(true);
executor.shutdownNow();
}
return listener;
}
use of org.junit.runner.Runner in project junit5 by junit-team.
the class TestClassRequestResolver method determineRunnerTestDescriptor.
private RunnerTestDescriptor determineRunnerTestDescriptor(Class<?> testClass, Runner runner, List<RunnerTestDescriptorAwareFilter> filters, UniqueId engineId) {
RunnerTestDescriptor runnerTestDescriptor = createCompleteRunnerTestDescriptor(testClass, runner, engineId);
if (!filters.isEmpty()) {
if (runner instanceof Filterable) {
Filter filter = createOrFilter(filters, runnerTestDescriptor);
Runner filteredRunner = runnerTestDescriptor.toRequest().filterWith(filter).getRunner();
runnerTestDescriptor = createCompleteRunnerTestDescriptor(testClass, filteredRunner, engineId);
} else {
Runner runnerToReport = (runner instanceof RunnerDecorator) ? ((RunnerDecorator) runner).getDecoratedRunner() : runner;
logger.warn(() -> //
"Runner " + runnerToReport.getClass().getName() + " (used on " + testClass.getName() + //
") does not support filtering" + " and will therefore be run completely.");
}
}
return runnerTestDescriptor;
}
use of org.junit.runner.Runner in project kanonizo by kanonizo.
the class JUnit4TestRunner method getRequest.
private Request getRequest(TestCase tc) {
Class<?> testClass = tc.getTestClass();
Method testMethod = tc.getMethod();
try {
final RunWith runWith = testClass.getAnnotation(RunWith.class);
if (runWith != null) {
final Class<? extends Runner> runnerClass = runWith.value();
if (runnerClass.isAssignableFrom(Parameterized.class)) {
try {
if (tc instanceof ParameterisedTestCase) {
ParameterisedTestCase ptc = (ParameterisedTestCase) tc;
Class.forName(// ignore IgnoreIgnored for junit4.4 and <
"org.junit.runners.BlockJUnit4ClassRunner");
return Request.runner(new ParameterizedMethodRunner(testClass, testMethod.getName(), ptc.getParameters()));
}
} catch (Throwable thrown) {
logger.error(thrown);
}
} else {
Constructor<?> con = Util.getConstructor(runnerClass, new Class[] { Class.class });
if (con != null) {
try {
Runner runner = (Runner) con.newInstance(new Object[] { testClass });
return Request.runner(runner).filterWith(Description.createTestDescription(testClass, testMethod.getName()));
} catch (InvocationTargetException e) {
}
}
}
} else {
if (testMethod != null && testMethod.getAnnotation(Ignore.class) != null) {
// override ignored case only
final Request classRequest = new ClassRequest(testClass) {
public Runner getRunner() {
try {
return new IgnoreIgnoredTestJUnit4ClassRunner(testClass);
} catch (Exception ignored) {
}
return super.getRunner();
}
};
return classRequest.filterWith(Description.createTestDescription(testClass, testMethod.getName()));
}
}
} catch (Exception ignored) {
logger.error(ignored);
}
return new Request() {
@Override
public Runner getRunner() {
try {
return new BlockJUnit4ClassRunner(testCase.getTestClass()) {
@Override
protected Statement withBeforeClasses(Statement statement) {
List<FrameworkMethod> beforeClass = getTestClass().getAnnotatedMethods(BeforeClass.class);
TestClass testClass = getTestClass();
if (beforeClass.size() > 0 && !initialisedClasses.stream().anyMatch(tc -> tc.getJavaClass().equals(testClass.getJavaClass()))) {
initialisedClasses.add(testClass);
return super.withBeforeClasses(statement);
} else {
return statement;
}
}
};
} catch (InitializationError e) {
return new ErrorReportingRunner(tc.getTestClass(), e);
}
}
}.filterWith(Description.createTestDescription(testClass, testMethod.getName()));
}
Aggregations