CacheBase implements ApplicationContextAware, InitializingBean { protected void addCache(final Cache cache) { if (cache == null) { getLogger().warn("One of the cache is null"); return; } if (caches.put(cache.getName(), cache) != null) { String errorMsg = "There are two or more caches with the same name '" + cache.getName() + "'"; getLogger().error(errorMsg); throw new IllegalStateException(errorMsg); } for (String alias : cache.getAliases()) { if (caches.containsKey(alias)) { String errorMsg = String.format("The cache with name '%s' uses alias '%s' already defined by cache with name '%s'", cache.getName(), alias, caches.get(alias).getName()); getLogger().error(errorMsg); throw new IllegalStateException(errorMsg); } else { caches.put(alias, cache); } } }  @Override void afterPropertiesSet(); @Override void setApplicationContext(final ApplicationContext applicationContext); void setCacheKeyBuilder(final CacheKeyBuilder cacheKeyBuilder); CacheKeyBuilder getCacheKeyBuilder(); BridgeMethodMappingStore getBridgeMethodMappingStore(); void setBridgeMethodMappingStore(final BridgeMethodMappingStore bridgeMethodMappingStore); Cache getCache(final AnnotationData data); boolean isCacheDisabled(); Method getMethodToCache(final JoinPoint jp, final Class<T> annotationClass); @SuppressWarnings("unchecked") T getUpdateData(final AnnotationData data, final Method method, final Object[] args, final Object returnValue); static final String DISABLE_CACHE_PROPERTY; }
@Test(expected = IllegalStateException.class) public void invalidAddCacheDuplicateName() { String cacheName = "cache1"; Cache cache1 = Mockito.mock(Cache.class); Cache cache2 = Mockito.mock(Cache.class); Mockito.when(cache1.getName()).thenReturn(cacheName); Mockito.when(cache2.getName()).thenReturn(cacheName); cut.addCache(cache1); cut.addCache(cache2); }
@Test(expected = IllegalStateException.class) public void invalidAddCacheDuplicateAlias() { Cache cache1 = Mockito.mock(Cache.class); Cache cache2 = Mockito.mock(Cache.class); Mockito.when(cache1.getName()).thenReturn("cache1"); Mockito.when(cache2.getName()).thenReturn("cache2"); Mockito.when(cache2.getAliases()).thenReturn(Arrays.asList("cache1")); cut.addCache(cache1); cut.addCache(cache2); }