1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| package pub.wii.cook.springboot.cache;
import com.google.common.collect.Lists; import lombok.SneakyThrows; import org.junit.jupiter.api.Test; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import pub.wii.cook.springboot.config.CookSpringBootConfiguration;
import javax.annotation.Resource;
import java.util.ArrayList; import java.util.List;
import static org.junit.jupiter.api.Assertions.*; import static pub.wii.cook.springboot.cache.CustomCacheManager.CACHE_NAME;
@ContextConfiguration(classes = CookSpringBootConfiguration.class) @SpringJUnitConfig(classes = CookSpringBootConfiguration.class) class CacheableServiceTest {
interface ICache { List<Integer> cacheWithEmpty(List<Integer> echo);
List<Integer> cacheWithoutEmpty(List<Integer> echo); }
@Configuration @EnableCaching static class Config { static class ICacheImpl implements ICache {
@Cacheable( cacheManager = CACHE_NAME, cacheNames = CACHE_NAME, keyGenerator = "customKeyGenerator" ) @Override public List<Integer> cacheWithEmpty(List<Integer> echo) { return echo; }
@Cacheable( cacheManager = CACHE_NAME, cacheNames = CACHE_NAME, keyGenerator = "customKeyGenerator", unless = "#result == null or #result.size() == 0" ) @Override public List<Integer> cacheWithoutEmpty(List<Integer> echo) { return echo; } }
@Bean ICache iCache() { return new ICacheImpl(); } }
@Resource(name = CACHE_NAME) CacheManager cacheManager;
@Resource ICache iCache;
@SuppressWarnings("ConstantConditions") @SneakyThrows @Test void cache() { List<Integer> nonEmpty = Lists.newArrayList(1, 2, 3); List<Integer> empty = new ArrayList<>(); List<Integer> nil = null; Cache cache = cacheManager.getCache(CACHE_NAME); KeyGenerator kg = new CustomKeyGenerator(); assertNotNull(cache);
iCache.cacheWithEmpty(nonEmpty); iCache.cacheWithEmpty(empty); iCache.cacheWithEmpty(nil); iCache.cacheWithoutEmpty(nonEmpty); iCache.cacheWithoutEmpty(empty); iCache.cacheWithoutEmpty(nil);
assertEquals(cache.get(genKeyWithEmpty(kg, nonEmpty)).get(), nonEmpty); assertEquals(cache.get(genKeyWithEmpty(kg, empty)).get(), empty); assertEquals(cache.get(genKeyWithEmpty(kg, nil)).get(), nil); assertEquals(cache.get(genKeyWithoutEmpty(kg, nonEmpty)).get(), nonEmpty); assertEquals(cache.get(genKeyWithoutEmpty(kg, empty)), nil); assertEquals(cache.get(genKeyWithoutEmpty(kg, nil)), nil); }
@SneakyThrows Object genKeyWithEmpty(KeyGenerator kg, List<Integer> arg) { return kg.generate(new Config.ICacheImpl(), ICache.class.getMethod("cacheWithEmpty", List.class), arg); }
@SneakyThrows Object genKeyWithoutEmpty(KeyGenerator kg, List<Integer> arg) { return kg.generate(new Config.ICacheImpl(), ICache.class.getMethod("cacheWithoutEmpty", List.class), arg); } }
|