下面列出了怎么用org.junit.internal.AssumptionViolatedException的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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);
}
};
}
/**
* 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);
}
};
}
/**
* 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);
}
};
}
/**
* 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, final Object[] params) {
return new Statement() {
public void evaluate() throws Throwable {
ArrayList<Throwable> errors = new ArrayList<Throwable>();
ParameterizedTestWatcher.this.startingQuietly(description, errors, params);
try {
base.evaluate();
ParameterizedTestWatcher.this.succeededQuietly(description, errors, params);
} catch (AssumptionViolatedException var7) {
errors.add(var7);
ParameterizedTestWatcher.this.skippedQuietly(var7, description, errors, params);
} catch (Throwable var8) {
errors.add(var8);
ParameterizedTestWatcher.this.failedQuietly(var8, description, errors, params);
} finally {
ParameterizedTestWatcher.this.finishedQuietly(description, errors, params);
}
MultipleFailureException.assertEmpty(errors);
}
};
}
@Test
public void testUnsafe() {
int m = 42;
long addr1 = createBuffer();
long addr2 = createBuffer();
try {
ResolvedJavaMethod method = getResolvedJavaMethod("readWriteReadUnsafe");
Object receiver = method.isStatic() ? null : this;
Result expect = executeExpected(method, receiver, addr1, m);
if (getCodeCache() == null) {
return;
}
testAgainstExpected(method, expect, receiver, addr2, m);
} catch (AssumptionViolatedException e) {
// Suppress so that subsequent calls to this method within the
// same Junit @Test annotated method can proceed.
} finally {
disposeBuffer(addr1);
disposeBuffer(addr2);
}
}
@Test
public void testByteBuffer() {
int m = 42;
try {
ResolvedJavaMethod method = getResolvedJavaMethod("readWriteReadByteBuffer");
Object receiver = method.isStatic() ? null : this;
Result expect = executeExpected(method, receiver, ByteBuffer.allocateDirect(32), m);
if (getCodeCache() == null) {
return;
}
ByteBuffer warmupBuffer = ByteBuffer.allocateDirect(32);
for (int i = 0; i < 10000; ++i) {
readWriteReadByteBuffer(warmupBuffer, (i % 50) + 1);
warmupBuffer.putInt(0, 0);
}
testAgainstExpected(method, expect, receiver, ByteBuffer.allocateDirect(32), m);
} catch (AssumptionViolatedException e) {
// Suppress so that subsequent calls to this method within the
// same Junit @Test annotated method can proceed.
}
}
public static S3AFileSystem createTestFileSystem(Configuration conf) throws
IOException {
String fsname = conf.getTrimmed(TestS3AFileSystemContract.TEST_FS_S3A_NAME, "");
boolean liveTest = !StringUtils.isEmpty(fsname);
URI testURI = null;
if (liveTest) {
testURI = URI.create(fsname);
liveTest = testURI.getScheme().equals(Constants.FS_S3A);
}
if (!liveTest) {
// This doesn't work with our JUnit 3 style test cases, so instead we'll
// make this whole class not run by default
throw new AssumptionViolatedException(
"No test filesystem in " + TestS3AFileSystemContract.TEST_FS_S3A_NAME);
}
S3AFileSystem fs1 = new S3AFileSystem();
//enable purging in tests
conf.setBoolean(Constants.PURGE_EXISTING_MULTIPART, true);
conf.setInt(Constants.PURGE_EXISTING_MULTIPART_AGE, 0);
fs1.initialize(testURI, conf);
return fs1;
}
public static S3AFileSystem createTestFileSystem(Configuration conf) throws
IOException {
String fsname = conf.getTrimmed(TestS3AFileSystemContract.TEST_FS_S3A_NAME, "");
boolean liveTest = !StringUtils.isEmpty(fsname);
URI testURI = null;
if (liveTest) {
testURI = URI.create(fsname);
liveTest = testURI.getScheme().equals(Constants.FS_S3A);
}
if (!liveTest) {
// This doesn't work with our JUnit 3 style test cases, so instead we'll
// make this whole class not run by default
throw new AssumptionViolatedException(
"No test filesystem in " + TestS3AFileSystemContract.TEST_FS_S3A_NAME);
}
S3AFileSystem fs1 = new S3AFileSystem();
//enable purging in tests
conf.setBoolean(Constants.PURGE_EXISTING_MULTIPART, true);
conf.setInt(Constants.PURGE_EXISTING_MULTIPART_AGE, 0);
fs1.initialize(testURI, conf);
return fs1;
}
/**
* Helper method for {@link #expectThrows} and {@link #expectThrowsAnyOf} that takes care of propagating
* any {@link AssertionError} or {@link AssumptionViolatedException} instances thrown if and only if they
* are super classes of the <code>expectedTypes</code>. Otherwise simply returns any {@link Throwable}
* thrown, regardless of type, or null if the <code>runnable</code> completed w/o error.
*/
private static Throwable _expectThrows(List<? extends Class<?>> expectedTypes, ThrowingRunnable runnable) {
try {
runnable.run();
} catch (AssertionError | AssumptionViolatedException ae) {
for (Class<?> expectedType : expectedTypes) {
if (expectedType.isInstance(ae)) { // user is expecting this type explicitly
return ae;
}
}
throw ae;
} catch (Throwable e) {
return e;
}
return null;
}
/**
* Tests that {@link #expectThrows} behaves correctly when the Runnable contains an
* assumption that does not pass: by allowing that assumption to propogate and cause
* the test to <code>SKIP</code>.
*/
public void testNestedAssume() {
final AtomicBoolean ran = new AtomicBoolean(false);
AssumptionViolatedException caught = null;
try {
final IOException returned = expectThrows(IOException.class, () -> {
ran.getAndSet(true);
assumeTrue("this assumption should propogate", false);
});
fail("must not complete"); // NOTE: we don't use expectThrows to test expectThrows
} catch (AssumptionViolatedException ave) {
caught = ave;
}
assertTrue(ran.get());
assertNotNull(caught);
assertEquals("this assumption should propogate", caught.getMessage());
}
/**
* Tests that {@link #expectThrows} behaves correctly when the Runnable contains an
* assumption that does not pass but the caller has explicitly said they expect an Exception of that type:
* by returning that assumption failure Exception.
*/
public void testExpectingNestedAssume() {
final AtomicBoolean ran = new AtomicBoolean(false);
AssumptionViolatedException returned = null;
try {
returned = expectThrows(AssumptionViolatedException.class, () -> {
ran.getAndSet(true);
assumeTrue("this assumption should be returned, not propogated", false);
});
} catch (AssumptionViolatedException caught) { // NOTE: we don't use expectThrows to test expectThrows
assertNull("An exception should not have been thrown", caught);
}
assertTrue(ran.get());
assertNotNull(returned);
assertEquals("this assumption should be returned, not propogated", returned.getMessage());
}
protected Statement prepareMethodBlock(final FrameworkMethod method, final RunNotifier notifier) {
final Statement methodBlock = superMethodBlock(method);
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
methodBlock.evaluate();
Description description = describeChild(method);
try {
notifier.fireTestStarted(description);
notifier.fireTestAssumptionFailed(new Failure(description, new AssumptionViolatedException("Method " + method.getName() + " did parse any input")));
} finally {
notifier.fireTestFinished(description);
}
} catch(TestDataCarrier testData) {
AbstractParallelScenarioRunner.this.testData.put(method, testData.getData());
}
}
};
}
protected void process(String data) throws Exception {
IInjectorProvider delegate = getOrCreateInjectorProvider().getDelegate();
if (delegate instanceof IRegistryConfigurator) {
IRegistryConfigurator registryConfigurator = (IRegistryConfigurator) delegate;
registryConfigurator.setupRegistry();
try {
ScenarioProcessor processor = delegate.getInjector().getInstance(processorClass);
String preProcessed = processor.preProcess(data);
if (preProcessed == null) {
throw new AssumptionViolatedException("Input is filtered by the pre processing step: " + data);
}
doProcess(preProcessed, processor);
} finally {
registryConfigurator.restoreRegistry();
}
}
}
/**
* 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<>();
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);
}
};
}
/**
* 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<>();
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);
}
MultipleFailureException.assertEmpty(errors);
}
};
}
/**
* 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);
}
};
}
/**
* 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<>();
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 void runChild(final FrameworkMethod method, final RunNotifier notifier) {
final Description description = describeChild(method);
if (this.isIgnored(method)) {
notifier.fireTestIgnored(description);
} else {
final EachTestNotifier eachNotifier = new EachTestNotifier(notifier, description);
eachNotifier.fireTestStarted();
boolean ignored = false;
try {
this.methodBlock(method).evaluate();
} catch (AssumptionViolatedException ave) {
eachNotifier.addFailedAssumption(ave);
} catch (Throwable e) {
if (validateForGraphComputer(e)) {
eachNotifier.fireTestIgnored();
logger.info(e.getMessage());
ignored = true;
} else
eachNotifier.addFailure(e);
} finally {
if (!ignored)
eachNotifier.fireTestFinished();
}
}
}
/**
* 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<>();
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);
}
};
}
/**
* 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<>();
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);
}
};
}
/**
* 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<>();
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);
}
};
}
/**
* 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<>();
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);
}
};
}
private void skippedQuietly(AssumptionViolatedException e, Description description, List<Throwable> errors) {
try {
skipped(e, description);
} catch (Throwable t) {
errors.add(t);
}
}
private void skippedQuietly(AssumptionViolatedException e, Description description, List<Throwable> errors) {
try {
skipped(e, description);
} catch (Throwable t) {
errors.add(t);
}
}
private void skippedQuietly(AssumptionViolatedException e, Description description, List<Throwable> errors,
Object[] params) {
try {
if (e instanceof org.junit.AssumptionViolatedException) {
this.skipped((org.junit.AssumptionViolatedException) e, description, params);
} else {
this.skipped(e, description, params);
}
} catch (Throwable var5) {
errors.add(var5);
}
}
private void skippedQuietly(AssumptionViolatedException e,
Description description, List<Throwable> errors) {
try {
skipped(e, description);
} catch (Throwable t) {
errors.add(t);
}
}
public void fireTestCaseFailure(Throwable throwable) {
if (throwable instanceof AssumptionViolatedException) {
getLifecycle().fire(new TestCaseCanceledEvent().withThrowable(throwable));
} else {
getLifecycle().fire(new TestCaseFailureEvent().withThrowable(throwable));
}
}
protected void test(String name, Object... args) {
try {
ResolvedJavaMethod method = getResolvedJavaMethod(name);
Object receiver = method.isStatic() ? null : this;
test(method, receiver, args);
} catch (AssumptionViolatedException e) {
// Suppress so that subsequent calls to this method within the
// same Junit @Test annotated method can proceed.
}
}
@Override
protected void setUp() throws Exception {
Configuration conf = new Configuration();
store = getFileSystemStore();
fs = new S3FileSystem(store);
String fsname = conf.get(KEY_TEST_FS);
if (StringUtils.isEmpty(fsname)) {
throw new AssumptionViolatedException(
"No test FS defined in :" + KEY_TEST_FS);
}
fs.initialize(URI.create(fsname), conf);
}