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