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