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 junit.framework.TestCase;
20  import net.sf.ehcache.Ehcache;
21  import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
22  
23  import net.sf.jsr107cache.Cache;
24  import net.sf.jsr107cache.CacheException;
25  import net.sf.jsr107cache.CacheFactory;
26  import net.sf.jsr107cache.CacheManager;
27  
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  /**
32   * @author Greg Luck
33   * @version $Id: CacheManagerTest.java 512 2007-07-10 09:18:45Z gregluck $
34   */
35  public class CacheManagerTest extends TestCase {
36  
37  
38      /**
39       * the CacheManager Singleton instance
40       */
41      protected CacheManager manager;
42  
43      /**
44       * a CacheManager which is created as an instance
45       */
46      protected CacheManager instanceManager;
47  
48      /**
49       * Shutdown managers.
50       * Check that the manager is removed from CacheManager.ALL_CACHE_MANAGERS
51       */
52      protected void tearDown() throws Exception {
53  
54      }
55  
56  
57      /**
58       * Tests the constructors.
59       * <p/>
60       * The factory method and new return different instances.
61       * <p/>
62       * getInstance always returns the same instance
63       */
64      public void testCacheManagerConstructor() {
65          CacheManager cacheManager = new CacheManager();
66          CacheManager cacheManager2 = CacheManager.getInstance();
67          CacheManager cacheManager3 = CacheManager.getInstance();
68          assertTrue(cacheManager != cacheManager2);
69          assertTrue(cacheManager2 == cacheManager3);
70      }
71  
72  
73      /**
74       * CacheManager requires a resource called net.sf.jsr107cache.CacheFactory containing the fully
75       * qualified class name of a cache factory be at /META-INF/services/net.sf.jsr107cache.CacheFactory.
76       *
77       * @throws CacheException
78       */
79      public void testLoadCacheFactory() throws CacheException {
80  
81          manager = CacheManager.getInstance();
82          CacheFactory cacheFactory = manager.getCacheFactory();
83          assertNotNull(cacheFactory);
84      }
85  
86  
87      /**
88       * CacheManager requires a resource called net.sf.jsr107cache.CacheFactory containing the fully
89       * qualified class name of a cache factory be at /META-INF/services/net.sf.jsr107cache.CacheFactory.
90       * <p/>
91       * Create a cache using found factory
92       *
93       * @throws CacheException
94       */
95      public void testCreateCacheFromFactory() throws CacheException {
96  
97          manager = CacheManager.getInstance();
98          CacheFactory cacheFactory = manager.getCacheFactory();
99          assertNotNull(cacheFactory);
100 
101         Map config = new HashMap();
102         config.put("name", "test");
103         config.put("maxElementsInMemory", "10");
104         config.put("memoryStoreEvictionPolicy", "LFU");
105         config.put("overflowToDisk", "true");
106         config.put("eternal", "false");
107         config.put("timeToLiveSeconds", "5");
108         config.put("timeToIdleSeconds", "5");
109         config.put("diskPersistent", "false");
110         config.put("diskExpiryThreadIntervalSeconds", "120");
111 
112         Cache cache = cacheFactory.createCache(config);
113         assertNotNull(cache);
114 
115     }
116 
117     /**
118      * Tests that the CacheManager was successfully created
119      */
120     public void testCreateCacheManager() throws net.sf.ehcache.CacheException {
121         manager = CacheManager.getInstance();
122         CacheManager singletonManager2 = CacheManager.getInstance();
123         assertNotNull(manager);
124         assertEquals(manager, singletonManager2);
125     }
126 
127 
128     /**
129      * Tests that the CacheManager was successfully created
130      */
131     public void testRegisterCache() throws net.sf.ehcache.CacheException {
132         manager = CacheManager.getInstance();
133         Ehcache ehcache = new net.sf.ehcache.Cache("name", 10, MemoryStoreEvictionPolicy.LFU,
134                 false, null, false, 10, 10, false, 60, null);
135         manager.registerCache("test", new JCache(ehcache, null));
136         Cache cache = manager.getCache("test");
137         assertNotNull(cache);
138     }
139 
140     /**
141      * Tests that we can use a Cache obtained from CacheManager
142      */
143     public void testUseCache() throws net.sf.ehcache.CacheException {
144         manager = CacheManager.getInstance();
145         Ehcache ehcache = new net.sf.ehcache.Cache("UseCache", 10, MemoryStoreEvictionPolicy.LFU,
146                 false, null, false, 10, 10, false, 60, null);
147         manager.registerCache("test", new JCache(ehcache, null));
148         Cache cache = manager.getCache("test");
149         assertNotNull(cache);
150     }
151 
152 
153 }