001/*
002 * Copyright (C) 2008 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License.  You may obtain a copy
006 * of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016
017package com.google.common.util.concurrent.testing;
018
019import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
020import static java.util.concurrent.TimeUnit.SECONDS;
021
022import com.google.common.annotations.GwtIncompatible;
023import com.google.common.util.concurrent.ListenableFuture;
024import java.util.concurrent.CountDownLatch;
025import java.util.concurrent.ExecutionException;
026import junit.framework.Assert;
027
028/**
029 * A simple mock implementation of {@code Runnable} that can be used for testing ListenableFutures.
030 *
031 * @author Nishant Thakkar
032 * @since 10.0
033 */
034@GwtIncompatible
035public class MockFutureListener implements Runnable {
036  private final CountDownLatch countDownLatch;
037  private final ListenableFuture<?> future;
038
039  public MockFutureListener(ListenableFuture<?> future) {
040    this.countDownLatch = new CountDownLatch(1);
041    this.future = future;
042
043    future.addListener(this, directExecutor());
044  }
045
046  @Override
047  public void run() {
048    countDownLatch.countDown();
049  }
050
051  /**
052   * Verify that the listener completes in a reasonable amount of time, and Asserts that the future
053   * returns the expected data.
054   *
055   * @throws Throwable if the listener isn't called or if it resulted in a throwable or if the
056   *     result doesn't match the expected value.
057   */
058  public void assertSuccess(Object expectedData) throws Throwable {
059    // Verify that the listener executed in a reasonable amount of time.
060    Assert.assertTrue(countDownLatch.await(1L, SECONDS));
061
062    try {
063      Assert.assertEquals(expectedData, future.get());
064    } catch (ExecutionException e) {
065      throw e.getCause();
066    }
067  }
068
069  /**
070   * Verify that the listener completes in a reasonable amount of time, and Asserts that the future
071   * throws an {@code ExecutableException} and that the cause of the {@code ExecutableException} is
072   * {@code expectedCause}.
073   */
074  public void assertException(Throwable expectedCause) throws Exception {
075    // Verify that the listener executed in a reasonable amount of time.
076    Assert.assertTrue(countDownLatch.await(1L, SECONDS));
077
078    try {
079      future.get();
080      Assert.fail("This call was supposed to throw an ExecutionException");
081    } catch (ExecutionException expected) {
082      Assert.assertSame(expectedCause, expected.getCause());
083    }
084  }
085
086  public void assertTimeout() throws Exception {
087    // Verify that the listener does not get called in a reasonable amount of
088    // time.
089    Assert.assertFalse(countDownLatch.await(1L, SECONDS));
090  }
091}