@Override public ListenableFuture<Integer> apply(Throwable t) throws Exception { assertThat(t).isSameAs(raisedException); return secondary; } });
@Override public Integer apply(Throwable t) { assertThat(t).isSameAs(raisedException); return 20; } });
@Override public ListenableFuture<Integer> apply(Throwable t) throws Exception { assertThat(t).isSameAs(raisedException); return immediateFuture(20); } });
public void testGetRootCause_Loop() { Exception cause = new Exception(); Exception exception = new Exception(cause); cause.initCause(exception); try { Throwables.getRootCause(cause); fail("Should have throw IAE"); } catch (IllegalArgumentException expected) { assertThat(expected).hasCauseThat().isSameAs(cause); } }
private static void assertExecutionException(Future<?> future, Exception expectedCause) throws Exception { try { future.get(); fail("Expected ExecutionException"); } catch (ExecutionException e) { assertThat(e).hasCauseThat().isSameAs(expectedCause); } }
public void testGetCasualChainLoop() { Exception cause = new Exception(); Exception exception = new Exception(cause); cause.initCause(exception); try { Throwables.getCausalChain(cause); fail("Should have throw IAE"); } catch (IllegalArgumentException expected) { assertThat(expected).hasCauseThat().isSameAs(cause); } }
private void checkLoggedCause(Throwable t) { assertThat(popLoggedThrowable()).hasCauseThat().isSameAs(t); }
@GwtIncompatible // Throwables.getCauseAs(Throwable, Class) public void testGetCauseAs() { SomeCheckedException cause = new SomeCheckedException(); SomeChainingException thrown = new SomeChainingException(cause); assertThat(thrown).hasCauseThat().isSameAs(cause); assertThat(Throwables.getCauseAs(thrown, SomeCheckedException.class)).isSameAs(cause); assertThat(Throwables.getCauseAs(thrown, Exception.class)).isSameAs(cause); try { Throwables.getCauseAs(thrown, IllegalStateException.class); fail("Should have thrown CCE"); } catch (ClassCastException expected) { assertThat(expected).hasCauseThat().isSameAs(thrown); } }
public void testGetCheckedTimed_withGoodAndBadExceptionConstructor() { try { getChecked( FAILED_FUTURE_CHECKED_EXCEPTION, ExceptionWithGoodAndBadConstructor.class, 1, TimeUnit.SECONDS); fail(); } catch (ExceptionWithGoodAndBadConstructor expected) { assertThat(expected).hasCauseThat().isSameAs(CHECKED_EXCEPTION); } }
public void testGetCheckedUntimed_withGoodAndBadExceptionConstructor() throws Exception { try { getChecked(FAILED_FUTURE_CHECKED_EXCEPTION, ExceptionWithGoodAndBadConstructor.class); fail(); } catch (ExceptionWithGoodAndBadConstructor expected) { assertThat(expected).hasCauseThat().isSameAs(CHECKED_EXCEPTION); } }
public void testGet_runtimeException() { final RuntimeException e = new RuntimeException(); LoadingCache<Object, Object> map = CacheBuilder.newBuilder() .maximumSize(0) .removalListener(listener) .build(exceptionLoader(e)); try { map.getUnchecked(new Object()); fail(); } catch (UncheckedExecutionException uee) { assertThat(uee).hasCauseThat().isSameAs(e); } assertTrue(listener.isEmpty()); checkEmpty(map); } }
public void testBulkLoadingExceptionWithCause() { final Exception cause = new Exception(); final UncheckedExecutionException uee = new UncheckedExecutionException(cause); final ExecutionException ee = new ExecutionException(cause); LoadingCache<Object, Object> cacheUnchecked = CacheBuilder.newBuilder().build(bulkLoader(exceptionLoader(uee))); LoadingCache<Object, Object> cacheChecked = CacheBuilder.newBuilder().build(bulkLoader(exceptionLoader(ee))); try { cacheUnchecked.getAll(asList(new Object())); fail(); } catch (ExecutionException e) { fail(); } catch (UncheckedExecutionException caughtEe) { assertThat(caughtEe).hasCauseThat().isSameAs(uee); } try { cacheChecked.getAll(asList(new Object())); fail(); } catch (ExecutionException caughtEe) { assertThat(caughtEe).hasCauseThat().isSameAs(ee); } }
private static void assertFailed(AbstractFuture<Integer> future, Throwable expectedException) throws InterruptedException, TimeoutException { assertDone(future); assertThat(future.isCancelled()).isFalse(); try { getDone(future); fail(); } catch (ExecutionException e) { assertThat(e.getCause()).isSameAs(expectedException); } try { getDoneFromTimeoutOverload(future); fail(); } catch (ExecutionException e) { assertThat(e).hasCauseThat().isSameAs(expectedException); } }
public void testException() throws InterruptedException { final Throwable failure = new Throwable(); AbstractFuture<String> future = new AbstractFuture<String>() { { setException(failure); } }; ExecutionException ee1 = getExpectingExecutionException(future); ExecutionException ee2 = getExpectingExecutionException(future); // Ensure we get a unique execution exception on each get assertNotSame(ee1, ee2); assertThat(ee1).hasCauseThat().isSameAs(failure); assertThat(ee2).hasCauseThat().isSameAs(failure); checkStackTrace(ee1); checkStackTrace(ee2); }
public void testSetException() throws Exception { SettableFuture<Object> future = SettableFuture.create(); Exception e = new Exception("foobarbaz"); assertTrue(future.setException(e)); // Later attempts to set the future should return false. assertFalse(future.set(23)); assertFalse(future.setException(new Exception("quux"))); assertFalse(future.setFuture(SettableFuture.create())); // Check that the future has been set properly. assertTrue(future.isDone()); assertFalse(future.isCancelled()); try { future.get(); fail("Expected ExecutionException"); } catch (ExecutionException ee) { assertThat(ee).hasCauseThat().isSameAs(e); } }
public void testEvilFuture_setFuture() throws Exception { final RuntimeException exception = new RuntimeException("you didn't say the magic word!"); AbstractFuture<String> evilFuture = new AbstractFuture<String>() { @Override public void addListener(Runnable r, Executor e) { throw exception; } }; AbstractFuture<String> normalFuture = new AbstractFuture<String>() {}; normalFuture.setFuture(evilFuture); assertTrue(normalFuture.isDone()); try { normalFuture.get(); fail(); } catch (ExecutionException e) { assertThat(e).hasCauseThat().isSameAs(exception); } }
public void testStart_failed() throws Exception { final Exception exception = new Exception("deliberate"); AbstractIdleService service = new DefaultService() { @Override protected void startUp() throws Exception { throw exception; } }; try { service.startAsync().awaitRunning(); fail(); } catch (RuntimeException e) { assertThat(e).hasCauseThat().isSameAs(exception); } assertEquals(Service.State.FAILED, service.state()); }
public void testStart_failed() { final Exception exception = new Exception("deliberate"); TestService service = new TestService() { @Override protected void startUp() throws Exception { super.startUp(); throw exception; } }; assertEquals(0, service.startUpCalled); try { service.startAsync().awaitRunning(); fail(); } catch (RuntimeException e) { assertThat(e).hasCauseThat().isSameAs(exception); } assertEquals(1, service.startUpCalled); assertEquals(Service.State.FAILED, service.state()); assertThat(service.transitionStates).containsExactly(Service.State.STARTING); }
public void testStop_failed() throws Exception { final Exception exception = new Exception("deliberate"); AbstractIdleService service = new DefaultService() { @Override protected void shutDown() throws Exception { throw exception; } }; service.startAsync().awaitRunning(); try { service.stopAsync().awaitTerminated(); fail(); } catch (RuntimeException e) { assertThat(e).hasCauseThat().isSameAs(exception); } assertEquals(Service.State.FAILED, service.state()); } }
public void testBulkLoadUncheckedException() throws ExecutionException { Exception e = new RuntimeException(); CacheLoader<Object, Object> loader = exceptionLoader(e); LoadingCache<Object, Object> cache = CacheBuilder.newBuilder().recordStats().build(bulkLoader(loader)); CacheStats stats = cache.stats(); assertEquals(0, stats.missCount()); assertEquals(0, stats.loadSuccessCount()); assertEquals(0, stats.loadExceptionCount()); assertEquals(0, stats.hitCount()); try { cache.getAll(asList(new Object())); fail(); } catch (UncheckedExecutionException expected) { assertThat(expected).hasCauseThat().isSameAs(e); } stats = cache.stats(); assertEquals(1, stats.missCount()); assertEquals(0, stats.loadSuccessCount()); assertEquals(1, stats.loadExceptionCount()); assertEquals(0, stats.hitCount()); }