1 package com.atlassian.cache;
2
3 import java.util.concurrent.TimeUnit;
4
5 import org.hamcrest.Matchers;
6 import org.junit.Test;
7
8 import static org.hamcrest.Matchers.not;
9
10 import static org.hamcrest.MatcherAssert.assertThat;
11 import static org.hamcrest.Matchers.equalTo;
12 import static org.hamcrest.Matchers.hasSize;
13 import static org.hamcrest.Matchers.nullValue;
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertFalse;
16 import static org.junit.Assert.assertNull;
17 import static org.junit.Assert.assertTrue;
18
19
20
21
22
23
24 public abstract class AbstractCacheEagerTest
25 {
26
27 protected CacheFactory factory;
28
29 @Test
30 public void testGetName() throws Exception
31 {
32 Cache<String, Long> cache = makeSimpleCache();
33
34 assertThat(((ManagedCache) cache).getName(), equalTo("mycache"));
35 }
36
37
38 @Test
39 public void testFactoryGeneratedName() throws Exception
40 {
41 Cache<String,Long> cache = factory.getCache(Object.class, "mycache");
42
43 assertThat(((ManagedCache) cache).getName(), equalTo("java.lang.Object.mycache"));
44 }
45
46 @Test
47 public void testGetKeys() throws Exception
48 {
49 Cache<String, Long> cache = makeSimpleCache();
50
51 cache.put("1", 1L);
52 cache.put("2", 2L);
53 cache.put("3", 3L);
54 cache.put("4", 4L);
55 assertThat(cache.get("1"), equalTo(1L));
56 assertThat(cache.get("2"), equalTo(2L));
57 assertThat(cache.get("3"), equalTo(3L));
58 assertThat(cache.get("4"), equalTo(4L));
59 assertSize(cache, 4);
60 }
61
62 @Test
63 public void testPut() throws Exception
64 {
65 Cache<String, Long> cache = makeSimpleCache();
66
67 cache.put("1", 11L);
68 cache.put("2", 12L);
69 cache.put("3", 13L);
70 cache.put("4", 14L);
71 assertThat(cache.get("1"), equalTo(11L));
72 assertThat(cache.get("2"), equalTo(12L));
73 assertThat(cache.get("3"), equalTo(13L));
74 assertThat(cache.get("4"), equalTo(14L));
75 assertSize(cache, 4);
76 }
77
78 @Test
79 public void testGet() throws Exception
80 {
81 Cache<String, Long> cache = makeSimpleCache();
82
83 cache.put("1", 11L);
84 cache.put("2", 12L);
85 cache.put("3", 13L);
86 cache.put("4", 14L);
87 assertThat(cache.get("1"), equalTo(11L));
88 assertThat(cache.get("2"), equalTo(12L));
89 assertThat(cache.get("3"), equalTo(13L));
90 assertThat(cache.get("4"), equalTo(14L));
91 assertThat(cache.get("5"), nullValue());
92 assertThat(cache.get("6"), nullValue());
93 assertThat(cache.get("7"), nullValue());
94 assertSize(cache, 4);
95 }
96
97 @Test
98 public void testRemove() throws Exception
99 {
100 Cache<String, Long> cache = makeSimpleCache();
101
102 cache.put("1", 11L);
103 cache.put("2", 12L);
104 cache.put("3", 13L);
105 cache.put("4", 14L);
106 assertThat(cache.get("1"), equalTo(11L));
107 assertThat(cache.get("2"), equalTo(12L));
108 assertThat(cache.get("3"), equalTo(13L));
109 assertThat(cache.get("4"), equalTo(14L));
110 assertSize(cache, 4);
111
112 cache.remove("1");
113 cache.remove("2");
114 cache.remove("3");
115 assertSize(cache, 1);
116
117
118 assertThat(cache.get("1"), nullValue());
119 assertThat(cache.get("2"), nullValue());
120 assertThat(cache.get("3"), nullValue());
121 assertThat(cache.get("4"), equalTo(14L));
122 assertSize(cache, 1);
123 }
124
125 @Test
126 public void testRemoveTwice() throws Exception
127 {
128 Cache<String, Long> cache = makeSimpleCache();
129
130 cache.put("1", 11L);
131 cache.put("2", 12L);
132 cache.put("3", 13L);
133 cache.put("4", 14L);
134 assertThat(cache.get("1"), equalTo(11L));
135 assertThat(cache.get("2"), equalTo(12L));
136 assertThat(cache.get("3"), equalTo(13L));
137 assertThat(cache.get("4"), equalTo(14L));
138 assertSize(cache, 4);
139
140 cache.remove("1");
141
142 cache.remove("1");
143 assertSize(cache, 3);
144
145
146 assertThat(cache.get("1"), nullValue());
147 assertThat(cache.get("2"), equalTo(12L));
148 assertThat(cache.get("3"), equalTo(13L));
149 assertThat(cache.get("4"), equalTo(14L));
150 assertSize(cache, 3);
151 }
152
153 @Test
154 public void testRemoveAll() throws Exception
155 {
156 Cache<String, Long> cache = makeSimpleCache();
157
158 cache.put("1", 11L);
159 cache.put("2", 12L);
160 cache.put("3", 13L);
161 cache.put("4", 14L);
162 assertThat(cache.get("1"), equalTo(11L));
163 assertThat(cache.get("2"), equalTo(12L));
164 assertThat(cache.get("3"), equalTo(13L));
165 assertThat(cache.get("4"), equalTo(14L));
166 assertSize(cache, 4);
167
168 cache.removeAll();
169 assertEmpty(cache);
170
171
172 assertThat(cache.get("1"), nullValue());
173 assertThat(cache.get("2"), nullValue());
174 assertThat(cache.get("3"), nullValue());
175 assertThat(cache.get("4"), nullValue());
176 assertEmpty(cache);
177 }
178
179 @Test
180 public void testClear() throws Exception
181 {
182 Cache<String, Long> cache = makeSimpleCache();
183
184 cache.put("1", 11L);
185 cache.put("2", 12L);
186 cache.put("3", 13L);
187 cache.put("4", 14L);
188 assertThat(cache.get("1"), equalTo(11L));
189 assertThat(cache.get("2"), equalTo(12L));
190 assertThat(cache.get("3"), equalTo(13L));
191 assertThat(cache.get("4"), equalTo(14L));
192 assertSize(cache, 4);
193
194
195 ((ManagedCache) cache).clear();
196 assertEmpty(cache);
197
198
199 assertThat(cache.get("1"), nullValue());
200 assertThat(cache.get("2"), nullValue());
201 assertThat(cache.get("3"), nullValue());
202 assertThat(cache.get("4"), nullValue());
203 assertEmpty(cache);
204 }
205
206 @Test
207 public void conditionalPutPutsIfAbsent()
208 {
209 Cache<String, Long> cache = makeSimpleCache();
210 assertNull(cache.putIfAbsent("A", 1L));
211 assertEquals(Long.valueOf(1), cache.get("A"));
212 }
213
214 @Test
215 public void conditionalPutDoesNotPutIfPresent()
216 {
217 Cache<String, Long> cache = makeSimpleCache();
218 cache.putIfAbsent("A", 1L);
219 assertEquals(Long.valueOf(1), cache.putIfAbsent("A", 2L));
220 assertEquals(Long.valueOf(1), cache.get("A"));
221 }
222
223 @Test
224 public void replaceDoesNotReplaceIfAbsent()
225 {
226 Cache<String, Long> cache = makeSimpleCache();
227 assertFalse(cache.replace("A", 1L, 2L));
228 assertNull(cache.get("A"));
229 }
230
231 @Test
232 public void replaceReplacesWhenValueMatches()
233 {
234 Cache<String, Long> cache = makeSimpleCache();
235 cache.put("A", 1L);
236 assertTrue(cache.replace("A", 1L, 2L));
237 assertEquals(Long.valueOf(2), cache.get("A"));
238 }
239
240 @Test
241 public void replaceDoesNothingWhenValueDoesNotMatch()
242 {
243 Cache<String, Long> cache = makeSimpleCache();
244 cache.put("A", 1L);
245 assertFalse(cache.replace("A", 2L, 3L));
246 assertEquals(Long.valueOf(1), cache.get("A"));
247 }
248
249 @Test
250 public void removeDoesNothingWhenNoKeyIsPresent()
251 {
252 Cache<String, Long> cache = makeSimpleCache();
253 assertFalse(cache.remove("A", 1L));
254 }
255
256 @Test
257 public void removeRemovesWhenValueMatches()
258 {
259 Cache<String, Long> cache = makeSimpleCache();
260 cache.put("A", 1L);
261 assertTrue(cache.remove("A", 1L));
262 assertNull(cache.get("A"));
263 }
264
265 @Test
266 public void removeDoesNotRemoveWhenValueDoesNotMatch()
267 {
268 Cache<String, Long> cache = makeSimpleCache();
269 cache.put("A", 2L);
270 assertFalse(cache.remove("A", 1L));
271 assertEquals(Long.valueOf(2), cache.get("A"));
272 }
273
274 @Test
275 public void testMaxEntries() throws Exception
276 {
277 Cache<String, Long> cache = makeSizeLimitedCache(3);
278
279
280 cache.put("1", 11L);
281 cache.put("2", 12L);
282 cache.put("3", 13L);
283 assertThat(cache.get("1"), equalTo(11L));
284 assertThat(cache.get("2"), equalTo(12L));
285 assertThat(cache.get("3"), equalTo(13L));
286 assertSize(cache, 3);
287 cache.put("4", 14L);
288 assertSize(cache, 3);
289 }
290
291 @Test
292 public void testBothExpireHintsSpecified()
293 {
294 CacheSettings required = new CacheSettingsBuilder().local().expireAfterAccess(60, TimeUnit.SECONDS).
295 expireAfterWrite(30, TimeUnit.SECONDS).build();
296 Cache<String, String> c = factory.getCache("fruity", null, required);
297 assertThat(c, not(nullValue()));
298 }
299
300 private Cache<String, Long> makeSimpleCache()
301 {
302
303 final Cache<String, Long> cache = factory.getCache("mycache");
304 assertEmpty(cache);
305 return cache;
306 }
307
308 private Cache<String, Long> makeSizeLimitedCache(int maxEntries)
309 {
310 CacheSettings required = new CacheSettingsBuilder().maxEntries(3).build();
311 final Cache<String, Long> cache = factory.getCache("mycache", null, required);
312 assertEmpty(cache);
313 return cache;
314 }
315
316
317
318
319
320 private static <K,V> void assertEmpty(Cache<K,V> cache)
321 {
322 assertThat(cache.getKeys(), Matchers.<K>empty());
323 }
324
325 private static <K,V> void assertSize(Cache<K,V> cache, final int expectedSize)
326 {
327 assertThat(cache.getKeys(), hasSize(expectedSize));
328 }
329 }