1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.ksb.cache;
17
18 import javax.xml.namespace.QName;
19
20 import org.junit.Test;
21 import org.kuali.rice.ksb.cache.RiceCacheAdministrator;
22 import org.kuali.rice.ksb.test.KSBTestCase;
23
24 public class RiceCacheAdministratorTest extends KSBTestCase {
25
26 String key = "_key";
27 Object value = "_value";
28 String group = "_group";
29
30 @Override
31 public boolean startClient1() {
32 return true;
33 }
34
35 @Test public void testCache() throws Exception {
36
37 loadCaches();
38 assertCachesNotEmpty();
39
40
41 RiceCacheAdministrator cache = (RiceCacheAdministrator)getSpringContextResourceLoader().getService(new QName("cache"));
42 cache.flushAll();
43
44 Thread.sleep(4000);
45 assertCachesEmpty();
46
47 loadCaches();
48 assertCachesNotEmpty();
49
50 cache.flushEntry(this.key);
51 Thread.sleep(4000);
52 assertCachesEmpty();
53
54 loadCaches();
55 assertCachesNotEmpty();
56
57 cache.flushGroup(this.group);
58 Thread.sleep(4000);
59 assertCachesEmpty();
60
61 loadCaches();
62 assertCachesNotEmpty();
63
64 }
65
66 @Test public void testCacheRefreshPeriod() throws Exception {
67
68 loadCaches();
69 assertCachesAreRefreshed();
70 }
71
72 private void assertCachesNotEmpty() throws Exception {
73 RiceCacheAdministrator cache = (RiceCacheAdministrator)getSpringContextResourceLoader().getService(new QName("cache"));
74 RiceCacheAdministrator client1Cache = (RiceCacheAdministrator)getServiceFromTestClient1SpringContext("cache");
75
76 assertEquals(this.value, cache.getFromCache(this.key));
77 assertEquals(this.value, client1Cache.getFromCache(this.key));
78 }
79
80 private void assertCachesEmpty() throws Exception {
81 RiceCacheAdministrator cache = (RiceCacheAdministrator)getSpringContextResourceLoader().getService(new QName("cache"));
82 RiceCacheAdministrator client1Cache = (RiceCacheAdministrator)getServiceFromTestClient1SpringContext("cache");
83
84 assertNull(cache.getFromCache(this.key));
85 assertNull(client1Cache.getFromCache(this.key));
86 }
87
88 private void loadCaches() throws Exception {
89 RiceCacheAdministrator cache = (RiceCacheAdministrator)getSpringContextResourceLoader().getService(new QName("cache"));
90 RiceCacheAdministrator client1Cache = (RiceCacheAdministrator)getServiceFromTestClient1SpringContext("cache");
91
92 cache.putInCache(this.key, this.value, this.group);
93 client1Cache.putInCache(this.key, this.value, this.group);
94 }
95
96 private void assertCachesAreRefreshed() throws Exception {
97 int refreshSeconds = 10;
98 RiceCacheAdministrator cache = (RiceCacheAdministrator)getSpringContextResourceLoader().getService(new QName("cache"));
99 RiceCacheAdministrator client1Cache = (RiceCacheAdministrator)getServiceFromTestClient1SpringContext("cache");
100
101 assertEquals(this.value, cache.getFromCache(this.key, refreshSeconds));
102 assertEquals(this.value, client1Cache.getFromCache(this.key, refreshSeconds));
103
104
105 Thread.sleep(12000);
106
107 assertNull(cache.getFromCache(this.key, refreshSeconds));
108 assertNull(client1Cache.getFromCache(this.key, refreshSeconds));
109 }
110 }
111