@ -1,5 +1,5 @@
/ *
/ *
* Copyright 2002 - 2014 the original author or authors .
* Copyright 2002 - 2015 the original author or authors .
*
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* you may not use this file except in compliance with the License .
@ -18,12 +18,15 @@ package org.springframework.cache.jcache.config;
import java.util.Arrays ;
import java.util.Arrays ;
import org.junit.Rule ;
import org.junit.Test ;
import org.junit.Test ;
import org.junit.rules.ExpectedException ;
import org.springframework.cache.Cache ;
import org.springframework.cache.Cache ;
import org.springframework.cache.CacheManager ;
import org.springframework.cache.CacheManager ;
import org.springframework.cache.annotation.EnableCaching ;
import org.springframework.cache.annotation.EnableCaching ;
import org.springframework.cache.concurrent.ConcurrentMapCache ;
import org.springframework.cache.concurrent.ConcurrentMapCache ;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager ;
import org.springframework.cache.config.SomeKeyGenerator ;
import org.springframework.cache.config.SomeKeyGenerator ;
import org.springframework.cache.interceptor.CacheErrorHandler ;
import org.springframework.cache.interceptor.CacheErrorHandler ;
import org.springframework.cache.interceptor.CacheResolver ;
import org.springframework.cache.interceptor.CacheResolver ;
@ -35,7 +38,6 @@ import org.springframework.cache.interceptor.SimpleKeyGenerator;
import org.springframework.cache.jcache.interceptor.AnnotatedJCacheableService ;
import org.springframework.cache.jcache.interceptor.AnnotatedJCacheableService ;
import org.springframework.cache.jcache.interceptor.DefaultJCacheOperationSource ;
import org.springframework.cache.jcache.interceptor.DefaultJCacheOperationSource ;
import org.springframework.cache.jcache.interceptor.JCacheInterceptor ;
import org.springframework.cache.jcache.interceptor.JCacheInterceptor ;
import org.springframework.cache.jcache.interceptor.SimpleExceptionCacheResolver ;
import org.springframework.cache.support.NoOpCacheManager ;
import org.springframework.cache.support.NoOpCacheManager ;
import org.springframework.cache.support.SimpleCacheManager ;
import org.springframework.cache.support.SimpleCacheManager ;
import org.springframework.context.ApplicationContext ;
import org.springframework.context.ApplicationContext ;
@ -51,6 +53,9 @@ import static org.junit.Assert.*;
* /
* /
public class JCacheJavaConfigTests extends AbstractJCacheAnnotationTests {
public class JCacheJavaConfigTests extends AbstractJCacheAnnotationTests {
@Rule
public final ExpectedException thrown = ExpectedException . none ( ) ;
@Override
@Override
protected ApplicationContext getApplicationContext ( ) {
protected ApplicationContext getApplicationContext ( ) {
return new AnnotationConfigApplicationContext ( EnableCachingConfig . class ) ;
return new AnnotationConfigApplicationContext ( EnableCachingConfig . class ) ;
@ -80,10 +85,7 @@ public class JCacheJavaConfigTests extends AbstractJCacheAnnotationTests {
assertEquals ( SimpleCacheResolver . class , cos . getCacheResolver ( ) . getClass ( ) ) ;
assertEquals ( SimpleCacheResolver . class , cos . getCacheResolver ( ) . getClass ( ) ) ;
assertSame ( context . getBean ( CacheManager . class ) ,
assertSame ( context . getBean ( CacheManager . class ) ,
( ( SimpleCacheResolver ) cos . getCacheResolver ( ) ) . getCacheManager ( ) ) ;
( ( SimpleCacheResolver ) cos . getCacheResolver ( ) ) . getCacheManager ( ) ) ;
assertNotNull ( cos . getExceptionCacheResolver ( ) ) ;
assertNull ( cos . getExceptionCacheResolver ( ) ) ;
assertEquals ( SimpleExceptionCacheResolver . class , cos . getExceptionCacheResolver ( ) . getClass ( ) ) ;
assertSame ( context . getBean ( CacheManager . class ) ,
( ( SimpleExceptionCacheResolver ) cos . getExceptionCacheResolver ( ) ) . getCacheManager ( ) ) ;
context . close ( ) ;
context . close ( ) ;
}
}
@ -99,6 +101,28 @@ public class JCacheJavaConfigTests extends AbstractJCacheAnnotationTests {
context . close ( ) ;
context . close ( ) ;
}
}
@Test
public void exceptionCacheResolverFallbacksToMainOne ( ) {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext (
NoExceptionCacheResolverConfig . class ) ;
try {
DefaultJCacheOperationSource cos = context . getBean ( DefaultJCacheOperationSource . class ) ;
assertSame ( context . getBean ( "cacheResolver" ) , cos . getCacheResolver ( ) ) ;
assertNull ( cos . getExceptionCacheResolver ( ) ) ;
JCacheableService < ? > service = context . getBean ( JCacheableService . class ) ;
service . cache ( "id" ) ;
// This call requires the cache manager to be set
thrown . expect ( IllegalStateException . class ) ;
service . cacheWithException ( "test" , false ) ;
}
finally {
context . close ( ) ;
}
}
@Configuration
@Configuration
@EnableCaching
@EnableCaching
@ -200,4 +224,20 @@ public class JCacheJavaConfigTests extends AbstractJCacheAnnotationTests {
}
}
}
}
@Configuration
@EnableCaching
static class NoExceptionCacheResolverConfig extends JCacheConfigurerSupport {
@Override
@Bean
public CacheResolver cacheResolver ( ) {
return new NamedCacheResolver ( new ConcurrentMapCacheManager ( ) , "default" ) ;
}
@Bean
public JCacheableService < ? > cacheableService ( ) {
return new AnnotatedJCacheableService ( new ConcurrentMapCache ( "default" ) ) ;
}
}
}
}