1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
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.loader;
18
19
20 import net.sf.ehcache.AbstractCacheTest;
21 import net.sf.ehcache.Cache;
22 import net.sf.ehcache.CacheManager;
23 import net.sf.ehcache.Element;
24 import net.sf.ehcache.Status;
25 import net.sf.ehcache.config.CacheConfiguration;
26 import net.sf.ehcache.extension.TestCacheExtension;
27 import org.junit.After;
28
29 import static org.hamcrest.CoreMatchers.equalTo;
30 import static org.junit.Assert.assertEquals;
31 import static org.junit.Assert.assertNotNull;
32 import static org.junit.Assert.assertNull;
33 import static org.junit.Assert.assertThat;
34 import static org.junit.Assert.assertTrue;
35
36 import org.junit.Before;
37 import org.junit.Test;
38
39 import java.util.Arrays;
40 import java.util.List;
41 import java.util.Map;
42
43 /***
44 * @author <a href="mailto:gluck@gregluck.com">Greg Luck</a>
45 * @version $Id: CacheLoaderTest.html 13146 2011-08-01 17:12:39Z oletizi $
46 */
47 public class CacheLoaderTest {
48
49 /***
50 * manager
51 */
52 protected CacheManager manager;
53
54
55 /***
56 * {@inheritDoc}
57 *
58 * @throws Exception
59 */
60 @Before
61 public void setUp() throws Exception {
62 manager = CacheManager.create(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache-loaderinteractions.xml");
63 }
64
65
66 /***
67 * {@inheritDoc}
68 *
69 * @throws Exception
70 */
71 @After
72 public void tearDown() throws Exception {
73 if (!manager.getStatus().equals(Status.STATUS_SHUTDOWN)) {
74 manager.shutdown();
75 }
76 }
77
78 @Test
79 public void testWorksWithTransactionalCaches() {
80 Cache cache = new Cache(new CacheConfiguration("txLoaderCache", 100)
81 .transactionalMode(CacheConfiguration.TransactionalMode.LOCAL));
82 manager.addCache(cache);
83 manager.getTransactionController().begin();
84 final Element element = cache.getWithLoader(10, new CountingCacheLoader(), null);
85 assertThat((Integer) element.getValue(), equalTo(0));
86 manager.getTransactionController().commit();
87 }
88
89 @Test
90 public void testGetAllWithLoaderExpiredKey() throws Exception {
91 Cache cache = manager.getCache("CCache");
92 cache.put(new Element(1, "one"));
93 Thread.sleep(1100);
94 cache.put(new Element(2, "two"));
95 cache.put(new Element(3, null));
96
97 Map cachedObjects = cache.getAllWithLoader(Arrays.asList(1, 2, 3), null);
98
99 assertTrue(cachedObjects.get(1).toString().equals("C(1)"));
100 assertTrue(cachedObjects.get(2).toString().equals("two"));
101 assertNull(cachedObjects.get(3));
102 assertTrue(cachedObjects.containsKey(3));
103 }
104
105 @Test
106 public void testLoaderChainNullFirst() {
107 Cache cache = manager.getCache("NullLoaderFirstCache");
108 assertNotNull(cache.getWithLoader("key", null, null));
109 NullCountingCacheLoader nullCountingCacheLoader = getNullCountingCacheLoader(cache);
110 assertEquals(1, nullCountingCacheLoader.getLoadCounter());
111 CountingCacheLoader countingCacheLoader = getCountingCacheLoader(cache);
112 assertEquals(1, countingCacheLoader.getLoadCounter());
113 }
114
115 @Test
116 public void testLoaderChainNullLast() {
117 Cache cache = manager.getCache("NullLoaderLastCache");
118 assertNotNull(cache.getWithLoader("key", null, null));
119 CountingCacheLoader countingCacheLoader = getCountingCacheLoader(cache);
120 assertEquals(1, countingCacheLoader.getLoadCounter());
121 NullCountingCacheLoader nullCountingCacheLoader = getNullCountingCacheLoader(cache);
122 assertEquals(0, nullCountingCacheLoader.getLoadCounter());
123 }
124
125 @Test
126 public void testLoaderChainNullBoth() {
127 Cache cache = manager.getCache("NullLoaderTwiceCache");
128 Element element = cache.getWithLoader("key", null, null);
129 assertNull(element);
130 NullCountingCacheLoader nullCountingCacheLoader = getNullCountingCacheLoader(cache);
131 List<CacheLoader> list = cache.getRegisteredCacheLoaders();
132 assertEquals(2, list.size());
133 for (CacheLoader cacheLoader : list) {
134 if (cacheLoader instanceof NullCountingCacheLoader) {
135 nullCountingCacheLoader = (NullCountingCacheLoader) cacheLoader;
136 assertEquals(1, nullCountingCacheLoader.getLoadCounter());
137 }
138 }
139
140 }
141
142 private CountingCacheLoader getCountingCacheLoader(Cache cache) {
143 List<CacheLoader> list = cache.getRegisteredCacheLoaders();
144 for (CacheLoader cacheLoader : list) {
145 if (cacheLoader instanceof CountingCacheLoader) {
146 return (CountingCacheLoader) cacheLoader;
147 }
148 }
149 return null;
150 }
151
152 private NullCountingCacheLoader getNullCountingCacheLoader(Cache cache) {
153 List<CacheLoader> list = cache.getRegisteredCacheLoaders();
154 for (CacheLoader cacheLoader : list) {
155 if (cacheLoader instanceof NullCountingCacheLoader) {
156 return (NullCountingCacheLoader) cacheLoader;
157 }
158 }
159 return null;
160 }
161
162
163 /***
164 * Tests the put listener.
165 */
166 @Test
167 public void testExtensionDirectly() {
168
169 manager.addCache("test");
170 TestCacheExtension testCacheExtension = new TestCacheExtension(manager.getCache("test"), "valueA");
171 assertEquals(Status.STATUS_UNINITIALISED, testCacheExtension.getStatus());
172 assertEquals("valueA", testCacheExtension.getPropertyA());
173
174 testCacheExtension.init();
175 assertEquals(Status.STATUS_ALIVE, testCacheExtension.getStatus());
176
177 testCacheExtension.dispose();
178 assertEquals(Status.STATUS_SHUTDOWN, testCacheExtension.getStatus());
179
180 }
181
182
183 /***
184 * We need to make sure that cloning a default cache results in a new cache with its own
185 * set of cache extensions.
186 */
187 @Test
188 public void testClone() {
189
190
191 manager.addCache("clonedCache");
192 }
193 }