1   /**
2    *  Copyright 2003-2007 Greg Luck
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
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   * Tests for CacheEntry
36   *
37   * @author Greg Luck
38   * @version $Id:JCacheEntryTest.java 318 2007-01-25 01:48:35Z gregluck $
39   */
40  public class JCacheEntryTest extends AbstractCacheTest {
41      private static final Log LOG = LogFactory.getLog(JCacheTest.class.getName());
42  
43  
44      /**
45       * teardown
46       * limits to what we can do here under jsr107
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       * Test create and access times
71       * jsr107 does not allow a CacheEntry to be put into a cache. So testing
72       * recycling of elements is pointless.
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          //check creation time does not change
92          assertEquals(entryCreationTime, entry.getCreationTime());
93          assertTrue(entry.getLastAccessTime() != 0);
94          assertTrue(entry.getHits() == 4);
95  
96      }
97  
98      /**
99       * This implementation does not have a notion of cost.
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      * Check the expiry time is correct.
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         //test expiry time s 1000ms after create time.
120         assertTrue(retrievedEntry.getExpirationTime() > (System.currentTimeMillis() + 995));
121         assertTrue(retrievedEntry.getExpirationTime() < (System.currentTimeMillis() + 1005));
122     }
123 
124     /**
125      * Check the expiry time is correct.
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      * Each get should cause a hit.
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      * Last access time should be 0 if not accessed and then the last get time.
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         //test access is in the last 5ms
176         assertTrue(retrievedEntry.getLastAccessTime() <= System.currentTimeMillis());
177 
178     }
179 
180     /**
181      * 0 when first created. 0 when first put. 1 when replaced.
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         //update it
191         cache.put(entry.getKey(), entry.getValue());
192         CacheEntry retrievedEntry = cache.getCacheEntry(entry.getKey());
193 
194         //test update is in the last 5ms
195         assertTrue(retrievedEntry.getLastUpdateTime() > System.currentTimeMillis() - 10);
196         assertTrue(retrievedEntry.getLastUpdateTime() <= System.currentTimeMillis());
197 
198     }
199 
200     /**
201      * 0 when first created. 1 when first put. > 2 when replaced.
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         //update it
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      * valid when first created. valid if not expired, invalid if expired.
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      * Test getting the entry set
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         //Entry Set works
253         Set entries = jcache.entrySet();
254         assertEquals(1, entries.size());
255 
256         //Entry Set is not live
257         jcache.remove("key1");
258         assertEquals(1, entries.size());
259 
260 
261     }
262 
263 }