@Test public void contains() { final String url = "store://content/url.bin"; Mockito.when(lookupTable.contains(Key.forUrl(url))).thenReturn(true); assertTrue(contentCache.contains(Key.forUrl(url))); assertTrue(contentCache.contains(url)); Mockito.when(lookupTable.contains(Key.forUrl(url))).thenReturn(false); assertFalse(contentCache.contains(Key.forUrl(url))); assertFalse(contentCache.contains(url)); }
@Test(expected = CacheMissException.class) public void getReaderWhenItemNotInCache() { final String url = "store://content/url.bin"; Mockito.when(lookupTable.contains(Key.forUrl(url))).thenReturn(false); contentCache.getReader(url); }
@Test(expected = CacheMissException.class) public void getReaderForItemInCacheButMissingContentFile() { final String url = "store://content/url.bin"; Mockito.when(lookupTable.contains(Key.forUrl(url))).thenReturn(true); final String path = "/no/content/file/at/this/path.bin"; Mockito.when(lookupTable.get(Key.forUrl(url))).thenReturn(path); try { contentCache.getReader(url); } finally { // Important the get(path) was called, so that the timeToIdle is reset // for the 'reverse lookup' as well as the URL to path mapping. Mockito.verify(lookupTable).get(Key.forCacheFile(path)); } }
@Test public void canGetReaderForItemInCacheHavingLiveFile() { final String url = "store://content/url.bin"; Mockito.when(lookupTable.contains(Key.forUrl(url))).thenReturn(true); final String path = tempfile().getAbsolutePath(); Mockito.when(lookupTable.get(Key.forUrl(url))).thenReturn(path); FileContentReader reader = (FileContentReader) contentCache.getReader(url); assertEquals("Reader should have correct URL", url, reader.getContentUrl()); assertEquals( "Reader should be for correct cached content file", path, reader.getFile().getAbsolutePath()); // Important the get(path) was called, so that the timeToIdle is reset // for the 'reverse lookup' as well as the URL to path mapping. Mockito.verify(lookupTable).get(Key.forCacheFile(path)); }
@Override public ContentReader getReader(String contentUrl) { Key url = Key.forUrl(contentUrl); if (memoryStore.contains(url)) { String path = memoryStore.get(url); // Getting the path for a URL from the memoryStore will reset the timeToIdle for // that URL. It is important to perform a reverse lookup as well to ensure that the // cache file path to URL mapping is also kept in the cache. memoryStore.get(Key.forCacheFile(path)); File cacheFile = new File(path); if (cacheFile.exists()) { return new FileContentReader(cacheFile, contentUrl); } } throw new CacheMissException(contentUrl); }
/** * Allows caller to perform lookup using a {@link Key}. * * @param key * @return true if the cache contains, false otherwise. */ public boolean contains(Key key) { return memoryStore.contains(key); }
@Override public boolean contains(String contentUrl) { return memoryStore.contains(Key.forUrl(contentUrl)); }