|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright 2002-2014 the original author or authors. | 
|  | 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 org.springframework.cache.jcache.config; | 
|  | 18 | + | 
|  | 19 | +import static org.junit.Assert.*; | 
|  | 20 | + | 
|  | 21 | +import java.io.IOException; | 
|  | 22 | +import java.util.Arrays; | 
|  | 23 | +import java.util.Map; | 
|  | 24 | + | 
|  | 25 | +import org.junit.After; | 
|  | 26 | +import org.junit.Before; | 
|  | 27 | +import org.junit.Test; | 
|  | 28 | + | 
|  | 29 | +import org.springframework.cache.Cache; | 
|  | 30 | +import org.springframework.cache.CacheManager; | 
|  | 31 | +import org.springframework.cache.annotation.EnableCaching; | 
|  | 32 | +import org.springframework.cache.concurrent.ConcurrentMapCache; | 
|  | 33 | +import org.springframework.cache.interceptor.CacheOperationInvoker; | 
|  | 34 | +import org.springframework.cache.jcache.interceptor.AnnotatedJCacheableService; | 
|  | 35 | +import org.springframework.cache.jcache.interceptor.JCacheInterceptor; | 
|  | 36 | +import org.springframework.cache.jcache.interceptor.JCacheOperationSource; | 
|  | 37 | +import org.springframework.cache.support.SimpleCacheManager; | 
|  | 38 | +import org.springframework.context.ConfigurableApplicationContext; | 
|  | 39 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; | 
|  | 40 | +import org.springframework.context.annotation.Bean; | 
|  | 41 | +import org.springframework.context.annotation.Configuration; | 
|  | 42 | + | 
|  | 43 | +/** | 
|  | 44 | + * | 
|  | 45 | + * @author Stephane Nicoll | 
|  | 46 | + */ | 
|  | 47 | +public class JCacheCustomInterceptorTests { | 
|  | 48 | + | 
|  | 49 | +	protected ConfigurableApplicationContext ctx; | 
|  | 50 | + | 
|  | 51 | +	protected JCacheableService<?> cs; | 
|  | 52 | + | 
|  | 53 | +	protected Cache exceptionCache; | 
|  | 54 | + | 
|  | 55 | +	@Before | 
|  | 56 | +	public void setup() { | 
|  | 57 | +		ctx = new AnnotationConfigApplicationContext(EnableCachingConfig.class); | 
|  | 58 | +		cs = ctx.getBean("service", JCacheableService.class); | 
|  | 59 | +		exceptionCache = ctx.getBean("exceptionCache", Cache.class); | 
|  | 60 | +	} | 
|  | 61 | + | 
|  | 62 | +	@After | 
|  | 63 | +	public void tearDown() { | 
|  | 64 | +		ctx.close(); | 
|  | 65 | +	} | 
|  | 66 | + | 
|  | 67 | +	@Test | 
|  | 68 | +	public void onlyOneInterceptorIsAvailable() { | 
|  | 69 | +		Map<String, JCacheInterceptor> interceptors = ctx.getBeansOfType(JCacheInterceptor.class); | 
|  | 70 | +		assertEquals("Only one interceptor should be defined", 1, interceptors.size()); | 
|  | 71 | +		JCacheInterceptor interceptor = interceptors.values().iterator().next(); | 
|  | 72 | +		assertEquals("Custom interceptor not defined", TestCacheInterceptor.class, interceptor.getClass()); | 
|  | 73 | +	} | 
|  | 74 | + | 
|  | 75 | +	@Test | 
|  | 76 | +	public void customInterceptorAppliesWithRuntimeException() { | 
|  | 77 | +		Object o = cs.cacheWithException("id", true); | 
|  | 78 | +		assertEquals(55L, o); // See TestCacheInterceptor | 
|  | 79 | +	} | 
|  | 80 | + | 
|  | 81 | +	@Test | 
|  | 82 | +	public void customInterceptorAppliesWithCheckedException() { | 
|  | 83 | +		try { | 
|  | 84 | +			cs.cacheWithCheckedException("id", true); | 
|  | 85 | +			fail("Should have failed"); | 
|  | 86 | +		} | 
|  | 87 | +		catch (RuntimeException e) { | 
|  | 88 | +			assertNotNull("missing original exception", e.getCause()); | 
|  | 89 | +			assertEquals(IOException.class, e.getCause().getClass()); | 
|  | 90 | +		} | 
|  | 91 | +		catch (Exception e) { | 
|  | 92 | +			fail("Wrong exception type " + e); | 
|  | 93 | +		} | 
|  | 94 | +	} | 
|  | 95 | + | 
|  | 96 | + | 
|  | 97 | +	@Configuration | 
|  | 98 | +	@EnableCaching | 
|  | 99 | +	static class EnableCachingConfig { | 
|  | 100 | + | 
|  | 101 | +		@Bean | 
|  | 102 | +		public CacheManager cacheManager() { | 
|  | 103 | +			SimpleCacheManager cm = new SimpleCacheManager(); | 
|  | 104 | +			cm.setCaches(Arrays.asList( | 
|  | 105 | +					defaultCache(), | 
|  | 106 | +					exceptionCache())); | 
|  | 107 | +			return cm; | 
|  | 108 | +		} | 
|  | 109 | + | 
|  | 110 | +		@Bean | 
|  | 111 | +		public JCacheableService<?> service() { | 
|  | 112 | +			return new AnnotatedJCacheableService(defaultCache()); | 
|  | 113 | +		} | 
|  | 114 | + | 
|  | 115 | +		@Bean | 
|  | 116 | +		public Cache defaultCache() { | 
|  | 117 | +			return new ConcurrentMapCache("default"); | 
|  | 118 | +		} | 
|  | 119 | + | 
|  | 120 | +		@Bean | 
|  | 121 | +		public Cache exceptionCache() { | 
|  | 122 | +			return new ConcurrentMapCache("exception"); | 
|  | 123 | +		} | 
|  | 124 | + | 
|  | 125 | +		@Bean | 
|  | 126 | +		public JCacheInterceptor jCacheInterceptor(JCacheOperationSource cacheOperationSource) { | 
|  | 127 | +			JCacheInterceptor cacheInterceptor = new TestCacheInterceptor(); | 
|  | 128 | +			cacheInterceptor.setCacheOperationSource(cacheOperationSource); | 
|  | 129 | +			return cacheInterceptor; | 
|  | 130 | +		} | 
|  | 131 | +	} | 
|  | 132 | + | 
|  | 133 | +	/** | 
|  | 134 | +	 * A test {@link org.springframework.cache.interceptor.CacheInterceptor} that handles special exception | 
|  | 135 | +	 * types. | 
|  | 136 | +	 */ | 
|  | 137 | +	static class TestCacheInterceptor extends JCacheInterceptor { | 
|  | 138 | + | 
|  | 139 | +		@Override | 
|  | 140 | +		protected Object invokeOperation(CacheOperationInvoker invoker) { | 
|  | 141 | +			try { | 
|  | 142 | +				return super.invokeOperation(invoker); | 
|  | 143 | +			} | 
|  | 144 | +			catch (CacheOperationInvoker.ThrowableWrapper e) { | 
|  | 145 | +				Throwable original = e.getOriginal(); | 
|  | 146 | +				if (original.getClass() == UnsupportedOperationException.class) { | 
|  | 147 | +					return 55L; | 
|  | 148 | +				} | 
|  | 149 | +				else { | 
|  | 150 | +					throw new CacheOperationInvoker.ThrowableWrapper( | 
|  | 151 | +							new RuntimeException("wrapping original", original)); | 
|  | 152 | +				} | 
|  | 153 | +			} | 
|  | 154 | +		} | 
|  | 155 | +	} | 
|  | 156 | + | 
|  | 157 | +} | 
0 commit comments