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 net.sf.ehcache.AbstractCacheTest;
20  import net.sf.jsr107cache.Cache;
21  import net.sf.jsr107cache.CacheException;
22  import net.sf.jsr107cache.CacheManager;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  /**
28   * @author Greg Luck
29   * @version $Id: JCacheFactoryTest.java 512 2007-07-10 09:18:45Z gregluck $
30   */
31  public class JCacheFactoryTest extends AbstractCacheTest {
32  
33  
34      /**
35       * Gets the sample cache 1
36       * <cache name="sampleCache1"
37       * maxElementsInMemory="10000"
38       * maxElementsOnDisk="1000"
39       * eternal="false"
40       * timeToIdleSeconds="360"
41       * timeToLiveSeconds="1000"
42       * overflowToDisk="true"
43       * memoryStoreEvictionPolicy="LRU">
44       * <cacheEventListenerFactory class="net.sf.ehcache.event.NullCacheEventListenerFactory"/>
45       * </cache>
46       */
47      public void testFactoryUsingCompleteEnvironment() throws CacheException {
48  
49          //sampleCache1
50          Map env = new HashMap();
51          env.put("name", "test1factory");
52          env.put("maxElementsInMemory", "10000");
53          env.put("maxElementsOnDisk", "1000");
54          env.put("memoryStoreEvictionPolicy", "LRU");
55          env.put("overflowToDisk", "true");
56          env.put("eternal", "false");
57          env.put("timeToLiveSeconds", "1000");
58          env.put("timeToIdleSeconds", "1000");
59          env.put("diskPersistent", "false");
60          env.put("diskExpiryThreadIntervalSeconds", "120");
61          env.put("cacheLoaderFactoryClassName", "net.sf.ehcache.jcache.CountingCacheLoaderFactory");
62          Cache cache = CacheManager.getInstance().getCacheFactory().createCache(env);
63          CacheManager.getInstance().registerCache("factoryTest1", cache);
64          assertEquals(((JCache)cache).getCacheLoader().getClass(), CountingCacheLoader.class);
65          assertNotNull(CacheManager.getInstance().getCache("factoryTest1"));
66      }
67  
68      /**
69       * A Null cacheloader factory string should not attempt to create it.
70       * @throws CacheException
71       */
72      public void testFactoryWithNullCacheLoaderFactory() throws CacheException {
73          Cache cache = CacheManager.getInstance().getCache("test2");
74          if (cache == null) {
75              Map env = new HashMap();
76              env.put("name", "test2");
77              env.put("maxElementsInMemory", "1");
78              env.put("overflowToDisk", "true");
79              env.put("eternal", "false");
80              env.put("timeToLiveSeconds", "1");
81              env.put("timeToIdleSeconds", "0");
82              env.put("cacheLoaderFactoryClassName", null);
83              cache = CacheManager.getInstance().getCacheFactory().createCache(env);
84              CacheManager.getInstance().registerCache("factoryTest2", cache);
85              assertNull(((JCache)cache).getCacheLoader());
86              assertNotNull(CacheManager.getInstance().getCache("factoryTest2"));
87          }
88      }
89  
90      /**
91       * Specifying a CacheLoaderFactory which cannot be found should throw an CacheException
92       * @throws CacheException
93       */
94      public void testFactoryWithWrongCacheLoaderFactory() throws CacheException {
95          Cache cache = CacheManager.getInstance().getCache("test4");
96          if (cache == null) {
97              Map env = new HashMap();
98              env.put("name", "test4");
99              env.put("maxElementsInMemory", "1000");
100             env.put("overflowToDisk", "true");
101             env.put("eternal", "true");
102             env.put("timeToLiveSeconds", "0");
103             env.put("timeToIdleSeconds", "0");
104             env.put("cacheLoaderFactoryClassName", "net.sf.ehcache.jcache.JCacheTest.Typo");
105 
106             try {
107                 cache = CacheManager.getInstance().getCacheFactory().createCache(env);
108             } catch (CacheException e) {
109                 //expected
110             }
111 
112         }
113     }
114 
115 
116 }