下面列出了org.junit.runner.Description#isSuite() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean test(Object receiver, String property, Object[] args,
Object expectedValue) {
if (receiver instanceof IStructuredSelection == false) {
return false;
}
Object element = ((IStructuredSelection) receiver).getFirstElement();
if (element == null || element instanceof Description == false) {
return false;
}
Description description = (Description) element;
if (property.equals("isTest")) {
return description.isTest();
}
if (property.equals("isSuite")) {
return description.isSuite();
}
return false;
}
@Override
public String getText(Object element) {
if (element instanceof Description == false) {
return "";
}
Description desc = ((Description) element);
if (desc.isSuite()) {
return N4IDEXpectFileNameUtil.getSuiteName(desc);
}
if (desc.isTest()) {
return N4IDEXpectFileNameUtil.getTestName(desc);
}
return "";
}
/**
* get icon based on item type (test/suite) and its status (pass/failed/exception/skip/in progress...)
*/
private ImageDescriptor getImageDescriptor(Object element) throws RuntimeException {
ImageDescriptor descriptor = null;
if (element instanceof Description == false) {
String msg = "Unknown type of element in tree of type " + element.getClass().getName();
Exception e = new RuntimeException(msg);
N4IDEXpectUIPlugin.logError("cannot obtain image descriptor, fallback to default", e);
return getImageDescriptor("n4_logo.png");
}
Description desc = (Description) element;
if (desc.isTest()) {
descriptor = getTestImageDescriptor(executionStatus.getStatus(desc));
} else if (desc.isSuite()) {
descriptor = getSuiteImageDescriptor(desc, executionStatus.getStatus(desc));
} else {
descriptor = getImageDescriptor("n4_logo.png");
}
return descriptor;
}
@Override
public Statement apply(final Statement s, final Description d) {
if (!d.isSuite()) {
throw new IllegalArgumentException("This is a @ClassRule (applies to suites only).");
}
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
description = d;
s.evaluate();
} finally {
description = null;
}
}
};
}
private void runRepeatedly(Statement statement, Description description,
RunNotifier notifier) {
notifier.addListener(new RunListener() {
@Override
public void testFailure(Failure failure) {
hasFailure = true;
}
});
for (Description desc : description.getChildren()) {
if (hasFailure) {
notifier.fireTestIgnored(desc);
} else if(!desc.isSuite()) {
runLeaf(statement, desc, notifier);
}
}
}
@Override
public Statement apply(final Statement base, final Description description) {
if (!description.isSuite()) {
return base;
}
suiteName = description.getDisplayName();
return new Statement() {
@Override
public void evaluate() throws Throwable {
runner = new EvictionBenchmarkRunner(suiteName);
if (readStoredResults) {
runner.readEvaluationResults();
}
base.evaluate();
runner.printRankingSummary(candidate, peers);
}
};
}
/***/
public static String getSuiteName(Description description) {
String text = null;
if (description.isSuite()) {
String s = description.getDisplayName();
int posSemi = s.indexOf(":");
text = s.substring(0, posSemi);
}
return text;
}
@Override
public void testFinished(Description description) throws IllegalAccessException {
if (description.isSuite()) {
testSuiteFinished(getSuiteUid(description));
} else {
getLifecycle().fire(new TestCaseFinishedEvent());
}
}
public String getSuiteUid(Description description) throws IllegalAccessException {
String suiteName = description.getClassName();
if (!description.isSuite()) {
suiteName = extractClassName(description);
}
if (!getSuites().containsKey(suiteName)) {
//Fix NPE
Description suiteDescription = Description.createSuiteDescription(suiteName);
testSuiteStarted(suiteDescription, suiteName);
}
return getSuites().get(suiteName);
}
@Override
public Statement apply(Statement base, Description description) {
if (description.isSuite()) {
return cluster.apply(base, description);
} else if (description.isTest()) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
membership.register();
Thread.sleep(100);
membership.awaitAdvanceInterruptibly(membership.arrive());
try {
activeCycle.awaitAdvanceInterruptibly(activeCycle.arrive());
try {
base.evaluate();
} finally {
activeCycle.arriveAndDeregister();
}
} finally {
membership.arriveAndDeregister();
}
}
};
} else {
return base;
}
}
@Override
public boolean shouldRun(Description description) {
if (description.isSuite()) {
return true;
}
boolean match = pattern.matcher(formatDescriptionName(description)).find();
return isNegated ? !match : match;
}
/**
* Creates a model for a JUnit4 suite. This can be expensive; callers should
* consider memoizing the result.
*
* @return model.
*/
@Override
public TestSuiteModel get() {
Description root = request.getRunner().getDescription();
// A test class annotated with @Ignore effectively has no test methods,
// which is what isSuite() tests for.
if (!root.isSuite()) {
return builder.build(suiteName);
} else {
return builder.build(suiteName, root);
}
}
@Override
public boolean shouldRun(Description description) {
if (description.isSuite()) {
return true;
}
int mod = description.getDisplayName().hashCode() % totalShards;
if (mod < 0) {
mod += totalShards;
}
if (mod < 0 || mod >= totalShards) {
throw new IllegalStateException();
}
return mod == shardIndex;
}
@Override
public boolean shouldRun(Description description) {
if (description.isSuite()) {
return true;
}
Integer testNumber = testToShardMap.get(description);
if (testNumber == null) {
throw new IllegalArgumentException("This filter keeps a mapping from each test "
+ "description to a shard, and the given description was not passed in when "
+ "filter was constructed: " + description);
}
return (testNumber % totalShards) == shardIndex;
}
@Override
public boolean shouldRun(Description description) {
if (description.isSuite()) {
return true;
}
if (!allDescriptions.contains(description)) {
throw new IllegalArgumentException("Not in the suite: " + description);
}
return descriptionsToRun.contains(description);
}
@Override
public Statement apply(final Statement st, final Description d) {
if (d.isSuite()) {
shared = true;
description = d;
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
st.evaluate();
} finally {
cleanupClass();
}
}
};
}
if (d.isTest()) {
description = d;
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
st.evaluate();
} finally {
cleanupMethod();
}
}
};
}
throw new UnsupportedOperationException("hey?");
}
/**
* When called will check if provided data contains {@link Description test description} with failed status stored
* in {@link N4IDEXpectView test view}. If that holds, will generate data for bug report in a console view,
* otherwise will show message to reconfigure and rerun Xpect tests.
*/
@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();
// handle failed suite
if (desc.isSuite()) {
final N4IDEXpectView finalview = view;
boolean suitePassed = desc.getChildren().stream()
.noneMatch(childDescription -> finalview.testsExecutionStatus.hasFailed(childDescription));
if (suitePassed) {
XpectFileContentsUtil.getXpectFileContentAccess(desc).ifPresent(
xpectFielContentAccess -> {
if (xpectFielContentAccess.containsFixme()) {
generateAndDisplayReport(
N4IDEXpectFileNameUtil.getSuiteName(desc),
xpectFielContentAccess.getContetns());
}
});
} else {
XpectConsole console = ConsoleDisplayMgr.getOrCreate("generated bug for "
+ N4IDEXpectFileNameUtil.getSuiteName(desc));
console.clear();
String ls = System.lineSeparator();
console.log("Suite must be passing and contain XPECT FIXME marker to be submited bug report. Please :"
+ ls + " - fix failing tests" + ls + " - mark test in question with XPECT FIXME");
}
}
return null;
}
private boolean isSuiteAssumptionFailure(Description description) {
return description.isSuite() && description.getAnnotation(Ignore.class) == null;
}