diff --git a/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheEntry.java b/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheEntry.java index 79210291c..719307a07 100644 --- a/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheEntry.java +++ b/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheEntry.java @@ -29,7 +29,9 @@ package org.apache.http.client.cache; import java.io.Serializable; import java.util.Collections; import java.util.Date; +import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Set; import org.apache.http.Header; @@ -58,28 +60,16 @@ public class HttpCacheEntry implements Serializable { private final HeaderGroup responseHeaders; private final Resource resource; private final Set variantURIs; + private final Map variantMap; - /** - * Create a new {@link HttpCacheEntry} - * - * @param requestDate - * Date/time when the request was made (Used for age - * calculations) - * @param responseDate - * Date/time that the response came back (Used for age - * calculations) - * @param statusLine - * HTTP status line - * @param responseHeaders - * Header[] from original HTTP Response - */ - public HttpCacheEntry( + private HttpCacheEntry( final Date requestDate, final Date responseDate, final StatusLine statusLine, final Header[] responseHeaders, final Resource resource, - final Set variants) { + final Set variants, + final Map variantMap) { super(); if (requestDate == null) { throw new IllegalArgumentException("Request date may not be null"); @@ -102,7 +92,50 @@ public class HttpCacheEntry implements Serializable { this.responseHeaders = new HeaderGroup(); this.responseHeaders.setHeaders(responseHeaders); this.resource = resource; - this.variantURIs = variants != null ? new HashSet(variants) : new HashSet(); + this.variantURIs = variants != null + ? new HashSet(variants) : null; + this.variantMap = variantMap != null + ? new HashMap(variantMap) + : null; + } + + /** + * Create a new {@link HttpCacheEntry} + * + * @param requestDate + * Date/time when the request was made (Used for age + * calculations) + * @param responseDate + * Date/time that the response came back (Used for age + * calculations) + * @param statusLine + * HTTP status line + * @param responseHeaders + * Header[] from original HTTP Response + */ + @Deprecated + public HttpCacheEntry( + final Date requestDate, + final Date responseDate, + final StatusLine statusLine, + final Header[] responseHeaders, + final Resource resource, + final Set variants) { + this(requestDate, responseDate, statusLine, responseHeaders, + resource, variants, null); + } + + public HttpCacheEntry(Date requestDate, Date responseDate, StatusLine statusLine, + Header[] headers, Resource resource) { + this(requestDate, responseDate, statusLine, headers, resource, null, + new HashMap()); + } + + public HttpCacheEntry(Date requestDate, Date responseDate, + StatusLine statusLine, Header[] headers, + Resource resource, Map variantMap) { + this(requestDate, responseDate, statusLine, headers, + resource, null, variantMap); } public StatusLine getStatusLine() { @@ -141,16 +174,28 @@ public class HttpCacheEntry implements Serializable { return responseHeaders.getHeaders(name); } + public Resource getResource() { + return this.resource; + } + public boolean hasVariants() { return getFirstHeader(HeaderConstants.VARY) != null; } + @Deprecated public Set getVariantURIs() { - return Collections.unmodifiableSet(this.variantURIs); + if (variantMap != null) { + return Collections.unmodifiableSet(new HashSet(variantMap.values())); + } + return Collections.unmodifiableSet(variantURIs); } - public Resource getResource() { - return this.resource; + public Map getVariantMap() { + if (variantMap == null) { + throw new UnsupportedOperationException("variant maps not" + + "supported if constructed with deprecated variant set"); + } + return Collections.unmodifiableMap(variantMap); } @Override @@ -158,5 +203,4 @@ public class HttpCacheEntry implements Serializable { return "[request date=" + this.requestDate + "; response date=" + this.responseDate + "; statusLine=" + this.statusLine + "]"; } - } diff --git a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java index b5240933c..205fa8f6d 100644 --- a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java +++ b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java @@ -28,7 +28,9 @@ package org.apache.http.impl.client.cache; import java.io.IOException; import java.util.Date; +import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Set; import org.apache.commons.logging.Log; @@ -165,8 +167,9 @@ class BasicHttpCache implements HttpCache { if (src == null) { src = entry; } - Set variants = new HashSet(src.getVariantURIs()); - variants.add(variantURI); + + Map variantMap = new HashMap(src.getVariantMap()); + variantMap.put(variantURI, variantURI); Resource resource = resourceFactory.copy(requestId, src.getResource()); return new HttpCacheEntry( src.getRequestDate(), @@ -174,7 +177,7 @@ class BasicHttpCache implements HttpCache { src.getStatusLine(), src.getAllHeaders(), resource, - variants); + variantMap); } public HttpCacheEntry updateCacheEntry(HttpHost target, HttpRequest request, @@ -211,8 +214,7 @@ class BasicHttpCache implements HttpCache { responseReceived, originResponse.getStatusLine(), originResponse.getAllHeaders(), - resource, - null); + resource); storeInCache(host, request, entry); return responseGenerator.generateResponse(entry); } @@ -243,7 +245,7 @@ class BasicHttpCache implements HttpCache { HttpCacheEntry root = storage.getEntry(uriExtractor.getURI(host, request)); if (root != null) { if (root.hasVariants()) { - for(String variantUri : root.getVariantURIs()) { + for(String variantUri : root.getVariantMap().values()) { variants.add(storage.getEntry(variantUri)); } } diff --git a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntryUpdater.java b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntryUpdater.java index 015bc1c65..47eb628b5 100644 --- a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntryUpdater.java +++ b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntryUpdater.java @@ -93,8 +93,7 @@ class CacheEntryUpdater { responseDate, entry.getStatusLine(), mergedHeaders, - resource, - null); + resource); } protected Header[] mergeHeaders(HttpCacheEntry entry, HttpResponse response) { diff --git a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheInvalidator.java b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheInvalidator.java index a32ce718a..86cd5b7c9 100644 --- a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheInvalidator.java +++ b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheInvalidator.java @@ -86,7 +86,7 @@ class CacheInvalidator { log.debug("parent entry: " + parent); if (parent != null) { - for (String variantURI : parent.getVariantURIs()) { + for (String variantURI : parent.getVariantMap().values()) { storage.removeEntry(variantURI); } storage.removeEntry(theUri); diff --git a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/URIExtractor.java b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/URIExtractor.java index 49bd7d1ff..447930565 100644 --- a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/URIExtractor.java +++ b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/URIExtractor.java @@ -140,7 +140,10 @@ class URIExtractor { if (varyHdrs == null || varyHdrs.length == 0) { return getURI(host, req); } + return getVariantKey(req, varyHdrs) + getURI(host, req); + } + public String getVariantKey(HttpRequest req, Header[] varyHdrs) { List variantHeaderNames = new ArrayList(); for (Header varyHdr : varyHdrs) { for (HeaderElement elt : varyHdr.getElements()) { @@ -149,8 +152,9 @@ class URIExtractor { } Collections.sort(variantHeaderNames); + StringBuilder buf; try { - StringBuilder buf = new StringBuilder("{"); + buf = new StringBuilder("{"); boolean first = true; for (String headerName : variantHeaderNames) { if (!first) { @@ -163,11 +167,10 @@ class URIExtractor { first = false; } buf.append("}"); - buf.append(getURI(host, req)); - return buf.toString(); } catch (UnsupportedEncodingException uee) { throw new RuntimeException("couldn't encode to UTF-8", uee); } + return buf.toString(); } } \ No newline at end of file diff --git a/httpclient-cache/src/test/java/org/apache/http/client/cache/TestHttpCacheEntry.java b/httpclient-cache/src/test/java/org/apache/http/client/cache/TestHttpCacheEntry.java index 6ed5b8dbc..8d450ea53 100644 --- a/httpclient-cache/src/test/java/org/apache/http/client/cache/TestHttpCacheEntry.java +++ b/httpclient-cache/src/test/java/org/apache/http/client/cache/TestHttpCacheEntry.java @@ -30,7 +30,9 @@ import static org.easymock.classextension.EasyMock.*; import static org.junit.Assert.*; import java.util.Date; +import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Set; import org.apache.http.Header; @@ -65,7 +67,7 @@ public class TestHttpCacheEntry { private HttpCacheEntry makeEntry(Header[] headers) { return new HttpCacheEntry(elevenSecondsAgo, nineSecondsAgo, - statusLine, headers, mockResource, null); + statusLine, headers, mockResource); } @Test @@ -142,7 +144,7 @@ public class TestHttpCacheEntry { public void mustProvideRequestDate() { try { new HttpCacheEntry(null, new Date(), statusLine, - new Header[]{}, mockResource, null); + new Header[]{}, mockResource); fail("Should have thrown exception"); } catch (IllegalArgumentException expected) { } @@ -152,7 +154,7 @@ public class TestHttpCacheEntry { public void mustProvideResponseDate() { try { new HttpCacheEntry(new Date(), null, statusLine, - new Header[]{}, mockResource, null); + new Header[]{}, mockResource); fail("Should have thrown exception"); } catch (IllegalArgumentException expected) { } @@ -162,7 +164,7 @@ public class TestHttpCacheEntry { public void mustProvideStatusLine() { try { new HttpCacheEntry(new Date(), new Date(), null, - new Header[]{}, mockResource, null); + new Header[]{}, mockResource); fail("Should have thrown exception"); } catch (IllegalArgumentException expected) { } @@ -172,7 +174,7 @@ public class TestHttpCacheEntry { public void mustProvideResponseHeaders() { try { new HttpCacheEntry(new Date(), new Date(), statusLine, - null, mockResource, null); + null, mockResource); fail("Should have thrown exception"); } catch (IllegalArgumentException expected) { } @@ -182,29 +184,23 @@ public class TestHttpCacheEntry { public void mustProvideResource() { try { new HttpCacheEntry(new Date(), new Date(), statusLine, - new Header[]{}, null, null); + new Header[]{}, null); fail("Should have thrown exception"); } catch (IllegalArgumentException expected) { } } - @Test - public void canProvideVariantSet() { - new HttpCacheEntry(new Date(), new Date(), statusLine, - new Header[]{}, mockResource, new HashSet()); - } - @Test public void canRetrieveOriginalStatusLine() { entry = new HttpCacheEntry(new Date(), new Date(), statusLine, - new Header[]{}, mockResource, null); + new Header[]{}, mockResource); assertSame(statusLine, entry.getStatusLine()); } @Test public void protocolVersionComesFromOriginalStatusLine() { entry = new HttpCacheEntry(new Date(), new Date(), statusLine, - new Header[]{}, mockResource, null); + new Header[]{}, mockResource); assertSame(statusLine.getProtocolVersion(), entry.getProtocolVersion()); } @@ -212,14 +208,14 @@ public class TestHttpCacheEntry { @Test public void reasonPhraseComesFromOriginalStatusLine() { entry = new HttpCacheEntry(new Date(), new Date(), statusLine, - new Header[]{}, mockResource, null); + new Header[]{}, mockResource); assertSame(statusLine.getReasonPhrase(), entry.getReasonPhrase()); } @Test public void statusCodeComesFromOriginalStatusLine() { entry = new HttpCacheEntry(new Date(), new Date(), statusLine, - new Header[]{}, mockResource, null); + new Header[]{}, mockResource); assertEquals(statusLine.getStatusCode(), entry.getStatusCode()); } @@ -227,7 +223,7 @@ public class TestHttpCacheEntry { public void canGetOriginalRequestDate() { final Date requestDate = new Date(); entry = new HttpCacheEntry(requestDate, new Date(), statusLine, - new Header[]{}, mockResource, null); + new Header[]{}, mockResource); assertSame(requestDate, entry.getRequestDate()); } @@ -235,14 +231,14 @@ public class TestHttpCacheEntry { public void canGetOriginalResponseDate() { final Date responseDate = new Date(); entry = new HttpCacheEntry(new Date(), responseDate, statusLine, - new Header[]{}, mockResource, null); + new Header[]{}, mockResource); assertSame(responseDate, entry.getResponseDate()); } @Test public void canGetOriginalResource() { entry = new HttpCacheEntry(new Date(), new Date(), statusLine, - new Header[]{}, mockResource, null); + new Header[]{}, mockResource); assertSame(mockResource, entry.getResource()); } @@ -253,7 +249,7 @@ public class TestHttpCacheEntry { new BasicHeader("Date", DateUtils.formatDate(now)) }; entry = new HttpCacheEntry(new Date(), new Date(), statusLine, - headers, mockResource, null); + headers, mockResource); Header[] result = entry.getAllHeaders(); assertEquals(headers.length, result.length); for(int i=0; i variants = new HashSet(); @@ -275,6 +272,22 @@ public class TestHttpCacheEntry { } } + @SuppressWarnings("deprecation") + @Test + public void throwsExceptionWhenRetrievingVariantMapIfConstructedWithVariantSet() { + Set variants = new HashSet(); + variants.add("variant1"); + variants.add("variant2"); + entry = new HttpCacheEntry(new Date(), new Date(), statusLine, + new Header[]{}, mockResource, variants); + try { + entry.getVariantMap(); + fail("should have thrown exception"); + } catch (UnsupportedOperationException expected) { + } + } + + @SuppressWarnings("deprecation") @Test public void variantSetIsNotModifiable() { Set variants = new HashSet(); @@ -294,11 +307,74 @@ public class TestHttpCacheEntry { } catch (UnsupportedOperationException expected) { } } + + @Test + public void canConstructWithoutVariants() { + new HttpCacheEntry(new Date(), new Date(), statusLine, + new Header[]{}, mockResource); + } + @Test + public void canProvideVariantMap() { + new HttpCacheEntry(new Date(), new Date(), statusLine, + new Header[]{}, mockResource, + new HashMap()); + } + + @Test + public void canRetrieveOriginalVariantMap() { + Map variantMap = new HashMap(); + variantMap.put("A","B"); + variantMap.put("C","D"); + entry = new HttpCacheEntry(new Date(), new Date(), statusLine, + new Header[]{}, mockResource, + variantMap); + Map result = entry.getVariantMap(); + assertEquals(2, result.size()); + assertEquals("B", result.get("A")); + assertEquals("D", result.get("C")); + } + + @SuppressWarnings("deprecation") + @Test + public void returnsVariantMapValuesForVariantSet() { + Map variantMap = new HashMap(); + variantMap.put("A","B"); + variantMap.put("C","D"); + entry = new HttpCacheEntry(new Date(), new Date(), statusLine, + new Header[]{}, mockResource, + variantMap); + Set result = entry.getVariantURIs(); + assertEquals(2, result.size()); + assertTrue(result.contains("B")); + assertTrue(result.contains("D")); + } + + @Test + public void retrievedVariantMapIsNotModifiable() { + Map variantMap = new HashMap(); + variantMap.put("A","B"); + variantMap.put("C","D"); + entry = new HttpCacheEntry(new Date(), new Date(), statusLine, + new Header[]{}, mockResource, + variantMap); + Map result = entry.getVariantMap(); + try { + result.remove("A"); + fail("Should have thrown exception"); + } catch (UnsupportedOperationException expected) { + } + try { + result.put("E","F"); + fail("Should have thrown exception"); + } catch (UnsupportedOperationException expected) { + } + } + @Test public void canConvertToString() { entry = new HttpCacheEntry(new Date(), new Date(), statusLine, - new Header[]{}, mockResource, null); + new Header[]{}, mockResource); assertNotNull(entry.toString()); assertFalse("".equals(entry.toString())); } diff --git a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/HttpTestUtils.java b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/HttpTestUtils.java index 84f1a985b..cff909652 100644 --- a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/HttpTestUtils.java +++ b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/HttpTestUtils.java @@ -28,9 +28,8 @@ package org.apache.http.impl.client.cache; import java.io.InputStream; import java.util.Date; +import java.util.Map; import java.util.Random; -import java.util.Set; - import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpMessage; @@ -254,24 +253,24 @@ public class HttpTestUtils { public static HttpCacheEntry makeCacheEntry(Date requestDate, Date responseDate, Header[] headers, byte[] bytes) { - Set variants = null; + Map variantMap = null; return makeCacheEntry(requestDate, responseDate, headers, bytes, - variants); + variantMap); } - public static HttpCacheEntry makeCacheEntry(Set variants) { + public static HttpCacheEntry makeCacheEntry(Map variantMap) { Date now = new Date(); return makeCacheEntry(now, now, getStockHeaders(now), - getRandomBytes(128), variants); + getRandomBytes(128), variantMap); } - + public static HttpCacheEntry makeCacheEntry(Date requestDate, Date responseDate, Header[] headers, byte[] bytes, - Set variants) { + Map variantMap) { StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); - return new HttpCacheEntry(requestDate, responseDate, statusLine, headers, new HeapResource(bytes), variants); + return new HttpCacheEntry(requestDate, responseDate, statusLine, headers, new HeapResource(bytes), variantMap); } - + public static HttpCacheEntry makeCacheEntry(Header[] headers, byte[] bytes) { Date now = new Date(); return makeCacheEntry(now, now, headers, bytes); diff --git a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestBasicHttpCache.java b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestBasicHttpCache.java index 6fb083c28..a44951d43 100644 --- a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestBasicHttpCache.java +++ b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestBasicHttpCache.java @@ -35,7 +35,8 @@ import static org.junit.Assert.assertTrue; import java.io.InputStream; import java.util.Date; -import java.util.HashSet; +import java.util.HashMap; +import java.util.Map; import java.util.Set; import org.apache.http.Header; @@ -198,15 +199,15 @@ public class TestBasicHttpCache { final String parentKey = "parentKey"; final String variantKey = "variantKey"; final String existingVariantKey = "existingVariantKey"; - final Set existingVariants = new HashSet(); - existingVariants.add(existingVariantKey); + final Map existingVariants = new HashMap(); + existingVariants.put(existingVariantKey,existingVariantKey); final HttpCacheEntry parent = HttpTestUtils.makeCacheEntry(existingVariants); final HttpCacheEntry variant = HttpTestUtils.makeCacheEntry(); HttpCacheEntry result = impl.doGetUpdatedParentEntry(parentKey, parent, variant, variantKey); - assertEquals(2, result.getVariantURIs().size()); - assertTrue(result.getVariantURIs().contains(existingVariantKey)); - assertTrue(result.getVariantURIs().contains(variantKey)); + assertEquals(2, result.getVariantMap().size()); + assertTrue(result.getVariantMap().containsKey(existingVariantKey)); + assertTrue(result.getVariantMap().containsKey(variantKey)); } @Test diff --git a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCacheInvalidator.java b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCacheInvalidator.java index d82c1de94..57ba76a48 100644 --- a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCacheInvalidator.java +++ b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCacheInvalidator.java @@ -27,9 +27,8 @@ package org.apache.http.impl.client.cache; import java.io.IOException; -import java.util.HashSet; -import java.util.Set; - +import java.util.HashMap; +import java.util.Map; import org.apache.http.HttpEntityEnclosingRequest; import org.apache.http.HttpHost; import org.apache.http.HttpRequest; @@ -78,8 +77,8 @@ public class TestCacheInvalidator { HttpRequest request = new BasicHttpRequest("POST","/path", HTTP_1_1); final String theUri = "http://foo.example.com:80/path"; - Set variantURIs = new HashSet(); - cacheEntryHasVariantURIs(variantURIs); + Map variantMap = new HashMap(); + cacheEntryHasVariantMap(variantMap); cacheReturnsEntryForUri(theUri); entryIsRemoved(theUri); @@ -100,8 +99,7 @@ public class TestCacheInvalidator { request.setHeader("Content-Location", contentLocation); final String theUri = "http://foo.example.com:80/"; - Set variantURIs = new HashSet(); - cacheEntryHasVariantURIs(variantURIs); + cacheEntryHasVariantMap(new HashMap()); cacheReturnsEntryForUri(theUri); entryIsRemoved(theUri); @@ -124,8 +122,7 @@ public class TestCacheInvalidator { request.setHeader("Location",contentLocation); final String theUri = "http://foo.example.com:80/"; - Set variantURIs = new HashSet(); - cacheEntryHasVariantURIs(variantURIs); + cacheEntryHasVariantMap(new HashMap()); cacheReturnsEntryForUri(theUri); entryIsRemoved(theUri); @@ -148,8 +145,7 @@ public class TestCacheInvalidator { request.setHeader("Content-Location",relativePath); final String theUri = "http://foo.example.com:80/"; - Set variantURIs = new HashSet(); - cacheEntryHasVariantURIs(variantURIs); + cacheEntryHasVariantMap(new HashMap()); cacheReturnsEntryForUri(theUri); entryIsRemoved(theUri); @@ -172,8 +168,7 @@ public class TestCacheInvalidator { request.setHeader("Content-Location",contentLocation); final String theUri = "http://foo.example.com:80/"; - Set variantURIs = new HashSet(); - cacheEntryHasVariantURIs(variantURIs); + cacheEntryHasVariantMap(new HashMap()); cacheReturnsEntryForUri(theUri); entryIsRemoved(theUri); @@ -238,11 +233,11 @@ public class TestCacheInvalidator { final String theUri = "http://foo.example.com:80/"; final String variantUri = "theVariantURI"; - Set listOfURIs = new HashSet(); - listOfURIs.add(variantUri); + Map mapOfURIs = new HashMap(); + mapOfURIs.put(variantUri,variantUri); cacheReturnsEntryForUri(theUri); - cacheEntryHasVariantURIs(listOfURIs); + cacheEntryHasVariantMap(mapOfURIs); entryIsRemoved(variantUri); entryIsRemoved(theUri); @@ -264,10 +259,8 @@ public class TestCacheInvalidator { } // Expectations - - - private void cacheEntryHasVariantURIs(Set variantURIs) { - org.easymock.EasyMock.expect(mockEntry.getVariantURIs()).andReturn(variantURIs); + private void cacheEntryHasVariantMap(Map variantMap) { + org.easymock.EasyMock.expect(mockEntry.getVariantMap()).andReturn(variantMap); } private void cacheReturnsEntryForUri(String theUri) throws IOException { diff --git a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestHttpCacheEntrySerializers.java b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestHttpCacheEntrySerializers.java index 7d5f93280..81cb27b78 100644 --- a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestHttpCacheEntrySerializers.java +++ b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestHttpCacheEntrySerializers.java @@ -34,11 +34,11 @@ import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import java.util.Arrays; import java.util.Date; +import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Set; -import junit.framework.TestCase; - import org.apache.commons.codec.binary.Base64; import org.apache.http.Header; import org.apache.http.ProtocolVersion; @@ -48,30 +48,46 @@ import org.apache.http.client.cache.HttpCacheEntrySerializer; import org.apache.http.client.cache.Resource; import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicStatusLine; +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; -public class TestHttpCacheEntrySerializers extends TestCase { +public class TestHttpCacheEntrySerializers { private static final Charset UTF8 = Charset.forName("UTF-8"); - public void testDefaultSerializer() throws Exception { - readWriteVerify(new DefaultHttpCacheEntrySerializer()); + private HttpCacheEntrySerializer impl; + + @Before + public void setUp() { + impl = new DefaultHttpCacheEntrySerializer(); + } + + @Test + public void canSerializeOldEntriesWithVariantSets() throws Exception { + readWriteVerify(makeCacheEntryWithOldVariantSet()); } - public void readWriteVerify(HttpCacheEntrySerializer serializer) throws IOException { + @Test + public void canSerializeEntriesWithVariantMaps() throws Exception { + readWriteVerify(makeCacheEntryWithVariantMap()); + } + + public void readWriteVerify(HttpCacheEntry writeEntry) throws IOException { // write the entry - HttpCacheEntry writeEntry = newCacheEntry(); ByteArrayOutputStream out = new ByteArrayOutputStream(); - serializer.writeTo(writeEntry, out); + impl.writeTo(writeEntry, out); // read the entry ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - HttpCacheEntry readEntry = serializer.readFrom(in); + HttpCacheEntry readEntry = impl.readFrom(in); // compare assertTrue(areEqual(readEntry, writeEntry)); } - private HttpCacheEntry newCacheEntry() throws UnsupportedEncodingException { + @SuppressWarnings("deprecation") + private HttpCacheEntry makeCacheEntryWithOldVariantSet() throws UnsupportedEncodingException { Header[] headers = new Header[5]; for (int i = 0; i < headers.length; i++) { headers[i] = new BasicHeader("header" + i, "value" + i); @@ -83,7 +99,6 @@ public class TestHttpCacheEntrySerializers extends TestCase { Set variants = new HashSet(); variants.add("test variant 1"); variants.add("test variant 2"); - HttpCacheEntry cacheEntry = new HttpCacheEntry(new Date(), new Date(), slObj, headers, new HeapResource(Base64.decodeBase64(body .getBytes(UTF8.name()))), variants); @@ -91,6 +106,25 @@ public class TestHttpCacheEntrySerializers extends TestCase { return cacheEntry; } + private HttpCacheEntry makeCacheEntryWithVariantMap() throws UnsupportedEncodingException { + Header[] headers = new Header[5]; + for (int i = 0; i < headers.length; i++) { + headers[i] = new BasicHeader("header" + i, "value" + i); + } + String body = "Lorem ipsum dolor sit amet"; + + ProtocolVersion pvObj = new ProtocolVersion("HTTP", 1, 1); + StatusLine slObj = new BasicStatusLine(pvObj, 200, "ok"); + Map variantMap = new HashMap(); + variantMap.put("test variant 1","true"); + variantMap.put("test variant 2","true"); + HttpCacheEntry cacheEntry = new HttpCacheEntry(new Date(), new Date(), + slObj, headers, new HeapResource(Base64.decodeBase64(body + .getBytes(UTF8.name()))), variantMap); + + return cacheEntry; + } + private boolean areEqual(HttpCacheEntry one, HttpCacheEntry two) throws IOException { // dates are only stored with second precision, so scrub milliseconds if (!((one.getRequestDate().getTime() / 1000) == (two.getRequestDate()