CachingContentStorage implements IContentStorage { public byte[] load(int id) throws BrokenStorageException { byte[] result = findInCache(id); if (result != null) return result; result = mySubject.load(id); cacheContent(result, id); return result; } CachingContentStorage(IContentStorage s); void save(); void close(); int store(byte[] content); byte[] load(int id); void remove(int id); void setVersion(int version); int getVersion(); static final int MAX_CACHED_CONTENT_LENGTH; }
@Test public void testCachingOnLoad() throws Exception { expect(subject.load(2)).andReturn("content".getBytes()).times(1); replay(subject); assertArrayEquals("content".getBytes(), s.load(2)); assertArrayEquals("content".getBytes(), s.load(2)); verify(subject); }
@Test public void testDoesNotCacheBigContent() throws Exception { byte[] b = new byte[CachingContentStorage.MAX_CACHED_CONTENT_LENGTH + 1]; expect(subject.load(1)).andReturn(b).times(2); replay(subject); s.load(1); s.load(1); verify(subject); }