ForwardingLoadingCacheTest.java revision 1d580d0f6ee4f21eb309ba7b509d2c6d671c4044
1/*
2 * Copyright (C) 2009 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.cache;
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.collect.ImmutableList;
25import com.google.common.collect.ImmutableMap;
26
27import junit.framework.TestCase;
28
29import java.util.concurrent.ExecutionException;
30
31/**
32 * Unit test for {@link ForwardingLoadingCache}.
33 *
34 * @author Charles Fry
35 */
36public class ForwardingLoadingCacheTest extends TestCase {
37  private LoadingCache<String, Boolean> forward;
38  private LoadingCache<String, Boolean> mock;
39
40  @SuppressWarnings("unchecked") // createMock
41  @Override public void setUp() throws Exception {
42    super.setUp();
43    /*
44     * Class parameters must be raw, so we can't create a proxy with generic
45     * type arguments. The created proxy only records calls and returns null, so
46     * the type is irrelevant at runtime.
47     */
48    mock = createMock(LoadingCache.class);
49    forward = new ForwardingLoadingCache<String, Boolean>() {
50      @Override protected LoadingCache<String, Boolean> delegate() {
51        return mock;
52      }
53    };
54  }
55
56  public void testGet() throws ExecutionException {
57    expect(mock.get("key")).andReturn(Boolean.TRUE);
58    replay(mock);
59    assertSame(Boolean.TRUE, forward.get("key"));
60    verify(mock);
61  }
62
63  public void testGetUnchecked() {
64    expect(mock.getUnchecked("key")).andReturn(Boolean.TRUE);
65    replay(mock);
66    assertSame(Boolean.TRUE, forward.getUnchecked("key"));
67    verify(mock);
68  }
69
70  public void testGetAll() throws ExecutionException {
71    expect(mock.getAll(ImmutableList.of("key"))).andReturn(ImmutableMap.of("key", Boolean.TRUE));
72    replay(mock);
73    assertEquals(ImmutableMap.of("key", Boolean.TRUE), forward.getAll(ImmutableList.of("key")));
74    verify(mock);
75  }
76
77  public void testApply() {
78    expect(mock.apply("key")).andReturn(Boolean.TRUE);
79    replay(mock);
80    assertSame(Boolean.TRUE, forward.apply("key"));
81    verify(mock);
82  }
83
84  public void testInvalidate() {
85    mock.invalidate("key");
86    replay(mock);
87    forward.invalidate("key");
88    verify(mock);
89  }
90
91  public void testRefresh() throws ExecutionException {
92    mock.refresh("key");
93    replay(mock);
94    forward.refresh("key");
95    verify(mock);
96  }
97
98  public void testInvalidateAll() {
99    mock.invalidateAll();
100    replay(mock);
101    forward.invalidateAll();
102    verify(mock);
103  }
104
105  public void testSize() {
106    expect(mock.size()).andReturn(0L);
107    replay(mock);
108    forward.size();
109    verify(mock);
110  }
111
112  public void testStats() {
113    expect(mock.stats()).andReturn(null);
114    replay(mock);
115    assertNull(forward.stats());
116    verify(mock);
117  }
118
119  public void testAsMap() {
120    expect(mock.asMap()).andReturn(null);
121    replay(mock);
122    assertNull(forward.asMap());
123    verify(mock);
124  }
125
126  public void testCleanUp() {
127    mock.cleanUp();
128    replay(mock);
129    forward.cleanUp();
130    verify(mock);
131  }
132
133  /**
134   * Make sure that all methods are forwarded.
135   */
136  private static class OnlyGet<K, V> extends ForwardingLoadingCache<K, V> {
137    @Override
138    protected LoadingCache<K, V> delegate() {
139      return null;
140    }
141  }
142}
143