@Test public void testReplace2ArgsWithNoCacheEntry_should_not_call_writer() throws Exception { doThrow(new Exception("Mock Exception: cannot write 1")).when(cacheLoaderWriter).write(eq(1), eq("one#2")); testCache.replace(1, "one#2"); }
@Test public void testPutWithWriterException() throws Exception { doThrow(new Exception("Mock Exception: cannot write 1")).when(cacheLoaderWriter).write(eq(1), eq("one")); try { testCache.put(1, "one"); fail("expected CacheWritingException"); } catch (CacheWritingException ex) { // expected } }
@Test public void testReplace3ArgsWithNotMatchingCacheEntry_should_not_call_writer() throws Exception { doThrow(new Exception("Mock Exception: cannot write 1")).when(cacheLoaderWriter).write(eq(1), eq("one#2")); testCache.put(1, "un"); testCache.replace(1, "one", "one#2"); }
@Test public void putFailure_failedLoaderWriter() throws Exception { doThrow(exception).when(loaderWriter).write(1, 1L); assertThatThrownBy(() -> strategy.putFailure(1, 1L, accessException)) .isExactlyInstanceOf(CacheWritingException.class) .hasCause(exception); verify(store).obliterate(1); verify(loaderWriter).write(1, 1L); }
@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 testReplace2ArgsWithWriterException_should_call_writer() throws Exception { doThrow(new Exception("Mock Exception: cannot write 1")).when(cacheLoaderWriter).write(eq(1), eq("one#2")); testCache.put(1, "one"); try { testCache.replace(1, "one#2"); fail("expected CacheWritingException"); } catch (CacheWritingException ex) { // expected } }
@Test public void testReplace3ArgsWithWriterException_should_call_writer() throws Exception { doThrow(new Exception("Mock Exception: cannot write 1")).when(cacheLoaderWriter).write(eq(1), eq("one#2")); testCache.put(1, "one"); try { testCache.replace(1, "one", "one#2"); fail("expected CacheWritingException"); } catch (CacheWritingException ex) { // expected } }
@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 replaceFailure1_foundEquals() throws Exception { when(loaderWriter.load(1)).thenReturn(1L); assertThat(strategy.replaceFailure(1, 1L, 2L, accessException)).isTrue(); verify(store).obliterate(1); verify(loaderWriter).load(1); verify(loaderWriter).write(1, 2L); }
@Test public void putIfAbsentFailure_writeFails() throws Exception { when(loaderWriter.load(1)).thenReturn(null); doThrow(exception).when(loaderWriter).write(1, 1L); assertThatThrownBy(() -> strategy.putIfAbsentFailure(1, 1L, accessException)) .isExactlyInstanceOf(CacheWritingException.class) .hasCause(exception); verify(store).obliterate(1); verify(loaderWriter).load(1); verify(loaderWriter).write(1, 1L); }
@Test public void replaceFailure_found() throws Exception { when(loaderWriter.load(1)).thenReturn(2L); assertThat(strategy.replaceFailure(1, 1L, accessException)).isEqualTo(2L); verify(store).obliterate(1); verify(loaderWriter).load(1); verify(loaderWriter).write(1, 1L); }
@Test @SuppressWarnings("unchecked") public void testSimpleReplace2ArgsWithLoaderAndWriter_existsInStore() throws Exception { testCache.put(1, "un"); reset(cacheLoaderWriter); assertThat(testCache.replace(1, "one"), Matchers.<CharSequence>equalTo("un")); assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one")); verify(cacheLoaderWriter, times(1)).write(eq(1), eq("one")); verifyNoMoreInteractions(cacheLoaderWriter); }
@Test @SuppressWarnings("unchecked") public void testSimpleReplace3ArgsWithLoaderAndWriter_existsInStore() throws Exception { testCache.put(1, "un"); reset(cacheLoaderWriter); assertThat(testCache.replace(1, "un", "one"), is(true)); assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one")); verify(cacheLoaderWriter, times(1)).write(eq(1), eq("one")); verifyNoMoreInteractions(cacheLoaderWriter); }
@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 putFailure() throws Exception { strategy.putFailure(1, 1L, accessException); verify(store).obliterate(1); verify(loaderWriter).write(1, 1L); }
@Test public void testPutIfAbsentWithWriterException_should_call_writer() throws Exception { doThrow(new Exception("Mock Exception: cannot write 1")).when(cacheLoaderWriter).write(eq(1), eq("one")); try { testCache.putIfAbsent(1, "one"); fail("expected CacheWritingException"); } catch (CacheWritingException ex) { // expected } testCache.put(2, "two"); testCache.putIfAbsent(2, "two#2"); }
@Test public void testSimpleReplace2ArgsWithLoaderAndWriter_existsInSor() throws Exception { when(cacheLoaderWriter.load(eq(1))).thenAnswer((Answer) invocation -> "un"); assertThat(testCache.containsKey(1), is(false)); assertThat(testCache.replace(1, "one"), Matchers.<CharSequence>equalTo("un")); 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 putIfAbsentFailure_notFound() throws Exception { when(loaderWriter.load(1)).thenReturn(null); assertThat(strategy.putIfAbsentFailure(1, 1L, accessException)).isNull(); verify(store).obliterate(1); verify(loaderWriter).load(1); verify(loaderWriter).write(1, 1L); }
@Test public void putIfAbsentLoaderWriterNotAtomic_absent() throws Exception { createNotAtomicCacheManager(); CacheLoaderWriter<Integer, String> loader = mockLoader(); CacheConfigurationBuilder<Integer, String> builder = baseConfig() .withLoaderWriter(loader); cache = createCache(builder); assertThat(cache.putIfAbsent(1, "a")).isNull(); verify(loader).write(1, "a"); assertThat(cache.get(1)).isEqualTo("a"); changesOf(1, 1, 1, 0); }
@Test public void putIfAbsentLoaderWriter_present() throws Exception { createCacheManager(); CacheLoaderWriter<Integer, String> loader = mockLoader(); CacheConfigurationBuilder<Integer, String> builder = baseConfig() .withLoaderWriter(loader); cache = createCache(builder); cache.put(1, "a"); assertThat(cache.putIfAbsent(1, "b")).isEqualTo("a"); verify(loader).write(1, "a"); changesOf(1, 0, 1, 0); }