2
* Copyright 2003-2009 Terracotta, Inc.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package net.sf.ehcache.exceptionhandler;
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
import static org.junit.Assert.assertEquals;
29
import static org.junit.Assert.fail;
30
import org.junit.Before;
31
import org.junit.Test;
33
import java.util.ArrayList;
34
import java.util.List;
37
* @author <a href="mailto:gluck@gregluck.com">Greg Luck</a>
38
* @version $Id: CacheExceptionHandlerTest.java 1012 2009-08-20 04:23:00Z gregluck $
40
public class CacheExceptionHandlerTest {
45
protected CacheManager manager;
47
* the cache name we wish to test
49
protected String cacheName = "exceptionHandlingCache";
51
* the cache we wish to test
53
protected Ehcache cache;
61
public void setUp() throws Exception {
62
CountingCacheEventListener.resetCounters();
63
manager = CacheManager.create(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache.xml");
64
cache = manager.getEhcache(cacheName);
75
public void tearDown() throws Exception {
76
CountingExceptionHandler.resetCounters();
81
* Test a cache which has been configured to have a CountingExceptionHandler configured
84
public void testConfiguredCache() {
85
manager.removeCache("exceptionHandlingCache");
86
//Would normally throw an IllegalStateException
89
assertEquals(1, CountingExceptionHandler.HANDLED_EXCEPTIONS.size());
90
assertEquals(null, ((CountingExceptionHandler.HandledException) CountingExceptionHandler.HANDLED_EXCEPTIONS.get(0)).getKey());
91
assertEquals(IllegalStateException.class, ((CountingExceptionHandler.HandledException) CountingExceptionHandler.HANDLED_EXCEPTIONS
92
.get(0)).getException().getClass());
96
* Test a cache which has been configured to have an ExceptionThrowingLoader screw up loading.
97
* This one should have a key set.
100
public void testKeyWithConfiguredCache() {
102
List<CacheLoader> loaders = new ArrayList<CacheLoader>(cache.getRegisteredCacheLoaders());
103
for (CacheLoader loader : loaders) {
104
cache.unregisterCacheLoader(loader);
107
cache.registerCacheLoader(new ExceptionThrowingLoader());
108
cache.getWithLoader("key1", null, null);
110
assertEquals(1, CountingExceptionHandler.HANDLED_EXCEPTIONS.size());
111
assertEquals("key1", ((CountingExceptionHandler.HandledException) CountingExceptionHandler.HANDLED_EXCEPTIONS.get(0)).getKey());
112
assertEquals(CacheException.class, ((CountingExceptionHandler.HandledException) CountingExceptionHandler.HANDLED_EXCEPTIONS
113
.get(0)).getException().getClass());
120
public void testCacheExceptionHandler() {
121
Ehcache proxiedCache = ExceptionHandlingDynamicCacheProxy.createProxy(cache);
123
List<CacheLoader> loaders = new ArrayList<CacheLoader>(cache.getRegisteredCacheLoaders());
124
for (CacheLoader loader : loaders) {
125
cache.unregisterCacheLoader(loader);
127
cache.registerCacheLoader(new CustomExceptionThrowingLoader());
128
proxiedCache.getWithLoader("key1", null, null);
131
//Would normally throw an IllegalArgumentException
132
// proxiedCache.put(null);
134
assertEquals(1, CountingExceptionHandler.HANDLED_EXCEPTIONS.size());
135
assertEquals("key1", ((CountingExceptionHandler.HandledException) CountingExceptionHandler.HANDLED_EXCEPTIONS.get(0)).getKey());
136
assertEquals(CacheException.class, ((CountingExceptionHandler.HandledException) CountingExceptionHandler.HANDLED_EXCEPTIONS
137
.get(0)).getException().getClass());
142
* Test some gnarly parsing code
145
public void testKeyExtraction() {
147
String testMessage = "For key 1234";
148
String key = ExceptionHandlingDynamicCacheProxy.extractKey(testMessage);
149
assertEquals("1234", key);
151
testMessage = "key 1234";
152
key = ExceptionHandlingDynamicCacheProxy.extractKey(testMessage);
153
assertEquals("1234", key);
156
key = ExceptionHandlingDynamicCacheProxy.extractKey(testMessage);
157
assertEquals(null, key);
160
key = ExceptionHandlingDynamicCacheProxy.extractKey(testMessage);
161
assertEquals(null, key);
163
testMessage = "key 1234 ";
164
key = ExceptionHandlingDynamicCacheProxy.extractKey(testMessage);
165
assertEquals("1234", key);
167
testMessage = "key 1234 .";
168
key = ExceptionHandlingDynamicCacheProxy.extractKey(testMessage);
169
assertEquals("1234", key);
171
testMessage = "key .";
172
key = ExceptionHandlingDynamicCacheProxy.extractKey(testMessage);
173
assertEquals(".", key);
176
key = ExceptionHandlingDynamicCacheProxy.extractKey(testMessage);
177
assertEquals(null, key);
182
* Tests that the exception thrown by a configured loader, is
183
* actually passed on to exception handler
186
public void testExceptionThrown() {
188
List<CacheLoader> loaders = new ArrayList<CacheLoader>(cache.getRegisteredCacheLoaders());
189
for (CacheLoader loader : loaders) {
190
cache.unregisterCacheLoader(loader);
192
cache.registerCacheLoader(new CustomExceptionThrowingLoader());
194
cache.getWithLoader("key1", null, null);
196
assertEquals(1, CountingExceptionHandler.HANDLED_EXCEPTIONS.size());
197
assertEquals("key1", ((CountingExceptionHandler.HandledException) CountingExceptionHandler.HANDLED_EXCEPTIONS.get(0)).getKey());
200
Class expectedExceptionClass = UnsupportedOperationException.class;
202
Exception e = ((CountingExceptionHandler.HandledException) CountingExceptionHandler.HANDLED_EXCEPTIONS
203
.get(0)).getException();
206
boolean foundExceptionInChain = false;
209
//Recurse through the chain
210
while ((cause = cause.getCause()) != null) {
212
if (cause.getClass().equals(expectedExceptionClass)) {
213
foundExceptionInChain = true;
218
if (!foundExceptionInChain) {