@Override public ValueHolder<V> get(K key) throws StoreAccessException { Function<K, V> mappingFunction = k -> { try { return cacheLoaderWriter.load(k); } catch (Exception e) { throw new StorePassThroughException(newCacheLoadingException(e)); } }; return delegate.computeIfAbsent(key, mappingFunction); }
@Test public void testGetWithLoaderException() throws Exception { when(cacheLoaderWriter.load(eq(1))).thenThrow(new Exception("TestException: cannot load data")); try { testCache.get(1); fail("expected CacheLoadingException"); } catch (CacheLoadingException ex) { // expected } verify(cacheLoaderWriter, times(1)).load(eq(1)); }
@Test public void testSimpleReplace2ArgsWithLoaderAndWriter_absent() throws Exception { when(cacheLoaderWriter.load(eq(1))).thenAnswer((Answer) invocation -> null); assertThat(testCache.containsKey(1), is(false)); assertThat(testCache.replace(1, "one"), is(nullValue())); verify(cacheLoaderWriter, times(1)).load(eq(1)); verifyNoMoreInteractions(cacheLoaderWriter); }
@Test public void testSimpleReplace3ArgsWithLoaderAndWriter_absent() throws Exception { when(cacheLoaderWriter.load(eq(1))).thenAnswer((Answer) invocation -> null); assertThat(testCache.containsKey(1), is(false)); assertThat(testCache.replace(1, "un", "one"), is(false)); verify(cacheLoaderWriter, times(1)).load(eq(1)); verifyNoMoreInteractions(cacheLoaderWriter); }
@Test public void testSimpleRemove2ArgsWithLoaderAndWriter_absent() throws Exception { when(cacheLoaderWriter.load(eq(1))).thenAnswer((Answer) invocation -> null); assertThat(testCache.containsKey(1), is(false)); assertThat(testCache.remove(1, "one"), is(false)); verify(cacheLoaderWriter, times(1)).load(eq(1)); verifyNoMoreInteractions(cacheLoaderWriter); }
@Test public void putIfAbsentFailure_loadFails() throws Exception { when(loaderWriter.load(1)).thenThrow(exception); assertThatThrownBy(() -> strategy.putIfAbsentFailure(1, 1L, accessException)) .isExactlyInstanceOf(CacheLoadingException.class) .hasCause(exception); verify(store).obliterate(1); verify(loaderWriter).load(1); }
@Test public void removeFailure1_notFound() throws Exception { assertThat(strategy.removeFailure(1, 1L, accessException)).isFalse(); verify(store).obliterate(1); verify(loaderWriter).load(1); }
@Test public void replaceFailure_loadFails() throws Exception { when(loaderWriter.load(1)).thenThrow(exception); assertThatThrownBy(() -> strategy.replaceFailure(1, 1L, accessException)) .isExactlyInstanceOf(CacheLoadingException.class) .hasCause(exception); verify(store).obliterate(1); verify(loaderWriter).load(1); }
@Test public void getFailure_failedLoaderWriter() throws Exception { when(loaderWriter.load(1)).thenThrow(exception); assertThatThrownBy(() -> strategy.getFailure(1, accessException)) .isExactlyInstanceOf(CacheLoadingException.class) .hasCause(exception); verify(store).obliterate(1); verify(loaderWriter).load(1); }
@Test public void replaceFailure1_notFound() throws Exception { assertThat(strategy.replaceFailure(1, 1L, 2L, accessException)).isFalse(); verify(store).obliterate(1); verify(loaderWriter).load(1); }
@Test public void testSimplePutIfAbsentWithLoaderAndWriter_existsInSor() throws Exception { when(cacheLoaderWriter.load(eq(1))).thenAnswer((Answer) invocation -> "un"); assertThat(testCache.containsKey(1), is(false)); assertThat(testCache.putIfAbsent(1, "one"), Matchers.<CharSequence>equalTo("un")); assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("un")); verify(cacheLoaderWriter, times(1)).load(eq(1)); verifyNoMoreInteractions(cacheLoaderWriter); }
@Test public void replaceFailure1_writeFails() throws Exception { when(loaderWriter.load(1)).thenReturn(1L); doThrow(exception).when(loaderWriter).write(1, 2L); assertThatThrownBy(() -> strategy.replaceFailure(1, 1L, 2L, accessException)) .isExactlyInstanceOf(CacheWritingException.class) .hasCause(exception); verify(store).obliterate(1); verify(loaderWriter).load(1); verify(loaderWriter).write(1, 2L); }
@Test public void removeFailure1_deleteFails() throws Exception { when(loaderWriter.load(1)).thenReturn(1L); doThrow(exception).when(loaderWriter).delete(1); assertThatThrownBy(() -> strategy.removeFailure(1, 1L, accessException)) .isExactlyInstanceOf(CacheWritingException.class) .hasCause(exception); verify(store).obliterate(1); verify(loaderWriter).load(1); verify(loaderWriter).delete(1); }
@Test public void replaceFailure_writeFails() throws Exception { when(loaderWriter.load(1)).thenReturn(2L); doThrow(exception).when(loaderWriter).write(1, 1L); assertThatThrownBy(() -> strategy.replaceFailure(1, 1L, accessException)) .isExactlyInstanceOf(CacheWritingException.class) .hasCause(exception); verify(store).obliterate(1); verify(loaderWriter).load(1); verify(loaderWriter).write(1, 1L); }
@Test public void testSimplePutIfAbsentWithLoaderAndWriter_absent() throws Exception { assertThat(testCache.containsKey(1), is(false)); assertThat(testCache.putIfAbsent(1, "one"), is(nullValue())); assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one")); verify(cacheLoaderWriter, times(1)).load(eq(1)); verify(cacheLoaderWriter, times(1)).write(eq(1), eq("one")); }
@Test public void replaceFailure_notFound() throws Exception { assertThat(strategy.replaceFailure(1, 1L, accessException)).isNull(); verify(store).obliterate(1); verify(loaderWriter).load(1); }