下面列出了org.junit.runners.Suite#org.junit.runner.Description 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public final Statement apply(final Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
launchActivity();
base.evaluate();
if(!activity.isFinishing()) {
activity.finish();
}
activity = null; // Eager reference kill in case someone leaked our reference.
}
};
}
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelectionChecked(event);
IWorkbenchWindow[] windows = N4IDEXpectUIPlugin.getDefault().getWorkbench().getWorkbenchWindows();
try {
view = (N4IDEXpectView) windows[0].getActivePage().showView(
N4IDEXpectView.ID);
} catch (PartInitException e) {
N4IDEXpectUIPlugin.logError("cannot refresh test view window", e);
}
Description desc = (Description) selection.getFirstElement();
if (desc.isTest() && view.testsExecutionStatus.hasFailed(desc)) {
Throwable failureException = view.testsExecutionStatus.getFailure(desc).getException();
if (failureException instanceof ComparisonFailure) {
ComparisonFailure cf = (ComparisonFailure) failureException;
// display comparison view
displayComparisonView(cf, desc);
}
}
return null;
}
@Override
public Statement apply(Statement s, Description d) {
return new StatementAdapter(s) {
@Override
protected void before() throws Throwable {
if (!applied.getAndSet(true)) {
UncaughtExceptionHandler p = Thread.getDefaultUncaughtExceptionHandler();
try {
// Try to initialize a zookeeper class that reinitializes default exception handler.
Class<?> cl = NIOServerCnxnFactory.class;
// Make sure static initializers have been called.
Class.forName(cl.getName(), true, cl.getClassLoader());
} finally {
if (p == Thread.getDefaultUncaughtExceptionHandler()) {
// throw new RuntimeException("Zookeeper no longer resets default thread handler.");
}
Thread.setDefaultUncaughtExceptionHandler(p);
}
}
}
};
}
/**
* Implementation based on {@link TestWatcher}.
*/
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
List<Throwable> errors = new ArrayList<Throwable>();
startingQuietly(description, errors);
try {
base.evaluate();
succeededQuietly(description, errors);
} catch (AssumptionViolatedException e) {
errors.add(e);
skippedQuietly(e, description, errors);
} catch (Throwable t) {
errors.add(t);
failedQuietly(t, description, errors);
} finally {
finishedQuietly(description, errors);
}
MultipleFailureException.assertEmpty(errors);
}
};
}
@Override
public Statement apply(final Statement base, final Description description) {
if (!description.isTest()) return base;
final String contextPlatform = SeleniumConfig.getConfig().getContextPlatform();
final TargetPlatform targetPlatform = description.getAnnotation(TargetPlatform.class);
platform = TargetPlatformHandler.resolveTargetPlatform(testObject, targetPlatform);
if (TargetPlatformHandler.shouldRun(contextPlatform, platform)) {
return base;
} else {
return new Statement() {
@Override
public void evaluate() throws Throwable {
String message = String.format("%s.%s() doesn't specify platform '%s'",
description.getClassName(), description.getMethodName(), contextPlatform);
throw new AssumptionViolatedException(message);
}
};
}
}
@Override
public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
before();
base.evaluate();
StubRunnerRule.this.stubFinder().close();
}
private void before() {
stubFinder(new BatchStubRunnerFactory(builder().build(), verifier())
.buildBatchStubRunner());
StubRunnerRule.this.stubFinder().runStubs();
}
};
}
@Override
protected void starting(Description descriptor)
{
super.starting(descriptor);
deleteDirectory();
operator = new JsonFormatter();
validDataSink = new CollectorTestSink<Object>();
invalidDataSink = new CollectorTestSink<String>();
TestUtils.setSink(operator.out, validDataSink);
TestUtils.setSink(operator.err, invalidDataSink);
operator.setup(null);
operator.beginWindow(0);
}
@Override
default Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
before();
try {
base.evaluate();
} catch (final Throwable testThrowable) {
try {
afterTestFailure();
} catch (final Throwable afterFailureThrowable) {
testThrowable.addSuppressed(afterFailureThrowable);
}
throw testThrowable;
}
afterTestSuccess();
}
};
}
@Override
public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
beforeStart();
try {
base.evaluate();
finishExecutors();
} catch (Throwable t) {
throw new RuntimeException(t);
} finally {
afterFinished();
}
}
};
}
public void testIgnored(Description description) {
logger.info(">>> testIgnored called");
printDescription(description, 1);
if (description == null || description.getDisplayName() == null) {
logger.info("<<< testFinished called");
return;
}
TestSuiteResultsStore store = getStore(description);
// create new entry
TestCaseResult testCaseResult = new TestCaseResult(getTestCaseName(description),
getTestSuiteClassName(description),
//new Long(endTimeTestCase - startTimeTestCase).toString(),
"0.000",
"ignored", // status
null, // no failure
null // no systemOut
);
store.testCaseResults.add(testCaseResult);
store.skipped++;
logger.info("<<< testIgnored called");
}
public Statement apply( final Statement base, final Description description ) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
base.evaluate();
}
catch ( Throwable t ) {
if ( !isMissingCredsException( t ) ) {
throw t;
}
//do this so our test gets marked as ignored. Not pretty, but it works
Assume.assumeTrue( false );
}
}
};
}
private void skippedQuietly(AssumptionViolatedException e, Description description, List<Throwable> errors) {
try {
skipped(e, description);
} catch (Throwable t) {
errors.add(t);
}
}
private Single( Class<?> testClass, FrameworkMethod method ) throws Throwable
{
super( testClass );
this.method = method;
this.parameter = method.invokeExplosively( null );
this.description = Description.createTestDescription(
testClass, method.getName(), method.getAnnotations() );
}
private TestDescriptorInternal nullSafeDescriptor(Object id, Description description) {
if (methodName(description) != null) {
return new DefaultTestDescriptor(id, className(description), methodName(description));
} else {
return new DefaultTestDescriptor(id, className(description), "classMethod");
}
}
@Test
public void shouldPrintTheWarningSoICanSeeIt() throws Exception {
Failure failure = new Failure(Description.EMPTY, new RuntimeException("foo"));
//when
hacker.appendWarnings(failure, "unused stubbing blah");
//then
System.out.println(failure.getException());
}
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
deleteDir();
try {
base.evaluate();
} finally {
deleteDir();
}
}
};
}
private void succeededQuietly(Description description, List<Throwable> errors) {
try {
succeeded(description);
} catch (Throwable t) {
errors.add(t);
}
}
@Test
public void fromFailure() {
Class<SampleJUnitTest> testClass = SampleJUnitTest.class;
Description jUnitDescription = Description.createTestDescription(testClass, "sampleTest");
Throwable throwable = new RuntimeException("Your test is bad and you should feel bad.");
Failure jUnitFailure = new Failure(jUnitDescription, throwable);
ParcelableFailure parcelableFailure =
BundleJUnitUtils.getFailure(
parcelBundle(BundleJUnitUtils.getBundleFromFailure(jUnitFailure)));
assertThat(parcelableFailure.getTrace(), is(jUnitFailure.getTrace()));
compareDescription(parcelableFailure.getDescription(), jUnitFailure.getDescription());
}
@Override
public void testAssumptionFailure(Failure failure) {
logger.info(">>> testAssumptionFailure called");
Description description = failure.getDescription();
printDescription(description, 1);
// should have been created already in previous call to testStarted
TestSuiteResultsStore store = getStore(description);
store.errors++;
logger.info("<<< testAssumptionFailure called");
}
@Override
public void testStarted(Description description) throws Exception {
numTests++;
testFailure = null;
testStartTime = System.currentTimeMillis();
printf("Test Case '-[%s]' started.\n", parseDescription(description));
}
@Override
protected void starting(Description description) {
try {
insertData(cc.toString(), dd.toString(), spliceClassWatcher);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
spliceClassWatcher.closeAll();
}
}
@Override
public void testFinished(Description description) {
try {
final CaptureLog log = Capturer.stopCapture();
LOG.trace("Carving test {}.{}", description.getClassName(), description.getMethodName());
List<Class<?>> observedClasses = this.processLog(description, log);
for(Class<?> clazz : observedClasses) {
TestUsagePoolManager.getInstance().addTest(clazz.getName(), description.getClassName());
}
Capturer.clear();
} catch(Exception e) {
LOG.warn("Error in capturing log of class {}.",description.getClassName());
}
}
private void runEnabledTests(RunNotifier nested) {
if (enabledTests.isEmpty()) {
return;
}
Runner runner;
try {
runner = createExecutionRunner();
} catch (Throwable t) {
runner = new CannotExecuteRunner(getDisplayName(), target, t);
}
try {
if (!disabledTests.isEmpty()) {
((Filterable) runner).filter(new Filter() {
@Override
public boolean shouldRun(Description description) {
return !disabledTests.contains(description);
}
@Override
public String describe() {
return "disabled tests";
}
});
}
} catch (NoTestsRemainException e) {
return;
}
runner.run(nested);
}
@Override
public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
before();
try {
base.evaluate();
verify();
} finally {
after();
}
}
};
}
private static Function<Description, Stream<Class<?>>> descriptionToTestClass() {
return a -> {
final Class<?> clazz = a.getTestClass();
if (clazz != null) {
return Stream.of(clazz);
} else {
return Stream.empty();
}
};
}
List<Description> getAllDescriptions(Description description, String className) {
final AllExceptIgnoredTestRunnerBuilder allExceptIgnoredTestRunnerBuilder = new AllExceptIgnoredTestRunnerBuilder();
try {
final Class<?> testClass = description.getClass().getClassLoader().loadClass(className);
Runner runner = allExceptIgnoredTestRunnerBuilder.runnerForClass(testClass);
if (runner == null) {
//fall back to default runner
runner = Request.aClass(testClass).getRunner();
}
final Description runnerDescription = runner.getDescription();
return runnerDescription.getChildren();
} catch (Throwable throwable) {
throw new TestSuiteExecutionException(String.format("Unable to process Ignored class %s.", className), throwable);
}
}
@Override
public TestRule applyTo(final JenkinsInstance jenkins) {
return new TestWatcher() {
@Override protected void starting(Description description) {
warmUpUpdateCenterCacheFor(jenkins);
jenkins.client().installPlugins(requiredPlugins);
}
};
}
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override public void evaluate() throws Throwable {
Assert.assertNotNull(bean);
Assert.assertNotNull(url);
Assert.assertThat(url.toString(), CoreMatchers.containsString(AbstractTestCase.DEPLOYMENT_NAME));
base.evaluate();
}
};
}
@Override
public Statement apply(Statement statement, Description description) {
Statement result = statement;
Repeat repeat = description.getAnnotation(Repeat.class);
if (repeat != null) {
int times = repeat.value();
boolean parallel = repeat.parallel();
long timeout = repeat.timeout();
result = new RepeatStatement(times, parallel, timeout, statement);
}
return result;
}
/**
* Tests that the sharding is complete (each test is run at least once) and
* partitioned (each test is run at most once) -- in other words, that
* each test is run exactly once. This is a requirement of all test
* sharding functions.
*/
protected static void assertShardingIsCompleteAndPartitioned(List<Filter> filters,
List<Description> descriptions) {
Map<Filter, List<Description>> run = simulateTestRun(filters, descriptions);
assertThatCollectionContainsExactlyElementsInList(getAllValuesInMap(run), descriptions);
run = simulateSelfRandomizingTestRun(filters, descriptions);
assertThatCollectionContainsExactlyElementsInList(getAllValuesInMap(run), descriptions);
}