AsyncMethodCallbacks { public static <T> void transfer(CompletionStage<T> src, AsyncMethodCallback<? super T> dest) { requireNonNull(src, "src"); requireNonNull(dest, "dest"); src.handle((res, cause) -> { try { if (cause != null) { invokeOnError(dest, cause); } else { dest.onComplete(res); } } catch (Exception e) { CompletionActions.log(e); } return null; }); } private  AsyncMethodCallbacks(); static void transfer(CompletionStage<T> src, AsyncMethodCallback<? super T> dest); static void invokeOnError(AsyncMethodCallback<?> callback, Throwable cause);  }
@Test public void transferFailure_Exception() { @SuppressWarnings("unchecked") final AsyncMethodCallback<String> callback = mock(AsyncMethodCallback.class); AsyncMethodCallbacks.transfer(exceptionallyCompletedFuture(new AnticipatedException()), callback); verify(callback, only()).onError(isA(AnticipatedException.class)); }
@Test public void transferFailure_Throwable() { @SuppressWarnings("unchecked") final AsyncMethodCallback<String> callback = mock(AsyncMethodCallback.class); AsyncMethodCallbacks.transfer(exceptionallyCompletedFuture(new Throwable("foo")), callback); verify(callback, only()).onError(argThat(argument -> { return argument instanceof CompletionException && "foo".equals(argument.getCause().getMessage()); })); }
@Test public void transferCallbackError() { @SuppressWarnings("unchecked") final AsyncMethodCallback<String> callback = mock(AsyncMethodCallback.class); doThrow(new AnticipatedException()).when(callback).onComplete(any()); AsyncMethodCallbacks.transfer(completedFuture("foo"), callback); verify(callback, only()).onComplete("foo"); }
@Test public void transferSuccess() { @SuppressWarnings("unchecked") final AsyncMethodCallback<String> callback = mock(AsyncMethodCallback.class); AsyncMethodCallbacks.transfer(completedFuture("foo"), callback); verify(callback, only()).onComplete("foo"); }