1 /* 2 * Copyright (C) 2016 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.common.util.concurrent; 16 17 import static com.google.common.truth.Truth.assertThat; 18 import static com.google.common.util.concurrent.Futures.getDone; 19 import static com.google.common.util.concurrent.Futures.immediateCancelledFuture; 20 import static com.google.common.util.concurrent.Futures.immediateFailedFuture; 21 import static com.google.common.util.concurrent.Futures.immediateFuture; 22 23 import com.google.common.annotations.GwtCompatible; 24 import java.util.concurrent.CancellationException; 25 import java.util.concurrent.ExecutionException; 26 import junit.framework.TestCase; 27 import org.checkerframework.checker.nullness.qual.Nullable; 28 29 /** Unit tests for {@link Futures#getDone}. */ 30 @GwtCompatible 31 public class FuturesGetDoneTest extends TestCase { testSuccessful()32 public void testSuccessful() throws ExecutionException { 33 assertThat(getDone(immediateFuture("a"))).isEqualTo("a"); 34 } 35 testSuccessfulNull()36 public void testSuccessfulNull() throws ExecutionException { 37 assertThat(getDone(Futures.<@Nullable String>immediateFuture(null))).isEqualTo(null); 38 } 39 testFailed()40 public void testFailed() { 41 Exception failureCause = new Exception(); 42 try { 43 getDone(immediateFailedFuture(failureCause)); 44 fail(); 45 } catch (ExecutionException expected) { 46 assertThat(expected).hasCauseThat().isEqualTo(failureCause); 47 } 48 } 49 testCancelled()50 public void testCancelled() throws ExecutionException { 51 try { 52 getDone(immediateCancelledFuture()); 53 fail(); 54 } catch (CancellationException expected) { 55 } 56 } 57 testPending()58 public void testPending() throws ExecutionException { 59 try { 60 getDone(SettableFuture.create()); 61 fail(); 62 } catch (IllegalStateException expected) { 63 } 64 } 65 } 66