use of org.ehcache.core.config.DefaultConfiguration in project ehcache3 by ehcache.
the class EhcacheManagerTest method testLifeCyclesCacheLoaders.
@Ignore
@Test
public void testLifeCyclesCacheLoaders() throws Exception {
ResourcePools resourcePools = ResourcePoolsHelper.createResourcePools(100L);
final CacheLoaderWriterProvider cacheLoaderWriterProvider = mock(CacheLoaderWriterProvider.class);
final CacheConfiguration<Long, Long> barConfig = mock(CacheConfiguration.class);
when(barConfig.getClassLoader()).thenReturn(getClass().getClassLoader());
when(barConfig.getResourcePools()).thenReturn(resourcePools);
final CacheConfiguration<Integer, CharSequence> fooConfig = mock(CacheConfiguration.class);
when(fooConfig.getClassLoader()).thenReturn(getClass().getClassLoader());
when(fooConfig.getResourcePools()).thenReturn(resourcePools);
CacheLoaderWriter fooLoaderWriter = mock(CacheLoaderWriter.class);
final WriteBehindProvider decoratorLoaderWriterProvider = mock(WriteBehindProvider.class);
when(cacheLoaderWriterProvider.createCacheLoaderWriter("foo", fooConfig)).thenReturn(fooLoaderWriter);
Map<String, CacheConfiguration<?, ?>> caches = newCacheMap();
caches.put("bar", barConfig);
caches.put("foo", fooConfig);
Configuration cfg = new DefaultConfiguration(caches, getClass().getClassLoader());
Store.Provider storeProvider = mock(Store.Provider.class);
when(storeProvider.rank(any(Set.class), any(Collection.class))).thenReturn(1);
Store mock = mock(Store.class);
CacheEventDispatcherFactory cenlProvider = mock(CacheEventDispatcherFactory.class);
CacheEventDispatcher<Object, Object> cenlServiceMock = mock(CacheEventDispatcher.class);
when(cenlProvider.createCacheEventDispatcher(mock)).thenReturn(cenlServiceMock);
Collection<Service> services = getServices(cacheLoaderWriterProvider, decoratorLoaderWriterProvider, storeProvider, cenlProvider);
when(storeProvider.createStore(ArgumentMatchers.<Store.Configuration>any(), ArgumentMatchers.<ServiceConfiguration[]>any())).thenReturn(mock);
EhcacheManager manager = new EhcacheManager(cfg, services);
manager.init();
verify(cacheLoaderWriterProvider).createCacheLoaderWriter("bar", barConfig);
verify(cacheLoaderWriterProvider).createCacheLoaderWriter("foo", fooConfig);
manager.removeCache("bar");
verify(cacheLoaderWriterProvider, never()).releaseCacheLoaderWriter(anyString(), Mockito.any());
manager.removeCache("foo");
verify(cacheLoaderWriterProvider).releaseCacheLoaderWriter(anyString(), fooLoaderWriter);
}
use of org.ehcache.core.config.DefaultConfiguration in project ehcache3 by ehcache.
the class EhcacheManagerTest method testNoClassLoaderSpecified.
@Test
public void testNoClassLoaderSpecified() {
Map<String, CacheConfiguration<?, ?>> caches = newCacheMap();
caches.put("foo", new TestCacheConfig<>(Object.class, Object.class));
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());
}
use of org.ehcache.core.config.DefaultConfiguration in project ehcache3 by ehcache.
the class EhcacheManagerTest method testCachesAddedAtRuntimeGetReInited.
@Test
public void testCachesAddedAtRuntimeGetReInited() {
Store.Provider storeProvider = mock(Store.Provider.class);
when(storeProvider.rank(any(Set.class), any(Collection.class))).thenReturn(1);
Store store = mock(Store.class);
CacheEventDispatcherFactory cacheEventNotificationListenerServiceProvider = mock(CacheEventDispatcherFactory.class);
when(storeProvider.createStore(any(Store.Configuration.class), ArgumentMatchers.<ServiceConfiguration>any())).thenReturn(store);
when(store.getConfigurationChangeListeners()).thenReturn(new ArrayList<>());
when(cacheEventNotificationListenerServiceProvider.createCacheEventDispatcher(store)).thenReturn(mock(CacheEventDispatcher.class));
CacheConfiguration<Long, String> cache1Configuration = new TestCacheConfig<>(Long.class, String.class);
Map<String, CacheConfiguration<?, ?>> caches = newCacheMap();
caches.put("cache1", cache1Configuration);
DefaultConfiguration config = new DefaultConfiguration(caches, null);
CacheManager cacheManager = new EhcacheManager(config, Arrays.asList(storeProvider, mock(CacheLoaderWriterProvider.class), mock(WriteBehindProvider.class), cacheEventNotificationListenerServiceProvider, mock(CacheEventListenerProvider.class), mock(LocalPersistenceService.class), mock(ResilienceStrategyProvider.class)));
cacheManager.init();
CacheConfiguration<Long, String> cache2Configuration = new TestCacheConfig<>(Long.class, String.class, ResourcePoolsHelper.createResourcePools(100L));
cacheManager.createCache("cache2", cache2Configuration);
cacheManager.removeCache("cache1");
cacheManager.close();
cacheManager.init();
try {
assertThat(cacheManager.getCache("cache1", Long.class, String.class), nullValue());
assertThat(cacheManager.getCache("cache2", Long.class, String.class), notNullValue());
} finally {
cacheManager.close();
}
}
use of org.ehcache.core.config.DefaultConfiguration in project ehcache3 by ehcache.
the class EhcacheManagerTest method testReturnsNullForNonExistCache.
@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());
}
use of org.ehcache.core.config.DefaultConfiguration in project ehcache3 by ehcache.
the class EhcacheManagerTest method testClosesAllCachesDownWhenCloseThrows.
@Test
public void testClosesAllCachesDownWhenCloseThrows() {
final Set<String> caches = new HashSet<>();
final CacheConfiguration<Object, Object> cacheConfiguration = new TestCacheConfig<>(Object.class, Object.class);
final Store.Provider storeProvider = mock(Store.Provider.class);
when(storeProvider.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(storeProvider, cenlProvider);
final RuntimeException thrown = new RuntimeException();
when(storeProvider.createStore(ArgumentMatchers.<Store.Configuration>any())).thenReturn(mock(Store.class));
Map<String, CacheConfiguration<?, ?>> cacheMap = newCacheMap();
cacheMap.put("foo", cacheConfiguration);
cacheMap.put("bar", cacheConfiguration);
cacheMap.put("foobar", cacheConfiguration);
DefaultConfiguration config = new DefaultConfiguration(cacheMap, null);
EhcacheManager cacheManager = new EhcacheManager(config, services) {
@Override
<K, V> InternalCache<K, V> createNewEhcache(final String alias, final CacheConfiguration<K, V> config, final Class<K> keyType, final Class<V> valueType) {
final InternalCache<K, V> ehcache = super.createNewEhcache(alias, config, keyType, valueType);
caches.add(alias);
return ehcache;
}
@Override
protected void closeEhcache(final String alias, final InternalCache<?, ?> ehcache) {
super.closeEhcache(alias, ehcache);
if (alias.equals("foobar")) {
throw thrown;
}
caches.remove(alias);
}
};
cacheManager.init();
try {
cacheManager.close();
fail();
} catch (StateTransitionException e) {
assertThat(cacheManager.getStatus(), is(Status.UNINITIALIZED));
assertThat(e.getCause(), sameInstance(thrown));
}
assertThat(caches.contains("foobar"), is(true));
}
Aggregations