1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.util.concurrent;
18
19 import com.google.common.util.concurrent.testing.MockFutureListener;
20
21 import junit.framework.TestCase;
22
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.TimeoutException;
25
26
27
28
29
30
31
32
33 public abstract class AbstractChainedListenableFutureTest<T> extends TestCase {
34 protected static final int EXCEPTION_DATA = -1;
35 protected static final int VALID_INPUT_DATA = 1;
36 protected static final Exception EXCEPTION = new Exception("Test exception");
37
38 protected SettableFuture<Integer> inputFuture;
39 protected ListenableFuture<T> resultFuture;
40 protected MockFutureListener listener;
41
42 @Override
43 protected void setUp() throws Exception {
44 super.setUp();
45
46 inputFuture = SettableFuture.create();
47 resultFuture = buildChainingFuture(inputFuture);
48 listener = new MockFutureListener(resultFuture);
49
50 }
51
52 public void testFutureGetBeforeCallback() throws Exception {
53
54 try {
55 resultFuture.get(1L, TimeUnit.MILLISECONDS);
56 fail("The data is not yet ready, so a TimeoutException is expected");
57 } catch (TimeoutException expected) {}
58 }
59
60 public void testFutureGetThrowsWrappedException() throws Exception {
61 inputFuture.setException(EXCEPTION);
62 listener.assertException(EXCEPTION);
63 }
64
65 public void testFutureGetThrowsWrappedError() throws Exception {
66 Error error = new Error();
67 inputFuture.setException(error);
68
69
70 listener.assertException(error);
71 }
72
73 public void testAddListenerAfterCallback() throws Throwable {
74 inputFuture.set(VALID_INPUT_DATA);
75
76 listener.assertSuccess(getSuccessfulResult());
77 }
78
79 public void testFutureBeforeCallback() throws Throwable {
80 inputFuture.set(VALID_INPUT_DATA);
81
82 listener.assertSuccess(getSuccessfulResult());
83 }
84
85
86
87
88
89
90 protected abstract ListenableFuture<T> buildChainingFuture(
91 ListenableFuture<Integer> inputFuture);
92
93
94
95
96
97 protected abstract T getSuccessfulResult();
98 }