|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2014 the original author or authors. |
| 2 | + * Copyright 2002-2015 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
18 | 18 |
|
19 | 19 | import com.google.common.cache.CacheBuilder;
|
20 | 20 | import com.google.common.cache.CacheLoader;
|
| 21 | +import com.google.common.util.concurrent.UncheckedExecutionException; |
| 22 | +import org.junit.Rule; |
21 | 23 | import org.junit.Test;
|
| 24 | +import org.junit.rules.ExpectedException; |
22 | 25 |
|
23 | 26 | import org.springframework.cache.Cache;
|
24 | 27 | import org.springframework.cache.CacheManager;
|
|
32 | 35 | */
|
33 | 36 | public class GuavaCacheManagerTests {
|
34 | 37 |
|
| 38 | + @Rule |
| 39 | + public final ExpectedException thrown = ExpectedException.none(); |
| 40 | + |
35 | 41 | @Test
|
36 | 42 | public void testDynamicMode() {
|
37 | 43 | CacheManager cm = new GuavaCacheManager();
|
@@ -150,6 +156,28 @@ public void setCacheNameNullRestoreDynamicMode() {
|
150 | 156 | assertNotNull(cm.getCache("someCache"));
|
151 | 157 | }
|
152 | 158 |
|
| 159 | + @Test |
| 160 | + public void cacheLoaderUseLoadingCache() { |
| 161 | + GuavaCacheManager cm = new GuavaCacheManager("c1"); |
| 162 | + cm.setCacheLoader(new CacheLoader<Object, Object>() { |
| 163 | + @Override |
| 164 | + public Object load(Object key) throws Exception { |
| 165 | + if ("ping".equals(key)) { |
| 166 | + return "pong"; |
| 167 | + } |
| 168 | + throw new IllegalArgumentException("I only know ping"); |
| 169 | + } |
| 170 | + }); |
| 171 | + Cache cache1 = cm.getCache("c1"); |
| 172 | + Cache.ValueWrapper value = cache1.get("ping"); |
| 173 | + assertNotNull(value); |
| 174 | + assertEquals("pong", value.get()); |
| 175 | + |
| 176 | + thrown.expect(UncheckedExecutionException.class); |
| 177 | + thrown.expectMessage("I only know ping"); |
| 178 | + assertNull(cache1.get("foo")); |
| 179 | + } |
| 180 | + |
153 | 181 | @SuppressWarnings("unchecked")
|
154 | 182 | private CacheLoader<Object, Object> mockCacheLoader() {
|
155 | 183 | return mock(CacheLoader.class);
|
|
0 commit comments