KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > cache > ehcache > EhCacheSupportTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
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 org.springframework.cache.ehcache;
18
19 import junit.framework.TestCase;
20 import net.sf.ehcache.Cache;
21 import net.sf.ehcache.CacheManager;
22
23 import org.springframework.core.io.ClassPathResource;
24
25 /**
26  * @author Dmitriy Kopylenko
27  * @author Juergen Hoeller
28  * @since 27.09.2004
29  */

30 public class EhCacheSupportTests extends TestCase {
31
32     public void testLoadingBlankCacheManager() throws Exception JavaDoc {
33         EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean();
34         cacheManagerFb.afterPropertiesSet();
35         try {
36             CacheManager cm = (CacheManager) cacheManagerFb.getObject();
37             assertTrue("Loaded CacheManager with no caches", cm.getCacheNames().length == 0);
38             Cache myCache1 = cm.getCache("myCache1");
39             assertTrue("No myCache1 defined", myCache1 == null);
40         }
41         finally {
42             cacheManagerFb.destroy();
43         }
44     }
45
46     public void testLoadingCacheManagerFromConfigFile() throws Exception JavaDoc {
47         EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean();
48         cacheManagerFb.setConfigLocation(new ClassPathResource("testEhcache.xml", getClass()));
49         cacheManagerFb.afterPropertiesSet();
50         try {
51             CacheManager cm = (CacheManager) cacheManagerFb.getObject();
52             assertTrue("Correct number of caches loaded", cm.getCacheNames().length == 1);
53             Cache myCache1 = cm.getCache("myCache1");
54             assertFalse("myCache1 is not eternal", myCache1.isEternal());
55             assertTrue("myCache1.maxElements == 300", myCache1.getMaxElementsInMemory() == 300);
56         }
57         finally {
58             cacheManagerFb.destroy();
59         }
60     }
61
62     public void testEhCacheFactoryBean() throws Exception JavaDoc {
63         Cache cache = null;
64
65         EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean();
66         cacheManagerFb.setConfigLocation(new ClassPathResource("testEhcache.xml", getClass()));
67         cacheManagerFb.afterPropertiesSet();
68
69         try {
70             EhCacheFactoryBean cacheFb = new EhCacheFactoryBean();
71             cacheFb.setCacheManager((CacheManager) cacheManagerFb.getObject());
72             cacheFb.setCacheName("myCache1");
73             cacheFb.afterPropertiesSet();
74             cache = (Cache) cacheFb.getObject();
75             assertEquals("myCache1", cache.getName());
76             assertFalse("myCache1 is not eternal", cache.isEternal());
77             assertTrue("myCache1.maxElements == 300", cache.getMaxElementsInMemory() == 300);
78
79             // Cache region is not defined. Should create one with default properties.
80
cacheFb = new EhCacheFactoryBean();
81             cacheFb.setCacheManager((CacheManager) cacheManagerFb.getObject());
82             cacheFb.setCacheName("undefinedCache");
83             cacheFb.afterPropertiesSet();
84             cache = (Cache) cacheFb.getObject();
85             assertEquals("undefinedCache", cache.getName());
86             assertTrue("default maxElements is correct", cache.getMaxElementsInMemory() == 10000);
87             assertTrue("default overflowToDisk is correct", cache.isOverflowToDisk());
88             assertFalse("default eternal is correct", cache.isEternal());
89             assertTrue("default timeToLive is correct", cache.getTimeToLiveSeconds() == 120);
90             assertTrue("default timeToIdle is correct", cache.getTimeToIdleSeconds() == 120);
91             assertTrue("default diskPersistent is correct", !cache.isDiskPersistent());
92             assertTrue("default diskExpiryThreadIntervalSeconds is correct", cache.getDiskExpiryThreadIntervalSeconds() == 120);
93
94             // overriding the default properties
95
cacheFb = new EhCacheFactoryBean();
96             cacheFb.setCacheManager((CacheManager) cacheManagerFb.getObject());
97             cacheFb.setBeanName("undefinedCache2");
98             cacheFb.setMaxElementsInMemory(5);
99             cacheFb.setOverflowToDisk(false);
100             cacheFb.setEternal(true);
101             cacheFb.setTimeToLive(8);
102             cacheFb.setTimeToIdle(7);
103             cacheFb.setDiskPersistent(true);
104             cacheFb.setDiskExpiryThreadIntervalSeconds(10);
105             cacheFb.afterPropertiesSet();
106             cache = (Cache) cacheFb.getObject();
107             assertEquals("undefinedCache2", cache.getName());
108             assertTrue("overridden maxElements is correct", cache.getMaxElementsInMemory() == 5);
109             assertFalse("overridden overflowToDisk is correct", cache.isOverflowToDisk());
110             assertTrue("overridden eternal is correct", cache.isEternal());
111             assertTrue("default timeToLive is correct", cache.getTimeToLiveSeconds() == 8);
112             assertTrue("default timeToIdle is correct", cache.getTimeToIdleSeconds() == 7);
113             assertTrue("overridden diskPersistent is correct", cache.isDiskPersistent());
114             assertTrue("overridden diskExpiryThreadIntervalSeconds is correct", cache.getDiskExpiryThreadIntervalSeconds() == 10);
115         }
116         finally {
117             cacheManagerFb.destroy();
118         }
119     }
120
121 }
122
Popular Tags