/** * {@inheritDoc} */ @Override public void init() { cache.init(); }
private void assertCacheDelegatesLifecycleCallsToStore(InternalCache cache) throws Exception { final LifeCycled mock = mock(LifeCycled.class); cache.addHook(mock); cache.init(); verify(mock).init(); cache.close(); verify(mock).close(); }
@Test public void testPutAll() throws Exception { @SuppressWarnings("unchecked") Store<Number, CharSequence> store = mock(Store.class); InternalCache<Number, CharSequence> ehcache = getCache(store); ehcache.init(); Map<Number, CharSequence> map = new HashMap<>(3); map.put(1, "one"); map.put(2, "two"); map.put(3, "three"); ehcache.putAll(map); verify(store).bulkCompute((Set<? extends Number>) argThat(hasItems((Number)1, 2, 3)), any(Function.class)); }
@Test public void testRemoveAll() throws Exception { Store<Number, CharSequence> store = mock(Store.class); InternalCache<Number, CharSequence> ehcache = getCache(store); ehcache.init(); ehcache.removeAll(new HashSet<Number>(Arrays.asList(1, 2, 3))); verify(store).bulkCompute((Set<? extends Number>) argThat(hasItems(1, 2, 3)), any(Function.class)); }
@Test public void testDelegatesLifecycleCallsToStore() throws Exception { InternalCache ehcache = getCache(mock(Store.class)); final LifeCycled mock = mock(LifeCycled.class); ehcache.addHook(mock); ehcache.init(); verify(mock).init(); ehcache.close(); verify(mock).close(); }
@Test public void testFailingTransitionGoesToLowestStatus() throws Exception { final LifeCycled mock = mock(LifeCycled.class); InternalCache ehcache = getCache(mock(Store.class)); doThrow(new Exception()).when(mock).init(); ehcache.addHook(mock); try { ehcache.init(); fail(); } catch (StateTransitionException e) { assertThat(ehcache.getStatus(), is(Status.UNINITIALIZED)); } reset(mock); ehcache.init(); assertThat(ehcache.getStatus(), is(Status.AVAILABLE)); ehcache.close(); }
try { cache = createNewEhcache(alias, config, keyType, valueType); cache.init(); if (addToConfig) { configuration.addCacheConfiguration(alias, cache.getRuntimeConfiguration());
private void assertFailingTransitionGoesToLowestStatus(InternalCache cache) throws Exception { final LifeCycled mock = mock(LifeCycled.class); cache.addHook(mock); doThrow(new Exception()).when(mock).init(); try { cache.init(); fail(); } catch (StateTransitionException e) { assertThat(cache.getStatus(), CoreMatchers.is(Status.UNINITIALIZED)); } reset(mock); cache.init(); assertThat(cache.getStatus(), is(Status.AVAILABLE)); doThrow(new Exception()).when(mock).close(); try { cache.close(); fail(); } catch (StateTransitionException e) { assertThat(cache.getStatus(), is(Status.UNINITIALIZED)); } }
@Test public void testTransistionsState() { Store<Object, Object> store = mock(Store.class); InternalCache ehcache = getCache(store); assertThat(ehcache.getStatus(), CoreMatchers.is(Status.UNINITIALIZED)); ehcache.init(); assertThat(ehcache.getStatus(), is(Status.AVAILABLE)); ehcache.close(); assertThat(ehcache.getStatus(), is(Status.UNINITIALIZED)); }
@Test public void testGetAll() throws Exception { Store<Number, CharSequence> store = mock(Store.class); when(store.bulkComputeIfAbsent((Set<? extends Number>)argThat(hasItems(1, 2, 3)), any(Function.class))).thenAnswer(invocation -> { Function<Iterable<? extends Number>, Iterable<? extends Map.Entry<? extends Number, ? extends CharSequence>>> function = (Function<Iterable<? extends Number>, Iterable<? extends Map.Entry<? extends Number, ? extends CharSequence>>>) invocation.getArguments()[1]; Set<? extends Number> keys = (Set<? extends Number>) invocation.getArguments()[0]; function.apply(keys); Map<Number, ValueHolder<String>> map = new HashMap<>(); map.put(1, null); map.put(2, null); map.put(3, valueHolder("three")); return map; }); InternalCache<Number, CharSequence> ehcache = getCache(store); ehcache.init(); Map<Number, CharSequence> result = ehcache.getAll(new HashSet<Number>(Arrays.asList(1, 2, 3))); assertThat(result, hasEntry(1, null)); assertThat(result, hasEntry(2, null)); assertThat(result, hasEntry(3, "three")); verify(store).bulkComputeIfAbsent((Set<? extends Number>)argThat(hasItems(1, 2, 3)), any(Function.class)); }
}); InternalCache<Object, Object> ehcache = getCache(store); ehcache.init(); assertThat(ehcache.putIfAbsent("foo", value), nullValue()); assertThat(ehcache.putIfAbsent("foo", "foo"), CoreMatchers.is(value));
@Test public void testInvokesHooks() { Store store = mock(Store.class); InternalCache ehcache = getCache(store); final LifeCycled hook = mock(LifeCycled.class); ehcache.addHook(hook); ehcache.init(); try { verify(hook).init(); } catch (Exception e) { fail(); } reset(hook); try { if (ehcache instanceof Ehcache) { ((Ehcache)ehcache).removeHook(hook); } else { ((Ehcache)ehcache).removeHook(hook); } fail(); } catch (IllegalStateException e) { // expected } ehcache.close(); try { verify(hook).close(); } catch (Exception e) { fail(); } }
ehcache.init(); final Iterator iterator = ehcache.iterator(); ehcache.close();
/** * {@inheritDoc} */ @Override public void init() { cache.init(); }
try { cache = createNewEhcache(alias, config, keyType, valueType); cache.init(); if (addToConfig) { configuration.addCacheConfiguration(alias, cache.getRuntimeConfiguration());