HTTPCLIENT-1032: change variant storage mechanism in HttpCacheEntry from
a Set to a Map; Set-based interface retained for backwards-compatibility. Currently, the map just maps a variantURI to itself--in essence it is still really just a set; however, this sets us up for mapping different variant keys to the same cache key (coming next). git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1044818 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
9771dcd667
commit
e025613aab
|
@ -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<String> variantURIs;
|
||||
private final Map<String,String> 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<String> variants) {
|
||||
final Set<String> variants,
|
||||
final Map<String,String> 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<String>(variants) : new HashSet<String>();
|
||||
this.variantURIs = variants != null
|
||||
? new HashSet<String>(variants) : null;
|
||||
this.variantMap = variantMap != null
|
||||
? new HashMap<String,String>(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<String> 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<String,String>());
|
||||
}
|
||||
|
||||
public HttpCacheEntry(Date requestDate, Date responseDate,
|
||||
StatusLine statusLine, Header[] headers,
|
||||
Resource resource, Map<String, String> 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<String> getVariantURIs() {
|
||||
return Collections.unmodifiableSet(this.variantURIs);
|
||||
if (variantMap != null) {
|
||||
return Collections.unmodifiableSet(new HashSet<String>(variantMap.values()));
|
||||
}
|
||||
return Collections.unmodifiableSet(variantURIs);
|
||||
}
|
||||
|
||||
public Resource getResource() {
|
||||
return this.resource;
|
||||
public Map<String, String> 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 + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<String> variants = new HashSet<String>(src.getVariantURIs());
|
||||
variants.add(variantURI);
|
||||
|
||||
Map<String,String> variantMap = new HashMap<String,String>(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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -93,8 +93,7 @@ class CacheEntryUpdater {
|
|||
responseDate,
|
||||
entry.getStatusLine(),
|
||||
mergedHeaders,
|
||||
resource,
|
||||
null);
|
||||
resource);
|
||||
}
|
||||
|
||||
protected Header[] mergeHeaders(HttpCacheEntry entry, HttpResponse response) {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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<String> variantHeaderNames = new ArrayList<String>();
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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<String>());
|
||||
}
|
||||
|
||||
@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<headers.length; i++) {
|
||||
|
@ -261,6 +257,7 @@ public class TestHttpCacheEntry {
|
|||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
public void canRetrieveOriginalVariantSet() {
|
||||
Set<String> variants = new HashSet<String>();
|
||||
|
@ -275,6 +272,22 @@ public class TestHttpCacheEntry {
|
|||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
public void throwsExceptionWhenRetrievingVariantMapIfConstructedWithVariantSet() {
|
||||
Set<String> variants = new HashSet<String>();
|
||||
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<String> variants = new HashSet<String>();
|
||||
|
@ -295,10 +308,73 @@ public class TestHttpCacheEntry {
|
|||
}
|
||||
}
|
||||
|
||||
@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<String,String>());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canRetrieveOriginalVariantMap() {
|
||||
Map<String,String> variantMap = new HashMap<String,String>();
|
||||
variantMap.put("A","B");
|
||||
variantMap.put("C","D");
|
||||
entry = new HttpCacheEntry(new Date(), new Date(), statusLine,
|
||||
new Header[]{}, mockResource,
|
||||
variantMap);
|
||||
Map<String,String> result = entry.getVariantMap();
|
||||
assertEquals(2, result.size());
|
||||
assertEquals("B", result.get("A"));
|
||||
assertEquals("D", result.get("C"));
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
public void returnsVariantMapValuesForVariantSet() {
|
||||
Map<String,String> variantMap = new HashMap<String,String>();
|
||||
variantMap.put("A","B");
|
||||
variantMap.put("C","D");
|
||||
entry = new HttpCacheEntry(new Date(), new Date(), statusLine,
|
||||
new Header[]{}, mockResource,
|
||||
variantMap);
|
||||
Set<String> result = entry.getVariantURIs();
|
||||
assertEquals(2, result.size());
|
||||
assertTrue(result.contains("B"));
|
||||
assertTrue(result.contains("D"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retrievedVariantMapIsNotModifiable() {
|
||||
Map<String,String> variantMap = new HashMap<String,String>();
|
||||
variantMap.put("A","B");
|
||||
variantMap.put("C","D");
|
||||
entry = new HttpCacheEntry(new Date(), new Date(), statusLine,
|
||||
new Header[]{}, mockResource,
|
||||
variantMap);
|
||||
Map<String,String> 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()));
|
||||
}
|
||||
|
|
|
@ -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,22 +253,22 @@ public class HttpTestUtils {
|
|||
|
||||
public static HttpCacheEntry makeCacheEntry(Date requestDate,
|
||||
Date responseDate, Header[] headers, byte[] bytes) {
|
||||
Set<String> variants = null;
|
||||
Map<String,String> variantMap = null;
|
||||
return makeCacheEntry(requestDate, responseDate, headers, bytes,
|
||||
variants);
|
||||
variantMap);
|
||||
}
|
||||
|
||||
public static HttpCacheEntry makeCacheEntry(Set<String> variants) {
|
||||
public static HttpCacheEntry makeCacheEntry(Map<String,String> 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<String> variants) {
|
||||
Map<String,String> 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) {
|
||||
|
|
|
@ -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<String> existingVariants = new HashSet<String>();
|
||||
existingVariants.add(existingVariantKey);
|
||||
final Map<String,String> existingVariants = new HashMap<String,String>();
|
||||
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
|
||||
|
|
|
@ -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<String> variantURIs = new HashSet<String>();
|
||||
cacheEntryHasVariantURIs(variantURIs);
|
||||
Map<String,String> variantMap = new HashMap<String,String>();
|
||||
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<String> variantURIs = new HashSet<String>();
|
||||
cacheEntryHasVariantURIs(variantURIs);
|
||||
cacheEntryHasVariantMap(new HashMap<String,String>());
|
||||
|
||||
cacheReturnsEntryForUri(theUri);
|
||||
entryIsRemoved(theUri);
|
||||
|
@ -124,8 +122,7 @@ public class TestCacheInvalidator {
|
|||
request.setHeader("Location",contentLocation);
|
||||
|
||||
final String theUri = "http://foo.example.com:80/";
|
||||
Set<String> variantURIs = new HashSet<String>();
|
||||
cacheEntryHasVariantURIs(variantURIs);
|
||||
cacheEntryHasVariantMap(new HashMap<String,String>());
|
||||
|
||||
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<String> variantURIs = new HashSet<String>();
|
||||
cacheEntryHasVariantURIs(variantURIs);
|
||||
cacheEntryHasVariantMap(new HashMap<String,String>());
|
||||
|
||||
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<String> variantURIs = new HashSet<String>();
|
||||
cacheEntryHasVariantURIs(variantURIs);
|
||||
cacheEntryHasVariantMap(new HashMap<String,String>());
|
||||
|
||||
cacheReturnsEntryForUri(theUri);
|
||||
entryIsRemoved(theUri);
|
||||
|
@ -238,11 +233,11 @@ public class TestCacheInvalidator {
|
|||
final String theUri = "http://foo.example.com:80/";
|
||||
final String variantUri = "theVariantURI";
|
||||
|
||||
Set<String> listOfURIs = new HashSet<String>();
|
||||
listOfURIs.add(variantUri);
|
||||
Map<String,String> mapOfURIs = new HashMap<String,String>();
|
||||
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<String> variantURIs) {
|
||||
org.easymock.EasyMock.expect(mockEntry.getVariantURIs()).andReturn(variantURIs);
|
||||
private void cacheEntryHasVariantMap(Map<String,String> variantMap) {
|
||||
org.easymock.EasyMock.expect(mockEntry.getVariantMap()).andReturn(variantMap);
|
||||
}
|
||||
|
||||
private void cacheReturnsEntryForUri(String theUri) throws IOException {
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
public void readWriteVerify(HttpCacheEntrySerializer serializer) throws IOException {
|
||||
@Test
|
||||
public void canSerializeOldEntriesWithVariantSets() throws Exception {
|
||||
readWriteVerify(makeCacheEntryWithOldVariantSet());
|
||||
}
|
||||
|
||||
@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<String> variants = new HashSet<String>();
|
||||
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<String,String> variantMap = new HashMap<String,String>();
|
||||
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()
|
||||
|
|
Loading…
Reference in New Issue