use of org.junit.runner.Runner in project intellij-community by JetBrains.
the class JUnit4TestRunnerUtil method getParameterizedRequest.
private static Request getParameterizedRequest(final String parameterString, final String methodName, Class clazz, RunWith clazzAnnotation) {
if (clazzAnnotation == null)
return null;
final Class runnerClass = clazzAnnotation.value();
if (Parameterized.class.isAssignableFrom(runnerClass)) {
try {
if (methodName != null) {
final Method method = clazz.getMethod(methodName, new Class[0]);
if (method != null && !method.isAnnotationPresent(Test.class) && TestCase.class.isAssignableFrom(clazz)) {
return Request.runner(JUnit45ClassesRequestBuilder.createIgnoreAnnotationAndJUnit4ClassRunner(clazz));
}
}
//ignore for junit4.4 and <
Class.forName("org.junit.runners.BlockJUnit4ClassRunner");
final Constructor runnerConstructor = runnerClass.getConstructor(new Class[] { Class.class });
return Request.runner((Runner) runnerConstructor.newInstance(new Object[] { clazz })).filterWith(new Filter() {
public boolean shouldRun(Description description) {
final String descriptionMethodName = description.getMethodName();
//filter by params
if (parameterString != null && descriptionMethodName != null && !descriptionMethodName.endsWith(parameterString)) {
return false;
}
//filter only selected method
if (methodName != null && descriptionMethodName != null && //valid for any parameter for current method
!descriptionMethodName.startsWith(methodName + "[") && !descriptionMethodName.equals(methodName)) {
//if fork mode used, parameter is included in the name itself
return false;
}
return true;
}
public String describe() {
if (parameterString == null) {
return methodName + " with any parameter";
}
if (methodName == null) {
return "Parameter " + parameterString + " for any method";
}
return methodName + " with parameter " + parameterString;
}
});
} catch (Throwable throwable) {
//return simple method runner
}
}
return null;
}
use of org.junit.runner.Runner in project gerrit by GerritCodeReview.
the class ConfigSuite method runnersFor.
private static List<Runner> runnersFor(Class<?> clazz) {
Method defaultConfig = getDefaultConfig(clazz);
List<Method> configs = getConfigs(clazz);
Map<String, org.eclipse.jgit.lib.Config> configMap = callConfigMapMethod(getConfigMap(clazz), configs);
Field parameterField = getOnlyField(clazz, Parameter.class);
checkArgument(parameterField != null, "No @ConfigSuite.Parameter found");
Field nameField = getOnlyField(clazz, Name.class);
List<Runner> result = Lists.newArrayListWithCapacity(configs.size() + 1);
try {
result.add(new ConfigRunner(clazz, parameterField, nameField, null, callConfigMethod(defaultConfig)));
for (Method m : configs) {
result.add(new ConfigRunner(clazz, parameterField, nameField, m.getName(), callConfigMethod(m)));
}
for (Map.Entry<String, org.eclipse.jgit.lib.Config> e : configMap.entrySet()) {
result.add(new ConfigRunner(clazz, parameterField, nameField, e.getKey(), e.getValue()));
}
return result;
} catch (InitializationError e) {
System.err.println("Errors initializing runners:");
for (Throwable t : e.getCauses()) {
t.printStackTrace();
}
throw new RuntimeException(e);
}
}
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;
}
Aggregations