• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.google.common.cache;
18 
19 import static org.easymock.EasyMock.createMock;
20 import static org.easymock.EasyMock.expect;
21 import static org.easymock.EasyMock.replay;
22 import static org.easymock.EasyMock.verify;
23 
24 import com.google.common.collect.ImmutableList;
25 import com.google.common.collect.ImmutableMap;
26 
27 import junit.framework.TestCase;
28 
29 import java.util.concurrent.ExecutionException;
30 
31 /**
32  * Unit test for {@link ForwardingCache}.
33  *
34  * @author Charles Fry
35  */
36 public class ForwardingCacheTest extends TestCase {
37   private Cache<String, Boolean> forward;
38   private Cache<String, Boolean> mock;
39 
40   @SuppressWarnings("unchecked") // createMock
setUp()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 
testGetIfPresent()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 
testGetAllPresent()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 
testInvalidate()72   public void testInvalidate() {
73     mock.invalidate("key");
74     replay(mock);
75     forward.invalidate("key");
76     verify(mock);
77   }
78 
testInvalidateAllIterable()79   public void testInvalidateAllIterable() {
80     mock.invalidateAll(ImmutableList.of("key"));
81     replay(mock);
82     forward.invalidateAll(ImmutableList.of("key"));
83     verify(mock);
84   }
85 
testInvalidateAll()86   public void testInvalidateAll() {
87     mock.invalidateAll();
88     replay(mock);
89     forward.invalidateAll();
90     verify(mock);
91   }
92 
testSize()93   public void testSize() {
94     expect(mock.size()).andReturn(0L);
95     replay(mock);
96     forward.size();
97     verify(mock);
98   }
99 
testStats()100   public void testStats() {
101     expect(mock.stats()).andReturn(null);
102     replay(mock);
103     assertNull(forward.stats());
104     verify(mock);
105   }
106 
testAsMap()107   public void testAsMap() {
108     expect(mock.asMap()).andReturn(null);
109     replay(mock);
110     assertNull(forward.asMap());
111     verify(mock);
112   }
113 
testCleanUp()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
delegate()126     protected Cache<K, V> delegate() {
127       return null;
128     }
129   }
130 }
131