1 package com.atlassian.vcache.internal.test;
2
3 import java.util.Arrays;
4 import java.util.Map;
5 import java.util.Optional;
6 import java.util.Set;
7 import java.util.function.Function;
8 import java.util.stream.Collectors;
9
10 import com.atlassian.vcache.LocalCacheOperations;
11
12 import org.hamcrest.Matchers;
13 import org.junit.Before;
14 import org.junit.Rule;
15 import org.junit.Test;
16 import org.junit.rules.ExpectedException;
17
18 import static org.hamcrest.Matchers.is;
19 import static org.hamcrest.Matchers.notNullValue;
20 import static org.junit.Assert.assertThat;
21
22
23
24
25 public abstract class AbstractLocalCacheOperationsTest
26 {
27 @Rule
28 public ExpectedException thrown = ExpectedException.none();
29
30 private LocalCacheOperations<String, String> cache;
31
32 protected abstract LocalCacheOperations<String, String> createCache(String name);
33
34 @Before
35 public void init()
36 {
37 cache = createCache("some.smart.name");
38 }
39
40 @Test
41 public void testGet() throws Exception
42 {
43 final Optional<String> result1 = cache.get("Parlez-Vouis Francais");
44
45 assertThat(result1, notNullValue());
46 assertThat(result1, is(Optional.empty()));
47
48 final String supplied = cache.get("Parlez-Vouis Francais", () -> "Magic Fountain");
49
50 assertThat(supplied, is("Magic Fountain"));
51
52 final Optional<String> result2 = cache.get("Parlez-Vouis Francais");
53
54 assertThat(result2, notNullValue());
55 assertThat(result2, is(Optional.of("Magic Fountain")));
56 }
57
58 @Test
59 public void testPut() throws Exception
60 {
61 final Optional<String> get1 = cache.get("aqua");
62
63 assertThat(get1, notNullValue());
64 assertThat(get1, is(Optional.empty()));
65
66 cache.put("aqua", "water");
67 final Optional<String> put1 = cache.get("aqua");
68
69 assertThat(put1, notNullValue());
70 assertThat(put1, is(Optional.of("water")));
71 }
72
73 @Test
74 public void testPutIfAbsent() throws Exception
75 {
76 final Optional<String> get1 = cache.get("aqua");
77
78 assertThat(get1, notNullValue());
79 assertThat(get1, is(Optional.empty()));
80
81 final Optional<String> put1 = cache.putIfAbsent("aqua", "water");
82
83 assertThat(put1, notNullValue());
84 assertThat(put1, is(Optional.empty()));
85
86 final Optional<String> put2 = cache.putIfAbsent("aqua", "water2");
87
88 assertThat(put2, notNullValue());
89 assertThat(put2, is(Optional.of("water")));
90
91 final Optional<String> get2 = cache.get("aqua");
92
93 assertThat(get2, notNullValue());
94 assertThat(get2, is(Optional.of("water")));
95 }
96
97 @Test
98 public void testReplaceIfOkay() throws Exception
99 {
100 cache.put("aqua", "water");
101 final boolean replaced = cache.replaceIf("aqua", "water", "water2");
102
103 assertThat(replaced, is(true));
104 assertThat(cache.get("aqua"), is(Optional.of("water2")));
105 }
106
107 @Test
108 public void testReplaceIfMismatch() throws Exception
109 {
110 cache.put("aqua", "water");
111 final boolean replaced = cache.replaceIf("aqua", "tea", "water2");
112
113 assertThat(replaced, is(false));
114 assertThat(cache.get("aqua"), is(Optional.of("water")));
115 }
116
117 @Test
118 public void testReplaceIfMissing() throws Exception
119 {
120 final boolean replaced = cache.replaceIf("aqua", "water", "water2");
121
122 assertThat(replaced, is(false));
123 assertThat(cache.get("aqua"), is(Optional.empty()));
124 }
125
126 @Test
127 public void testRemoveIfOkay() throws Exception
128 {
129 cache.put("aqua", "water");
130
131 final boolean removed = cache.removeIf("aqua", "water");
132
133 assertThat(removed, is(true));
134 assertThat(cache.get("aqua"), is(Optional.empty()));
135 }
136
137 @Test
138 public void testRemoveIfMismatch() throws Exception
139 {
140 cache.put("aqua", "water");
141
142 final boolean removed = cache.removeIf("aqua", "tea");
143
144 assertThat(removed, is(false));
145 assertThat(cache.get("aqua"), is(Optional.of("water")));
146 }
147
148 @Test
149 public void testRemoveIfMissing() throws Exception
150 {
151 final boolean removed = cache.removeIf("aqua", "water");
152
153 assertThat(removed, is(false));
154 assertThat(cache.get("aqua"), is(Optional.empty()));
155 }
156
157
158 @Test
159 public void testRemoveOkay() throws Exception
160 {
161 cache.put("aqua", "water");
162
163 cache.remove("aqua");
164
165 assertThat(cache.get("aqua"), is(Optional.empty()));
166 }
167
168 @Test
169 public void testRemoveMissing() throws Exception
170 {
171 cache.remove("aqua");
172
173 assertThat(cache.get("aqua"), is(Optional.empty()));
174 }
175
176 @Test
177 public void testRemoveAll() throws Exception
178 {
179 cache.put("k1", "v1");
180 cache.put("k2", "v2");
181
182 assertThat(cache.get("k1"), is(Optional.of("v1")));
183 assertThat(cache.get("k2"), is(Optional.of("v2")));
184
185 cache.removeAll();
186
187 assertThat(cache.get("k1"), is(Optional.empty()));
188 assertThat(cache.get("k2"), is(Optional.empty()));
189 }
190
191 @Test
192 public void testGetBulkEmpty() throws Exception
193 {
194 final Map<String, Optional<String>> data = cache.getBulk();
195
196 assertThat(data, notNullValue());
197 assertThat(data.size(), is(0));
198 }
199
200 @Test
201 public void testGetBulkSingleMiss() throws Exception
202 {
203 final Map<String, Optional<String>> data = cache.getBulk("k1");
204
205 assertThat(data, notNullValue());
206 assertThat(data.keySet(), Matchers.containsInAnyOrder("k1"));
207 assertThat(data.values(), Matchers.containsInAnyOrder(Optional.empty()));
208 }
209
210 @Test
211 public void testGetBulkOneMissOneKey() throws Exception
212 {
213 cache.put("hit", "boom");
214 final Map<String, Optional<String>> data = cache.getBulk(Arrays.asList("hit", "miss"));
215
216 assertThat(data, notNullValue());
217 assertThat(data.keySet(), Matchers.containsInAnyOrder("hit", "miss"));
218 assertThat(data.get("hit"), is(Optional.of("boom")));
219 assertThat(data.get("miss"), is(Optional.empty()));
220 }
221
222 @Test
223 public void testGetBulkWithFunction() throws Exception
224 {
225 cache.put("k2", "kayak");
226 final Function<Set<String>, Map<String, String>> factory = keys ->
227 keys.stream().collect(Collectors.toMap(k -> k, k -> "val-" + k));
228 final Map<String, String> data = cache.getBulk(factory, "k1", "k2", "k3");
229
230 assertThat(data, notNullValue());
231 assertThat(data.keySet(), Matchers.containsInAnyOrder("k1", "k2", "k3"));
232 assertThat(data.get("k1"), is("val-k1"));
233 assertThat(data.get("k2"), is("kayak"));
234 assertThat(data.get("k3"), is("val-k3"));
235
236
237 assertThat(cache.get("k1"), is(Optional.of("val-k1")));
238 assertThat(cache.get("k2"), is(Optional.of("kayak")));
239 assertThat(cache.get("k3"), is(Optional.of("val-k3")));
240 }
241
242 @Test
243 public void testInvalidName()
244 {
245 thrown.expect(IllegalArgumentException.class);
246 thrown.expectMessage("Invalid cache name: illegal%name for !@#");
247
248 cache = createCache("illegal%name for !@#");
249 }
250
251 }