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
This commit is contained in:
		
							parent
							
								
									813108a928
								
							
						
					
					
						commit
						ab62edeeaa
					
				| 
						 | 
					@ -102,7 +102,12 @@ public class NoOpCacheManager implements CacheManager {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		@Override
 | 
							@Override
 | 
				
			||||||
		public <T> T get(Object key, Callable<T> valueLoader) {
 | 
							public <T> T get(Object key, Callable<T> valueLoader) {
 | 
				
			||||||
			return null;
 | 
								try {
 | 
				
			||||||
 | 
									return valueLoader.call();
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								catch (Exception ex) {
 | 
				
			||||||
 | 
									throw new ValueRetrievalException(key, valueLoader, ex);
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		@Override
 | 
							@Override
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -18,33 +18,33 @@ package org.springframework.cache;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.UUID;
 | 
					import java.util.UUID;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import org.junit.Before;
 | 
					 | 
				
			||||||
import org.junit.Test;
 | 
					import org.junit.Test;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import org.springframework.cache.support.NoOpCacheManager;
 | 
					import org.springframework.cache.support.NoOpCacheManager;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import static org.junit.Assert.*;
 | 
					import static org.junit.Assert.*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Tests for {@link NoOpCacheManager}.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @author Costin Leau
 | 
				
			||||||
 | 
					 * @author Stephane Nicoll
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
public class NoOpCacheManagerTests {
 | 
					public class NoOpCacheManagerTests {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private CacheManager manager;
 | 
						private final CacheManager manager = new NoOpCacheManager();
 | 
				
			||||||
 | 
					 | 
				
			||||||
	@Before
 | 
					 | 
				
			||||||
	public void setup() {
 | 
					 | 
				
			||||||
		manager = new NoOpCacheManager();
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Test
 | 
						@Test
 | 
				
			||||||
	public void testGetCache() throws Exception {
 | 
						public void testGetCache() throws Exception {
 | 
				
			||||||
		Cache cache = manager.getCache("bucket");
 | 
							Cache cache = this.manager.getCache("bucket");
 | 
				
			||||||
		assertNotNull(cache);
 | 
							assertNotNull(cache);
 | 
				
			||||||
		assertSame(cache, manager.getCache("bucket"));
 | 
							assertSame(cache, this.manager.getCache("bucket"));
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Test
 | 
						@Test
 | 
				
			||||||
	public void testNoOpCache() throws Exception {
 | 
						public void testNoOpCache() throws Exception {
 | 
				
			||||||
		String name = UUID.randomUUID().toString();
 | 
							String name = createRandomKey();
 | 
				
			||||||
		Cache cache = manager.getCache(name);
 | 
							Cache cache = this.manager.getCache(name);
 | 
				
			||||||
		assertEquals(name, cache.getName());
 | 
							assertEquals(name, cache.getName());
 | 
				
			||||||
		Object key = new Object();
 | 
							Object key = new Object();
 | 
				
			||||||
		cache.put(key, new Object());
 | 
							cache.put(key, new Object());
 | 
				
			||||||
| 
						 | 
					@ -56,8 +56,37 @@ public class NoOpCacheManagerTests {
 | 
				
			||||||
	@Test
 | 
						@Test
 | 
				
			||||||
	public void testCacheName() throws Exception {
 | 
						public void testCacheName() throws Exception {
 | 
				
			||||||
		String name = "bucket";
 | 
							String name = "bucket";
 | 
				
			||||||
		assertFalse(manager.getCacheNames().contains(name));
 | 
							assertFalse(this.manager.getCacheNames().contains(name));
 | 
				
			||||||
		manager.getCache(name);
 | 
							this.manager.getCache(name);
 | 
				
			||||||
		assertTrue(manager.getCacheNames().contains(name));
 | 
							assertTrue(this.manager.getCacheNames().contains(name));
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						@Test
 | 
				
			||||||
 | 
						public void testCacheCallable() throws Exception {
 | 
				
			||||||
 | 
							String name = createRandomKey();
 | 
				
			||||||
 | 
							Cache cache = this.manager.getCache(name);
 | 
				
			||||||
 | 
							Object returnValue = new Object();
 | 
				
			||||||
 | 
							Object value = cache.get(new Object(), () -> returnValue);
 | 
				
			||||||
 | 
							assertEquals(returnValue, value);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						@Test
 | 
				
			||||||
 | 
						public void testCacheGetCallableFail() {
 | 
				
			||||||
 | 
							Cache cache = this.manager.getCache(createRandomKey());
 | 
				
			||||||
 | 
							String key = createRandomKey();
 | 
				
			||||||
 | 
							try {
 | 
				
			||||||
 | 
								cache.get(key, () -> {
 | 
				
			||||||
 | 
									throw new UnsupportedOperationException("Expected exception");
 | 
				
			||||||
 | 
								});
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							catch (Cache.ValueRetrievalException ex) {
 | 
				
			||||||
 | 
								assertNotNull(ex.getCause());
 | 
				
			||||||
 | 
								assertEquals(UnsupportedOperationException.class, ex.getCause().getClass());
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private String createRandomKey() {
 | 
				
			||||||
 | 
							return UUID.randomUUID().toString();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue