Search in sources :

Example 1 with DefaultConfiguration

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);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Configuration(org.ehcache.config.Configuration) ServiceConfiguration(org.ehcache.spi.service.ServiceConfiguration) CacheConfiguration(org.ehcache.config.CacheConfiguration) DefaultConfiguration(org.ehcache.core.config.DefaultConfiguration) ServiceCreationConfiguration(org.ehcache.spi.service.ServiceCreationConfiguration) CacheLoaderWriter(org.ehcache.spi.loaderwriter.CacheLoaderWriter) Store(org.ehcache.core.spi.store.Store) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ServiceConfiguration(org.ehcache.spi.service.ServiceConfiguration) CacheConfiguration(org.ehcache.config.CacheConfiguration) CacheEventDispatcherFactory(org.ehcache.core.events.CacheEventDispatcherFactory) ResourcePools(org.ehcache.config.ResourcePools) Service(org.ehcache.spi.service.Service) LocalPersistenceService(org.ehcache.core.spi.service.LocalPersistenceService) MaintainableService(org.ehcache.spi.service.MaintainableService) DefaultConfiguration(org.ehcache.core.config.DefaultConfiguration) CacheLoaderWriterProvider(org.ehcache.spi.loaderwriter.CacheLoaderWriterProvider) WriteBehindProvider(org.ehcache.spi.loaderwriter.WriteBehindProvider) Collection(java.util.Collection) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with DefaultConfiguration

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());
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) CacheEventDispatcherFactory(org.ehcache.core.events.CacheEventDispatcherFactory) Store(org.ehcache.core.spi.store.Store) Service(org.ehcache.spi.service.Service) LocalPersistenceService(org.ehcache.core.spi.service.LocalPersistenceService) MaintainableService(org.ehcache.spi.service.MaintainableService) DefaultConfiguration(org.ehcache.core.config.DefaultConfiguration) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ServiceConfiguration(org.ehcache.spi.service.ServiceConfiguration) Collection(java.util.Collection) CacheConfiguration(org.ehcache.config.CacheConfiguration) Test(org.junit.Test)

Example 3 with DefaultConfiguration

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();
    }
}
Also used : TestCacheConfig(org.ehcache.core.util.TestCacheConfig) Set(java.util.Set) HashSet(java.util.HashSet) CacheEventDispatcherFactory(org.ehcache.core.events.CacheEventDispatcherFactory) Configuration(org.ehcache.config.Configuration) ServiceConfiguration(org.ehcache.spi.service.ServiceConfiguration) CacheConfiguration(org.ehcache.config.CacheConfiguration) DefaultConfiguration(org.ehcache.core.config.DefaultConfiguration) ServiceCreationConfiguration(org.ehcache.spi.service.ServiceCreationConfiguration) Store(org.ehcache.core.spi.store.Store) CacheEventDispatcher(org.ehcache.core.events.CacheEventDispatcher) DefaultConfiguration(org.ehcache.core.config.DefaultConfiguration) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Collection(java.util.Collection) PersistentCacheManager(org.ehcache.PersistentCacheManager) CacheManager(org.ehcache.CacheManager) CacheConfiguration(org.ehcache.config.CacheConfiguration) Test(org.junit.Test)

Example 4 with DefaultConfiguration

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());
}
Also used : DefaultConfiguration(org.ehcache.core.config.DefaultConfiguration) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CacheConfiguration(org.ehcache.config.CacheConfiguration) Test(org.junit.Test)

Example 5 with DefaultConfiguration

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));
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Store(org.ehcache.core.spi.store.Store) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CacheConfiguration(org.ehcache.config.CacheConfiguration) HashSet(java.util.HashSet) TestCacheConfig(org.ehcache.core.util.TestCacheConfig) CacheEventDispatcherFactory(org.ehcache.core.events.CacheEventDispatcherFactory) Service(org.ehcache.spi.service.Service) LocalPersistenceService(org.ehcache.core.spi.service.LocalPersistenceService) MaintainableService(org.ehcache.spi.service.MaintainableService) DefaultConfiguration(org.ehcache.core.config.DefaultConfiguration) Collection(java.util.Collection) StateTransitionException(org.ehcache.StateTransitionException) Test(org.junit.Test)

Aggregations

DefaultConfiguration (org.ehcache.core.config.DefaultConfiguration)35 Test (org.junit.Test)28 CacheConfiguration (org.ehcache.config.CacheConfiguration)25 Matchers.containsString (org.hamcrest.Matchers.containsString)23 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)23 Store (org.ehcache.core.spi.store.Store)17 Collection (java.util.Collection)15 HashSet (java.util.HashSet)15 Set (java.util.Set)15 Service (org.ehcache.spi.service.Service)14 CacheEventDispatcherFactory (org.ehcache.core.events.CacheEventDispatcherFactory)13 LocalPersistenceService (org.ehcache.core.spi.service.LocalPersistenceService)13 TestCacheConfig (org.ehcache.core.util.TestCacheConfig)13 MaintainableService (org.ehcache.spi.service.MaintainableService)13 ServiceConfiguration (org.ehcache.spi.service.ServiceConfiguration)12 Configuration (org.ehcache.config.Configuration)7 DefaultPersistenceConfiguration (org.ehcache.impl.config.persistence.DefaultPersistenceConfiguration)7 CacheManager (org.ehcache.CacheManager)6 EhcacheCachingProvider (org.ehcache.jsr107.EhcacheCachingProvider)6 ServiceCreationConfiguration (org.ehcache.spi.service.ServiceCreationConfiguration)6