diff options
Diffstat (limited to 'android/guava-tests/test/com/google/common/util/concurrent/AbstractFutureTest.java')
-rw-r--r-- | android/guava-tests/test/com/google/common/util/concurrent/AbstractFutureTest.java | 66 |
1 files changed, 33 insertions, 33 deletions
diff --git a/android/guava-tests/test/com/google/common/util/concurrent/AbstractFutureTest.java b/android/guava-tests/test/com/google/common/util/concurrent/AbstractFutureTest.java index 89d92c8c4..6bf4c99d1 100644 --- a/android/guava-tests/test/com/google/common/util/concurrent/AbstractFutureTest.java +++ b/android/guava-tests/test/com/google/common/util/concurrent/AbstractFutureTest.java @@ -20,6 +20,8 @@ import static com.google.common.base.StandardSystemProperty.JAVA_SPECIFICATION_V import static com.google.common.base.StandardSystemProperty.OS_NAME; import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertWithMessage; +import static com.google.common.util.concurrent.MoreExecutors.directExecutor; +import static org.junit.Assert.assertThrows; import com.google.common.annotations.GwtIncompatible; import com.google.common.collect.Iterables; @@ -97,13 +99,8 @@ public class AbstractFutureTest extends TestCase { assertTrue(future.isDone()); assertFalse(future.wasInterrupted()); assertFalse(future.interruptTaskWasCalled); - try { - future.get(); - fail("Expected CancellationException"); - } catch (CancellationException e) { - // See AbstractFutureCancellationCauseTest for how to set causes - assertThat(e).hasCauseThat().isNull(); - } + CancellationException e = assertThrows(CancellationException.class, () -> future.get()); + assertThat(e).hasCauseThat().isNull(); } public void testCancel_notDoneInterrupt() throws Exception { @@ -113,13 +110,8 @@ public class AbstractFutureTest extends TestCase { assertTrue(future.isDone()); assertTrue(future.wasInterrupted()); assertTrue(future.interruptTaskWasCalled); - try { - future.get(); - fail("Expected CancellationException"); - } catch (CancellationException e) { - // See AbstractFutureCancellationCauseTest for how to set causes - assertThat(e).hasCauseThat().isNull(); - } + CancellationException e = assertThrows(CancellationException.class, () -> future.get()); + assertThat(e).hasCauseThat().isNull(); } public void testCancel_done() throws Exception { @@ -156,12 +148,8 @@ public class AbstractFutureTest extends TestCase { AbstractFuture<String> normalFuture = new AbstractFuture<String>() {}; normalFuture.setFuture(evilFuture); assertTrue(normalFuture.isDone()); - try { - normalFuture.get(); - fail(); - } catch (ExecutionException e) { - assertThat(e).hasCauseThat().isSameInstanceAs(exception); - } + ExecutionException e = assertThrows(ExecutionException.class, () -> normalFuture.get()); + assertThat(e).hasCauseThat().isSameInstanceAs(exception); } public void testRemoveWaiter_interruption() throws Exception { @@ -266,13 +254,10 @@ public class AbstractFutureTest extends TestCase { assertThat(testFuture.toString()) .matches( "[^\\[]+\\[status=PENDING, info=\\[cause=\\[Because this test isn't done\\]\\]\\]"); - try { - testFuture.get(1, TimeUnit.NANOSECONDS); - fail(); - } catch (TimeoutException e) { - assertThat(e.getMessage()).contains("1 nanoseconds"); - assertThat(e.getMessage()).contains("Because this test isn't done"); - } + TimeoutException e = + assertThrows(TimeoutException.class, () -> testFuture.get(1, TimeUnit.NANOSECONDS)); + assertThat(e.getMessage()).contains("1 nanoseconds"); + assertThat(e.getMessage()).contains("Because this test isn't done"); } public void testToString_completesDuringToString() throws Exception { @@ -1072,6 +1057,25 @@ public class AbstractFutureTest extends TestCase { t.join(); } + public void testCatchesUndeclaredThrowableFromListener() { + AbstractFuture<String> f = new AbstractFuture<String>() {}; + f.set("foo"); + f.addListener(() -> sneakyThrow(new SomeCheckedException()), directExecutor()); + } + + private static final class SomeCheckedException extends Exception {} + + /** Throws an undeclared checked exception. */ + private static void sneakyThrow(Throwable t) { + class SneakyThrower<T extends Throwable> { + @SuppressWarnings("unchecked") // intentionally unsafe for test + void throwIt(Throwable t) throws T { + throw (T) t; + } + } + new SneakyThrower<Error>().throwIt(t); + } + public void testTrustedGetFailure_Completed() { SettableFuture<String> future = SettableFuture.create(); future.set("261"); @@ -1171,12 +1175,8 @@ public class AbstractFutureTest extends TestCase { SettableFuture<String> normalFuture = SettableFuture.create(); normalFuture.setFuture(new FailFuture(exception)); assertTrue(normalFuture.isDone()); - try { - normalFuture.get(); - fail(); - } catch (ExecutionException e) { - assertSame(exception, e.getCause()); - } + ExecutionException e = assertThrows(ExecutionException.class, () -> normalFuture.get()); + assertSame(exception, e.getCause()); } private static void awaitUnchecked(final CyclicBarrier barrier) { |