CachingContentStore implements ContentStore, ApplicationEventPublisherAware, BeanNameAware { @Override public ContentReader getReader(String contentUrl) { ReadLock readLock = readWriteLock(contentUrl).readLock(); readLock.lock(); try { if (cache.contains(contentUrl)) { return cache.getReader(contentUrl); } } catch(CacheMissException e) { } finally { readLock.unlock(); } return cacheAndRead(contentUrl); } CachingContentStore();  CachingContentStore(ContentStore backingStore, ContentCache cache, boolean cacheOnInbound); void init(); @Override boolean isContentUrlSupported(String contentUrl); @Override boolean isWriteSupported(); @Override long getTotalSize(); @Override long getSpaceUsed(); @Override long getSpaceFree(); @Override long getSpaceTotal(); @Override String getRootLocation(); @Override boolean exists(String contentUrl); @Override ContentReader getReader(String contentUrl); @Override ContentWriter getWriter(final ContentContext context); @Override ContentWriter getWriter(ContentReader existingContentReader, String newContentUrl); @Override void getUrls(ContentUrlHandler handler); @Override void getUrls(Date createdAfter, Date createdBefore, ContentUrlHandler handler); @Override boolean delete(String contentUrl); ReentrantReadWriteLock readWriteLock(String url); @Required void setBackingStore(ContentStore backingStore); String getBackingStoreType(); String getBackingStoreDescription(); @Required void setCache(ContentCache cache); ContentCache getCache(); void setCacheOnInbound(boolean cacheOnInbound); boolean isCacheOnInbound(); int getMaxCacheTries(); void setMaxCacheTries(int maxCacheTries); @Required void setQuota(QuotaManagerStrategy quota); QuotaManagerStrategy getQuota(); @Override void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher); @Override void setBeanName(String name); String getBeanName();  }
@Test public void getReaderForItemInCache() { when(cache.contains("url")).thenReturn(true); when(cache.getReader("url")).thenReturn(cachedContent); ContentReader returnedReader = cachingStore.getReader("url"); assertSame(returnedReader, cachedContent); verify(backingStore, never()).getReader(anyString()); }
@Test public void getReaderForItemMissingFromCacheWillGiveUpAfterRetrying() { when(cache.getReader("url")).thenThrow(new CacheMissException("url")); when(backingStore.getReader("url")).thenReturn(sourceContent); when(cache.put("url", sourceContent)).thenReturn(true); ContentReader returnedReader = cachingStore.getReader("url"); verify(cache, atLeastOnce()).remove("url"); assertSame(returnedReader, sourceContent); }
@Test public void getReaderForItemMissingFromCacheWillRetryAndCanSucceed() { when(cache.getReader("url")). thenThrow(new CacheMissException("url")). thenReturn(cachedContent); when(backingStore.getReader("url")).thenReturn(sourceContent); when(cache.put("url", sourceContent)).thenReturn(true); ContentReader returnedReader = cachingStore.getReader("url"); assertSame(returnedReader, cachedContent); }
@Test public void getReaderForItemMissingFromCacheButNoContentToCache() { when(cache.getReader("url")).thenThrow(new CacheMissException("url")); when(backingStore.getReader("url")).thenReturn(sourceContent); when(cache.put("url", sourceContent)).thenReturn(false); cachingStore.getReader("url"); }