1 /*
2  * Copyright (C) 2011 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.google.common.cache;
16 
17 import static java.util.Arrays.asList;
18 
19 import com.google.common.collect.ImmutableList;
20 import com.google.common.collect.ImmutableMap;
21 import junit.framework.TestCase;
22 
23 /** @author Charles Fry */
24 public class CacheManualTest extends TestCase {
25 
testGetIfPresent()26   public void testGetIfPresent() {
27     Cache<Object, Object> cache = CacheBuilder.newBuilder().recordStats().build();
28     CacheStats stats = cache.stats();
29     assertEquals(0, stats.missCount());
30     assertEquals(0, stats.loadSuccessCount());
31     assertEquals(0, stats.loadExceptionCount());
32     assertEquals(0, stats.hitCount());
33 
34     Object one = new Object();
35     Object two = new Object();
36 
37     assertNull(cache.getIfPresent(one));
38     stats = cache.stats();
39     assertEquals(1, stats.missCount());
40     assertEquals(0, stats.loadSuccessCount());
41     assertEquals(0, stats.loadExceptionCount());
42     assertEquals(0, stats.hitCount());
43     assertNull(cache.asMap().get(one));
44     assertFalse(cache.asMap().containsKey(one));
45     assertFalse(cache.asMap().containsValue(two));
46 
47     assertNull(cache.getIfPresent(two));
48     stats = cache.stats();
49     assertEquals(2, stats.missCount());
50     assertEquals(0, stats.loadSuccessCount());
51     assertEquals(0, stats.loadExceptionCount());
52     assertEquals(0, stats.hitCount());
53     assertNull(cache.asMap().get(two));
54     assertFalse(cache.asMap().containsKey(two));
55     assertFalse(cache.asMap().containsValue(one));
56 
57     cache.put(one, two);
58 
59     assertSame(two, cache.getIfPresent(one));
60     stats = cache.stats();
61     assertEquals(2, stats.missCount());
62     assertEquals(0, stats.loadSuccessCount());
63     assertEquals(0, stats.loadExceptionCount());
64     assertEquals(1, stats.hitCount());
65     assertSame(two, cache.asMap().get(one));
66     assertTrue(cache.asMap().containsKey(one));
67     assertTrue(cache.asMap().containsValue(two));
68 
69     assertNull(cache.getIfPresent(two));
70     stats = cache.stats();
71     assertEquals(3, stats.missCount());
72     assertEquals(0, stats.loadSuccessCount());
73     assertEquals(0, stats.loadExceptionCount());
74     assertEquals(1, stats.hitCount());
75     assertNull(cache.asMap().get(two));
76     assertFalse(cache.asMap().containsKey(two));
77     assertFalse(cache.asMap().containsValue(one));
78 
79     cache.put(two, one);
80 
81     assertSame(two, cache.getIfPresent(one));
82     stats = cache.stats();
83     assertEquals(3, stats.missCount());
84     assertEquals(0, stats.loadSuccessCount());
85     assertEquals(0, stats.loadExceptionCount());
86     assertEquals(2, stats.hitCount());
87     assertSame(two, cache.asMap().get(one));
88     assertTrue(cache.asMap().containsKey(one));
89     assertTrue(cache.asMap().containsValue(two));
90 
91     assertSame(one, cache.getIfPresent(two));
92     stats = cache.stats();
93     assertEquals(3, stats.missCount());
94     assertEquals(0, stats.loadSuccessCount());
95     assertEquals(0, stats.loadExceptionCount());
96     assertEquals(3, stats.hitCount());
97     assertSame(one, cache.asMap().get(two));
98     assertTrue(cache.asMap().containsKey(two));
99     assertTrue(cache.asMap().containsValue(one));
100   }
101 
testGetAllPresent()102   public void testGetAllPresent() {
103     Cache<Integer, Integer> cache = CacheBuilder.newBuilder().recordStats().build();
104     CacheStats stats = cache.stats();
105     assertEquals(0, stats.missCount());
106     assertEquals(0, stats.loadSuccessCount());
107     assertEquals(0, stats.loadExceptionCount());
108     assertEquals(0, stats.hitCount());
109 
110     assertEquals(ImmutableMap.of(), cache.getAllPresent(ImmutableList.<Integer>of()));
111     stats = cache.stats();
112     assertEquals(0, stats.missCount());
113     assertEquals(0, stats.loadSuccessCount());
114     assertEquals(0, stats.loadExceptionCount());
115     assertEquals(0, stats.hitCount());
116 
117     assertEquals(ImmutableMap.of(), cache.getAllPresent(asList(1, 2, 3)));
118     stats = cache.stats();
119     assertEquals(3, stats.missCount());
120     assertEquals(0, stats.loadSuccessCount());
121     assertEquals(0, stats.loadExceptionCount());
122     assertEquals(0, stats.hitCount());
123 
124     cache.put(2, 22);
125 
126     assertEquals(ImmutableMap.of(2, 22), cache.getAllPresent(asList(1, 2, 3)));
127     stats = cache.stats();
128     assertEquals(5, stats.missCount());
129     assertEquals(0, stats.loadSuccessCount());
130     assertEquals(0, stats.loadExceptionCount());
131     assertEquals(1, stats.hitCount());
132 
133     cache.put(3, 33);
134 
135     assertEquals(ImmutableMap.of(2, 22, 3, 33), cache.getAllPresent(asList(1, 2, 3)));
136     stats = cache.stats();
137     assertEquals(6, stats.missCount());
138     assertEquals(0, stats.loadSuccessCount());
139     assertEquals(0, stats.loadExceptionCount());
140     assertEquals(3, stats.hitCount());
141 
142     cache.put(1, 11);
143 
144     assertEquals(ImmutableMap.of(1, 11, 2, 22, 3, 33), cache.getAllPresent(asList(1, 2, 3)));
145     stats = cache.stats();
146     assertEquals(6, stats.missCount());
147     assertEquals(0, stats.loadSuccessCount());
148     assertEquals(0, stats.loadExceptionCount());
149     assertEquals(6, stats.hitCount());
150   }
151 }
152