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 org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 import net.sf.jsr107cache.CacheManager;
23 import net.sf.jsr107cache.Cache;
24 import net.sf.jsr107cache.CacheException;
25 import net.sf.jsr107cache.CacheEntry;
26
27 import net.sf.ehcache.AbstractCacheTest;
28 import net.sf.ehcache.Element;
29
30 import java.util.Map;
31 import java.util.HashMap;
32 import java.util.Set;
33
34
35
36
37
38
39
40 public class JCacheEntryTest extends AbstractCacheTest {
41 private static final Log LOG = LogFactory.getLog(JCacheTest.class.getName());
42
43
44
45
46
47
48 protected void tearDown() throws Exception {
49 getTestCache().clear();
50 }
51
52 private Cache getTestCache() throws CacheException {
53 Cache cache = CacheManager.getInstance().getCache("testCacheEntry");
54 if (cache == null) {
55 Map env = new HashMap();
56 env.put("name", "testCacheEntry");
57 env.put("maxElementsInMemory", "1");
58 env.put("overflowToDisk", "true");
59 env.put("eternal", "false");
60 env.put("timeToLiveSeconds", "1");
61 env.put("timeToIdleSeconds", "0");
62 cache = CacheManager.getInstance().getCacheFactory().createCache(env);
63 CacheManager.getInstance().registerCache("testCacheEntry", cache);
64 }
65 return CacheManager.getInstance().getCache("testCacheEntry");
66 }
67
68
69
70
71
72
73
74 public void testAccessTimes() throws Exception {
75 Cache cache = getTestCache();
76
77 CacheEntry entry = new JCacheEntry(new Element("key1", "value1"));
78 long creationTime = entry.getCreationTime();
79 assertTrue(entry.getCreationTime() > (System.currentTimeMillis() - 500));
80 assertTrue(entry.getHits() == 0);
81 assertTrue(entry.getLastAccessTime() == 0);
82
83 cache.put(entry.getKey(), entry.getValue());
84
85 entry = cache.getCacheEntry("key1");
86 long entryCreationTime = entry.getCreationTime();
87 assertNotNull(entry);
88 cache.get("key1");
89 cache.get("key1");
90 cache.get("key1");
91
92 assertEquals(entryCreationTime, entry.getCreationTime());
93 assertTrue(entry.getLastAccessTime() != 0);
94 assertTrue(entry.getHits() == 4);
95
96 }
97
98
99
100
101 public void testCost() throws Exception {
102 Cache cache = getTestCache();
103
104 CacheEntry entry = new JCacheEntry(new Element("key1", "value1"));
105 assertEquals(0, entry.getCost());
106 }
107
108
109
110
111
112 public void testExpirationTime() throws Exception {
113
114 Cache cache = getTestCache();
115 CacheEntry entry = new JCacheEntry(new Element("key1", "value1"));
116 cache.put(entry.getKey(), entry.getValue());
117 CacheEntry retrievedEntry = cache.getCacheEntry(entry.getKey());
118
119
120 assertTrue(retrievedEntry.getExpirationTime() > (System.currentTimeMillis() + 995));
121 assertTrue(retrievedEntry.getExpirationTime() < (System.currentTimeMillis() + 1005));
122 }
123
124
125
126
127 public void testCannotSet() throws Exception {
128
129 Cache cache = getTestCache();
130 CacheEntry entry = new JCacheEntry(new Element("key1", "value1"));
131 cache.put(entry.getKey(), entry.getValue());
132 CacheEntry retrievedEntry = cache.getCacheEntry(entry.getKey());
133
134 try {
135 retrievedEntry.setValue("test value");
136 } catch (UnsupportedOperationException e) {
137 assertEquals("Ehcache does not support modification of Elements. They are immutable.", e.getMessage());
138 }
139
140 }
141
142
143
144
145 public void testHits() throws Exception {
146
147 Cache cache = getTestCache();
148 CacheEntry entry = new JCacheEntry(new Element("key1", "value1"));
149 assertEquals(0, entry.getHits());
150
151 cache.put(entry.getKey(), entry.getValue());
152 CacheEntry retrievedEntry = cache.getCacheEntry(entry.getKey());
153
154 assertEquals(1, retrievedEntry.getHits());
155
156 cache.get(entry.getKey());
157 retrievedEntry = cache.getCacheEntry(entry.getKey());
158
159 assertEquals(3, retrievedEntry.getHits());
160
161 }
162
163
164
165
166 public void testLastAccessTime() throws Exception {
167
168 Cache cache = getTestCache();
169 CacheEntry entry = new JCacheEntry(new Element("key1", "value1"));
170 assertEquals(0, entry.getLastAccessTime());
171
172 cache.put(entry.getKey(), entry.getValue());
173 CacheEntry retrievedEntry = cache.getCacheEntry(entry.getKey());
174
175
176 assertTrue(retrievedEntry.getLastAccessTime() <= System.currentTimeMillis());
177
178 }
179
180
181
182
183 public void testLastUpdateTime() throws Exception {
184
185 Cache cache = getTestCache();
186 CacheEntry entry = new JCacheEntry(new Element("key1", "value1"));
187 assertEquals(0, entry.getLastUpdateTime());
188
189 cache.put(entry.getKey(), entry.getValue());
190
191 cache.put(entry.getKey(), entry.getValue());
192 CacheEntry retrievedEntry = cache.getCacheEntry(entry.getKey());
193
194
195 assertTrue(retrievedEntry.getLastUpdateTime() > System.currentTimeMillis() - 10);
196 assertTrue(retrievedEntry.getLastUpdateTime() <= System.currentTimeMillis());
197
198 }
199
200
201
202
203 public void testVersion() throws Exception {
204
205 Cache cache = getTestCache();
206 CacheEntry entry = new JCacheEntry(new Element("key1", "value1"));
207 assertEquals(1, entry.getVersion());
208
209
210 cache.put(entry.getKey(), entry.getValue());
211
212 cache.put(entry.getKey(), entry.getValue());
213 CacheEntry retrievedEntry = cache.getCacheEntry(entry.getKey());
214
215 assertTrue(retrievedEntry.getVersion() >= 2);
216 }
217
218
219
220
221
222 public void testIsValid() throws Exception {
223
224 Cache cache = getTestCache();
225 CacheEntry entry = new JCacheEntry(new Element("key1", "value1"));
226 assertEquals(true, entry.isValid());
227
228
229 cache.put(entry.getKey(), entry.getValue());
230 CacheEntry retrievedEntry = cache.getCacheEntry(entry.getKey());
231 assertEquals(true, retrievedEntry.isValid());
232
233
234 Thread.sleep(1020);
235 assertEquals(false, retrievedEntry.isValid());
236
237 }
238
239
240
241
242
243
244 public void testEntrySet() throws Exception {
245
246 JCache jcache = (JCache) getTestCache();
247 CacheEntry entry = new JCacheEntry(new Element("key1", "value1"));
248 assertEquals(true, entry.isValid());
249
250 jcache.put(entry.getKey(), entry.getValue());
251
252
253 Set entries = jcache.entrySet();
254 assertEquals(1, entries.size());
255
256
257 jcache.remove("key1");
258 assertEquals(1, entries.size());
259
260
261 }
262
263 }