ForwardingCheckedFutureTest.java revision 1d580d0f6ee4f21eb309ba7b509d2c6d671c4044
1/*
2 * Copyright (C) 2011 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
17package com.google.common.util.concurrent;
18
19import static org.easymock.EasyMock.createMock;
20import static org.easymock.EasyMock.expect;
21import static org.easymock.EasyMock.replay;
22import static org.easymock.EasyMock.verify;
23
24import com.google.common.util.concurrent.ForwardingCheckedFuture.SimpleForwardingCheckedFuture;
25
26import junit.framework.TestCase;
27
28import java.io.IOException;
29import java.util.concurrent.TimeUnit;
30import java.util.concurrent.TimeoutException;
31
32/**
33 * Test for {@link ForwardingCheckedFuture}
34 *
35 * @author Anthony Zana
36 */
37public class ForwardingCheckedFutureTest extends TestCase {
38  private static final String VALUE = "delegated";
39  private static final TimeUnit TIME_UNIT = TimeUnit.MILLISECONDS;
40
41  @SuppressWarnings("unchecked")
42  private CheckedFuture<String, IOException> delegate =
43      createMock(CheckedFuture.class);
44
45  private TestDelegateFuture forwarded = new TestDelegateFuture();
46  private TestSimpleFuture simple = new TestSimpleFuture();
47
48  public void testCheckedGet() throws IOException {
49    expect(delegate.checkedGet()).andReturn(VALUE).times(2);
50    replay(delegate);
51    assertEquals(VALUE, forwarded.checkedGet());
52    assertEquals(VALUE, simple.checkedGet());
53    verify(delegate);
54  }
55
56  public void testTimedCheckedGet() throws TimeoutException, IOException {
57    expect(delegate.checkedGet(100, TIME_UNIT)).andReturn(VALUE).times(2);
58    replay(delegate);
59    assertEquals(VALUE, forwarded.checkedGet(100, TIME_UNIT));
60    assertEquals(VALUE, simple.checkedGet(100, TIME_UNIT));
61    verify(delegate);
62  }
63
64  public void testTimedCheckedGet_timeout()
65      throws IOException, TimeoutException {
66    expect(delegate.checkedGet(100, TIME_UNIT))
67        .andThrow(new TimeoutException()).times(2);
68    replay(delegate);
69    try {
70      forwarded.checkedGet(100, TIME_UNIT);
71      fail();
72    } catch (TimeoutException expected) {}
73    try {
74      simple.checkedGet(100, TIME_UNIT);
75      fail();
76    } catch (TimeoutException expected) {}
77    verify(delegate);
78  }
79
80  public void testCheckedGetException() throws IOException {
81    IOException expected = new IOException("expected");
82    expect(delegate.checkedGet()).andThrow(expected).times(2);
83    replay(delegate);
84    try {
85      delegate.checkedGet();
86      fail();
87    } catch (IOException e) {
88      assertEquals(expected.getMessage(), e.getMessage());
89    }
90    try {
91      simple.checkedGet();
92      fail();
93    } catch (IOException e) {
94      assertEquals(expected.getMessage(), e.getMessage());
95    }
96    verify(delegate);
97  }
98
99  private class TestDelegateFuture
100      extends ForwardingCheckedFuture<String, IOException> {
101    @Override
102    protected CheckedFuture<String, IOException> delegate() {
103      return delegate;
104    }
105  }
106
107  private class TestSimpleFuture
108      extends SimpleForwardingCheckedFuture<String, IOException> {
109    public TestSimpleFuture() {
110      super(delegate);
111    }
112  }
113
114}
115