001: /**
002: * Copyright 2003-2007 Luck Consulting Pty Ltd
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package net.sf.ehcache.jcache;
016:
017: import org.apache.commons.logging.Log;
018: import org.apache.commons.logging.LogFactory;
019:
020: import net.sf.jsr107cache.CacheManager;
021: import net.sf.jsr107cache.Cache;
022: import net.sf.jsr107cache.CacheException;
023: import net.sf.jsr107cache.CacheEntry;
024:
025: import net.sf.ehcache.AbstractCacheTest;
026: import net.sf.ehcache.Element;
027:
028: import java.util.Map;
029: import java.util.HashMap;
030: import java.util.Set;
031:
032: /**
033: * Tests for CacheEntry
034: *
035: * @author Greg Luck
036: * @version $Id:JCacheEntryTest.java 318 2007-01-25 01:48:35Z gregluck $
037: */
038: public class JCacheEntryTest extends AbstractCacheTest {
039: private static final Log LOG = LogFactory.getLog(JCacheTest.class
040: .getName());
041:
042: /**
043: * teardown
044: * limits to what we can do here under jsr107
045: */
046: protected void tearDown() throws Exception {
047: getTestCache().clear();
048: }
049:
050: private Cache getTestCache() throws CacheException {
051: Cache cache = CacheManager.getInstance().getCache(
052: "testCacheEntry");
053: if (cache == null) {
054: Map env = new HashMap();
055: env.put("name", "testCacheEntry");
056: env.put("maxElementsInMemory", "1");
057: env.put("overflowToDisk", "true");
058: env.put("eternal", "false");
059: env.put("timeToLiveSeconds", "1");
060: env.put("timeToIdleSeconds", "0");
061: cache = CacheManager.getInstance().getCacheFactory()
062: .createCache(env);
063: CacheManager.getInstance().registerCache("testCacheEntry",
064: cache);
065: }
066: return CacheManager.getInstance().getCache("testCacheEntry");
067: }
068:
069: /**
070: * Test create and access times
071: * jsr107 does not allow a CacheEntry to be put into a cache. So testing
072: * recycling of elements is pointless.
073: */
074: public void testAccessTimes() throws Exception {
075: Cache cache = getTestCache();
076:
077: CacheEntry entry = new JCacheEntry(
078: new Element("key1", "value1"));
079: long creationTime = entry.getCreationTime();
080: assertTrue(entry.getCreationTime() > (System
081: .currentTimeMillis() - 500));
082: assertTrue(entry.getHits() == 0);
083: assertTrue(entry.getLastAccessTime() == 0);
084:
085: cache.put(entry.getKey(), entry.getValue());
086:
087: entry = cache.getCacheEntry("key1");
088: long entryCreationTime = entry.getCreationTime();
089: assertNotNull(entry);
090: cache.get("key1");
091: cache.get("key1");
092: cache.get("key1");
093: //check creation time does not change
094: assertEquals(entryCreationTime, entry.getCreationTime());
095: assertTrue(entry.getLastAccessTime() != 0);
096: assertTrue(entry.getHits() == 4);
097:
098: }
099:
100: /**
101: * This implementation does not have a notion of cost.
102: */
103: public void testCost() throws Exception {
104: Cache cache = getTestCache();
105:
106: CacheEntry entry = new JCacheEntry(
107: new Element("key1", "value1"));
108: assertEquals(0, entry.getCost());
109: }
110:
111: /**
112: * Check the expiry time is correct.
113: */
114: public void testExpirationTime() throws Exception {
115:
116: Cache cache = getTestCache();
117: CacheEntry entry = new JCacheEntry(
118: new Element("key1", "value1"));
119: cache.put(entry.getKey(), entry.getValue());
120: CacheEntry retrievedEntry = cache.getCacheEntry(entry.getKey());
121:
122: //test expiry time s 1000ms after create time.
123: assertTrue(retrievedEntry.getExpirationTime() > (System
124: .currentTimeMillis() + 995));
125: assertTrue(retrievedEntry.getExpirationTime() < (System
126: .currentTimeMillis() + 1005));
127: }
128:
129: /**
130: * Check the expiry time is correct.
131: */
132: public void testCannotSet() throws Exception {
133:
134: Cache cache = getTestCache();
135: CacheEntry entry = new JCacheEntry(
136: new Element("key1", "value1"));
137: cache.put(entry.getKey(), entry.getValue());
138: CacheEntry retrievedEntry = cache.getCacheEntry(entry.getKey());
139:
140: try {
141: retrievedEntry.setValue("test value");
142: } catch (UnsupportedOperationException e) {
143: assertEquals(
144: "Ehcache does not support modification of Elements. They are immutable.",
145: e.getMessage());
146: }
147:
148: }
149:
150: /**
151: * Each get should cause a hit.
152: */
153: public void testHits() throws Exception {
154:
155: Cache cache = getTestCache();
156: CacheEntry entry = new JCacheEntry(
157: new Element("key1", "value1"));
158: assertEquals(0, entry.getHits());
159:
160: cache.put(entry.getKey(), entry.getValue());
161: CacheEntry retrievedEntry = cache.getCacheEntry(entry.getKey());
162:
163: assertEquals(1, retrievedEntry.getHits());
164:
165: cache.get(entry.getKey());
166: retrievedEntry = cache.getCacheEntry(entry.getKey());
167:
168: assertEquals(3, retrievedEntry.getHits());
169:
170: }
171:
172: /**
173: * Last access time should be 0 if not accessed and then the last get time.
174: */
175: public void testLastAccessTime() throws Exception {
176:
177: Cache cache = getTestCache();
178: CacheEntry entry = new JCacheEntry(
179: new Element("key1", "value1"));
180: assertEquals(0, entry.getLastAccessTime());
181:
182: cache.put(entry.getKey(), entry.getValue());
183: CacheEntry retrievedEntry = cache.getCacheEntry(entry.getKey());
184:
185: //test access is in the last 5ms
186: assertTrue(retrievedEntry.getLastAccessTime() <= System
187: .currentTimeMillis());
188:
189: }
190:
191: /**
192: * 0 when first created. 0 when first put. 1 when replaced.
193: */
194: public void testLastUpdateTime() throws Exception {
195:
196: Cache cache = getTestCache();
197: CacheEntry entry = new JCacheEntry(
198: new Element("key1", "value1"));
199: assertEquals(0, entry.getLastUpdateTime());
200:
201: cache.put(entry.getKey(), entry.getValue());
202: //update it
203: cache.put(entry.getKey(), entry.getValue());
204: CacheEntry retrievedEntry = cache.getCacheEntry(entry.getKey());
205:
206: //test update is in the last 5ms
207: assertTrue(retrievedEntry.getLastUpdateTime() > System
208: .currentTimeMillis() - 10);
209: assertTrue(retrievedEntry.getLastUpdateTime() <= System
210: .currentTimeMillis());
211:
212: }
213:
214: /**
215: * 0 when first created. 1 when first put. > 2 when replaced.
216: */
217: public void testVersion() throws Exception {
218:
219: Cache cache = getTestCache();
220: CacheEntry entry = new JCacheEntry(
221: new Element("key1", "value1"));
222: assertEquals(1, entry.getVersion());
223:
224: cache.put(entry.getKey(), entry.getValue());
225: //update it
226: cache.put(entry.getKey(), entry.getValue());
227: CacheEntry retrievedEntry = cache.getCacheEntry(entry.getKey());
228:
229: assertTrue(retrievedEntry.getVersion() >= 2);
230: }
231:
232: /**
233: * valid when first created. valid if not expired, invalid if expired.
234: */
235: public void testIsValid() throws Exception {
236:
237: Cache cache = getTestCache();
238: CacheEntry entry = new JCacheEntry(
239: new Element("key1", "value1"));
240: assertEquals(true, entry.isValid());
241:
242: cache.put(entry.getKey(), entry.getValue());
243: CacheEntry retrievedEntry = cache.getCacheEntry(entry.getKey());
244: assertEquals(true, retrievedEntry.isValid());
245:
246: Thread.sleep(1020);
247: assertEquals(false, retrievedEntry.isValid());
248:
249: }
250:
251: /**
252: * Test getting the entry set
253: */
254: public void testEntrySet() throws Exception {
255:
256: JCache jcache = (JCache) getTestCache();
257: CacheEntry entry = new JCacheEntry(
258: new Element("key1", "value1"));
259: assertEquals(true, entry.isValid());
260:
261: jcache.put(entry.getKey(), entry.getValue());
262:
263: //Entry Set works
264: Set entries = jcache.entrySet();
265: assertEquals(1, entries.size());
266:
267: //Entry Set is not live
268: jcache.remove("key1");
269: assertEquals(1, entries.size());
270:
271: }
272:
273: }
|