1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.ehcache.jcache;
18
19 import net.sf.ehcache.AbstractCacheTest;
20 import net.sf.ehcache.CacheException;
21 import net.sf.jsr107cache.Cache;
22
23 import java.io.IOException;
24 import java.io.Serializable;
25 import java.util.Date;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30
31
32
33
34
35
36 public class CacheListenerTest extends AbstractCacheTest {
37
38
39
40
41 protected void setUp() throws Exception {
42 super.setUp();
43
44 }
45
46
47
48
49
50 protected void tearDown() throws Exception {
51 getTest1Cache().clear();
52 }
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 protected Cache getTest1Cache() throws net.sf.jsr107cache.CacheException {
69 Cache cache = net.sf.jsr107cache.CacheManager.getInstance().getCache("listenerTest1");
70 if (cache == null) {
71
72 Map env = new HashMap();
73 env.put("name", "test1");
74 env.put("maxElementsInMemory", "10000");
75 env.put("maxElementsOnDisk", "1000");
76 env.put("memoryStoreEvictionPolicy", "LRU");
77 env.put("overflowToDisk", "true");
78 env.put("eternal", "false");
79 env.put("timeToLiveSeconds", "1");
80 env.put("timeToIdleSeconds", "1");
81 env.put("diskPersistent", "false");
82 env.put("diskExpiryThreadIntervalSeconds", "120");
83 env.put("cacheLoaderFactoryClassName", "net.sf.ehcache.jcache.CountingCacheLoaderFactory");
84 cache = net.sf.jsr107cache.CacheManager.getInstance().getCacheFactory().createCache(env);
85 net.sf.jsr107cache.CacheManager.getInstance().registerCache("listenerTest1", cache);
86 }
87 return net.sf.jsr107cache.CacheManager.getInstance().getCache("listenerTest1");
88 }
89
90
91
92
93
94
95 public void testSundryOperations() throws net.sf.jsr107cache.CacheException, CloneNotSupportedException {
96
97 Cache cache = getTest1Cache();
98 cache.put("1", new Date());
99 CountingCacheListener countingCacheListener = new CountingCacheListener();
100 JCacheListenerAdaptor jCacheListenerAdaptor = new JCacheListenerAdaptor(countingCacheListener);
101 try {
102 jCacheListenerAdaptor.clone();
103 } catch (CloneNotSupportedException e) {
104 assertEquals("Cannot clone JCacheListenerAdaptor", e.getMessage());
105 }
106
107 assertEquals(countingCacheListener, jCacheListenerAdaptor.getCacheListener());
108
109
110 jCacheListenerAdaptor.dispose();
111 }
112
113
114
115
116 public void testPutNotifications() throws net.sf.jsr107cache.CacheException {
117
118 Cache cache = getTest1Cache();
119 cache.put("1", new Date());
120 CountingCacheListener countingCacheListener = new CountingCacheListener();
121 cache.addListener(countingCacheListener);
122
123 cache.put("2", new Date());
124
125 List notifications = countingCacheListener.getCacheElementsPut();
126
127
128 assertTrue(notifications.size() == 1);
129 assertEquals("2", notifications.get(0));
130
131
132 cache.put("2", new Date());
133 notifications = countingCacheListener.getCacheElementsPut();
134 assertTrue(notifications.size() == 2);
135
136 cache.removeListener(countingCacheListener);
137
138
139 cache.put("3", new Date());
140 notifications = countingCacheListener.getCacheElementsPut();
141 assertTrue(notifications.size() == 2);
142
143
144 }
145
146
147
148
149
150 public void testRemoveNotifications() throws net.sf.jsr107cache.CacheException {
151
152 Serializable key = "1";
153 Serializable value = new Date();
154
155 Cache cache = getTest1Cache();
156 CountingCacheListener countingCacheListener = new CountingCacheListener();
157 cache.addListener(countingCacheListener);
158
159
160 cache.put(key, value);
161
162
163 cache.remove(key);
164
165
166 List notifications = countingCacheListener.getCacheElementsRemoved();
167 assertEquals(key, notifications.get(0));
168
169
170 cache.remove(key);
171 notifications = countingCacheListener.getCacheElementsRemoved();
172 assertEquals(2, notifications.size());
173
174
175 cache.remove(null);
176
177 }
178
179
180
181
182 public void testExpiryNotifications() throws InterruptedException, net.sf.jsr107cache.CacheException {
183
184 Serializable key = "1";
185 Serializable value = new Date();
186
187 Cache cache = getTest1Cache();
188 CountingCacheListener countingCacheListener = new CountingCacheListener();
189 cache.addListener(countingCacheListener);
190
191
192 cache.put(key, value);
193
194
195 Thread.sleep(1020);
196
197
198 Object expired = cache.get(key);
199 assertEquals(new Integer(0), expired);
200
201
202 List notifications = countingCacheListener.getCacheElementsEvicted();
203 assertEquals(1, notifications.size());
204
205
206 cache.remove(null);
207
208 }
209
210
211
212
213
214
215 public void testEvictNotificationsWhereNoOverflow() {
216
217 JCache cache2 = new JCache(manager.getCache("sampleCache2"), null);
218 CountingCacheListener countingCacheListener = new CountingCacheListener();
219 cache2.addListener(countingCacheListener);
220
221
222 for (int i = 0; i < 1001; i++) {
223 cache2.put("" + i, new Date());
224 }
225
226 List notifications = countingCacheListener.getCacheElementsEvicted();
227 assertEquals(1, notifications.size());
228 }
229
230
231
232
233
234 public void testEvictNotificationsWhereOverflow() {
235
236
237 JCache cache1 = new JCache(manager.getCache("sampleCache1"), null);
238 CountingCacheListener countingCacheListener = new CountingCacheListener();
239 cache1.addListener(countingCacheListener);
240
241
242 for (int i = 0; i < 10001; i++) {
243 cache1.put("" + i, new Date());
244 }
245
246 List notifications = countingCacheListener.getCacheElementsEvicted();
247 assertEquals(0, notifications.size());
248 }
249
250
251
252
253 public void testClearNotification() {
254
255 JCache cache2 = new JCache(manager.getCache("sampleCache2"), null);
256 CountingCacheListener countingCacheListener = new CountingCacheListener();
257 cache2.addListener(countingCacheListener);
258
259
260 for (int i = 0; i < 11; i++) {
261 cache2.put("" + i, new Date());
262 }
263
264 List notifications = countingCacheListener.getCacheRemoveAlls();
265 assertEquals(0, notifications.size());
266
267
268 cache2.clear();
269 notifications = countingCacheListener.getCacheRemoveAlls();
270 assertEquals(1, notifications.size());
271 }
272
273
274
275
276
277
278 public void testRemoveNotificationWhereElementDidNotExist() throws net.sf.jsr107cache.CacheException {
279
280 Serializable key = "1";
281
282 Cache cache = getTest1Cache();
283 CountingCacheListener countingCacheListener = new CountingCacheListener();
284 cache.addListener(countingCacheListener);
285
286
287
288
289
290 cache.remove(key);
291
292
293 List notifications = countingCacheListener.getCacheElementsRemoved();
294 assertEquals(key, notifications.get(0));
295
296
297 cache.remove(key);
298 notifications = countingCacheListener.getCacheElementsRemoved();
299 assertEquals(2, notifications.size());
300
301
302 cache.remove(null);
303
304 }
305
306
307
308
309
310
311 public void testEvictionFromLRUMemoryStoreNotSerializable() throws IOException, CacheException, InterruptedException {
312 String sampleCache1 = "sampleCache1";
313 Cache cache = new JCache(manager.getCache(sampleCache1), null);
314 cache.clear();
315 CountingCacheListener countingCacheListener = new CountingCacheListener();
316 cache.addListener(countingCacheListener);
317
318
319 cache.put("non-serializable", new Object());
320
321 for (int i = 0; i < 10000; i++) {
322 cache.put(i + "", new Date());
323 }
324
325 List evictionNotifications = countingCacheListener.getCacheElementsEvicted();
326 assertEquals(1, evictionNotifications.size());
327 }
328
329
330
331
332
333
334
335
336 public void testEvictionFromLRUMemoryStoreExpiry() throws IOException, CacheException, InterruptedException {
337 String sampleCache2 = "sampleCache1";
338 Cache cache = new JCache(manager.getCache(sampleCache2), null);
339 cache.clear();
340 CountingCacheListener countingCacheListener = new CountingCacheListener();
341 cache.addListener(countingCacheListener);
342 for (int i = 0; i < 10000; i++) {
343 cache.put(i + "", new Date());
344 }
345
346 Thread.sleep(1030);
347 cache.put("new", new Date());
348
349 List removalNotifications = countingCacheListener.getCacheElementsEvicted();
350 assertEquals(0, removalNotifications.size());
351
352 }
353
354 }