@Test public void get_afterPutWithExistingItem_returnsNewItem() { PutWithExistingEntryHarness harness = new PutWithExistingEntryHarness(); harness.cache.put(harness.key, harness.first); harness.cache.put(harness.key, harness.second); assertThat(harness.cache.get(harness.key)).isEqualTo(harness.second); }
@Test public void clearMemory_afterPutWithExistingItem_evictsOnlyNewItem() { PutWithExistingEntryHarness harness = new PutWithExistingEntryHarness(); harness.cache.put(harness.key, harness.first); harness.cache.put(harness.key, harness.second); verify(harness.listener).onResourceRemoved(harness.first); verify(harness.listener, never()).onResourceRemoved(harness.second); harness.cache.clearMemory(); verify(harness.listener, times(1)).onResourceRemoved(harness.first); verify(harness.listener).onResourceRemoved(harness.second); }
@Test public void put_withExistingItem_updatesSizeCorrectly() { PutWithExistingEntryHarness harness = new PutWithExistingEntryHarness(); harness.cache.put(harness.key, harness.first); harness.cache.put(harness.key, harness.second); assertThat(harness.cache.getCurrentSize()).isEqualTo(harness.second.getSize()); }
@Test public void testResourceRemovedListenerIsNotifiedWhenResourceIsRemoved() { LruResourceCache resourceCache = new LruResourceCache(100); Resource<?> resource = mockResource(); when(resource.getSize()).thenReturn(200); ResourceRemovedListener listener = mock(ResourceRemovedListener.class); resourceCache.setResourceRemovedListener(listener); resourceCache.put(new MockKey(), resource); verify(listener).onResourceRemoved(eq(resource)); }
@Test public void testSizeIsBasedOnResource() { LruResourceCache resourceCache = new LruResourceCache(100); Resource<?> first = getResource(50); MockKey firstKey = new MockKey(); resourceCache.put(firstKey, first); Resource<?> second = getResource(50); MockKey secondKey = new MockKey(); resourceCache.put(secondKey, second); assertTrue(resourceCache.contains(firstKey)); assertTrue(resourceCache.contains(secondKey)); Resource<?> third = getResource(50); MockKey thirdKey = new MockKey(); resourceCache.put(thirdKey, third); assertFalse(resourceCache.contains(firstKey)); assertTrue(resourceCache.contains(secondKey)); assertTrue(resourceCache.contains(thirdKey)); }