Skip to content

Commit 15c3cdd

Browse files
committed
Fix NoOpCache handling of get(key,callable)
This commit fixes the method that takes a Callable to actually always invoke it rather than returning null. Issue: SPR-14445
1 parent 629b95b commit 15c3cdd

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

spring-context/src/main/java/org/springframework/cache/support/NoOpCacheManager.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,12 @@ public <T> T get(Object key, Class<T> type) {
102102

103103
@Override
104104
public <T> T get(Object key, Callable<T> valueLoader) {
105-
return null;
105+
try {
106+
return valueLoader.call();
107+
}
108+
catch (Exception ex) {
109+
throw new ValueRetrievalException(key, valueLoader, ex);
110+
}
106111
}
107112

108113
@Override

spring-context/src/test/java/org/springframework/cache/NoOpCacheManagerTests.java

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,33 @@
1818

1919
import java.util.UUID;
2020

21-
import org.junit.Before;
2221
import org.junit.Test;
2322

2423
import org.springframework.cache.support.NoOpCacheManager;
2524

2625
import static org.junit.Assert.*;
2726

27+
/**
28+
* Tests for {@link NoOpCacheManager}.
29+
*
30+
* @author Costin Leau
31+
* @author Stephane Nicoll
32+
*/
2833
public class NoOpCacheManagerTests {
2934

30-
private CacheManager manager;
31-
32-
@Before
33-
public void setup() {
34-
manager = new NoOpCacheManager();
35-
}
35+
private final CacheManager manager = new NoOpCacheManager();
3636

3737
@Test
3838
public void testGetCache() throws Exception {
39-
Cache cache = manager.getCache("bucket");
39+
Cache cache = this.manager.getCache("bucket");
4040
assertNotNull(cache);
41-
assertSame(cache, manager.getCache("bucket"));
41+
assertSame(cache, this.manager.getCache("bucket"));
4242
}
4343

4444
@Test
4545
public void testNoOpCache() throws Exception {
46-
String name = UUID.randomUUID().toString();
47-
Cache cache = manager.getCache(name);
46+
String name = createRandomKey();
47+
Cache cache = this.manager.getCache(name);
4848
assertEquals(name, cache.getName());
4949
Object key = new Object();
5050
cache.put(key, new Object());
@@ -56,8 +56,37 @@ public void testNoOpCache() throws Exception {
5656
@Test
5757
public void testCacheName() throws Exception {
5858
String name = "bucket";
59-
assertFalse(manager.getCacheNames().contains(name));
60-
manager.getCache(name);
61-
assertTrue(manager.getCacheNames().contains(name));
59+
assertFalse(this.manager.getCacheNames().contains(name));
60+
this.manager.getCache(name);
61+
assertTrue(this.manager.getCacheNames().contains(name));
62+
}
63+
64+
@Test
65+
public void testCacheCallable() throws Exception {
66+
String name = createRandomKey();
67+
Cache cache = this.manager.getCache(name);
68+
Object returnValue = new Object();
69+
Object value = cache.get(new Object(), () -> returnValue);
70+
assertEquals(returnValue, value);
6271
}
72+
73+
@Test
74+
public void testCacheGetCallableFail() {
75+
Cache cache = this.manager.getCache(createRandomKey());
76+
String key = createRandomKey();
77+
try {
78+
cache.get(key, () -> {
79+
throw new UnsupportedOperationException("Expected exception");
80+
});
81+
}
82+
catch (Cache.ValueRetrievalException ex) {
83+
assertNotNull(ex.getCause());
84+
assertEquals(UnsupportedOperationException.class, ex.getCause().getClass());
85+
}
86+
}
87+
88+
private String createRandomKey() {
89+
return UUID.randomUUID().toString();
90+
}
91+
6392
}

0 commit comments

Comments
 (0)