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 28 /** Unit tests for {@link Futures#getDone}. */ 29 @GwtCompatible 30 public class FuturesGetDoneTest extends TestCase { testSuccessful()31 public void testSuccessful() throws ExecutionException { 32 assertThat(getDone(immediateFuture("a"))).isEqualTo("a"); 33 } 34 testSuccessfulNull()35 public void testSuccessfulNull() throws ExecutionException { 36 assertThat(getDone(immediateFuture((String) null))).isEqualTo(null); 37 } 38 testFailed()39 public void testFailed() { 40 Exception failureCause = new Exception(); 41 try { 42 getDone(immediateFailedFuture(failureCause)); 43 fail(); 44 } catch (ExecutionException expected) { 45 assertThat(expected).hasCauseThat().isEqualTo(failureCause); 46 } 47 } 48 testCancelled()49 public void testCancelled() throws ExecutionException { 50 try { 51 getDone(immediateCancelledFuture()); 52 fail(); 53 } catch (CancellationException expected) { 54 } 55 } 56 testPending()57 public void testPending() throws ExecutionException { 58 try { 59 getDone(SettableFuture.create()); 60 fail(); 61 } catch (IllegalStateException expected) { 62 } 63 } 64 } 65