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