1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.ehcache.event;
18
19 import junit.framework.TestCase;
20 import net.sf.ehcache.AbstractCacheTest;
21 import net.sf.ehcache.CacheException;
22 import net.sf.ehcache.CacheManager;
23 import net.sf.ehcache.Ehcache;
24 import net.sf.ehcache.Element;
25
26 import java.io.IOException;
27 import java.io.Serializable;
28 import java.util.Date;
29 import java.util.List;
30
31
32
33
34
35
36 public class CacheEventListenerTest extends TestCase {
37
38
39
40
41 protected CacheManager manager;
42
43
44
45 protected String cacheName = "sampleCache1";
46
47
48
49 protected Ehcache cache;
50
51
52
53
54
55
56 protected void setUp() throws Exception {
57 CountingCacheEventListener.resetCounters();
58 manager = CacheManager.create(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache-countinglisteners.xml");
59 cache = manager.getCache(cacheName);
60 cache.removeAll();
61 }
62
63
64
65
66
67
68
69 protected void tearDown() throws Exception {
70 CountingCacheEventListener.resetCounters();
71 manager.shutdown();
72 }
73
74
75
76
77
78 public void testPutNotifications() {
79
80 Serializable key = new Date();
81 Serializable value = new Date();
82 Element element = new Element(key, value);
83
84
85 cache.put(element);
86
87 List notifications = CountingCacheEventListener.getCacheElementsPut(cache);
88
89 assertTrue(notifications.size() == 1);
90 assertEquals(key, ((Element) notifications.get(0)).getObjectKey());
91 assertEquals(element.getObjectValue(), ((Element) notifications.get(0)).getObjectValue());
92
93
94 cache.put(element);
95 notifications = CountingCacheEventListener.getCacheElementsPut(cache);
96 assertTrue(notifications.size() == 1);
97
98 }
99
100
101
102
103 public void testUpdateNotifications() {
104
105
106 for (int i = 0; i < 11; i++) {
107 cache.put(new Element("" + i, "" + i));
108 cache.put(new Element("" + i, "" + i));
109 }
110
111
112 cache.put(new Element("20", "20"));
113
114
115 List notifications = CountingCacheEventListener.getCacheElementsPut(cache);
116 assertTrue(notifications.size() == 12);
117 assertEquals("0", ((Element) notifications.get(0)).getObjectKey());
118 assertEquals("0", ((Element) notifications.get(0)).getObjectValue());
119
120 notifications = CountingCacheEventListener.getCacheElementsUpdated(cache);
121 assertTrue(notifications.size() == 11);
122 assertEquals("0", ((Element) notifications.get(0)).getObjectKey());
123 assertEquals("0", ((Element) notifications.get(0)).getObjectValue());
124
125
126 }
127
128
129
130
131 public void testRemoveNotifications() {
132
133 Serializable key = "1";
134 Serializable value = new Date();
135 Element element = new Element(key, value);
136
137
138 cache.put(element);
139
140
141 cache.remove(key);
142
143
144 List notifications = CountingCacheEventListener.getCacheElementsRemoved(cache);
145 assertEquals(element, notifications.get(0));
146
147
148 cache.remove(key);
149 notifications = CountingCacheEventListener.getCacheElementsRemoved(cache);
150 assertEquals(2, notifications.size());
151
152
153 cache.remove(null);
154
155 }
156
157
158
159
160
161
162 public void testEvictNotificationsWhereNoOverflow() {
163
164 Ehcache cache2 = manager.getCache("sampleCache2");
165
166
167 Element element = null;
168 for (int i = 0; i < 11; i++) {
169 element = new Element("" + i, new Date());
170 cache2.put(element);
171 }
172
173 List notifications = CountingCacheEventListener.getCacheElementsEvicted(cache2);
174 assertEquals(1, notifications.size());
175 }
176
177
178
179
180
181 public void testEvictNotificationsWhereOverflow() {
182
183 Ehcache cache1 = manager.getCache("sampleCache1");
184
185
186 Element element = null;
187 for (int i = 0; i < 11; i++) {
188 element = new Element("" + i, new Date());
189 cache1.put(element);
190 }
191
192 List notifications = CountingCacheEventListener.getCacheElementsEvicted(cache1);
193 assertEquals(0, notifications.size());
194 }
195
196
197
198
199 public void testRemoveAllNotification() {
200
201 Ehcache cache2 = manager.getCache("sampleCache2");
202
203
204 Element element = null;
205 for (int i = 0; i < 11; i++) {
206 element = new Element("" + i, new Date());
207 cache2.put(element);
208 }
209
210 List notifications = CountingCacheEventListener.getCacheRemoveAlls(cache2);
211 assertEquals(0, notifications.size());
212
213
214 cache2.removeAll();
215 notifications = CountingCacheEventListener.getCacheRemoveAlls(cache2);
216 assertEquals(1, notifications.size());
217 }
218
219
220
221
222
223
224 public void testRemoveNotificationWhereElementDidNotExist() {
225
226 Serializable key = "1";
227
228
229
230
231
232 cache.remove(key);
233
234
235 List notifications = CountingCacheEventListener.getCacheElementsRemoved(cache);
236 assertEquals(key, ((Element) notifications.get(0)).getKey());
237
238
239 cache.remove(key);
240 notifications = CountingCacheEventListener.getCacheElementsRemoved(cache);
241 assertEquals(2, notifications.size());
242
243
244 cache.remove(null);
245
246 }
247
248
249
250
251
252 public void testExpiryNotifications() throws InterruptedException {
253
254 Serializable key = "1";
255 Serializable value = new Date();
256 Element element = new Element(key, value);
257
258 cache.getCacheEventNotificationService().registerListener(new TestCacheEventListener());
259
260
261 cache.put(element);
262
263
264 Thread.sleep(1020);
265
266
267 Element expiredElement = cache.get(key);
268 assertEquals(null, expiredElement);
269
270
271 Element newElement = cache.get(key);
272 assertEquals("set on notify", newElement.getValue());
273 assertNotNull(newElement);
274
275
276 List notifications = CountingCacheEventListener.getCacheElementsExpired(cache);
277 assertEquals(element, notifications.get(0));
278 assertEquals(1, notifications.size());
279
280
281 cache.remove(null);
282
283 }
284
285
286
287
288 class TestCacheEventListener implements CacheEventListener {
289
290
291
292
293 public void notifyElementRemoved(final Ehcache cache, final Element element) throws CacheException {
294
295 }
296
297
298
299
300
301
302
303
304
305
306
307 public void notifyElementPut(final Ehcache cache, final Element element) throws CacheException {
308
309 }
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324 public void notifyElementUpdated(final Ehcache cache, final Element element) throws CacheException {
325
326 }
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351 public void notifyElementExpired(final Ehcache cache, final Element element) {
352 cache.put(new Element(element.getKey(), "set on notify"));
353 }
354
355
356
357
358 public void notifyElementEvicted(final Ehcache cache, final Element element) {
359
360 }
361
362
363
364
365 public void notifyRemoveAll(final Ehcache cache) {
366
367 }
368
369
370
371
372 public void dispose() {
373
374 }
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434 public Object clone() throws CloneNotSupportedException {
435 return super.clone();
436 }
437 }
438
439
440
441
442
443
444 public void testEvictionFromLRUMemoryStoreNoExpiry() throws IOException, CacheException, InterruptedException {
445 String sampleCache2 = "sampleCache2";
446 cache = manager.getCache(sampleCache2);
447 cache.removeAll();
448 for (int i = 0; i < 10; i++) {
449 cache.put(new Element(i + "", new Date()));
450 }
451 cache.put(new Element(11 + "", new Date()));
452 List evictionNotifications = CountingCacheEventListener.getCacheElementsEvicted(cache);
453 assertEquals(1, evictionNotifications.size());
454
455 List expiryNotifications = CountingCacheEventListener.getCacheElementsExpired(cache);
456 assertEquals(0, expiryNotifications.size());
457 }
458
459
460
461
462
463
464 public void testEvictionFromLRUMemoryStoreNotSerializable() throws IOException, CacheException, InterruptedException {
465 String sampleCache1 = "sampleCache1";
466 cache = manager.getCache(sampleCache1);
467 cache.removeAll();
468
469
470 cache.put(new Element(12 + "", new Object()));
471
472 for (int i = 0; i < 10; i++) {
473 cache.put(new Element(i + "", new Date()));
474 }
475
476 List evictionNotifications = CountingCacheEventListener.getCacheElementsEvicted(cache);
477 assertEquals(1, evictionNotifications.size());
478
479 List expiryNotifications = CountingCacheEventListener.getCacheElementsExpired(cache);
480 assertEquals(0, expiryNotifications.size());
481 }
482
483
484
485
486
487
488
489
490 public void testEvictionFromLRUMemoryStoreExpiry() throws IOException, CacheException, InterruptedException {
491 String sampleCache2 = "sampleCache2";
492 cache = manager.getCache(sampleCache2);
493 cache.removeAll();
494 for (int i = 0; i < 10; i++) {
495 cache.put(new Element(i + "", new Date()));
496 }
497
498 Thread.sleep(1030);
499 cache.put(new Element(11 + "", new Date()));
500
501 List removalNotifications = CountingCacheEventListener.getCacheElementsEvicted(cache);
502 assertEquals(0, removalNotifications.size());
503
504 List expiryNotifications = CountingCacheEventListener.getCacheElementsExpired(cache);
505 assertEquals(1, expiryNotifications.size());
506 }
507
508
509
510
511
512
513 public void testEvictionFromFIFOMemoryStoreNoExpiry() throws IOException, CacheException {
514 String sampleCache3 = "sampleCache3";
515 cache = manager.getCache(sampleCache3);
516 cache.removeAll();
517 for (int i = 0; i < 10; i++) {
518 cache.put(new Element(i + "", new Date()));
519 }
520
521 cache.put(new Element(11 + "", new Date()));
522
523 List removalNotifications = CountingCacheEventListener.getCacheElementsEvicted(cache);
524 assertEquals(1, removalNotifications.size());
525
526 List expiryNotifications = CountingCacheEventListener.getCacheElementsExpired(cache);
527 assertEquals(0, expiryNotifications.size());
528 }
529
530
531
532
533
534
535
536
537 public void testEvictionFromFIFOMemoryStoreExpiry() throws IOException, CacheException, InterruptedException {
538 String sampleCache3 = "sampleCache3";
539 cache = manager.getCache(sampleCache3);
540 cache.removeAll();
541 for (int i = 0; i < 10; i++) {
542 cache.put(new Element(i + "", new Date()));
543 }
544
545 Thread.sleep(1020);
546 cache.put(new Element(11 + "", new Date()));
547
548 List notifications = CountingCacheEventListener.getCacheElementsEvicted(cache);
549 assertEquals(0, notifications.size());
550
551 List expiryNotifications = CountingCacheEventListener.getCacheElementsExpired(cache);
552 assertEquals(1, expiryNotifications.size());
553 }
554
555
556
557
558
559
560 public void testEvictionFromLFUMemoryStoreNoExpiry() throws IOException, CacheException {
561 String sampleCache4 = "sampleCache4";
562 cache = manager.getCache(sampleCache4);
563 cache.removeAll();
564 for (int i = 0; i < 10; i++) {
565 cache.put(new Element(i + "", new Date()));
566 }
567
568 cache.put(new Element(11 + "", new Date()));
569
570 List notifications = CountingCacheEventListener.getCacheElementsEvicted(cache);
571 assertEquals(1, notifications.size());
572
573 List expiryNotifications = CountingCacheEventListener.getCacheElementsExpired(cache);
574 assertEquals(0, expiryNotifications.size());
575 }
576
577
578
579
580
581
582
583
584 public void testEvictionFromLFUMemoryStoreExpiry() throws IOException, CacheException, InterruptedException {
585 String sampleCache4 = "sampleCache4";
586 cache = manager.getCache(sampleCache4);
587 cache.removeAll();
588 for (int i = 0; i < 10; i++) {
589 cache.put(new Element(i + "", new Date()));
590 }
591
592 Thread.sleep(1020);
593 cache.put(new Element(11 + "", new Date()));
594
595 List notifications = CountingCacheEventListener.getCacheElementsEvicted(cache);
596 assertEquals(0, notifications.size());
597
598 List expiryNotifications = CountingCacheEventListener.getCacheElementsExpired(cache);
599 assertEquals(1, expiryNotifications.size());
600 }
601
602
603
604
605
606
607
608
609 public void testExpiryViaMemoryStoreCheckingOnGet() throws InterruptedException, CacheException, IOException {
610
611 cache.removeAll();
612 CountingCacheEventListener.resetCounters();
613
614 Serializable key = "1";
615 Serializable value = new Date();
616 Element element = new Element(key, value);
617
618
619 cache.put(element);
620 Thread.sleep(1020);
621
622
623 cache.get(key);
624 List notifications = CountingCacheEventListener.getCacheElementsExpired(cache);
625 assertEquals(1, notifications.size());
626 assertEquals(element, notifications.get(0));
627 }
628
629
630
631
632
633
634
635
636 public void testExpiryViaDiskStoreCheckingOnGet() throws InterruptedException, CacheException, IOException {
637
638 for (int i = 0; i < 20; i++) {
639 Element element = new Element("" + i, new Date());
640 cache.put(element);
641 }
642
643
644 Thread.sleep(1020);
645
646
647 for (int i = 0; i < 20; i++) {
648 cache.get("" + i);
649 }
650
651 List notifications = CountingCacheEventListener.getCacheElementsExpired(cache);
652 for (int i = 0; i < notifications.size(); i++) {
653 Element element = (Element) notifications.get(i);
654 element.getObjectKey();
655 }
656 assertTrue(notifications.size() >= 10);
657 }
658
659
660
661
662
663
664
665 public void testExpiryViaDiskStoreExpiryThread() throws InterruptedException {
666
667 for (int i = 0; i < 20; i++) {
668 Element element = new Element("" + i, new Date());
669 cache.put(element);
670 }
671
672
673 Thread.sleep(2120);
674
675 List notifications = CountingCacheEventListener.getCacheElementsExpired(cache);
676 for (int i = 0; i < notifications.size(); i++) {
677 Element element = (Element) notifications.get(i);
678 element.getObjectKey();
679 }
680 assertEquals(10, notifications.size());
681
682 }
683
684
685
686
687
688
689 public void xTestEvictionFromDiskStoreWithExpiry() throws IOException, CacheException, InterruptedException {
690
691 cache.removeAll();
692
693 for (int i = 0; i < 20; i++) {
694 Element element = new Element("" + i, new Date());
695 cache.put(element);
696 }
697 cache.put(new Element(21 + "", new Date()));
698 Thread.sleep(2100);
699
700 List notifications = CountingCacheEventListener.getCacheElementsEvicted(cache);
701 assertEquals(1, notifications.size());
702
703 List expiryNotifications = CountingCacheEventListener.getCacheElementsExpired(cache);
704 assertEquals(10, expiryNotifications.size());
705 }
706
707 }