Index: httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ehcache/TestEhcacheHttpCache.java =================================================================== --- httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ehcache/TestEhcacheHttpCache.java (revision 0) +++ httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ehcache/TestEhcacheHttpCache.java (revision 0) @@ -0,0 +1,65 @@ +package org.apache.http.impl.client.cache.ehcache; + +import java.io.IOException; + +import junit.framework.TestCase; +import net.sf.ehcache.Ehcache; +import net.sf.ehcache.Element; + +import org.apache.http.client.cache.HttpCacheEntry; +import org.apache.http.impl.client.cache.CacheEntry; +import org.easymock.EasyMock; +import org.junit.Test; + +public class TestEhcacheHttpCache extends TestCase { + + private Ehcache mockCache; + private EhcacheHttpCache impl; + + public void setUp() { + mockCache = EasyMock.createMock(Ehcache.class); + impl = new EhcacheHttpCache(mockCache); + } + + @Test + public void testCachePut() throws IOException { + final String key = "foo"; + final HttpCacheEntry value = new CacheEntry(); + + Element e = new Element(key, value); + + mockCache.put(e); + + EasyMock.replay(mockCache); + impl.putEntry(key, value); + EasyMock.verify(mockCache); + } + + @Test + public void testCacheGet() { + final String key = "foo"; + final HttpCacheEntry cachedValue = new CacheEntry(); + Element element = new Element(key, cachedValue); + + EasyMock.expect(mockCache.get(key)) + .andReturn(element); + + EasyMock.replay(mockCache); + HttpCacheEntry resultingEntry = impl.getEntry(key); + EasyMock.verify(mockCache); + + assertSame(cachedValue, resultingEntry); + } + + @Test + public void testCacheRemove() { + final String key = "foo"; + + EasyMock.expect(mockCache.remove(key)).andReturn(true); + + EasyMock.replay(mockCache); + impl.removeEntry(key); + EasyMock.verify(mockCache); + } + +} Index: httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ehcache/TestEhcacheProtcolRequirements.java =================================================================== --- httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ehcache/TestEhcacheProtcolRequirements.java (revision 0) +++ httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ehcache/TestEhcacheProtcolRequirements.java (revision 0) @@ -0,0 +1,53 @@ +package org.apache.http.impl.client.cache.ehcache; + +import net.sf.ehcache.CacheManager; + +import org.apache.http.HttpHost; +import org.apache.http.HttpVersion; +import org.apache.http.client.HttpClient; +import org.apache.http.client.cache.HttpCache; +import org.apache.http.impl.client.cache.CacheConfig; +import org.apache.http.impl.client.cache.CachingHttpClient; +import org.apache.http.impl.client.cache.HeapResourceFactory; +import org.apache.http.impl.client.cache.HttpTestUtils; +import org.apache.http.impl.client.cache.TestProtocolRequirements; +import org.apache.http.message.BasicHttpRequest; +import org.easymock.classextension.EasyMock; +import org.junit.After; +import org.junit.Before; + +public class TestEhcacheProtcolRequirements extends TestProtocolRequirements{ + private final String TEST_EHCACHE_NAME = "TestEhcacheProtocolRequirements-cache"; + + private CacheManager manager; + + @Override + @Before + public void setUp() { + host = new HttpHost("foo.example.com"); + + body = HttpTestUtils.makeBody(entityLength); + + request = new BasicHttpRequest("GET", "/foo", HttpVersion.HTTP_1_1); + + originResponse = make200Response(); + + manager = CacheManager.create(); + if(manager.cacheExists(TEST_EHCACHE_NAME)){ + manager.removeCache(TEST_EHCACHE_NAME); + } + manager.addCache(TEST_EHCACHE_NAME); + cache = new EhcacheHttpCache(manager.getCache(TEST_EHCACHE_NAME)); + mockBackend = EasyMock.createMock(HttpClient.class); + mockCache = EasyMock.createMock(HttpCache.class); + params = new CacheConfig(); + params.setMaxObjectSizeBytes(MAX_BYTES); + impl = new CachingHttpClient(mockBackend, cache, new HeapResourceFactory(), params); + } + + @After + public void tearDown(){ + manager.removeCache(TEST_EHCACHE_NAME); + manager.shutdown(); + } +} Index: httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ehcache/EhcacheHttpCache.java =================================================================== --- httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ehcache/EhcacheHttpCache.java (revision 0) +++ httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ehcache/EhcacheHttpCache.java (revision 0) @@ -0,0 +1,49 @@ +package org.apache.http.impl.client.cache.ehcache; + +import java.io.IOException; + +import net.sf.ehcache.Ehcache; +import net.sf.ehcache.Element; + +import org.apache.http.client.cache.HttpCache; +import org.apache.http.client.cache.HttpCacheEntry; +import org.apache.http.client.cache.HttpCacheUpdateCallback; + +public class EhcacheHttpCache implements HttpCache { + private Ehcache cache; + + public EhcacheHttpCache(Ehcache cache) { + this.cache = cache; + } + + public synchronized void putEntry(String key, HttpCacheEntry entry) throws IOException { + cache.put(new Element(key, entry)); + } + + public synchronized HttpCacheEntry getEntry(String url) { + Element e = cache.get(url); + return (e != null) ? (HttpCacheEntry)e.getValue() : null; + } + + public synchronized void removeEntry(String url) { + cache.remove(url); + } + + public synchronized void updateEntry(String key, HttpCacheUpdateCallback callback) + throws IOException { + Element e = cache.get(key); + HttpCacheEntry existingEntry = (e != null) ? (HttpCacheEntry)e.getValue() : null; + HttpCacheEntry updatedEntry = callback.update(existingEntry); + + if(e == null){ + putEntry(key, updatedEntry); + }else{ + // Attempt to do a CAS replace, if we fail throw an IOException for now + // While this operation should work fine within this instance, multiple instances + // could trample each others' data + if(!cache.replace(e, new Element(key, updatedEntry))){ + throw new IOException(); + } + } + } +} \ No newline at end of file Index: httpclient-cache/pom.xml =================================================================== --- httpclient-cache/pom.xml (revision 985195) +++ httpclient-cache/pom.xml (working copy) @@ -79,6 +79,16 @@ ${easymock.version} test + + net.sf.ehcache + ehcache-core + 2.2.0 + + + org.slf4j + slf4j-simple + 1.5.8 +