Skip to content

Commit 2fe3108

Browse files
committed
Merge pull request #1292 from dbrimley:master
* pr/1292: Polish Polish contribution Honour ErrorHandler if `Cache.put` fails
2 parents 7796c4d + 85b20aa commit 2fe3108

File tree

2 files changed

+73
-17
lines changed

2 files changed

+73
-17
lines changed

spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheResultInterceptor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ protected Object invoke(CacheOperationInvocationContext<CacheResultOperation> co
5959

6060
try {
6161
Object invocationResult = invoker.invoke();
62-
cache.put(cacheKey, invocationResult);
62+
doPut(cache, cacheKey, invocationResult);
6363
return invocationResult;
6464
}
6565
catch (CacheOperationInvoker.ThrowableWrapper ex) {
@@ -88,7 +88,7 @@ protected void cacheException(Cache exceptionCache, ExceptionTypeFilter filter,
8888
return;
8989
}
9090
if (filter.match(ex.getClass())) {
91-
exceptionCache.put(cacheKey, ex);
91+
doPut(exceptionCache, cacheKey, ex);
9292
}
9393
}
9494

spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/JCacheErrorHandlerTests.java

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.springframework.context.annotation.Bean;
4242
import org.springframework.context.annotation.Configuration;
4343

44+
import static org.junit.Assert.*;
4445
import static org.mockito.BDDMockito.*;
4546

4647
/**
@@ -53,55 +54,94 @@ public class JCacheErrorHandlerTests {
5354

5455
private Cache cache;
5556

57+
private Cache errorCache;
58+
5659
private CacheErrorHandler errorHandler;
5760

5861
private SimpleService simpleService;
5962

6063
@Before
6164
public void setup() {
62-
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
65+
AnnotationConfigApplicationContext context =
66+
new AnnotationConfigApplicationContext(Config.class);
6367
this.cache = context.getBean("mockCache", Cache.class);
68+
this.errorCache = context.getBean("mockErrorCache", Cache.class);
6469
this.errorHandler = context.getBean(CacheErrorHandler.class);
6570
this.simpleService = context.getBean(SimpleService.class);
6671
}
6772

6873
@Test
6974
public void getFail() {
70-
UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on get");
75+
UnsupportedOperationException exception =
76+
new UnsupportedOperationException("Test exception on get");
77+
Object key = SimpleKeyGenerator.generateKey(0L);
78+
willThrow(exception).given(this.cache).get(key);
79+
80+
this.simpleService.get(0L);
81+
verify(this.errorHandler).handleCacheGetError(exception, this.cache, key);
82+
}
83+
84+
@Test
85+
public void getPutNewElementFail() {
86+
UnsupportedOperationException exception =
87+
new UnsupportedOperationException("Test exception on put");
7188
Object key = SimpleKeyGenerator.generateKey(0L);
72-
willThrow(exception).given(cache).get(key);
89+
given(this.cache.get(key)).willReturn(null);
90+
willThrow(exception).given(this.cache).put(key, 0L);
7391

7492
this.simpleService.get(0L);
75-
verify(errorHandler).handleCacheGetError(exception, cache, key);
93+
verify(this.errorHandler).handleCachePutError(exception, this.cache, key, 0L);
94+
}
95+
96+
@Test
97+
public void getFailPutExceptionFail() {
98+
UnsupportedOperationException exceptionOnPut =
99+
new UnsupportedOperationException("Test exception on put");
100+
Object key = SimpleKeyGenerator.generateKey(0L);
101+
given(this.cache.get(key)).willReturn(null);
102+
willThrow(exceptionOnPut).given(this.errorCache).put(key,
103+
SimpleService.TEST_EXCEPTION);
104+
105+
try {
106+
this.simpleService.getFail(0L);
107+
}
108+
catch (IllegalStateException ex) {
109+
assertEquals("Test exception", ex.getMessage());
110+
}
111+
verify(this.errorHandler).handleCachePutError(exceptionOnPut,
112+
this.errorCache, key, SimpleService.TEST_EXCEPTION);
76113
}
77114

78115
@Test
79116
public void putFail() {
80-
UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on put");
117+
UnsupportedOperationException exception =
118+
new UnsupportedOperationException("Test exception on put");
81119
Object key = SimpleKeyGenerator.generateKey(0L);
82-
willThrow(exception).given(cache).put(key, 234L);
120+
willThrow(exception).given(this.cache).put(key, 234L);
83121

84122
this.simpleService.put(0L, 234L);
85-
verify(errorHandler).handleCachePutError(exception, cache, key, 234L);
123+
verify(this.errorHandler).handleCachePutError(exception, this.cache, key, 234L);
86124
}
87125

88126
@Test
89127
public void evictFail() {
90-
UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on evict");
128+
UnsupportedOperationException exception =
129+
new UnsupportedOperationException("Test exception on evict");
91130
Object key = SimpleKeyGenerator.generateKey(0L);
92-
willThrow(exception).given(cache).evict(key);
131+
willThrow(exception).given(this.cache).evict(key);
93132

94133
this.simpleService.evict(0L);
95-
verify(errorHandler).handleCacheEvictError(exception, cache, key);
134+
verify(this.errorHandler).handleCacheEvictError(exception, this.cache, key);
96135
}
97136

98137
@Test
99138
public void clearFail() {
100-
UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on evict");
101-
willThrow(exception).given(cache).clear();
139+
UnsupportedOperationException exception =
140+
new UnsupportedOperationException("Test exception on evict");
141+
willThrow(exception).given(this.cache).clear();
102142

103143
this.simpleService.clear();
104-
verify(errorHandler).handleCacheClearError(exception, cache);
144+
verify(this.errorHandler).handleCacheClearError(exception, this.cache);
105145
}
106146

107147

@@ -113,7 +153,7 @@ static class Config extends JCacheConfigurerSupport {
113153
@Override
114154
public CacheManager cacheManager() {
115155
SimpleCacheManager cacheManager = new SimpleCacheManager();
116-
cacheManager.setCaches(Arrays.asList(mockCache()));
156+
cacheManager.setCaches(Arrays.asList(mockCache(), mockErrorCache()));
117157
return cacheManager;
118158
}
119159

@@ -135,15 +175,31 @@ public Cache mockCache() {
135175
return cache;
136176
}
137177

178+
@Bean
179+
public Cache mockErrorCache() {
180+
Cache cache = mock(Cache.class);
181+
given(cache.getName()).willReturn("error");
182+
return cache;
183+
}
184+
138185
}
139186

140187
@CacheDefaults(cacheName = "test")
141188
public static class SimpleService {
189+
190+
private static final IllegalStateException TEST_EXCEPTION =
191+
new IllegalStateException("Test exception");
192+
142193
private AtomicLong counter = new AtomicLong();
143194

144195
@CacheResult
145196
public Object get(long id) {
146-
return counter.getAndIncrement();
197+
return this.counter.getAndIncrement();
198+
}
199+
200+
@CacheResult(exceptionCacheName = "error")
201+
public Object getFail(long id) {
202+
throw TEST_EXCEPTION;
147203
}
148204

149205
@CachePut

0 commit comments

Comments
 (0)