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.exceptionhandler;
18
19
20 import net.sf.ehcache.AbstractCacheTest;
21 import net.sf.ehcache.CacheException;
22 import net.sf.ehcache.CacheManager;
23 import net.sf.ehcache.Ehcache;
24 import net.sf.ehcache.event.CountingCacheEventListener;
25 import net.sf.ehcache.loader.CacheLoader;
26 import net.sf.ehcache.loader.ExceptionThrowingLoader;
27 import org.junit.After;
28
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.fail;
31
32 import org.junit.Before;
33 import org.junit.Test;
34
35 import java.util.ArrayList;
36 import java.util.List;
37
38 /***
39 * @author <a href="mailto:gluck@gregluck.com">Greg Luck</a>
40 * @version $Id: CacheExceptionHandlerTest.html 13146 2011-08-01 17:12:39Z oletizi $
41 */
42 public class CacheExceptionHandlerTest {
43
44 /***
45 * manager
46 */
47 protected CacheManager manager;
48 /***
49 * the cache name we wish to test
50 */
51 protected String cacheName = "exceptionHandlingCache";
52 /***
53 * the cache we wish to test
54 */
55 protected Ehcache cache;
56
57 /***
58 * {@inheritDoc}
59 *
60 * @throws Exception
61 */
62 @Before
63 public void setUp() throws Exception {
64 CountingCacheEventListener.resetCounters();
65 manager = CacheManager.create(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache.xml");
66 cache = manager.getEhcache(cacheName);
67 cache.removeAll();
68 }
69
70
71 /***
72 * {@inheritDoc}
73 *
74 * @throws Exception
75 */
76 @After
77 public void tearDown() throws Exception {
78 CountingExceptionHandler.resetCounters();
79 manager.shutdown();
80 }
81
82 /***
83 * Test a cache which has been configured to have a CountingExceptionHandler configured
84 */
85 @Test
86 public void testConfiguredCache() {
87 manager.removeCache("exceptionHandlingCache");
88
89 cache.get("key1");
90
91 assertEquals(1, CountingExceptionHandler.HANDLED_EXCEPTIONS.size());
92 assertEquals(null, ((CountingExceptionHandler.HandledException) CountingExceptionHandler.HANDLED_EXCEPTIONS.get(0)).getKey());
93 assertEquals(IllegalStateException.class, ((CountingExceptionHandler.HandledException) CountingExceptionHandler.HANDLED_EXCEPTIONS
94 .get(0)).getException().getClass());
95 }
96
97 /***
98 * Test a cache which has been configured to have an ExceptionThrowingLoader screw up loading.
99 * This one should have a key set.
100 */
101 @Test
102 public void testKeyWithConfiguredCache() {
103
104 List<CacheLoader> loaders = new ArrayList<CacheLoader>(cache.getRegisteredCacheLoaders());
105 for (CacheLoader loader : loaders) {
106 cache.unregisterCacheLoader(loader);
107 }
108
109 cache.registerCacheLoader(new ExceptionThrowingLoader());
110 cache.getWithLoader("key1", null, null);
111
112 assertEquals(1, CountingExceptionHandler.HANDLED_EXCEPTIONS.size());
113 assertEquals("key1", ((CountingExceptionHandler.HandledException) CountingExceptionHandler.HANDLED_EXCEPTIONS.get(0)).getKey());
114 assertEquals(CacheException.class, ((CountingExceptionHandler.HandledException) CountingExceptionHandler.HANDLED_EXCEPTIONS
115 .get(0)).getException().getClass());
116 }
117
118 /***
119 * Double proxy test
120 */
121 @Test
122 public void testCacheExceptionHandler() {
123 Ehcache proxiedCache = ExceptionHandlingDynamicCacheProxy.createProxy(cache);
124
125 List<CacheLoader> loaders = new ArrayList<CacheLoader>(cache.getRegisteredCacheLoaders());
126 for (CacheLoader loader : loaders) {
127 cache.unregisterCacheLoader(loader);
128 }
129 cache.registerCacheLoader(new CustomExceptionThrowingLoader());
130 proxiedCache.getWithLoader("key1", null, null);
131
132
133
134
135
136 assertEquals(1, CountingExceptionHandler.HANDLED_EXCEPTIONS.size());
137 assertEquals("key1", ((CountingExceptionHandler.HandledException) CountingExceptionHandler.HANDLED_EXCEPTIONS.get(0)).getKey());
138 assertEquals(CacheException.class, ((CountingExceptionHandler.HandledException) CountingExceptionHandler.HANDLED_EXCEPTIONS
139 .get(0)).getException().getClass());
140 }
141
142
143 /***
144 * Test some gnarly parsing code
145 */
146 @Test
147 public void testKeyExtraction() {
148
149 String testMessage = "For key 1234";
150 String key = ExceptionHandlingDynamicCacheProxy.extractKey(testMessage);
151 assertEquals("1234", key);
152
153 testMessage = "key 1234";
154 key = ExceptionHandlingDynamicCacheProxy.extractKey(testMessage);
155 assertEquals("1234", key);
156
157 testMessage = null;
158 key = ExceptionHandlingDynamicCacheProxy.extractKey(testMessage);
159 assertEquals(null, key);
160
161 testMessage = "";
162 key = ExceptionHandlingDynamicCacheProxy.extractKey(testMessage);
163 assertEquals(null, key);
164
165 testMessage = "key 1234 ";
166 key = ExceptionHandlingDynamicCacheProxy.extractKey(testMessage);
167 assertEquals("1234", key);
168
169 testMessage = "key 1234 .";
170 key = ExceptionHandlingDynamicCacheProxy.extractKey(testMessage);
171 assertEquals("1234", key);
172
173 testMessage = "key .";
174 key = ExceptionHandlingDynamicCacheProxy.extractKey(testMessage);
175 assertEquals(".", key);
176
177 testMessage = "key";
178 key = ExceptionHandlingDynamicCacheProxy.extractKey(testMessage);
179 assertEquals(null, key);
180
181 }
182
183 /***
184 * Tests that the exception thrown by a configured loader, is
185 * actually passed on to exception handler
186 */
187 @Test
188 public void testExceptionThrown() {
189
190 List<CacheLoader> loaders = new ArrayList<CacheLoader>(cache.getRegisteredCacheLoaders());
191 for (CacheLoader loader : loaders) {
192 cache.unregisterCacheLoader(loader);
193 }
194 cache.registerCacheLoader(new CustomExceptionThrowingLoader());
195
196 cache.getWithLoader("key1", null, null);
197
198 assertEquals(1, CountingExceptionHandler.HANDLED_EXCEPTIONS.size());
199 assertEquals("key1", ((CountingExceptionHandler.HandledException) CountingExceptionHandler.HANDLED_EXCEPTIONS.get(0)).getKey());
200
201
202 Class expectedExceptionClass = UnsupportedOperationException.class;
203
204 Exception e = ((CountingExceptionHandler.HandledException) CountingExceptionHandler.HANDLED_EXCEPTIONS
205 .get(0)).getException();
206
207 Throwable cause = e;
208 boolean foundExceptionInChain = false;
209
210
211
212 while ((cause = cause.getCause()) != null) {
213
214 if (cause.getClass().equals(expectedExceptionClass)) {
215 foundExceptionInChain = true;
216 break;
217 }
218 }
219
220 if (!foundExceptionInChain) {
221 fail();
222 }
223
224
225 }
226 }