/** * Creates a new {@link CacheManager} based on the provided configuration. * The returned {@code CacheManager} is uninitialized. * * @param configuration the configuration to use * @return a {@code CacheManager} */ public static CacheManager newCacheManager(final Configuration configuration) { return new EhcacheManager(configuration); }
.entrySet()) { final String alias = cacheConfigurationEntry.getKey(); createCache(alias, cacheConfigurationEntry.getValue(), false); initiatedCaches.push(alias); String toBeClosed = initiatedCaches.pop(); try { removeCache(toBeClosed, false); } catch (Exception exceptionClosingCache) { LOGGER.error("Cache '{}' could not be removed after initialization failure due to ", toBeClosed, exceptionClosingCache);
@Override protected void closeEhcache(final String alias, final InternalCache<?, ?> ehcache) { super.closeEhcache(alias, ehcache); caches.remove(ehcache); } };
@Override public void destroy() throws CachePersistenceException { StatusTransitioner.Transition st = statusTransitioner.maintenance(); try { startMaintainableServices(MaintainableService.MaintenanceScope.CACHE_MANAGER); st.succeeded(); } catch (Throwable t) { throw st.failed(t); } destroyInternal(); st = statusTransitioner.exitMaintenance(); try { stopMaintainableServices(); st.succeeded(); } catch (Throwable t) { throw st.failed(t); } LOGGER.info("All persistent data destroyed for {}", simpleName); }
@Test public void testCacheDestroyTriggersCacheRemoved() throws Exception { Cache<Long, String> cache = cacheManager.getCache(CACHE, Long.class, String.class); cacheManager.destroyCache(CACHE); verify(cacheManagerListener).cacheRemoved(CACHE, cache); }
@Test public void testCacheRemoved() throws Exception { Cache<Long, String> cache = cacheManager.getCache(CACHE, Long.class, String.class); cacheManager.removeCache(CACHE); verify(cacheManagerListener).cacheRemoved(CACHE, cache); }
@Test public void testDoesNotifyAboutCache() { final CacheConfiguration<Object, Object> cacheConfiguration = new BaseCacheConfiguration<>(Object.class, Object.class, null, null, null, ResourcePoolsHelper .createHeapOnlyPools()); final Store.Provider mock = mock(Store.Provider.class); when(mock.rank(any(Set.class), any(Collection.class))).thenReturn(1); final CacheEventDispatcherFactory cenlProvider = mock(CacheEventDispatcherFactory.class); final CacheEventDispatcher<Object, Object> cenlServiceMock = mock(CacheEventDispatcher.class); when(cenlProvider.createCacheEventDispatcher(any(Store.class))).thenReturn(cenlServiceMock); final Collection<Service> services = getServices(mock, cenlProvider); when(mock.createStore(ArgumentMatchers.<Store.Configuration>any())).thenReturn(mock(Store.class)); Map<String, CacheConfiguration<?, ?>> caches = newCacheMap(); DefaultConfiguration config = new DefaultConfiguration(caches, null); EhcacheManager cacheManager = new EhcacheManager(config, services); final CacheManagerListener listener = mock(CacheManagerListener.class); cacheManager.registerListener(listener); cacheManager.init(); final String cacheAlias = "bar"; cacheManager.createCache(cacheAlias, cacheConfiguration); final Cache<Object, Object> bar = cacheManager.getCache(cacheAlias, Object.class, Object.class); verify(listener).cacheAdded(cacheAlias, bar); cacheManager.removeCache(cacheAlias); verify(listener).cacheRemoved(cacheAlias, bar); }
@Test public void testDoesNotifyAboutLifecycle() { Map<String, CacheConfiguration<?, ?>> caches = newCacheMap(); DefaultConfiguration config = new DefaultConfiguration(caches, null); EhcacheManager cacheManager = new EhcacheManager(config, getServices(null, null)); final CacheManagerListener listener = mock(CacheManagerListener.class); cacheManager.registerListener(listener); cacheManager.init(); verify(listener).stateTransition(Status.UNINITIALIZED, Status.AVAILABLE); cacheManager.close(); verify(listener).stateTransition(Status.AVAILABLE, Status.UNINITIALIZED); }
caches.put("bar", cacheConfiguration); DefaultConfiguration config = new DefaultConfiguration(caches, null); EhcacheManager cacheManager = new EhcacheManager(config, services); try { cacheManager.removeCache("foo"); fail(); } catch (IllegalStateException e) { cacheManager.createCache("foo", (CacheConfiguration) null); fail(); } catch (IllegalStateException e) { cacheManager.getCache("foo", Object.class, Object.class); fail(); } catch (IllegalStateException e) {
@Test public void testThrowsWhenAddingExistingCache() { CacheConfiguration<Object, Object> cacheConfiguration = new BaseCacheConfiguration<>(Object.class, Object.class, null, null, null, ResourcePoolsHelper .createHeapOnlyPools()); final Store.Provider storeProvider = mock(Store.Provider.class); when(storeProvider.rank(any(Set.class), any(Collection.class))).thenReturn(1); final Store mock = mock(Store.class); final CacheEventDispatcherFactory cenlProvider = mock(CacheEventDispatcherFactory.class); final CacheEventDispatcher<Object, Object> cenlServiceMock = mock(CacheEventDispatcher.class); when(cenlProvider.createCacheEventDispatcher(mock)).thenReturn(cenlServiceMock); final Collection<Service> services = getServices(storeProvider, cenlProvider); when(storeProvider .createStore(ArgumentMatchers.<Store.Configuration>any(), ArgumentMatchers.<ServiceConfiguration[]>any())).thenReturn(mock); Map<String, CacheConfiguration<?, ?>> caches = newCacheMap(); caches.put("bar", cacheConfiguration); DefaultConfiguration config = new DefaultConfiguration(caches, null); EhcacheManager cacheManager = new EhcacheManager(config, services); cacheManager.init(); final Cache<Object, Object> cache = cacheManager.getCache("bar", Object.class, Object.class); assertNotNull(cache); try { cacheManager.createCache("bar", cacheConfiguration); fail("Should have thrown"); } catch (IllegalArgumentException e) { assertTrue(e.getMessage().contains("bar")); } }
@Test public void testCacheManagerListener_called_after_configuration_updated() throws Exception { EhcacheManager cacheManager = (EhcacheManager) CacheManagerBuilder.newCacheManagerBuilder() .build(); CacheManagerListener cacheManagerListener = spy(new AssertiveCacheManagerListener(cacheManager.getRuntimeConfiguration())); cacheManager.registerListener(cacheManagerListener); cacheManager.init(); CacheConfiguration<String, String> cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class, newResourcePoolsBuilder() .heap(10, EntryUnit.ENTRIES) .offheap(1, MemoryUnit.MB)) .build(); cacheManager.createCache(CACHE_NAME, cacheConfiguration); verify(cacheManagerListener).cacheAdded(eq(CACHE_NAME), (Cache<?, ?>) isNotNull()); cacheManager.removeCache(CACHE_NAME); verify(cacheManagerListener).cacheRemoved(eq(CACHE_NAME), (Cache<?, ?>) isNotNull()); }
@Test public void testNoClassLoaderSpecified() { Map<String, CacheConfiguration<?, ?>> caches = newCacheMap(); caches.put("foo", new BaseCacheConfiguration<>(Object.class, Object.class, null, null, null, ResourcePoolsHelper.createHeapOnlyPools())); DefaultConfiguration config = new DefaultConfiguration(caches, null); final Store.Provider storeProvider = mock(Store.Provider.class); when(storeProvider.rank(any(Set.class), any(Collection.class))).thenReturn(1); final Store mock = mock(Store.class); final CacheEventDispatcherFactory cenlProvider = mock(CacheEventDispatcherFactory.class); final CacheEventDispatcher<Object, Object> cenlServiceMock = mock(CacheEventDispatcher.class); when(cenlProvider.createCacheEventDispatcher(mock)).thenReturn(cenlServiceMock); final Collection<Service> services = getServices(storeProvider, cenlProvider); when(storeProvider .createStore(ArgumentMatchers.<Store.Configuration>any(), ArgumentMatchers.<ServiceConfiguration[]>any())).thenReturn(mock); EhcacheManager cacheManager = new EhcacheManager(config, services); cacheManager.init(); assertSame(ClassLoading.getDefaultClassLoader(), cacheManager.getClassLoader()); assertSame(cacheManager.getClassLoader(), cacheManager.getCache("foo", Object.class, Object.class).getRuntimeConfiguration().getClassLoader()); }
.createStore(ArgumentMatchers.<Store.Configuration>any(), ArgumentMatchers.<ServiceConfiguration[]>any())).thenReturn(mock); EhcacheManager manager = new EhcacheManager(cfg, services); manager.init(); manager.removeCache("bar"); verify(cacheLoaderWriterProvider, never()).releaseCacheLoaderWriter(anyString(), (CacheLoaderWriter<?, ?>)Mockito.any()); manager.removeCache("foo"); verify(cacheLoaderWriterProvider).releaseCacheLoaderWriter(anyString(), fooLoaderWriter);
@Test public void testReturnsNullForNonExistCache() { Map<String, CacheConfiguration<?, ?>> caches = newCacheMap(); DefaultConfiguration config = new DefaultConfiguration(caches, null); EhcacheManager cacheManager = new EhcacheManager(config, getServices(null, null)); cacheManager.init(); assertThat(cacheManager.getCache("foo", Object.class, Object.class), nullValue()); }
startMaintainableServices(MaintainableService.MaintenanceScope.CACHE); maintenance.succeeded(); } catch (Throwable t) { removeCache(alias, true); destroyPersistenceSpace(alias); } finally { StatusTransitioner.Transition st = statusTransitioner.exitMaintenance(); try { stopMaintainableServices(); st.succeeded(); } catch (Throwable t) {
@Test public void testDestroyCacheFailsAndStopIfStartingServicesFails() throws CachePersistenceException, InterruptedException { Map<String, CacheConfiguration<?, ?>> caches = newCacheMap(); DefaultConfiguration config = new DefaultConfiguration(caches, null); List<Service> services = minimumCacheManagerServices(); MaintainableService service = mock(MaintainableService.class); doThrow(new RuntimeException("failed")).when(service) .startForMaintenance(Mockito.<ServiceProvider<MaintainableService>>any(), eq(MaintainableService.MaintenanceScope.CACHE)); services.add(service); EhcacheManager manager = new EhcacheManager(config, services); expectedException.expect(StateTransitionException.class); expectedException.expectMessage("failed"); manager.destroyCache("test"); assertThat(manager.getStatus(), equalTo(Status.UNINITIALIZED)); }
@Test public void testDestroyCacheFailsIfAlreadyInMaintenanceMode() throws CachePersistenceException, InterruptedException { Map<String, CacheConfiguration<?, ?>> caches = newCacheMap(); DefaultConfiguration config = new DefaultConfiguration(caches, null); final EhcacheManager manager = new EhcacheManager(config, minimumCacheManagerServices()); Thread thread = new Thread(() -> manager.getStatusTransitioner().maintenance().succeeded()); thread.start(); thread.join(1000); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("State is MAINTENANCE, yet you don't own it!"); manager.destroyCache("test"); }
@Test public void testStopAllServicesWhenCacheInitializationFails() { Map<String, CacheConfiguration<?, ?>> caches = newCacheMap(); caches.put("myCache", mock(CacheConfiguration.class)); DefaultConfiguration config = new DefaultConfiguration(caches, null); List<Service> services = minimumCacheManagerServices(); EhcacheManager cacheManager = new EhcacheManager(config, services); Store.Provider storeProvider = (Store.Provider) services.get(0); // because I know it's the first of the list try { cacheManager.init(); fail("Should have thrown..."); } catch (StateTransitionException ste) { verify(storeProvider).stop(); } }
@Override public <K, V> Cache<K, V> createCache(final String alias, CacheConfiguration<K, V> config) throws IllegalArgumentException { return createCache(alias, config, true); }
@Override public void removeCache(final String alias) { if (alias == null) { throw new NullPointerException("Alias cannot be null"); } removeCache(alias, true); }