1 /* 2 * Copyright (C) 2008 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.util.concurrent.Futures.getUnchecked; 18 import static com.google.common.util.concurrent.Futures.immediateFuture; 19 import static com.google.common.util.concurrent.FuturesGetCheckedInputs.CHECKED_EXCEPTION; 20 import static com.google.common.util.concurrent.FuturesGetCheckedInputs.ERROR; 21 import static com.google.common.util.concurrent.FuturesGetCheckedInputs.ERROR_FUTURE; 22 import static com.google.common.util.concurrent.FuturesGetCheckedInputs.FAILED_FUTURE_CHECKED_EXCEPTION; 23 import static com.google.common.util.concurrent.FuturesGetCheckedInputs.FAILED_FUTURE_ERROR; 24 import static com.google.common.util.concurrent.FuturesGetCheckedInputs.FAILED_FUTURE_OTHER_THROWABLE; 25 import static com.google.common.util.concurrent.FuturesGetCheckedInputs.FAILED_FUTURE_UNCHECKED_EXCEPTION; 26 import static com.google.common.util.concurrent.FuturesGetCheckedInputs.OTHER_THROWABLE; 27 import static com.google.common.util.concurrent.FuturesGetCheckedInputs.RUNTIME_EXCEPTION; 28 import static com.google.common.util.concurrent.FuturesGetCheckedInputs.RUNTIME_EXCEPTION_FUTURE; 29 import static com.google.common.util.concurrent.FuturesGetCheckedInputs.UNCHECKED_EXCEPTION; 30 31 import com.google.common.annotations.GwtCompatible; 32 import com.google.common.annotations.GwtIncompatible; 33 import com.google.common.annotations.J2ktIncompatible; 34 import java.util.concurrent.CancellationException; 35 import java.util.concurrent.Future; 36 import junit.framework.TestCase; 37 38 /** Unit tests for {@link Futures#getUnchecked(Future)}. */ 39 @GwtCompatible(emulated = true) 40 public class FuturesGetUncheckedTest extends TestCase { testGetUnchecked_success()41 public void testGetUnchecked_success() { 42 assertEquals("foo", getUnchecked(immediateFuture("foo"))); 43 } 44 45 @J2ktIncompatible 46 @GwtIncompatible // Thread.interrupt testGetUnchecked_interrupted()47 public void testGetUnchecked_interrupted() { 48 Thread.currentThread().interrupt(); 49 try { 50 assertEquals("foo", getUnchecked(immediateFuture("foo"))); 51 assertTrue(Thread.currentThread().isInterrupted()); 52 } finally { 53 Thread.interrupted(); 54 } 55 } 56 testGetUnchecked_cancelled()57 public void testGetUnchecked_cancelled() { 58 SettableFuture<String> future = SettableFuture.create(); 59 future.cancel(true); 60 try { 61 getUnchecked(future); 62 fail(); 63 } catch (CancellationException expected) { 64 } 65 } 66 testGetUnchecked_executionExceptionChecked()67 public void testGetUnchecked_executionExceptionChecked() { 68 try { 69 getUnchecked(FAILED_FUTURE_CHECKED_EXCEPTION); 70 fail(); 71 } catch (UncheckedExecutionException expected) { 72 assertEquals(CHECKED_EXCEPTION, expected.getCause()); 73 } 74 } 75 testGetUnchecked_executionExceptionUnchecked()76 public void testGetUnchecked_executionExceptionUnchecked() { 77 try { 78 getUnchecked(FAILED_FUTURE_UNCHECKED_EXCEPTION); 79 fail(); 80 } catch (UncheckedExecutionException expected) { 81 assertEquals(UNCHECKED_EXCEPTION, expected.getCause()); 82 } 83 } 84 testGetUnchecked_executionExceptionError()85 public void testGetUnchecked_executionExceptionError() { 86 try { 87 getUnchecked(FAILED_FUTURE_ERROR); 88 fail(); 89 } catch (ExecutionError expected) { 90 assertEquals(ERROR, expected.getCause()); 91 } 92 } 93 testGetUnchecked_executionExceptionOtherThrowable()94 public void testGetUnchecked_executionExceptionOtherThrowable() { 95 try { 96 getUnchecked(FAILED_FUTURE_OTHER_THROWABLE); 97 fail(); 98 } catch (UncheckedExecutionException expected) { 99 assertEquals(OTHER_THROWABLE, expected.getCause()); 100 } 101 } 102 testGetUnchecked_runtimeException()103 public void testGetUnchecked_runtimeException() { 104 try { 105 getUnchecked(RUNTIME_EXCEPTION_FUTURE); 106 fail(); 107 } catch (RuntimeException expected) { 108 assertEquals(RUNTIME_EXCEPTION, expected); 109 } 110 } 111 testGetUnchecked_error()112 public void testGetUnchecked_error() { 113 try { 114 getUnchecked(ERROR_FUTURE); 115 } catch (Error expected) { 116 assertEquals(ERROR, expected); 117 return; 118 } 119 fail(); 120 } 121 } 122