static void fail(Throwable cause, Object message) { AssertionFailedError assertionFailedError = new AssertionFailedError(String.valueOf(message)); assertionFailedError.initCause(cause); throw assertionFailedError; }
private static AssertionFailedError sanityError( Class<?> cls, List<String> explicitTestNames, String description, Throwable e) { String message = String.format( Locale.ROOT, "Error in automated %s of %s\n" + "If the class is better tested explicitly, you can add %s() to %sTest", description, cls, explicitTestNames.get(0), cls.getName()); AssertionFailedError error = new AssertionFailedError(message); error.initCause(e); return error; }
Object getResult() { if (throwable != null) { throw (AssertionFailedError) new AssertionFailedError().initCause(throwable); } return result; }
static AssertionFailedError failureWithCause(Throwable cause, String message) { AssertionFailedError failure = new AssertionFailedError(message); failure.initCause(cause); return failure; }
public int await() { try { return super.await(2 * LONG_DELAY_MS, MILLISECONDS); } catch (TimeoutException e) { throw new AssertionFailedError("timed out"); } catch (Exception e) { AssertionFailedError afe = new AssertionFailedError("Unexpected exception: " + e); afe.initCause(e); throw afe; } } }
/** Sleeps until the given time has elapsed. Throws AssertionFailedError if interrupted. */ void sleep(long millis) { try { delay(millis); } catch (InterruptedException ie) { AssertionFailedError afe = new AssertionFailedError("Unexpected InterruptedException"); afe.initCause(ie); throw afe; } }
private static int findStackFrame(ExecutionException e, String clazz, String method) { StackTraceElement[] elements = e.getStackTrace(); for (int i = 0; i < elements.length; i++) { StackTraceElement element = elements[i]; if (element.getClassName().equals(clazz) && element.getMethodName().equals(method)) { return i; } } AssertionFailedError failure = new AssertionFailedError( "Expected element " + clazz + "." + method + " not found in stack trace"); failure.initCause(e); throw failure; }
/** * Extra checks that get done for all test cases. * * <p>Triggers test case failure if any thread assertions have failed, by rethrowing, in the test * harness thread, any exception recorded earlier by threadRecordFailure. * * <p>Triggers test case failure if interrupt status is set in the main thread. */ public void tearDown() throws Exception { Throwable t = threadFailure.getAndSet(null); if (t != null) { if (t instanceof Error) throw (Error) t; else if (t instanceof RuntimeException) throw (RuntimeException) t; else if (t instanceof Exception) throw (Exception) t; else { AssertionFailedError afe = new AssertionFailedError(t.toString()); afe.initCause(t); throw afe; } } if (Thread.interrupted()) throw new AssertionFailedError("interrupt status set in main thread"); }
/** * Records the given exception using {@link #threadRecordFailure}, then rethrows the exception, * wrapping it in an AssertionFailedError if necessary. */ public void threadUnexpectedException(Throwable t) { threadRecordFailure(t); t.printStackTrace(); if (t instanceof RuntimeException) throw (RuntimeException) t; else if (t instanceof Error) throw (Error) t; else { AssertionFailedError afe = new AssertionFailedError("unexpected exception: " + t); afe.initCause(t); throw afe; } }
@Override public int await() { try { return super.await(2 * LONG_DELAY_MS, MILLISECONDS); } catch (TimeoutException timedOut) { throw new AssertionFailedError("timed out"); } catch (Exception fail) { AssertionFailedError afe = new AssertionFailedError("Unexpected exception: " + fail); afe.initCause(fail); throw afe; } } }
Arrays.toString(params), cause)); error.initCause(cause); throw error; } catch (IllegalAccessException e) {
/** * Retrieves the result of a {@code Future} known to be done but uses the {@code get(long, * TimeUnit)} overload in order to test that method. */ static <V> V getDoneFromTimeoutOverload(Future<V> future) throws ExecutionException { checkState(future.isDone(), "Future was expected to be done: %s", future); try { return getUninterruptibly(future, 0, SECONDS); } catch (TimeoutException e) { AssertionFailedError error = new AssertionFailedError(e.getMessage()); error.initCause(e); throw error; } }
/** * Sleeps until the given time has elapsed. * Throws AssertionFailedError if interrupted. */ void sleep(long millis) { try { delay(millis); } catch (InterruptedException fail) { AssertionFailedError afe = new AssertionFailedError("Unexpected InterruptedException"); afe.initCause(fail); throw afe; } }
@SuppressWarnings("deprecation") @Override public void tearDown() throws Exception { stop(); join(); if (uncaughtThrowable != null) { throw (AssertionFailedError) new AssertionFailedError("Uncaught throwable in " + getName()) .initCause(uncaughtThrowable); } }
/** * Records the given exception using {@link #threadRecordFailure}, * then rethrows the exception, wrapping it in an * AssertionFailedError if necessary. */ public void threadUnexpectedException(Throwable t) { threadRecordFailure(t); t.printStackTrace(); if (t instanceof RuntimeException) { throw (RuntimeException) t; } else if (t instanceof Error) { throw (Error) t; } else { AssertionFailedError afe = new AssertionFailedError("unexpected exception: " + t); afe.initCause(t); throw afe; } }
public void assertThrows(Class<? extends Throwable> expectedExceptionClass, Runnable... throwingActions) { for (Runnable throwingAction : throwingActions) { boolean threw = false; try { throwingAction.run(); } catch (Throwable t) { threw = true; if (!expectedExceptionClass.isInstance(t)) { AssertionFailedError afe = new AssertionFailedError ("Expected " + expectedExceptionClass.getName() + ", got " + t.getClass().getName()); afe.initCause(t); threadUnexpectedException(afe); } } if (!threw) { shouldThrow(expectedExceptionClass.getName()); } } }
AssertionFailedError afe = new AssertionFailedError(t.toString()); afe.initCause(t); throw afe;
"Got " + exceptions.size() + " exception(s)"); afe.initCause(exceptions.getFirst()); throw afe;