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.mockito.Mockito.mock;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.when;
22 
23 import com.google.common.collect.ImmutableList;
24 import com.google.common.collect.ImmutableMap;
25 import java.util.concurrent.ExecutionException;
26 import junit.framework.TestCase;
27 
28 /**
29  * Unit test for {@link ForwardingCache}.
30  *
31  * @author Charles Fry
32  */
33 public class ForwardingCacheTest extends TestCase {
34   private Cache<String, Boolean> forward;
35   private Cache<String, Boolean> mock;
36 
37   @SuppressWarnings({"unchecked", "DoNotMock"}) // mock
38   @Override
setUp()39   public void setUp() throws Exception {
40     super.setUp();
41     /*
42      * Class parameters must be raw, so we can't create a proxy with generic
43      * type arguments. The created proxy only records calls and returns null, so
44      * the type is irrelevant at runtime.
45      */
46     mock = mock(Cache.class);
47     forward =
48         new ForwardingCache<String, Boolean>() {
49           @Override
50           protected Cache<String, Boolean> delegate() {
51             return mock;
52           }
53         };
54   }
55 
testGetIfPresent()56   public void testGetIfPresent() throws ExecutionException {
57     when(mock.getIfPresent("key")).thenReturn(Boolean.TRUE);
58     assertSame(Boolean.TRUE, forward.getIfPresent("key"));
59   }
60 
testGetAllPresent()61   public void testGetAllPresent() throws ExecutionException {
62     when(mock.getAllPresent(ImmutableList.of("key")))
63         .thenReturn(ImmutableMap.of("key", Boolean.TRUE));
64     assertEquals(
65         ImmutableMap.of("key", Boolean.TRUE), forward.getAllPresent(ImmutableList.of("key")));
66   }
67 
testInvalidate()68   public void testInvalidate() {
69     forward.invalidate("key");
70     verify(mock).invalidate("key");
71   }
72 
testInvalidateAllIterable()73   public void testInvalidateAllIterable() {
74     forward.invalidateAll(ImmutableList.of("key"));
75     verify(mock).invalidateAll(ImmutableList.of("key"));
76   }
77 
testInvalidateAll()78   public void testInvalidateAll() {
79     forward.invalidateAll();
80     verify(mock).invalidateAll();
81   }
82 
testSize()83   public void testSize() {
84     when(mock.size()).thenReturn(0L);
85     assertEquals(0, forward.size());
86   }
87 
testStats()88   public void testStats() {
89     when(mock.stats()).thenReturn(null);
90     assertNull(forward.stats());
91   }
92 
testAsMap()93   public void testAsMap() {
94     when(mock.asMap()).thenReturn(null);
95     assertNull(forward.asMap());
96   }
97 
testCleanUp()98   public void testCleanUp() {
99     forward.cleanUp();
100     verify(mock).cleanUp();
101   }
102 
103   /** Make sure that all methods are forwarded. */
104   private static class OnlyGet<K, V> extends ForwardingCache<K, V> {
105     @Override
delegate()106     protected Cache<K, V> delegate() {
107       return null;
108     }
109   }
110 }
111