• 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 ForwardingLoadingCache}.
33  *
34  * @author Charles Fry
35  */
36 public class ForwardingLoadingCacheTest extends TestCase {
37   private LoadingCache<String, Boolean> forward;
38   private LoadingCache<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(LoadingCache.class);
49     forward = new ForwardingLoadingCache<String, Boolean>() {
50       @Override protected LoadingCache<String, Boolean> delegate() {
51         return mock;
52       }
53     };
54   }
55 
testGet()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 
testGetUnchecked()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 
testGetAll()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 
testApply()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 
testInvalidate()84   public void testInvalidate() {
85     mock.invalidate("key");
86     replay(mock);
87     forward.invalidate("key");
88     verify(mock);
89   }
90 
testRefresh()91   public void testRefresh() throws ExecutionException {
92     mock.refresh("key");
93     replay(mock);
94     forward.refresh("key");
95     verify(mock);
96   }
97 
testInvalidateAll()98   public void testInvalidateAll() {
99     mock.invalidateAll();
100     replay(mock);
101     forward.invalidateAll();
102     verify(mock);
103   }
104 
testSize()105   public void testSize() {
106     expect(mock.size()).andReturn(0L);
107     replay(mock);
108     forward.size();
109     verify(mock);
110   }
111 
testStats()112   public void testStats() {
113     expect(mock.stats()).andReturn(null);
114     replay(mock);
115     assertNull(forward.stats());
116     verify(mock);
117   }
118 
testAsMap()119   public void testAsMap() {
120     expect(mock.asMap()).andReturn(null);
121     replay(mock);
122     assertNull(forward.asMap());
123     verify(mock);
124   }
125 
testCleanUp()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
delegate()138     protected LoadingCache<K, V> delegate() {
139       return null;
140     }
141   }
142 }
143