1 /* 2 * Copyright (C) 2008 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.util.concurrent; 18 19 import static com.google.common.util.concurrent.Futures.transform; 20 import static com.google.common.util.concurrent.MoreExecutors.directExecutor; 21 22 import com.google.common.base.Function; 23 import java.lang.reflect.UndeclaredThrowableException; 24 25 /** 26 * Unit tests for {@link Futures#transform(ListenableFuture, Function, Executor)}. 27 * 28 * @author Nishant Thakkar 29 */ 30 public class FuturesTransformTest extends AbstractChainedListenableFutureTest<String> { 31 private static final String RESULT_DATA = "SUCCESS"; 32 private static final UndeclaredThrowableException WRAPPED_EXCEPTION = 33 new UndeclaredThrowableException(EXCEPTION); 34 35 @Override buildChainingFuture(ListenableFuture<Integer> inputFuture)36 protected ListenableFuture<String> buildChainingFuture(ListenableFuture<Integer> inputFuture) { 37 return transform(inputFuture, new ComposeFunction(), directExecutor()); 38 } 39 40 @Override getSuccessfulResult()41 protected String getSuccessfulResult() { 42 return RESULT_DATA; 43 } 44 45 private class ComposeFunction implements Function<Integer, String> { 46 @Override apply(Integer input)47 public String apply(Integer input) { 48 if (input.intValue() == VALID_INPUT_DATA) { 49 return RESULT_DATA; 50 } else { 51 throw WRAPPED_EXCEPTION; 52 } 53 } 54 } 55 testFutureGetThrowsFunctionException()56 public void testFutureGetThrowsFunctionException() throws Exception { 57 inputFuture.set(EXCEPTION_DATA); 58 listener.assertException(WRAPPED_EXCEPTION); 59 } 60 } 61