ForwardingCacheTest.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 ForwardingCache}.
33 *
34 * @author Charles Fry
35 */
36public class ForwardingCacheTest extends TestCase {
37  private Cache<String, Boolean> forward;
38  private Cache<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(Cache.class);
49    forward = new ForwardingCache<String, Boolean>() {
50      @Override protected Cache<String, Boolean> delegate() {
51        return mock;
52      }
53    };
54  }
55
56  public void testGetIfPresent() throws ExecutionException {
57    expect(mock.getIfPresent("key")).andReturn(Boolean.TRUE);
58    replay(mock);
59    assertSame(Boolean.TRUE, forward.getIfPresent("key"));
60    verify(mock);
61  }
62
63  public void testGetAllPresent() throws ExecutionException {
64    expect(mock.getAllPresent(ImmutableList.of("key")))
65        .andReturn(ImmutableMap.of("key", Boolean.TRUE));
66    replay(mock);
67    assertEquals(ImmutableMap.of("key", Boolean.TRUE),
68        forward.getAllPresent(ImmutableList.of("key")));
69    verify(mock);
70  }
71
72  public void testInvalidate() {
73    mock.invalidate("key");
74    replay(mock);
75    forward.invalidate("key");
76    verify(mock);
77  }
78
79  public void testInvalidateAllIterable() {
80    mock.invalidateAll(ImmutableList.of("key"));
81    replay(mock);
82    forward.invalidateAll(ImmutableList.of("key"));
83    verify(mock);
84  }
85
86  public void testInvalidateAll() {
87    mock.invalidateAll();
88    replay(mock);
89    forward.invalidateAll();
90    verify(mock);
91  }
92
93  public void testSize() {
94    expect(mock.size()).andReturn(0L);
95    replay(mock);
96    forward.size();
97    verify(mock);
98  }
99
100  public void testStats() {
101    expect(mock.stats()).andReturn(null);
102    replay(mock);
103    assertNull(forward.stats());
104    verify(mock);
105  }
106
107  public void testAsMap() {
108    expect(mock.asMap()).andReturn(null);
109    replay(mock);
110    assertNull(forward.asMap());
111    verify(mock);
112  }
113
114  public void testCleanUp() {
115    mock.cleanUp();
116    replay(mock);
117    forward.cleanUp();
118    verify(mock);
119  }
120
121  /**
122   * Make sure that all methods are forwarded.
123   */
124  private static class OnlyGet<K, V> extends ForwardingCache<K, V> {
125    @Override
126    protected Cache<K, V> delegate() {
127      return null;
128    }
129  }
130}
131