alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (FutureCallbackTest.java)

This example Java source code file (FutureCallbackTest.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

annotation, countingsamethreadexecutor, futurecallback, gwtincompatible, illegalargumentexception, listenablefuture, mockcallback, mockito, nullable, override, runtimeexception, safe, settablefuture, string, testerror, threading, threads

The FutureCallbackTest.java Java example source code

/*
 * Copyright (C) 2011 The Guava 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 com.google.common.util.concurrent;

import static com.google.common.truth.Truth.assertThat;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;

import junit.framework.TestCase;

import org.mockito.Mockito;

import java.util.concurrent.CancellationException;
import java.util.concurrent.Executor;

import javax.annotation.Nullable;

/**
 * Test for {@link FutureCallback}.
 *
 * @author Anthony Zana
 */
@GwtCompatible(emulated = true)
public class FutureCallbackTest extends TestCase {
  public void testSameThreadSuccess() {
    SettableFuture<String> f = SettableFuture.create();
    MockCallback callback = new MockCallback("foo");
    Futures.addCallback(f, callback);
    f.set("foo");
  }

  public void testExecutorSuccess() {
    CountingSameThreadExecutor ex = new CountingSameThreadExecutor();
    SettableFuture<String> f = SettableFuture.create();
    MockCallback callback = new MockCallback("foo");
    Futures.addCallback(f, callback, ex);
    f.set("foo");
    assertEquals(1, ex.runCount);
  }

  // Error cases
  public void testSameThreadExecutionException() {
    SettableFuture<String> f = SettableFuture.create();
    Exception e = new IllegalArgumentException("foo not found");
    MockCallback callback = new MockCallback(e);
    Futures.addCallback(f, callback);
    f.setException(e);
  }

  public void testCancel() {
    SettableFuture<String> f = SettableFuture.create();
    FutureCallback<String> callback =
        new FutureCallback<String>() {
          private boolean called = false;

          @Override
          public void onSuccess(String result) {
            fail("Was not expecting onSuccess() to be called.");
          }

          @Override
          public synchronized void onFailure(Throwable t) {
            assertFalse(called);
            assertThat(t).isInstanceOf(CancellationException.class);
            called = true;
          }
        };
    Futures.addCallback(f, callback);
    f.cancel(true);
  }

  public void testThrowErrorFromGet() {
    Error error = new AssertionError("ASSERT!");
    ListenableFuture<String> f = UncheckedThrowingFuture.throwingError(error);
    MockCallback callback = new MockCallback(error);
    Futures.addCallback(f, callback);
  }

  public void testRuntimeExeceptionFromGet() {
    RuntimeException e = new IllegalArgumentException("foo not found");
    ListenableFuture<String> f = UncheckedThrowingFuture.throwingRuntimeException(e);
    MockCallback callback = new MockCallback(e);
    Futures.addCallback(f, callback);
  }

  @GwtIncompatible // Mockito
  public void testOnSuccessThrowsRuntimeException() throws Exception {
    RuntimeException exception = new RuntimeException();
    String result = "result";
    SettableFuture<String> future = SettableFuture.create();
    @SuppressWarnings("unchecked") // Safe for a mock
    FutureCallback<String> callback = Mockito.mock(FutureCallback.class);
    Futures.addCallback(future, callback);
    Mockito.doThrow(exception).when(callback).onSuccess(result);
    future.set(result);
    assertEquals(result, future.get());
    Mockito.verify(callback).onSuccess(result);
    Mockito.verifyNoMoreInteractions(callback);
  }

  @GwtIncompatible // Mockito
  public void testOnSuccessThrowsError() throws Exception {
    class TestError extends Error {}
    TestError error = new TestError();
    String result = "result";
    SettableFuture<String> future = SettableFuture.create();
    @SuppressWarnings("unchecked") // Safe for a mock
    FutureCallback<String> callback = Mockito.mock(FutureCallback.class);
    Futures.addCallback(future, callback);
    Mockito.doThrow(error).when(callback).onSuccess(result);
    try {
      future.set(result);
      fail("Should have thrown");
    } catch (TestError e) {
      assertSame(error, e);
    }
    assertEquals(result, future.get());
    Mockito.verify(callback).onSuccess(result);
    Mockito.verifyNoMoreInteractions(callback);
  }

  public void testWildcardFuture() {
    SettableFuture<String> settable = SettableFuture.create();
    ListenableFuture<?> f = settable;
    FutureCallback<Object> callback = new FutureCallback() {
      @Override
      public void onSuccess(Object result) {}

      @Override
      public void onFailure(Throwable t) {}
    };
    Futures.addCallback(f, callback);
  }

  private class CountingSameThreadExecutor implements Executor {
    int runCount = 0;
    @Override
    public void execute(Runnable command) {
      command.run();
      runCount++;
    }
  }

  private final class MockCallback implements FutureCallback<String> {
    @Nullable private String value = null;
    @Nullable private Throwable failure = null;
    private boolean wasCalled = false;

    MockCallback(String expectedValue) {
      this.value = expectedValue;
    }

    public MockCallback(Throwable expectedFailure) {
      this.failure = expectedFailure;
    }

    @Override
    public synchronized void onSuccess(String result) {
      assertFalse(wasCalled);
      wasCalled = true;
      assertEquals(value, result);
    }

    @Override
    public synchronized void onFailure(Throwable t) {
      assertFalse(wasCalled);
      wasCalled = true;
      assertEquals(failure, t);
    }
  }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java FutureCallbackTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.