You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2010/08/18 21:15:45 UTC
svn commit: r986864 [2/2] - in
/httpcomponents/httpclient/trunk/httpclient-cache/src:
main/java/org/apache/http/client/cache/
main/java/org/apache/http/impl/client/cache/
main/java/org/apache/http/impl/client/cache/ehcache/
test/java/org/apache/http/im...
Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java?rev=986864&r1=986863&r2=986864&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java (original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java Wed Aug 18 19:15:43 2010
@@ -27,14 +27,11 @@
package org.apache.http.impl.client.cache;
import java.io.IOException;
-import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
-import org.apache.http.Header;
-import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
@@ -47,13 +44,9 @@ import org.apache.http.client.HttpClient
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.cache.HttpCache;
import org.apache.http.client.cache.HttpCacheEntry;
-import org.apache.http.client.cache.Resource;
-import org.apache.http.client.cache.ResourceFactory;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ClientConnectionManager;
-import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.message.BasicHttpRequest;
-import org.apache.http.message.BasicHttpResponse;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.BasicHttpContext;
@@ -73,17 +66,9 @@ public class TestCachingHttpClient {
private static final String REVALIDATE_CACHE_ENTRY = "revalidateCacheEntry";
- private static final String GET_CACHE_ENTRY = "getCacheEntry";
-
- private static final String STORE_IN_CACHE = "storeInCache";
-
- private static final String GET_RESPONSE_READER = "getResponseReader";
-
private CachingHttpClient impl;
private boolean mockedImpl;
- private ResourceFactory mockResourceFactory;
- private CacheInvalidator mockInvalidator;
private CacheValidityPolicy mockValidityPolicy;
private CacheableRequestPolicy mockRequestPolicy;
private HttpClient mockBackend;
@@ -92,20 +77,14 @@ public class TestCachingHttpClient {
private ResponseCachingPolicy mockResponsePolicy;
private HttpResponse mockBackendResponse;
private CacheEntry mockCacheEntry;
- private CacheEntry mockVariantCacheEntry;
- private CacheEntry mockUpdatedCacheEntry;
- private URIExtractor mockExtractor;
private CachedHttpResponseGenerator mockResponseGenerator;
- private SizeLimitedResponseReader mockResponseReader;
private ClientConnectionManager mockConnectionManager;
private ResponseHandler<Object> mockHandler;
private HttpUriRequest mockUriRequest;
private HttpResponse mockCachedResponse;
- private HttpResponse mockReconstructedResponse;
private ConditionalRequestBuilder mockConditionalRequestBuilder;
private HttpRequest mockConditionalRequest;
private StatusLine mockStatusLine;
- private CacheEntryUpdater mockCacheEntryUpdater;
private ResponseProtocolCompliance mockResponseProtocolCompliance;
private RequestProtocolCompliance mockRequestProtocolCompliance;
@@ -119,8 +98,6 @@ public class TestCachingHttpClient {
@SuppressWarnings("unchecked")
@Before
public void setUp() {
- mockResourceFactory = EasyMock.createMock(ResourceFactory.class);
- mockInvalidator = EasyMock.createMock(CacheInvalidator.class);
mockRequestPolicy = EasyMock.createMock(CacheableRequestPolicy.class);
mockValidityPolicy = EasyMock.createMock(CacheValidityPolicy.class);
mockBackend = EasyMock.createMock(HttpClient.class);
@@ -132,17 +109,11 @@ public class TestCachingHttpClient {
mockBackendResponse = EasyMock.createMock(HttpResponse.class);
mockUriRequest = EasyMock.createMock(HttpUriRequest.class);
mockCacheEntry = EasyMock.createMock(CacheEntry.class);
- mockUpdatedCacheEntry = EasyMock.createMock(CacheEntry.class);
- mockVariantCacheEntry = EasyMock.createMock(CacheEntry.class);
- mockExtractor = EasyMock.createMock(URIExtractor.class);
mockResponseGenerator = EasyMock.createMock(CachedHttpResponseGenerator.class);
mockCachedResponse = EasyMock.createMock(HttpResponse.class);
mockConditionalRequestBuilder = EasyMock.createMock(ConditionalRequestBuilder.class);
mockConditionalRequest = EasyMock.createMock(HttpRequest.class);
mockStatusLine = EasyMock.createMock(StatusLine.class);
- mockCacheEntryUpdater = EasyMock.createMock(CacheEntryUpdater.class);
- mockResponseReader = EasyMock.createMock(SizeLimitedResponseReader.class);
- mockReconstructedResponse = EasyMock.createMock(HttpResponse.class);
mockResponseProtocolCompliance = EasyMock.createMock(ResponseProtocolCompliance.class);
mockRequestProtocolCompliance = EasyMock.createMock(RequestProtocolCompliance.class);
@@ -154,32 +125,24 @@ public class TestCachingHttpClient {
params = new BasicHttpParams();
impl = new CachingHttpClient(
mockBackend,
- mockResourceFactory,
mockValidityPolicy,
mockResponsePolicy,
- mockExtractor,
mockCache,
mockResponseGenerator,
- mockInvalidator,
mockRequestPolicy,
mockSuitabilityChecker,
mockConditionalRequestBuilder,
- mockCacheEntryUpdater,
mockResponseProtocolCompliance,
mockRequestProtocolCompliance);
}
private void replayMocks() {
- EasyMock.replay(mockResourceFactory);
- EasyMock.replay(mockInvalidator);
EasyMock.replay(mockRequestPolicy);
EasyMock.replay(mockValidityPolicy);
EasyMock.replay(mockSuitabilityChecker);
EasyMock.replay(mockResponsePolicy);
EasyMock.replay(mockCacheEntry);
- EasyMock.replay(mockVariantCacheEntry);
EasyMock.replay(mockResponseGenerator);
- EasyMock.replay(mockExtractor);
EasyMock.replay(mockBackend);
EasyMock.replay(mockCache);
EasyMock.replay(mockConnectionManager);
@@ -190,9 +153,6 @@ public class TestCachingHttpClient {
EasyMock.replay(mockConditionalRequestBuilder);
EasyMock.replay(mockConditionalRequest);
EasyMock.replay(mockStatusLine);
- EasyMock.replay(mockCacheEntryUpdater);
- EasyMock.replay(mockResponseReader);
- EasyMock.replay(mockReconstructedResponse);
EasyMock.replay(mockResponseProtocolCompliance);
EasyMock.replay(mockRequestProtocolCompliance);
if (mockedImpl) {
@@ -201,16 +161,12 @@ public class TestCachingHttpClient {
}
private void verifyMocks() {
- EasyMock.verify(mockResourceFactory);
- EasyMock.verify(mockInvalidator);
EasyMock.verify(mockRequestPolicy);
EasyMock.verify(mockValidityPolicy);
EasyMock.verify(mockSuitabilityChecker);
EasyMock.verify(mockResponsePolicy);
EasyMock.verify(mockCacheEntry);
- EasyMock.verify(mockVariantCacheEntry);
EasyMock.verify(mockResponseGenerator);
- EasyMock.verify(mockExtractor);
EasyMock.verify(mockBackend);
EasyMock.verify(mockCache);
EasyMock.verify(mockConnectionManager);
@@ -221,9 +177,6 @@ public class TestCachingHttpClient {
EasyMock.verify(mockConditionalRequestBuilder);
EasyMock.verify(mockConditionalRequest);
EasyMock.verify(mockStatusLine);
- EasyMock.verify(mockCacheEntryUpdater);
- EasyMock.verify(mockResponseReader);
- EasyMock.verify(mockReconstructedResponse);
EasyMock.verify(mockResponseProtocolCompliance);
EasyMock.verify(mockRequestProtocolCompliance);
if (mockedImpl) {
@@ -233,20 +186,12 @@ public class TestCachingHttpClient {
@Test
public void testCacheableResponsesGoIntoCache() throws Exception {
- mockImplMethods(STORE_IN_CACHE, GET_RESPONSE_READER);
responsePolicyAllowsCaching(true);
responseProtocolValidationIsCalled();
- getMockResponseReader();
- responseRead();
- responseLimitReached(false);
- responseGetResource();
- storeInCacheWasCalled();
- responseIsGeneratedFromCache();
- responseStatusLineIsInspectable();
- responseGetHeaders();
- responseDoesNotHaveExplicitContentLength();
+ EasyMock.expect(mockCache.cacheAndReturnResponse(host, request, mockBackendResponse, requestDate, responseDate))
+ .andReturn(mockCachedResponse);
replayMocks();
HttpResponse result = impl.handleBackendResponse(host, request, requestDate,
@@ -284,36 +229,12 @@ public class TestCachingHttpClient {
}
@Test
- public void testStoreInCachePutsNonVariantEntryInPlace() throws Exception {
-
- final String theURI = "theURI";
-
- cacheEntryHasVariants(false);
- extractTheURI(theURI);
- putInCache(theURI);
-
- replayMocks();
- impl.storeInCache(host, request, mockCacheEntry);
- verifyMocks();
- }
-
- @Test
- public void testCacheUpdateAddsVariantURIToParentEntry() throws Exception {
- final String variantURI = "variantURI";
- final CacheEntry entry = new CacheEntry();
- copyResource();
- replayMocks();
- impl.doGetUpdatedParentEntry("/stuff", null, entry, variantURI);
- verifyMocks();
- }
-
-
- @Test
public void testCacheMissCausesBackendRequest() throws Exception {
- mockImplMethods(GET_CACHE_ENTRY, CALL_BACKEND);
+ mockImplMethods(CALL_BACKEND);
cacheInvalidatorWasCalled();
requestPolicyAllowsCaching(true);
getCacheEntryReturns(null);
+
requestProtocolValidationIsCalled();
requestIsFatallyNonCompliant(null);
@@ -331,7 +252,7 @@ public class TestCachingHttpClient {
@Test
public void testUnsuitableUnvalidatableCacheEntryCausesBackendRequest() throws Exception {
- mockImplMethods(GET_CACHE_ENTRY, CALL_BACKEND);
+ mockImplMethods(CALL_BACKEND);
cacheInvalidatorWasCalled();
requestPolicyAllowsCaching(true);
requestProtocolValidationIsCalled();
@@ -354,7 +275,7 @@ public class TestCachingHttpClient {
@Test
public void testUnsuitableValidatableCacheEntryCausesRevalidation() throws Exception {
- mockImplMethods(GET_CACHE_ENTRY, REVALIDATE_CACHE_ENTRY);
+ mockImplMethods(REVALIDATE_CACHE_ENTRY);
cacheInvalidatorWasCalled();
requestPolicyAllowsCaching(true);
requestProtocolValidationIsCalled();
@@ -384,11 +305,9 @@ public class TestCachingHttpClient {
backendCallWasMadeWithRequest(mockConditionalRequest);
getCurrentDateReturns(responseDate);
backendResponseCodeIs(HttpStatus.SC_OK);
- cacheEntryUpdaterCalled();
- cacheEntryHasVariants(false, mockUpdatedCacheEntry);
- extractTheURI("http://foo.example.com");
- putInCache("http://foo.example.com", mockUpdatedCacheEntry);
- responseIsGeneratedFromCache(mockUpdatedCacheEntry);
+ EasyMock.expect(mockCache.updateCacheEntry(host, request,
+ mockCacheEntry, mockBackendResponse, requestDate, responseDate))
+ .andReturn(mockCachedResponse);
replayMocks();
@@ -406,17 +325,15 @@ public class TestCachingHttpClient {
@Test
public void testRevalidationUpdatesCacheEntryAndPutsItToCacheWhen304ReturningCachedResponse()
throws Exception {
- mockImplMethods(GET_CURRENT_DATE, STORE_IN_CACHE);
+ mockImplMethods(GET_CURRENT_DATE);
conditionalRequestBuilderCalled();
getCurrentDateReturns(requestDate);
backendCallWasMadeWithRequest(mockConditionalRequest);
getCurrentDateReturns(responseDate);
backendResponseCodeIs(HttpStatus.SC_NOT_MODIFIED);
-
- cacheEntryUpdaterCalled();
- storeInCacheWasCalled(mockUpdatedCacheEntry);
-
- responseIsGeneratedFromCache(mockUpdatedCacheEntry);
+ EasyMock.expect(mockCache.updateCacheEntry(host, request,
+ mockCacheEntry, mockBackendResponse, requestDate, responseDate))
+ .andReturn(mockCachedResponse);
replayMocks();
@@ -432,7 +349,6 @@ public class TestCachingHttpClient {
@Test
public void testSuitableCacheEntryDoesNotCauseBackendRequest() throws Exception {
- mockImplMethods(GET_CACHE_ENTRY);
cacheInvalidatorWasCalled();
requestPolicyAllowsCaching(true);
requestProtocolValidationIsCalled();
@@ -465,13 +381,11 @@ public class TestCachingHttpClient {
@Test
public void testNonCacheableResponseIsNotCachedAndIsReturnedAsIs() throws Exception {
- final String theURI = "theURI";
Date currentDate = new Date();
responsePolicyAllowsCaching(false);
responseProtocolValidationIsCalled();
- extractTheURI(theURI);
- removeFromCache(theURI);
+ flushCache();
replayMocks();
HttpResponse result = impl.handleBackendResponse(host, request, currentDate,
@@ -481,115 +395,6 @@ public class TestCachingHttpClient {
Assert.assertSame(mockBackendResponse, result);
}
- @Test
- public void testGetCacheEntryReturnsNullOnCacheMiss() throws Exception {
-
- final String theURI = "theURI";
- extractTheURI(theURI);
- gotCacheMiss(theURI);
-
- replayMocks();
- HttpCacheEntry result = impl.getCacheEntry(host, request);
- verifyMocks();
- Assert.assertNull(result);
- }
-
- @Test
- public void testGetCacheEntryFetchesFromCacheOnCacheHitIfNoVariants() throws Exception {
-
- final String theURI = "theURI";
- extractTheURI(theURI);
- gotCacheHit(theURI);
- cacheEntryHasVariants(false);
-
- replayMocks();
- HttpCacheEntry result = impl.getCacheEntry(host, request);
- verifyMocks();
- Assert.assertSame(mockCacheEntry, result);
- }
-
- @Test
- public void testGetCacheEntryReturnsNullIfNoVariantInCache() throws Exception {
-
- final String theURI = "theURI";
- final String variantURI = "variantURI";
- extractTheURI(theURI);
- gotCacheHit(theURI);
- cacheEntryHasVariants(true);
- extractVariantURI(variantURI);
- gotCacheMiss(variantURI);
-
- replayMocks();
- HttpCacheEntry result = impl.getCacheEntry(host, request);
- verifyMocks();
- Assert.assertNull(result);
- }
-
- @Test
- public void testGetCacheEntryReturnsVariantIfPresentInCache() throws Exception {
-
- final String theURI = "theURI";
- final String variantURI = "variantURI";
- extractTheURI(theURI);
- gotCacheHit(theURI, mockCacheEntry);
- cacheEntryHasVariants(true);
- extractVariantURI(variantURI);
- gotCacheHit(variantURI, mockVariantCacheEntry);
-
- replayMocks();
- HttpCacheEntry result = impl.getCacheEntry(host, request);
- verifyMocks();
- Assert.assertSame(mockVariantCacheEntry, result);
- }
-
- @Test
- public void testTooLargeResponsesAreNotCached() throws Exception {
- mockImplMethods(GET_CURRENT_DATE, GET_RESPONSE_READER, STORE_IN_CACHE);
- getCurrentDateReturns(requestDate);
- backendCallWasMadeWithRequest(request);
- responseProtocolValidationIsCalled();
-
- getCurrentDateReturns(responseDate);
- responsePolicyAllowsCaching(true);
- getMockResponseReader();
- responseRead();
- responseLimitReached(true);
- responseGetReconstructed();
-
- replayMocks();
-
- impl.callBackend(host, request, context);
-
- verifyMocks();
- }
-
- @Test
- public void testSmallEnoughResponsesAreCached() throws Exception {
- requestDate = new Date();
- responseDate = new Date();
- mockImplMethods(GET_CURRENT_DATE, GET_RESPONSE_READER, STORE_IN_CACHE);
- getCurrentDateReturns(requestDate);
- responseProtocolValidationIsCalled();
-
- backendCallWasMadeWithRequest(request);
- getCurrentDateReturns(responseDate);
- responsePolicyAllowsCaching(true);
- getMockResponseReader();
- responseRead();
- responseLimitReached(false);
- responseGetResource();
- storeInCacheWasCalled();
- responseIsGeneratedFromCache();
- responseStatusLineIsInspectable();
- responseGetHeaders();
- responseDoesNotHaveExplicitContentLength();
-
- replayMocks();
-
- impl.callBackend(host, request, context);
-
- verifyMocks();
- }
@Test
public void testCallsSelfForExecuteOnHostRequestWithNullContext() throws Exception {
@@ -599,17 +404,13 @@ public class TestCachingHttpClient {
final HttpResponse theResponse = mockBackendResponse;
impl = new CachingHttpClient(
mockBackend,
- mockResourceFactory,
mockValidityPolicy,
mockResponsePolicy,
- mockExtractor,
mockCache,
mockResponseGenerator,
- mockInvalidator,
mockRequestPolicy,
mockSuitabilityChecker,
mockConditionalRequestBuilder,
- mockCacheEntryUpdater,
mockResponseProtocolCompliance,
mockRequestProtocolCompliance) {
@Override
@@ -641,17 +442,13 @@ public class TestCachingHttpClient {
final Object value = new Object();
impl = new CachingHttpClient(
mockBackend,
- mockResourceFactory,
mockValidityPolicy,
mockResponsePolicy,
- mockExtractor,
mockCache,
mockResponseGenerator,
- mockInvalidator,
mockRequestPolicy,
mockSuitabilityChecker,
mockConditionalRequestBuilder,
- mockCacheEntryUpdater,
mockResponseProtocolCompliance,
mockRequestProtocolCompliance) {
@Override
@@ -691,17 +488,13 @@ public class TestCachingHttpClient {
final HttpContext theContext = context;
impl = new CachingHttpClient(
mockBackend,
- mockResourceFactory,
mockValidityPolicy,
mockResponsePolicy,
- mockExtractor,
mockCache,
mockResponseGenerator,
- mockInvalidator,
mockRequestPolicy,
mockSuitabilityChecker,
mockConditionalRequestBuilder,
- mockCacheEntryUpdater,
mockResponseProtocolCompliance,
mockRequestProtocolCompliance) {
@Override
@@ -733,17 +526,13 @@ public class TestCachingHttpClient {
final HttpResponse theResponse = mockBackendResponse;
impl = new CachingHttpClient(
mockBackend,
- mockResourceFactory,
mockValidityPolicy,
mockResponsePolicy,
- mockExtractor,
mockCache,
mockResponseGenerator,
- mockInvalidator,
mockRequestPolicy,
mockSuitabilityChecker,
mockConditionalRequestBuilder,
- mockCacheEntryUpdater,
mockResponseProtocolCompliance,
mockRequestProtocolCompliance) {
@Override
@@ -773,17 +562,13 @@ public class TestCachingHttpClient {
final HttpResponse theResponse = mockBackendResponse;
impl = new CachingHttpClient(
mockBackend,
- mockResourceFactory,
mockValidityPolicy,
mockResponsePolicy,
- mockExtractor,
mockCache,
mockResponseGenerator,
- mockInvalidator,
mockRequestPolicy,
mockSuitabilityChecker,
mockConditionalRequestBuilder,
- mockCacheEntryUpdater,
mockResponseProtocolCompliance,
mockRequestProtocolCompliance) {
@Override
@@ -816,17 +601,13 @@ public class TestCachingHttpClient {
final Object theValue = new Object();
impl = new CachingHttpClient(
mockBackend,
- mockResourceFactory,
mockValidityPolicy,
mockResponsePolicy,
- mockExtractor,
mockCache,
mockResponseGenerator,
- mockInvalidator,
mockRequestPolicy,
mockSuitabilityChecker,
mockConditionalRequestBuilder,
- mockCacheEntryUpdater,
mockResponseProtocolCompliance,
mockRequestProtocolCompliance) {
@Override
@@ -861,17 +642,13 @@ public class TestCachingHttpClient {
final Object theValue = new Object();
impl = new CachingHttpClient(
mockBackend,
- mockResourceFactory,
mockValidityPolicy,
mockResponsePolicy,
- mockExtractor,
mockCache,
mockResponseGenerator,
- mockInvalidator,
mockRequestPolicy,
mockSuitabilityChecker,
mockConditionalRequestBuilder,
- mockCacheEntryUpdater,
mockResponseProtocolCompliance,
mockRequestProtocolCompliance) {
@Override
@@ -916,17 +693,13 @@ public class TestCachingHttpClient {
@Test
public void testResponseIsGeneratedWhenCacheEntryIsUsable() throws Exception {
- final String theURI = "http://foo";
-
requestIsFatallyNonCompliant(null);
requestProtocolValidationIsCalled();
cacheInvalidatorWasCalled();
requestPolicyAllowsCaching(true);
cacheEntrySuitable(true);
- extractTheURI(theURI);
- gotCacheHit(theURI);
+ getCacheEntryReturns(mockCacheEntry);
responseIsGeneratedFromCache();
- cacheEntryHasVariants(false);
replayMocks();
impl.execute(host, request, context);
@@ -954,122 +727,16 @@ public class TestCachingHttpClient {
}
@Test
- public void testCorrectIncompleteResponseDoesNotCorrectComplete200Response()
- throws Exception {
- HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
- byte[] bytes = HttpTestUtils.getRandomBytes(128);
- resp.setEntity(new ByteArrayEntity(bytes));
- resp.setHeader("Content-Length","128");
-
- HttpResponse result = impl.correctIncompleteResponse(resp, new HeapResource(bytes));
- Assert.assertTrue(HttpTestUtils.semanticallyTransparent(resp, result));
- }
-
- @Test
- public void testCorrectIncompleteResponseDoesNotCorrectComplete206Response()
- throws Exception {
- HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_PARTIAL_CONTENT, "Partial Content");
- byte[] bytes = HttpTestUtils.getRandomBytes(128);
- resp.setEntity(new ByteArrayEntity(bytes));
- resp.setHeader("Content-Length","128");
- resp.setHeader("Content-Range","bytes 0-127/255");
-
- HttpResponse result = impl.correctIncompleteResponse(resp, new HeapResource(bytes));
- Assert.assertTrue(HttpTestUtils.semanticallyTransparent(resp, result));
- }
-
- @Test
- public void testCorrectIncompleteResponseGenerates502ForIncomplete200Response()
- throws Exception {
- HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
- byte[] bytes = HttpTestUtils.getRandomBytes(128);
- resp.setEntity(new ByteArrayEntity(bytes));
- resp.setHeader("Content-Length","256");
-
- HttpResponse result = impl.correctIncompleteResponse(resp, new HeapResource(bytes));
- Assert.assertTrue(HttpStatus.SC_BAD_GATEWAY == result.getStatusLine().getStatusCode());
- }
-
- @Test
- public void testCorrectIncompleteResponseDoesNotCorrectIncompleteNon200Or206Responses()
- throws Exception {
- HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_FORBIDDEN, "Forbidden");
- byte[] bytes = HttpTestUtils.getRandomBytes(128);
- resp.setEntity(new ByteArrayEntity(bytes));
- resp.setHeader("Content-Length","256");
-
- HttpResponse result = impl.correctIncompleteResponse(resp, new HeapResource(bytes));
- Assert.assertTrue(HttpTestUtils.semanticallyTransparent(resp, result));
- }
-
- @Test
- public void testCorrectIncompleteResponseDoesNotCorrectResponsesWithoutExplicitContentLength()
- throws Exception {
- HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
- byte[] bytes = HttpTestUtils.getRandomBytes(128);
- resp.setEntity(new ByteArrayEntity(bytes));
-
- HttpResponse result = impl.correctIncompleteResponse(resp, new HeapResource(bytes));
- Assert.assertTrue(HttpTestUtils.semanticallyTransparent(resp, result));
- }
-
- @Test
- public void testCorrectIncompleteResponseDoesNotCorrectResponsesWithUnparseableContentLengthHeader()
- throws Exception {
- HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
- byte[] bytes = HttpTestUtils.getRandomBytes(128);
- resp.setHeader("Content-Length","foo");
- resp.setEntity(new ByteArrayEntity(bytes));
-
- HttpResponse result = impl.correctIncompleteResponse(resp, new HeapResource(bytes));
- Assert.assertTrue(HttpTestUtils.semanticallyTransparent(resp, result));
- }
-
- @Test
- public void testCorrectIncompleteResponseProvidesPlainTextErrorMessage()
- throws Exception {
- HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
- byte[] bytes = HttpTestUtils.getRandomBytes(128);
- resp.setEntity(new ByteArrayEntity(bytes));
- resp.setHeader("Content-Length","256");
-
- HttpResponse result = impl.correctIncompleteResponse(resp, new HeapResource(bytes));
- Header ctype = result.getFirstHeader("Content-Type");
- Assert.assertEquals("text/plain;charset=UTF-8", ctype.getValue());
- }
-
- @Test
- public void testCorrectIncompleteResponseProvidesNonEmptyErrorMessage()
- throws Exception {
- HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
- byte[] bytes = HttpTestUtils.getRandomBytes(128);
- resp.setEntity(new ByteArrayEntity(bytes));
- resp.setHeader("Content-Length","256");
-
- HttpResponse result = impl.correctIncompleteResponse(resp, new HeapResource(bytes));
- int clen = Integer.parseInt(result.getFirstHeader("Content-Length").getValue());
- Assert.assertTrue(clen > 0);
- HttpEntity body = result.getEntity();
- if (body.getContentLength() < 0) {
- InputStream is = body.getContent();
- int bytes_read = 0;
- while((is.read()) != -1) {
- bytes_read++;
- }
- is.close();
- Assert.assertEquals(clen, bytes_read);
- } else {
- Assert.assertTrue(body.getContentLength() == clen);
- }
- }
-
- @Test
public void testIsSharedCache() {
Assert.assertTrue(impl.isSharedCache());
}
+ private void getCacheEntryReturns(HttpCacheEntry result) throws IOException {
+ EasyMock.expect(mockCache.getCacheEntry(host, request)).andReturn(result);
+ }
+
private void cacheInvalidatorWasCalled() throws IOException {
- mockInvalidator.flushInvalidatedCacheEntries(
+ mockCache.flushInvalidatedCacheEntriesFor(
EasyMock.<HttpHost>anyObject(),
EasyMock.<HttpRequest>anyObject());
}
@@ -1096,22 +763,6 @@ public class TestCachingHttpClient {
EasyMock.<HttpCacheEntry>anyObject())).andReturn(b);
}
- private void cacheEntryUpdaterCalled() throws IOException {
- EasyMock.expect(
- mockCacheEntryUpdater.updateCacheEntry(
- EasyMock.<String>anyObject(),
- EasyMock.<HttpCacheEntry>anyObject(),
- EasyMock.<Date>anyObject(),
- EasyMock.<Date>anyObject(),
- EasyMock.<HttpResponse>anyObject())).andReturn(mockUpdatedCacheEntry);
- }
-
- private void getCacheEntryReturns(CacheEntry entry) throws IOException {
- EasyMock.expect(impl.getCacheEntry(
- EasyMock.<HttpHost>anyObject(),
- EasyMock.<HttpRequest>anyObject())).andReturn(entry);
- }
-
private void backendResponseCodeIs(int code) {
EasyMock.expect(mockBackendResponse.getStatusLine()).andReturn(mockStatusLine);
EasyMock.expect(mockStatusLine.getStatusCode()).andReturn(code);
@@ -1128,42 +779,11 @@ public class TestCachingHttpClient {
EasyMock.expect(impl.getCurrentDate()).andReturn(date);
}
- private void getMockResponseReader() {
- EasyMock.expect(impl.getResponseReader(
- EasyMock.<HttpRequest>anyObject(),
- EasyMock.<HttpResponse>anyObject())).andReturn(mockResponseReader);
- }
-
- private void removeFromCache(String theURI) throws Exception {
- mockCache.removeEntry(theURI);
- }
-
private void requestPolicyAllowsCaching(boolean allow) {
EasyMock.expect(mockRequestPolicy.isServableFromCache(
EasyMock.<HttpRequest>anyObject())).andReturn(allow);
}
- private void responseDoesNotHaveExplicitContentLength() {
- EasyMock.expect(mockBackendResponse.getFirstHeader("Content-Length"))
- .andReturn(null).anyTimes();
- }
-
- private void responseRead() throws Exception {
- mockResponseReader.readResponse();
- }
-
- private void responseLimitReached(boolean limitReached) throws Exception {
- EasyMock.expect(mockResponseReader.isLimitReached()).andReturn(limitReached);
- }
-
- private void responseGetResource() throws Exception {
- EasyMock.expect(mockResponseReader.getResource()).andReturn(new HeapResource(new byte[] {} ));
- }
-
- private void responseGetReconstructed() throws Exception {
- EasyMock.expect(mockResponseReader.getReconstructedResponse()).andReturn(mockReconstructedResponse);
- }
-
private void backendCallWasMadeWithRequest(HttpRequest request) throws IOException {
EasyMock.expect(mockBackend.execute(
EasyMock.<HttpHost>anyObject(),
@@ -1178,10 +798,6 @@ public class TestCachingHttpClient {
EasyMock.<HttpResponse>anyObject())).andReturn(allow);
}
- private void gotCacheMiss(String theURI) throws Exception {
- EasyMock.expect(mockCache.getEntry(theURI)).andReturn(null);
- }
-
private void cacheEntrySuitable(boolean suitable) {
EasyMock.expect(
mockSuitabilityChecker.canCachedResponseBeUsed(
@@ -1190,68 +806,13 @@ public class TestCachingHttpClient {
EasyMock.<HttpCacheEntry>anyObject())).andReturn(suitable);
}
- private void gotCacheHit(String theURI) throws Exception {
- EasyMock.expect(mockCache.getEntry(theURI)).andReturn(mockCacheEntry);
- }
-
- private void gotCacheHit(String theURI, CacheEntry entry) throws Exception {
- EasyMock.expect(mockCache.getEntry(theURI)).andReturn(entry);
- }
-
- private void cacheEntryHasVariants(boolean b) {
- EasyMock.expect(mockCacheEntry.hasVariants()).andReturn(b);
- }
-
- private void cacheEntryHasVariants(boolean b, CacheEntry entry) {
- EasyMock.expect(entry.hasVariants()).andReturn(b);
- }
-
private void responseIsGeneratedFromCache() {
EasyMock.expect(mockResponseGenerator.generateResponse(
EasyMock.<HttpCacheEntry>anyObject())).andReturn(mockCachedResponse);
}
- private void responseStatusLineIsInspectable() {
- EasyMock.expect(mockBackendResponse.getStatusLine()).andReturn(new OKStatus()).anyTimes();
- }
-
- private void responseGetHeaders() {
- EasyMock.expect(mockBackendResponse.getAllHeaders()).andReturn(new Header[] {}).anyTimes();
- }
-
- private void responseIsGeneratedFromCache(CacheEntry entry) {
- EasyMock.expect(mockResponseGenerator.generateResponse(entry))
- .andReturn(mockCachedResponse);
- }
-
- private void extractTheURI(String theURI) {
- EasyMock.expect(mockExtractor.getURI(host, request)).andReturn(theURI);
- }
-
- private void extractVariantURI(String variantURI) {
- extractVariantURI(variantURI,mockCacheEntry);
- }
-
- private void extractVariantURI(String variantURI, CacheEntry entry){
- EasyMock.expect(mockExtractor.getVariantURI(
- EasyMock.<HttpHost>anyObject(),
- EasyMock.<HttpRequest>anyObject(),
- EasyMock.same(entry))).andReturn(variantURI);
- }
-
- private void putInCache(String theURI) throws Exception {
- mockCache.putEntry(theURI, mockCacheEntry);
- }
-
- private void putInCache(String theURI, CacheEntry entry) throws Exception {
- mockCache.putEntry(theURI, entry);
- }
-
- private void copyResource() throws IOException {
- EasyMock.expect(
- mockResourceFactory.copy(
- EasyMock.<String>anyObject(),
- EasyMock.<Resource>anyObject())).andReturn(new HeapResource(new byte[] {}));
+ private void flushCache() throws IOException {
+ mockCache.flushCacheEntriesFor(host, request);
}
private void handleBackendResponseReturnsResponse(HttpRequest request, HttpResponse response)
@@ -1265,20 +826,6 @@ public class TestCachingHttpClient {
EasyMock.<HttpResponse>anyObject())).andReturn(response);
}
- private void storeInCacheWasCalled() throws IOException {
- impl.storeInCache(
- EasyMock.<HttpHost>anyObject(),
- EasyMock.<HttpRequest>anyObject(),
- EasyMock.<HttpCacheEntry>anyObject());
- }
-
- private void storeInCacheWasCalled(CacheEntry entry) throws IOException {
- impl.storeInCache(
- EasyMock.<HttpHost>anyObject(),
- EasyMock.<HttpRequest>anyObject(),
- EasyMock.same(entry));
- }
-
private void responseProtocolValidationIsCalled() throws ClientProtocolException {
mockResponseProtocolCompliance.ensureProtocolCompliance(
EasyMock.<HttpRequest>anyObject(),
@@ -1301,17 +848,13 @@ public class TestCachingHttpClient {
mockedImpl = true;
impl = EasyMock.createMockBuilder(CachingHttpClient.class).withConstructor(
mockBackend,
- mockResourceFactory,
mockValidityPolicy,
mockResponsePolicy,
- mockExtractor,
mockCache,
mockResponseGenerator,
- mockInvalidator,
mockRequestPolicy,
mockSuitabilityChecker,
mockConditionalRequestBuilder,
- mockCacheEntryUpdater,
mockResponseProtocolCompliance,
mockRequestProtocolCompliance).addMockedMethods(methods).createMock();
}
Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolDeviations.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolDeviations.java?rev=986864&r1=986863&r2=986864&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolDeviations.java (original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolDeviations.java Wed Aug 18 19:15:43 2010
@@ -93,14 +93,16 @@ public class TestProtocolDeviations {
originResponse = make200Response();
- HttpCache cache = new BasicHttpCache(MAX_ENTRIES);
+ CacheConfig params = new CacheConfig();
+ params.setMaxObjectSizeBytes(MAX_BYTES);
+ params.setMaxCacheEntries(MAX_ENTRIES);
+
+ HttpCache cache = new BasicHttpCache(params);
mockBackend = EasyMock.createMock(HttpClient.class);
mockEntity = EasyMock.createMock(HttpEntity.class);
mockCache = EasyMock.createMock(HttpCache.class);
- CacheConfig params = new CacheConfig();
- params.setMaxObjectSizeBytes(MAX_BYTES);
- impl = new CachingHttpClient(mockBackend, cache, new HeapResourceFactory(), params);
+ impl = new CachingHttpClient(mockBackend, cache, params);
}
private HttpResponse make200Response() {
Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolRequirements.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolRequirements.java?rev=986864&r1=986863&r2=986864&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolRequirements.java (original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolRequirements.java Wed Aug 18 19:15:43 2010
@@ -42,7 +42,6 @@ import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.ProtocolVersion;
import org.apache.http.client.ClientProtocolException;
-import org.apache.http.client.cache.HttpCacheEntry;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.RequestWrapper;
@@ -2220,9 +2219,9 @@ public class TestProtocolRequirements ex
CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, hdrs, bytes);
- mockCache.putEntry(EasyMock.eq("http://foo.example.com/thing"), EasyMock.isA(HttpCacheEntry.class));
+ impl = new CachingHttpClient(mockBackend, mockCache, params);
- impl = new CachingHttpClient(mockBackend, mockCache, new HeapResourceFactory(), params);
+ request = new BasicHttpRequest("GET", "/thing", HttpVersion.HTTP_1_1);
HttpRequest validate = new BasicHttpRequest("GET", "/thing", HttpVersion.HTTP_1_1);
validate.setHeader("If-None-Match", "\"etag\"");
@@ -2232,17 +2231,23 @@ public class TestProtocolRequirements ex
notModified.setHeader("Date", DateUtils.formatDate(now));
notModified.setHeader("ETag", "\"etag\"");
- EasyMock.expect(mockCache.getEntry("http://foo.example.com/thing")).andReturn(entry);
+ HttpResponse reconstructed = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
+
+ mockCache.flushInvalidatedCacheEntriesFor(host, request);
+ EasyMock.expect(mockCache.getCacheEntry(host, request)).andReturn(entry);
EasyMock.expect(
mockBackend.execute(EasyMock.eq(host), eqRequest(validate), (HttpContext) EasyMock
.isNull())).andReturn(notModified);
+ EasyMock.expect(mockCache.updateCacheEntry(EasyMock.same(host), EasyMock.same(request),
+ EasyMock.same(entry), EasyMock.same(notModified), EasyMock.isA(Date.class),
+ EasyMock.isA(Date.class)))
+ .andReturn(reconstructed);
replayMocks();
- request = new BasicHttpRequest("GET", "/thing", HttpVersion.HTTP_1_1);
HttpResponse result = impl.execute(host, request);
verifyMocks();
- Assert.assertEquals(200, result.getStatusLine().getStatusCode());
+ Assert.assertSame(reconstructed, result);
}
@Test
@@ -2264,12 +2269,13 @@ public class TestProtocolRequirements ex
CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, hdrs, bytes);
- impl = new CachingHttpClient(mockBackend, mockCache, new HeapResourceFactory(), params);
+ impl = new CachingHttpClient(mockBackend, mockCache, params);
+ request = new BasicHttpRequest("GET", "/thing", HttpVersion.HTTP_1_1);
- EasyMock.expect(mockCache.getEntry("http://foo.example.com/thing")).andReturn(entry);
+ mockCache.flushInvalidatedCacheEntriesFor(host, request);
+ EasyMock.expect(mockCache.getCacheEntry(host, request)).andReturn(entry);
replayMocks();
- request = new BasicHttpRequest("GET", "/thing", HttpVersion.HTTP_1_1);
HttpResponse result = impl.execute(host, request);
verifyMocks();
@@ -2305,16 +2311,17 @@ public class TestProtocolRequirements ex
CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, hdrs, bytes);
- impl = new CachingHttpClient(mockBackend, mockCache, new HeapResourceFactory(), params);
+ impl = new CachingHttpClient(mockBackend, mockCache, params);
+ request = new BasicHttpRequest("GET", "/thing", HttpVersion.HTTP_1_1);
- EasyMock.expect(mockCache.getEntry("http://foo.example.com/thing")).andReturn(entry);
+ mockCache.flushInvalidatedCacheEntriesFor(host, request);
+ EasyMock.expect(mockCache.getCacheEntry(host, request)).andReturn(entry);
EasyMock.expect(
mockBackend.execute(EasyMock.isA(HttpHost.class), EasyMock.isA(HttpRequest.class),
(HttpContext) EasyMock.isNull())).andThrow(
new IOException("can't talk to origin!")).anyTimes();
replayMocks();
- request = new BasicHttpRequest("GET", "/thing", HttpVersion.HTTP_1_1);
HttpResponse result = impl.execute(host, request);
@@ -2506,12 +2513,13 @@ public class TestProtocolRequirements ex
CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, hdrs, bytes);
- impl = new CachingHttpClient(mockBackend, mockCache, new HeapResourceFactory(), params);
+ impl = new CachingHttpClient(mockBackend, mockCache, params);
+ request = new BasicHttpRequest("GET", "/thing", HttpVersion.HTTP_1_1);
- EasyMock.expect(mockCache.getEntry("http://foo.example.com/thing")).andReturn(entry);
+ mockCache.flushInvalidatedCacheEntriesFor(host, request);
+ EasyMock.expect(mockCache.getCacheEntry(host, request)).andReturn(entry);
replayMocks();
- request = new BasicHttpRequest("GET", "/thing", HttpVersion.HTTP_1_1);
HttpResponse result = impl.execute(host, request);
verifyMocks();
@@ -2550,7 +2558,9 @@ public class TestProtocolRequirements ex
CacheEntry entry = new CacheEntry(requestTime, responseTime, hdrs, bytes);
- impl = new CachingHttpClient(mockBackend, mockCache, new HeapResourceFactory(), params);
+ impl = new CachingHttpClient(mockBackend, mockCache, params);
+
+ request = new BasicHttpRequest("GET", "/thing", HttpVersion.HTTP_1_1);
HttpResponse validated = make200Response();
validated.setHeader("Cache-Control", "public");
@@ -2558,18 +2568,20 @@ public class TestProtocolRequirements ex
validated.setHeader("Content-Length", "128");
validated.setEntity(new ByteArrayEntity(bytes));
- Capture<HttpRequest> cap = new Capture<HttpRequest>();
-
- EasyMock.expect(mockCache.getEntry("http://foo.example.com/thing")).andReturn(entry);
+ HttpResponse reconstructed = make200Response();
- mockCache.putEntry(EasyMock.isA(String.class), EasyMock.isA(HttpCacheEntry.class));
+ Capture<HttpRequest> cap = new Capture<HttpRequest>();
+ mockCache.flushInvalidatedCacheEntriesFor(host, request);
+ EasyMock.expect(mockCache.getCacheEntry(host, request)).andReturn(entry);
EasyMock.expect(
mockBackend.execute(EasyMock.isA(HttpHost.class), EasyMock.capture(cap),
(HttpContext) EasyMock.isNull())).andReturn(validated).times(0, 1);
+ EasyMock.expect(mockCache.updateCacheEntry(EasyMock.same(host), EasyMock.same(request), EasyMock.same(entry),
+ EasyMock.same(validated), EasyMock.isA(Date.class), EasyMock.isA(Date.class)))
+ .andReturn(reconstructed).times(0, 1);
replayMocks();
- request = new BasicHttpRequest("GET", "/thing", HttpVersion.HTTP_1_1);
HttpResponse result = impl.execute(host, request);
verifyMocks();
Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseCache.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseCache.java?rev=986864&r1=986863&r2=986864&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseCache.java (original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseCache.java Wed Aug 18 19:15:43 2010
@@ -1,171 +0,0 @@
-/*
- * ====================================================================
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation. For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
- */
-package org.apache.http.impl.client.cache;
-
-import java.io.ByteArrayOutputStream;
-import java.io.InputStream;
-
-import org.apache.http.client.cache.HttpCacheEntry;
-import org.apache.http.client.cache.HttpCacheUpdateCallback;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-
-public class TestResponseCache {
-
- private BasicHttpCache cache;
- private HttpCacheEntry entry;
-
- @Before
- public void setUp() {
- cache = new BasicHttpCache(5);
- entry = new CacheEntry();
- }
-
- @Test
- public void testEntryRemainsInCacheWhenPutThere() throws Exception {
- cache.putEntry("foo", entry);
-
- HttpCacheEntry cachedEntry = cache.getEntry("foo");
-
- Assert.assertSame(entry, cachedEntry);
- }
-
- @Test
- public void testRemovedEntriesDoNotExistAnymore() throws Exception {
- cache.putEntry("foo", entry);
-
- cache.removeEntry("foo");
-
- HttpCacheEntry nullEntry = cache.getEntry("foo");
-
- Assert.assertNull(nullEntry);
- }
-
- @Test
- public void testCacheHoldsNoMoreThanSpecifiedMaxEntries() throws Exception {
- BasicHttpCache cache = new BasicHttpCache(1);
-
- HttpCacheEntry entry1 = new CacheEntry();
- cache.putEntry("foo", entry1);
-
- HttpCacheEntry entry2 = new CacheEntry();
- cache.putEntry("bar", entry2);
-
- HttpCacheEntry entry3 = new CacheEntry();
- cache.putEntry("baz", entry3);
-
- HttpCacheEntry e1 = cache.getEntry("foo");
- Assert.assertNull("Got foo entry when we should not", e1);
-
- HttpCacheEntry e2 = cache.getEntry("bar");
- Assert.assertNull("Got bar entry when we should not", e2);
-
- HttpCacheEntry e3 = cache.getEntry("baz");
- Assert.assertNotNull("Did not get baz entry, but should have", e3);
- }
-
- @Test
- public void testSmallCacheKeepsMostRecentlyUsedEntry() throws Exception {
-
- final int max_size = 3;
- BasicHttpCache cache = new BasicHttpCache(max_size);
-
- // fill the cache with entries
- for (int i = 0; i < max_size; i++) {
- HttpCacheEntry entry = new CacheEntry();
- cache.putEntry("entry" + i, entry);
- }
-
- // read the eldest entry to make it the MRU entry
- cache.getEntry("entry0");
-
- // add another entry, which kicks out the eldest (should be the 2nd one
- // created), and becomes the new MRU entry
- HttpCacheEntry newMru = new CacheEntry();
- cache.putEntry("newMru", newMru);
-
- // get the original second eldest
- HttpCacheEntry gone = cache.getEntry("entry1");
- Assert.assertNull("entry1 should be gone", gone);
-
- HttpCacheEntry latest = cache.getEntry("newMru");
- Assert.assertNotNull("latest entry should still be there", latest);
-
- HttpCacheEntry originalEldest = cache.getEntry("entry0");
- Assert.assertNotNull("original eldest entry should still be there", originalEldest);
- }
-
- @Test
- public void testZeroMaxSizeCacheDoesNotStoreAnything() throws Exception {
- BasicHttpCache cache = new BasicHttpCache(0);
-
- HttpCacheEntry entry = new CacheEntry();
- cache.putEntry("foo", entry);
-
- HttpCacheEntry gone = cache.getEntry("foo");
-
- Assert.assertNull("This cache should not have anything in it!", gone);
- }
-
- @Test
- public void testCacheEntryCallbackUpdatesCacheEntry() throws Exception {
-
- final byte[] expectedArray = new byte[] { 1, 2, 3, 4, 5 };
-
- HttpCacheEntry entry = new CacheEntry();
-
- cache.putEntry("foo", entry);
-
- cache.updateEntry("foo", new HttpCacheUpdateCallback() {
-
- public HttpCacheEntry update(HttpCacheEntry existing) {
- HttpCacheEntry updated = new HttpCacheEntry(
- existing.getRequestDate(),
- existing.getRequestDate(),
- existing.getStatusLine(),
- existing.getAllHeaders(),
- new HeapResource(expectedArray),
- null);
- return updated;
- }
- });
-
- HttpCacheEntry afterUpdate = cache.getEntry("foo");
-
- ByteArrayOutputStream outstream = new ByteArrayOutputStream();
- InputStream instream = afterUpdate.getResource().getInputStream();
- byte[] buf = new byte[2048];
- int len;
- while ((len = instream.read(buf)) != -1) {
- outstream.write(buf, 0, len);
- }
- byte[] bytes = outstream.toByteArray();
- Assert.assertArrayEquals(expectedArray, bytes);
- }
-
-}
Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestSizeLimitedResponseReader.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestSizeLimitedResponseReader.java?rev=986864&r1=986863&r2=986864&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestSizeLimitedResponseReader.java (original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestSizeLimitedResponseReader.java Wed Aug 18 19:15:43 2010
@@ -26,18 +26,14 @@
*/
package org.apache.http.impl.client.cache;
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-
-import org.apache.http.Header;
-import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
-import org.apache.http.message.BasicRequestLine;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.message.BasicHttpResponse;
import org.apache.http.util.EntityUtils;
-import org.easymock.classextension.EasyMock;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@@ -46,56 +42,44 @@ public class TestSizeLimitedResponseRead
private static final long MAX_SIZE = 4;
+ private HttpRequest request;
private SizeLimitedResponseReader impl;
- private HttpRequest mockRequest;
- private HttpResponse mockResponse;
- private HttpEntity mockEntity;
-
- private boolean mockedImpl;
@Before
public void setUp() {
- mockRequest = EasyMock.createMock(HttpRequest.class);
- mockResponse = EasyMock.createMock(HttpResponse.class);
- mockEntity = EasyMock.createMock(HttpEntity.class);
+ request = new HttpGet("http://foo.example.com/bar");
}
@Test
public void testLargeResponseIsTooLarge() throws Exception {
- byte[] buf = new byte[] { 1, 2, 3, 4, 5};
- requestReturnsRequestLine();
- responseReturnsProtocolVersion();
- responseReturnsHeaders();
- responseReturnsContent(new ByteArrayInputStream(buf));
- initReader();
- replayMocks();
+ byte[] buf = new byte[] { 1, 2, 3, 4, 5 };
+ HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
+ response.setEntity(new ByteArrayEntity(buf));
+
+ impl = new SizeLimitedResponseReader(new HeapResourceFactory(), MAX_SIZE, request, response);
impl.readResponse();
boolean tooLarge = impl.isLimitReached();
- HttpResponse response = impl.getReconstructedResponse();
- byte[] result = EntityUtils.toByteArray(response.getEntity());
+ HttpResponse result = impl.getReconstructedResponse();
+ byte[] body = EntityUtils.toByteArray(result.getEntity());
- verifyMocks();
Assert.assertTrue(tooLarge);
- Assert.assertArrayEquals(buf, result);
+ Assert.assertArrayEquals(buf, body);
}
@Test
public void testExactSizeResponseIsNotTooLarge() throws Exception {
byte[] buf = new byte[] { 1, 2, 3, 4 };
- requestReturnsRequestLine();
- responseReturnsProtocolVersion();
- responseReturnsHeaders();
- responseReturnsContent(new ByteArrayInputStream(buf));
- initReader();
- replayMocks();
+ HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
+ response.setEntity(new ByteArrayEntity(buf));
+
+ impl = new SizeLimitedResponseReader(new HeapResourceFactory(), MAX_SIZE, request, response);
impl.readResponse();
boolean tooLarge = impl.isLimitReached();
- HttpResponse response = impl.getReconstructedResponse();
- byte[] result = EntityUtils.toByteArray(response.getEntity());
+ HttpResponse reconstructed = impl.getReconstructedResponse();
+ byte[] result = EntityUtils.toByteArray(reconstructed.getEntity());
- verifyMocks();
Assert.assertFalse(tooLarge);
Assert.assertArrayEquals(buf, result);
}
@@ -103,18 +87,15 @@ public class TestSizeLimitedResponseRead
@Test
public void testSmallResponseIsNotTooLarge() throws Exception {
byte[] buf = new byte[] { 1, 2, 3 };
- requestReturnsRequestLine();
- responseReturnsProtocolVersion();
- responseReturnsHeaders();
- responseReturnsContent(new ByteArrayInputStream(buf));
- initReader();
- replayMocks();
+ HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
+ response.setEntity(new ByteArrayEntity(buf));
+
+ impl = new SizeLimitedResponseReader(new HeapResourceFactory(), MAX_SIZE, request, response);
impl.readResponse();
boolean tooLarge = impl.isLimitReached();
- HttpResponse response = impl.getReconstructedResponse();
- byte[] result = EntityUtils.toByteArray(response.getEntity());
- verifyMocks();
+ HttpResponse reconstructed = impl.getReconstructedResponse();
+ byte[] result = EntityUtils.toByteArray(reconstructed.getEntity());
Assert.assertFalse(tooLarge);
Assert.assertArrayEquals(buf, result);
@@ -122,56 +103,14 @@ public class TestSizeLimitedResponseRead
@Test
public void testResponseWithNoEntityIsNotTooLarge() throws Exception {
- responseHasNullEntity();
+ HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
+
+ impl = new SizeLimitedResponseReader(new HeapResourceFactory(), MAX_SIZE, request, response);
- initReader();
- replayMocks();
impl.readResponse();
boolean tooLarge = impl.isLimitReached();
- verifyMocks();
Assert.assertFalse(tooLarge);
}
- private void responseReturnsContent(InputStream buffer) throws IOException {
- EasyMock.expect(mockResponse.getEntity()).andReturn(mockEntity);
- EasyMock.expect(mockEntity.getContent()).andReturn(buffer);
- }
-
- private void requestReturnsRequestLine() {
- EasyMock.expect(mockRequest.getRequestLine()).andReturn(
- new BasicRequestLine("GET", "/", HttpVersion.HTTP_1_1));
- }
-
- private void responseReturnsProtocolVersion() {
- EasyMock.expect(mockResponse.getProtocolVersion()).andReturn(HttpVersion.HTTP_1_1);
- }
-
- private void responseReturnsHeaders() {
- EasyMock.expect(mockResponse.getAllHeaders()).andReturn(new Header[] {});
- }
-
- private void responseHasNullEntity() {
- EasyMock.expect(mockResponse.getEntity()).andReturn(null);
- }
-
- private void verifyMocks() {
- EasyMock.verify(mockRequest, mockResponse, mockEntity);
- if (mockedImpl) {
- EasyMock.verify(impl);
- }
- }
-
- private void replayMocks() {
- EasyMock.replay(mockRequest, mockResponse, mockEntity);
- if (mockedImpl) {
- EasyMock.replay(impl);
- }
- }
-
- private void initReader() {
- impl = new SizeLimitedResponseReader(
- new HeapResourceFactory(), MAX_SIZE, mockRequest, mockResponse);
- }
-
}
Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ehcache/TestEhcacheHttpCache.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ehcache/TestEhcacheHttpCache.java?rev=986864&r1=986863&r2=986864&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ehcache/TestEhcacheHttpCache.java (original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ehcache/TestEhcacheHttpCache.java Wed Aug 18 19:15:43 2010
@@ -40,11 +40,11 @@ import org.junit.Test;
public class TestEhcacheHttpCache extends TestCase {
private Ehcache mockCache;
- private EhcacheHttpCache impl;
+ private EhcacheHttpCacheStorage impl;
public void setUp() {
mockCache = EasyMock.createMock(Ehcache.class);
- impl = new EhcacheHttpCache(mockCache);
+ impl = new EhcacheHttpCacheStorage(mockCache);
}
@Test
Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ehcache/TestEhcacheProtcolRequirements.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ehcache/TestEhcacheProtcolRequirements.java?rev=986864&r1=986863&r2=986864&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ehcache/TestEhcacheProtcolRequirements.java (original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ehcache/TestEhcacheProtcolRequirements.java Wed Aug 18 19:15:43 2010
@@ -1,99 +0,0 @@
-/*
- * ====================================================================
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation. For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
- */
-package org.apache.http.impl.client.cache.ehcache;
-
-import net.sf.ehcache.CacheManager;
-import net.sf.ehcache.config.CacheConfiguration;
-import net.sf.ehcache.config.Configuration;
-import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
-
-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.AfterClass;
-import org.junit.Before;
-import org.junit.BeforeClass;
-
-public class TestEhcacheProtcolRequirements extends TestProtocolRequirements{
-
- private final String TEST_EHCACHE_NAME = "TestEhcacheProtocolRequirements-cache";
-
- private static CacheManager CACHE_MANAGER;
-
- @BeforeClass
- public static void setUpGlobal() {
- Configuration config = new Configuration();
- config.addDefaultCache(
- new CacheConfiguration("default", Integer.MAX_VALUE)
- .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
- .overflowToDisk(false));
- CACHE_MANAGER = CacheManager.create(config);
- }
-
- @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();
-
- if (CACHE_MANAGER.cacheExists(TEST_EHCACHE_NAME)){
- CACHE_MANAGER.removeCache(TEST_EHCACHE_NAME);
- }
- CACHE_MANAGER.addCache(TEST_EHCACHE_NAME);
- cache = new EhcacheHttpCache(CACHE_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(){
- CACHE_MANAGER.removeCache(TEST_EHCACHE_NAME);
- }
-
- @AfterClass
- public static void tearDownGlobal(){
- CACHE_MANAGER.shutdown();
- }
-
-}