001: /**
002: * Copyright 2003-2007 Luck Consulting Pty Ltd
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package net.sf.ehcache.exceptionhandler;
016:
017: import junit.framework.TestCase;
018: import net.sf.ehcache.CacheManager;
019: import net.sf.ehcache.Ehcache;
020: import net.sf.ehcache.AbstractCacheTest;
021: import net.sf.ehcache.CacheException;
022: import net.sf.ehcache.loader.ExceptionThrowingLoader;
023: import net.sf.ehcache.event.CountingCacheEventListener;
024:
025: /**
026: * @author <a href="mailto:gluck@gregluck.com">Greg Luck</a>
027: * @version $Id: CacheExceptionHandlerTest.java 530 2007-08-06 10:21:54Z gregluck $
028: */
029: public class CacheExceptionHandlerTest extends TestCase {
030:
031: /**
032: * manager
033: */
034: protected CacheManager manager;
035: /**
036: * the cache name we wish to test
037: */
038: protected String cacheName = "exceptionHandlingCache";
039: /**
040: * the cache we wish to test
041: */
042: protected Ehcache cache;
043:
044: /**
045: * {@inheritDoc}
046: *
047: * @throws Exception
048: */
049: protected void setUp() throws Exception {
050: CountingCacheEventListener.resetCounters();
051: manager = CacheManager.create(AbstractCacheTest.TEST_CONFIG_DIR
052: + "ehcache.xml");
053: cache = manager.getEhcache(cacheName);
054: cache.removeAll();
055: }
056:
057: /**
058: * {@inheritDoc}
059: *
060: * @throws Exception
061: */
062: protected void tearDown() throws Exception {
063: CountingExceptionHandler.resetCounters();
064: manager.shutdown();
065: }
066:
067: /**
068: * Test a cache which has been configured to have a CountingExceptionHandler configured
069: */
070: public void testConfiguredCache() {
071: manager.removeCache("exceptionHandlingCache");
072: //Would normally throw an IllegalStateException
073: cache.get("key1");
074:
075: assertEquals(1, CountingExceptionHandler.HANDLED_EXCEPTIONS
076: .size());
077: assertEquals(
078: null,
079: ((CountingExceptionHandler.HandledException) CountingExceptionHandler.HANDLED_EXCEPTIONS
080: .get(0)).getKey());
081: assertEquals(
082: IllegalStateException.class,
083: ((CountingExceptionHandler.HandledException) CountingExceptionHandler.HANDLED_EXCEPTIONS
084: .get(0)).getException().getClass());
085: }
086:
087: /**
088: * Test a cache which has been configured to have an ExceptionThrowingLoader screw up loading.
089: * This one should have a key set.
090: */
091: public void testKeyWithConfiguredCache() {
092:
093: cache.setCacheLoader(new ExceptionThrowingLoader());
094: cache.getWithLoader("key1", null, null);
095:
096: assertEquals(1, CountingExceptionHandler.HANDLED_EXCEPTIONS
097: .size());
098: assertEquals(
099: "key1",
100: ((CountingExceptionHandler.HandledException) CountingExceptionHandler.HANDLED_EXCEPTIONS
101: .get(0)).getKey());
102: assertEquals(
103: CacheException.class,
104: ((CountingExceptionHandler.HandledException) CountingExceptionHandler.HANDLED_EXCEPTIONS
105: .get(0)).getException().getClass());
106: }
107:
108: /**
109: * Double proxy test
110: */
111: public void testCacheExceptionHandler() {
112: Ehcache proxiedCache = ExceptionHandlingDynamicCacheProxy
113: .createProxy(cache);
114:
115: //Would normally throw an IllegalArgumentException
116: proxiedCache.put(null);
117:
118: assertEquals(1, CountingExceptionHandler.HANDLED_EXCEPTIONS
119: .size());
120: assertEquals(
121: null,
122: ((CountingExceptionHandler.HandledException) CountingExceptionHandler.HANDLED_EXCEPTIONS
123: .get(0)).getKey());
124: assertEquals(
125: IllegalArgumentException.class,
126: ((CountingExceptionHandler.HandledException) CountingExceptionHandler.HANDLED_EXCEPTIONS
127: .get(0)).getException().getClass());
128: }
129:
130: /**
131: * Test some gnarly parsing code
132: */
133: public void testKeyExtraction() {
134:
135: String testMessage = "For key 1234";
136: String key = ExceptionHandlingDynamicCacheProxy
137: .extractKey(testMessage);
138: assertEquals("1234", key);
139:
140: testMessage = "key 1234";
141: key = ExceptionHandlingDynamicCacheProxy
142: .extractKey(testMessage);
143: assertEquals("1234", key);
144:
145: testMessage = null;
146: key = ExceptionHandlingDynamicCacheProxy
147: .extractKey(testMessage);
148: assertEquals(null, key);
149:
150: testMessage = "";
151: key = ExceptionHandlingDynamicCacheProxy
152: .extractKey(testMessage);
153: assertEquals(null, key);
154:
155: testMessage = "key 1234 ";
156: key = ExceptionHandlingDynamicCacheProxy
157: .extractKey(testMessage);
158: assertEquals("1234", key);
159:
160: testMessage = "key 1234 .";
161: key = ExceptionHandlingDynamicCacheProxy
162: .extractKey(testMessage);
163: assertEquals("1234", key);
164:
165: testMessage = "key .";
166: key = ExceptionHandlingDynamicCacheProxy
167: .extractKey(testMessage);
168: assertEquals(".", key);
169:
170: testMessage = "key";
171: key = ExceptionHandlingDynamicCacheProxy
172: .extractKey(testMessage);
173: assertEquals(null, key);
174:
175: }
176:
177: }
|