View Javadoc

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