@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"); }
private void replace(Cache<Long, String> cache, String value, boolean addToCacheRecords) { cache.replace(KEY, value); if (addToCacheRecords) { cacheRecords.add(new Record(KEY, cache.get(KEY))); } }
@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"); }
private void replace(Cache<Long, String> cache, String oldValue, String newValue, boolean addToCacheRecords) { cache.replace(KEY, oldValue, newValue); if (addToCacheRecords) { cacheRecords.add(new Record(KEY, cache.get(KEY))); } }
@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 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 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 @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 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 testSimpleReplace3ArgsWithLoaderAndWriter_existsInSor() throws Exception { when(cacheLoaderWriter.load(eq(1))).thenAnswer((Answer) invocation -> "un"); assertThat(testCache.containsKey(1), is(false)); assertThat(testCache.replace(1, "un", "one"), is(true)); 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 testSimpleReplace3ArgsWithLoaderAndWriter_existsInSor_notEquals() throws Exception { when(cacheLoaderWriter.load(eq(1))).thenAnswer((Answer) invocation -> "un"); assertThat(testCache.containsKey(1), is(false)); assertThat(testCache.replace(1, "uno", "one"), is(false)); assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("un")); verify(cacheLoaderWriter, times(1)).load(eq(1)); verifyNoMoreInteractions(cacheLoaderWriter); }
@Test @SuppressWarnings("unchecked") public void testSimpleReplace3ArgsWithLoaderAndWriter_existsInStore_notEquals() throws Exception { testCache.put(1, "un"); reset(cacheLoaderWriter); assertThat(testCache.replace(1, "uno", "one"), is(false)); assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("un")); verifyZeroInteractions(cacheLoaderWriter); }
@Test public void replaceKON() { expect(cache.replace(1, "a", "b")).isFalse(); changesOf(0, 1, 0, 0); cache.put(1, "a"); changesOf(0, 0, 1, 0); expect(cache.replace(1, "xxx", "b")).isFalse(); changesOf(0, 1, 0, 0); // FIXME: We have a hit on the cache but a miss on the store. Why? expect(cache.replace(1, "a", "b")).isTrue(); changesOf(1, 0, 1, 0); }
@Test public void testSimpleReplace3Args() throws Exception { Cache<Number, CharSequence> testCache = cacheManager.createCache("testCache", newCacheConfigurationBuilder(Number.class, CharSequence.class, heap(10))); testCache.put(1, "one"); assertThat(testCache.replace(1, "one_", "one@"), is(false)); assertThat(testCache.replace(1, "one", "one#"), is(true)); assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one#")); assertThat(testCache.replace(2, "two", "two#"), is(false)); }
@Test public void testSimplePutWithExpiry_replace2Args() throws Exception { insert(testCache, getEntries()); assertThat(cacheSize(testCache), is(2)); manualTimeSource.setTimeMillis(1001); assertThat(testCache.replace(1, "one#2"), is(nullValue())); assertThat(testCache.get(1), is(nullValue())); assertThat(testCache.replace(2, "two#2"), is(nullValue())); assertThat(testCache.get(2), is(nullValue())); }
@Test public void testSimpleReplace() throws Exception { Cache<Number, CharSequence> testCache = cacheManager.createCache("testCache", newCacheConfigurationBuilder(Number.class, CharSequence.class, heap(10))); testCache.put(1, "one"); assertThat(testCache.replace(1, "one_"), Matchers.<CharSequence>equalTo("one")); assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one_")); assertThat(testCache.replace(2, "two_"), is(nullValue())); }