Java tutorial
/** * Copyright 2009-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.javacrumbs.futureconverter.common.test.guava; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.MoreExecutors; import net.javacrumbs.futureconverter.common.test.AbstractConverterTest; import net.javacrumbs.futureconverter.common.test.ConvertedFutureTestHelper; import net.javacrumbs.futureconverter.common.test.common.CommonConvertedFutureTestHelper; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; public class GuavaConvertedFutureTestHelper extends CommonConvertedFutureTestHelper implements ConvertedFutureTestHelper<ListenableFuture<String>> { public final FutureCallback<String> callback = mock(FutureCallback.class); @Override public void waitForCalculationToFinish( com.google.common.util.concurrent.ListenableFuture<String> convertedFuture) throws InterruptedException { final CountDownLatch latch = new CountDownLatch(1); Futures.addCallback(convertedFuture, new FutureCallback<String>() { @Override public void onSuccess(String result) { latch.countDown(); } @Override public void onFailure(Throwable t) { latch.countDown(); } }, MoreExecutors.directExecutor()); latch.await(1, TimeUnit.SECONDS); } @Override public void verifyCallbackCalledWithException(Exception exception) { waitForCallback(); verify(callback).onFailure(exception); } @Override public void verifyCallbackCalledWithException(Class<? extends Exception> exceptionClass) { waitForCallback(); verify(callback).onFailure(any(exceptionClass)); } @Override public void verifyCallbackCalledWithCorrectValue() { waitForCallback(); verify(callback).onSuccess(AbstractConverterTest.VALUE); } @Override public void addCallbackTo(ListenableFuture<String> convertedFuture) { Futures.addCallback(convertedFuture, callback); convertedFuture.addListener(new Runnable() { @Override public void run() { callbackCalled(); } }, MoreExecutors.directExecutor()); } }