|
|
|
|
@ -41,6 +41,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
@@ -41,6 +41,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
|
|
|
|
|
import org.springframework.context.annotation.Bean; |
|
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.*; |
|
|
|
|
import static org.mockito.BDDMockito.*; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -53,6 +54,8 @@ public class JCacheErrorHandlerTests {
@@ -53,6 +54,8 @@ public class JCacheErrorHandlerTests {
|
|
|
|
|
|
|
|
|
|
private Cache cache; |
|
|
|
|
|
|
|
|
|
private Cache errorCache; |
|
|
|
|
|
|
|
|
|
private CacheErrorHandler errorHandler; |
|
|
|
|
|
|
|
|
|
private SimpleService simpleService; |
|
|
|
|
@ -61,6 +64,7 @@ public class JCacheErrorHandlerTests {
@@ -61,6 +64,7 @@ public class JCacheErrorHandlerTests {
|
|
|
|
|
public void setup() { |
|
|
|
|
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class); |
|
|
|
|
this.cache = context.getBean("mockCache", Cache.class); |
|
|
|
|
this.errorCache = context.getBean("mockErrorCache", Cache.class); |
|
|
|
|
this.errorHandler = context.getBean(CacheErrorHandler.class); |
|
|
|
|
this.simpleService = context.getBean(SimpleService.class); |
|
|
|
|
} |
|
|
|
|
@ -75,6 +79,35 @@ public class JCacheErrorHandlerTests {
@@ -75,6 +79,35 @@ public class JCacheErrorHandlerTests {
|
|
|
|
|
verify(errorHandler).handleCacheGetError(exception, cache, key); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void getPutNewElementFail() { |
|
|
|
|
UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on put"); |
|
|
|
|
Object key = SimpleKeyGenerator.generateKey(0L); |
|
|
|
|
given(this.cache.get(key)).willReturn(null); |
|
|
|
|
willThrow(exception).given(this.cache).put(key, 0L); |
|
|
|
|
|
|
|
|
|
this.simpleService.get(0L); |
|
|
|
|
verify(this.errorHandler).handleCachePutError(exception, this.cache, key, 0L); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void getFailPutExceptionFail() { |
|
|
|
|
UnsupportedOperationException exceptionOnPut = new UnsupportedOperationException("Test exception on put"); |
|
|
|
|
Object key = SimpleKeyGenerator.generateKey(0L); |
|
|
|
|
given(this.cache.get(key)).willReturn(null); |
|
|
|
|
willThrow(exceptionOnPut).given(this.errorCache).put(key, |
|
|
|
|
SimpleService.TEST_EXCEPTION); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
this.simpleService.getFail(0L); |
|
|
|
|
} |
|
|
|
|
catch (IllegalStateException ex) { |
|
|
|
|
assertEquals("Test exception", ex.getMessage()); |
|
|
|
|
} |
|
|
|
|
verify(this.errorHandler).handleCachePutError(exceptionOnPut, |
|
|
|
|
this.errorCache, key, SimpleService.TEST_EXCEPTION); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void putFail() { |
|
|
|
|
UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on put"); |
|
|
|
|
@ -113,7 +146,7 @@ public class JCacheErrorHandlerTests {
@@ -113,7 +146,7 @@ public class JCacheErrorHandlerTests {
|
|
|
|
|
@Override |
|
|
|
|
public CacheManager cacheManager() { |
|
|
|
|
SimpleCacheManager cacheManager = new SimpleCacheManager(); |
|
|
|
|
cacheManager.setCaches(Arrays.asList(mockCache())); |
|
|
|
|
cacheManager.setCaches(Arrays.asList(mockCache(), mockErrorCache())); |
|
|
|
|
return cacheManager; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -135,10 +168,21 @@ public class JCacheErrorHandlerTests {
@@ -135,10 +168,21 @@ public class JCacheErrorHandlerTests {
|
|
|
|
|
return cache; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
|
public Cache mockErrorCache() { |
|
|
|
|
Cache cache = mock(Cache.class); |
|
|
|
|
given(cache.getName()).willReturn("error"); |
|
|
|
|
return cache; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@CacheDefaults(cacheName = "test") |
|
|
|
|
public static class SimpleService { |
|
|
|
|
|
|
|
|
|
private static final IllegalStateException TEST_EXCEPTION = |
|
|
|
|
new IllegalStateException("Test exception"); |
|
|
|
|
|
|
|
|
|
private AtomicLong counter = new AtomicLong(); |
|
|
|
|
|
|
|
|
|
@CacheResult |
|
|
|
|
@ -146,6 +190,11 @@ public class JCacheErrorHandlerTests {
@@ -146,6 +190,11 @@ public class JCacheErrorHandlerTests {
|
|
|
|
|
return counter.getAndIncrement(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@CacheResult(exceptionCacheName = "error") |
|
|
|
|
public Object getFail(long id) { |
|
|
|
|
throw TEST_EXCEPTION; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@CachePut |
|
|
|
|
public void put(long id, @CacheValue Object object) { |
|
|
|
|
} |
|
|
|
|
|