View Javadoc

1   /**
2    *  Copyright 2003-2006 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.hibernate;
18  
19  import net.sf.ehcache.AbstractCacheTest;
20  import net.sf.ehcache.CacheTest;
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  import java.io.Serializable;
25  import java.util.Map;
26  import java.util.Properties;
27  
28  
29  /**
30   * Tests for a Cache
31   *
32   * @author Greg Luck, Claus Ibsen
33   * @version $Id: HibernateAPIUsageTest.java 49 2006-04-23 12:59:55Z gregluck $
34   */
35  public class HibernateAPIUsageTest extends AbstractCacheTest {
36      private static final Log LOG = LogFactory.getLog(CacheTest.class.getName());
37  
38  
39      /**
40       * teardown
41       */
42      protected void tearDown() throws Exception {
43          super.tearDown();
44      }
45  
46  
47      /**
48       * Make sure ehcache works with one of the main projects using it: Hibernate-2.1.8
49       */
50      public void testAPIAsUsedByHibernate2() throws org.hibernate.cache.CacheException {
51          org.hibernate.cache.EhCacheProvider provider = new org.hibernate.cache.EhCacheProvider();
52          provider.start(null);
53          org.hibernate.cache.Cache cache = provider.buildCache("sampleCache1", null);
54          assertNotNull(manager.getCache("sampleCache1"));
55  
56          Serializable key = "key";
57          Serializable value = "value";
58          cache.put(key, value);
59          assertEquals(value, cache.get(key));
60  
61          cache.remove(key);
62          assertEquals(null, cache.get(key));
63      }
64  
65  
66      /**
67       * Make sure ehcache works with one of the main projects using it: Hibernate-3.1.3
68       *
69       * Note that getElementCountInMemory() is broken. It reports the total cache size rather than the memory size
70       * getTimeout appears to be broken. It returns 4096 minutes!
71       */
72      public void testAPIAsUsedByHibernate3() {
73          org.hibernate.cache.EhCacheProvider provider = new org.hibernate.cache.EhCacheProvider();
74          provider.start(null);
75          org.hibernate.cache.Cache cache = provider.buildCache("sampleCache1", null);
76  
77          //Check created and name
78          assertNotNull(cache.getRegionName());
79          assertEquals("sampleCache1", cache.getRegionName());
80  
81          Serializable key = "key";
82          Serializable value = "value";
83  
84          cache.put(key, value);
85          assertEquals(value, cache.get(key));
86          assertEquals(value, cache.read(key));
87  
88          cache.remove(key);
89          assertEquals(null, cache.get(key));
90  
91          //Behaves like a put
92          cache.update(key, value);
93          assertEquals(value, cache.get(key));
94          cache.remove(key);
95  
96  
97          //Check counts and stats
98          for (int i = 0; i < 10010; i++) {
99              cache.put("" + i, value);
100         }
101         //this is broken!
102         assertEquals(10010, cache.getElementCountInMemory());
103         assertEquals(10, cache.getElementCountOnDisk());
104 
105         //clear
106         cache.clear();
107         assertEquals(0, cache.getElementCountInMemory());
108         cache.put(key, value);
109         assertTrue(213 == cache.getSizeInMemory());
110 
111         //locks
112         //timeout. This seems strange
113         assertEquals(245760000, cache.getTimeout());
114         cache.lock(key);
115         cache.unlock(key);
116 
117         //toMap
118         Map map = cache.toMap();
119         assertEquals(1, map.size());
120         assertEquals(value, map.get(key));
121 
122         long time1 = cache.nextTimestamp();
123         long time2 = cache.nextTimestamp();
124         assertTrue(time2 > time1);
125 
126         cache.destroy();
127         try {
128             cache.get(key);
129             fail();
130         } catch (IllegalStateException e) {
131             //expected
132         }
133 
134     }
135 
136     /**
137      * Test ehcache packaged provider and EhCache with Hibernate-3.1.3
138      * Leave broken timeout until get clarification from Emmanuel
139      */
140     public void testNewHibernateEhcacheAndProviderBackwardCompatible() {
141 
142         /*Shutdown cache manager so that hibernate can start one using the same cache.xml disk path
143           because it does not use the singleton CacheManager any more */
144         manager.shutdown();
145 
146         net.sf.ehcache.hibernate.EhCacheProvider provider = new net.sf.ehcache.hibernate.EhCacheProvider();
147         provider.start(null);
148         org.hibernate.cache.Cache cache = provider.buildCache("sampleCache1", null);
149 
150         //Check created and name
151         assertNotNull(cache.getRegionName());
152         assertEquals("sampleCache1", cache.getRegionName());
153 
154         Serializable key = "key";
155         Serializable value = "value";
156 
157         cache.put(key, value);
158         assertEquals(value, cache.get(key));
159         assertEquals(value, cache.read(key));
160 
161         cache.remove(key);
162         assertEquals(null, cache.get(key));
163 
164         //Behaves like a put
165         cache.update(key, value);
166         assertEquals(value, cache.get(key));
167         cache.remove(key);
168 
169 
170         //Check counts and stats
171         for (int i = 0; i < 10010; i++) {
172             cache.put("" + i, value);
173         }
174         assertEquals(10000, cache.getElementCountInMemory());
175         assertEquals(10, cache.getElementCountOnDisk());
176 
177         //clear
178         cache.clear();
179         assertEquals(0, cache.getElementCountInMemory());
180         cache.put(key, value);
181         assertTrue(213 == cache.getSizeInMemory());
182 
183         //locks
184         //timeout. This seems strange
185         assertEquals(245760000, cache.getTimeout());
186         cache.lock(key);
187         cache.unlock(key);
188 
189         //toMap
190         Map map = cache.toMap();
191         assertEquals(1, map.size());
192         assertEquals(value, map.get(key));
193 
194         long time1 = cache.nextTimestamp();
195         long time2 = cache.nextTimestamp();
196         assertTrue(time2 > time1);
197 
198         cache.destroy();
199         try {
200             cache.get(key);
201             fail();
202         } catch (IllegalStateException e) {
203             //expected
204         }
205 
206         ((net.sf.ehcache.hibernate.EhCache) cache).getBackingCache().getCacheManager().shutdown();
207 
208 
209     }
210 
211     /**
212      * Test ehcache packaged provider and EhCache with Hibernate-3.1.3
213      * Leave broken timeout until get clarification from Emmanuel
214      *
215      * Test new features:
216      * <ol>
217      * <li>Support for Object signatures
218      * <li>support for multiple SessionFactory objects in Hibernate, which presumably mean multiple providers.
219      * We can have two caches of the same name in different providers and interact with both
220      * </ol>
221      *
222      */
223     public void testNewHibernateEhcacheAndProviderNewFeatures() {
224 
225         /*Shutdown cache manager so that hibernate can start one using the same cache.xml disk path
226           because it does not use the singleton CacheManager any more */
227         manager.shutdown();
228 
229         net.sf.ehcache.hibernate.EhCacheProvider provider = new net.sf.ehcache.hibernate.EhCacheProvider();
230         provider.start(null);
231         org.hibernate.cache.Cache cache = provider.buildCache("sampleCache1", null);
232 
233         //start up second provider pointing to ehcache-failsage.xml because it is there
234         net.sf.ehcache.hibernate.EhCacheProvider provider2 = new net.sf.ehcache.hibernate.EhCacheProvider();
235 
236         //Fire up a second provider, CacheManager and cache concurrently
237         Properties properties = new Properties();
238         properties.setProperty(EhCacheProvider.NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME, "ehcache-2.xml");
239         provider2.start(properties);
240         org.hibernate.cache.Cache cache2 = provider.buildCache("sampleCache1", null);
241 
242         //Check created and name
243         assertNotNull(cache.getRegionName());
244         assertEquals("sampleCache1", cache.getRegionName());
245 
246         //Test with Object rather than Serializable
247         Object key = new Object();
248         Object value = new Object();
249 
250         cache.put(key, value);
251         assertEquals(value, cache.get(key));
252         assertEquals(value, cache.read(key));
253         cache2.put(key, value);
254         assertEquals(value, cache2.get(key));
255         assertEquals(value, cache2.read(key));
256 
257         cache.remove(key);
258         assertEquals(null, cache.get(key));
259         cache2.remove(key);
260         assertEquals(null, cache2.get(key));
261 
262         //Behaves like a put
263         cache.update(key, value);
264         assertEquals(value, cache.get(key));
265         cache.remove(key);
266         cache2.update(key, value);
267         assertEquals(value, cache2.get(key));
268         cache2.remove(key);
269 
270         //Check counts and stats
271         for (int i = 0; i < 10010; i++) {
272             cache.put("" + i, value);
273         }
274         assertEquals(10000, cache.getElementCountInMemory());
275         //objects don't overflow, only Serializable
276         assertEquals(0, cache.getElementCountOnDisk());
277 
278         //clear
279         cache.clear();
280         assertEquals(0, cache.getElementCountInMemory());
281         cache.put(key, value);
282         //Not Serializable therefore unmeasurable using ehcache's estimation algorithm
283         assertTrue(0 == cache.getSizeInMemory());
284 
285         //locks
286         //timeout. This seems strange
287         assertEquals(245760000, cache.getTimeout());
288         cache.lock(key);
289         cache.unlock(key);
290 
291         //toMap
292         Map map = cache.toMap();
293         assertEquals(1, map.size());
294         assertEquals(value, map.get(key));
295 
296         long time1 = cache.nextTimestamp();
297         long time2 = cache.nextTimestamp();
298         assertTrue(time2 > time1);
299 
300         cache.destroy();
301         try {
302             cache.get(key);
303             fail();
304         } catch (IllegalStateException e) {
305             //expected
306         }
307 
308         cache2.destroy();
309         try {
310             cache2.get(key);
311             fail();
312         } catch (IllegalStateException e) {
313             //expected
314         }
315 
316         ((net.sf.ehcache.hibernate.EhCache) cache).getBackingCache().getCacheManager().shutdown();
317     }
318 }