From 5059eddd97cbf90955bd49c3a716350de7180e24 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Fri, 23 Jul 2010 19:06:42 +0000 Subject: [PATCH] Code cleanups git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@967206 13f79535-47bb-0310-9956-ffa450edef68 --- .../http/impl/client/cache/CacheEntity.java | 13 ++- .../client/cache/CacheEntryGenerator.java | 9 +- .../http/impl/client/cache/CacheEntry.java | 28 +++++-- .../client/cache/TestCachingHttpClient.java | 3 +- .../cache/TestConditionalRequestBuilder.java | 14 +--- .../cache/TestProtocolRequirements.java | 84 ++++++++----------- .../impl/client/cache/TestResponseCache.java | 50 ++++------- 7 files changed, 91 insertions(+), 110 deletions(-) diff --git a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntity.java b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntity.java index 4db3217de..6b4f17a08 100644 --- a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntity.java +++ b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntity.java @@ -34,7 +34,6 @@ import java.io.Serializable; import org.apache.http.Header; import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; import org.apache.http.annotation.Immutable; import org.apache.http.message.BasicHeader; import org.apache.http.protocol.HTTP; @@ -48,15 +47,15 @@ class CacheEntity implements HttpEntity, Cloneable, Serializable { private final String contentType; private final String contentEncoding; - public CacheEntity(final byte[] b, final HttpResponse response) { + public CacheEntity(final byte[] b, final String contentType, final String contentEncoding) { super(); this.content = b; + this.contentType = contentType; + this.contentEncoding = contentEncoding; + } - Header ct = response.getFirstHeader(HTTP.CONTENT_TYPE); - Header ce = response.getFirstHeader(HTTP.CONTENT_ENCODING); - - this.contentType = ct != null ? ct.getValue() : null; - this.contentEncoding = ce != null ? ce.getValue() : null; + public CacheEntity(final byte[] b) { + this(b, null, null); } public Header getContentType() { diff --git a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntryGenerator.java b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntryGenerator.java index 3718f0b75..4b72780cb 100644 --- a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntryGenerator.java +++ b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntryGenerator.java @@ -28,9 +28,11 @@ package org.apache.http.impl.client.cache; import java.util.Date; +import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.annotation.Immutable; import org.apache.http.client.cache.HttpCacheEntry; +import org.apache.http.protocol.HTTP; /** * Generates a {@link CacheEntry} from a {@link HttpResponse} @@ -45,7 +47,12 @@ class CacheEntryGenerator { Date responseDate, HttpResponse response, byte[] body) { - CacheEntity entity = new CacheEntity(body, response); + Header ct = response.getFirstHeader(HTTP.CONTENT_TYPE); + Header ce = response.getFirstHeader(HTTP.CONTENT_ENCODING); + CacheEntity entity = new CacheEntity( + body, + ct != null ? ct.getValue() : null, + ce != null ? ce.getValue() : null); return new HttpCacheEntry(requestDate, responseDate, response.getStatusLine(), diff --git a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/CacheEntry.java b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/CacheEntry.java index 747f494ab..0edbd04e6 100644 --- a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/CacheEntry.java +++ b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/CacheEntry.java @@ -32,8 +32,6 @@ import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.StatusLine; import org.apache.http.client.cache.HttpCacheEntry; -import org.apache.http.entity.BasicHttpEntity; -import org.apache.http.entity.ByteArrayEntity; public class CacheEntry extends HttpCacheEntry { @@ -53,24 +51,37 @@ public class CacheEntry extends HttpCacheEntry { public CacheEntry( Date requestDate, Date responseDate) { - super(requestDate, responseDate, new OKStatus(), new Header[] {}, new BasicHttpEntity(), null); + super(requestDate, responseDate, new OKStatus(), new Header[] {}, + new CacheEntity(new byte[] {}), null); } public CacheEntry( Date requestDate, Date responseDate, Header[] headers) { - super(requestDate, responseDate, new OKStatus(), headers, new BasicHttpEntity(), null); + super(requestDate, responseDate, new OKStatus(), headers, + new CacheEntity(new byte[] {}), null); } - public CacheEntry(Header[] headers) { - super(new Date(), new Date(), new OKStatus(), headers, new BasicHttpEntity(), null); + public CacheEntry( + Date requestDate, + Date responseDate, + Header[] headers, + byte[] content) { + super(requestDate, responseDate, new OKStatus(), headers, + new CacheEntity(content), null); } public CacheEntry( Header[] headers, byte[] content) { - super(new Date(), new Date(), new OKStatus(), headers, new ByteArrayEntity(content), null); + super(new Date(), new Date(), new OKStatus(), headers, + new CacheEntity(content), null); + } + + public CacheEntry(Header[] headers) { + super(new Date(), new Date(), new OKStatus(), headers, + new CacheEntity(new byte[] {}), null); } public CacheEntry() { @@ -78,7 +89,8 @@ public class CacheEntry extends HttpCacheEntry { } public CacheEntry(byte[] content) { - super(new Date(), new Date(), new OKStatus(), new Header[] {}, new ByteArrayEntity(content), null); + super(new Date(), new Date(), new OKStatus(), new Header[] {}, + new CacheEntity(content), null); } } diff --git a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java index 5df149a5a..f36939d58 100644 --- a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java +++ b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java @@ -322,8 +322,7 @@ public class TestCachingHttpClient { final String variantURI = "variantURI"; - final CacheEntry entry = new CacheEntry(new Date(), new Date(), new OKStatus(), - new Header[] {}, new ByteArrayEntity(new byte[] {})); + final CacheEntry entry = new CacheEntry(); replayMocks(); diff --git a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestConditionalRequestBuilder.java b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestConditionalRequestBuilder.java index 94650de1f..d3e116438 100644 --- a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestConditionalRequestBuilder.java +++ b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestConditionalRequestBuilder.java @@ -33,7 +33,6 @@ import org.apache.http.HeaderElement; import org.apache.http.HttpRequest; import org.apache.http.HttpVersion; import org.apache.http.ProtocolException; -import org.apache.http.entity.ByteArrayEntity; import org.apache.http.impl.cookie.DateUtils; import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicHttpRequest; @@ -63,9 +62,7 @@ public class TestConditionalRequestBuilder { new BasicHeader("Date", DateUtils.formatDate(new Date())), new BasicHeader("Last-Modified", lastModified) }; - CacheEntry cacheEntry = new CacheEntry(new Date(), new Date(), - new OKStatus(), headers, - new ByteArrayEntity(new byte[] {})); + CacheEntry cacheEntry = new CacheEntry(headers); HttpRequest newRequest = impl.buildConditionalRequest(request, cacheEntry); Assert.assertNotSame(request, newRequest); @@ -97,8 +94,7 @@ public class TestConditionalRequestBuilder { new BasicHeader("Last-Modified", DateUtils.formatDate(new Date())), new BasicHeader("ETag", theETag) }; - CacheEntry cacheEntry = new CacheEntry(new Date(), new Date(), - new OKStatus(), headers, new ByteArrayEntity(new byte[] {})); + CacheEntry cacheEntry = new CacheEntry(headers); HttpRequest newRequest = impl.buildConditionalRequest(request, cacheEntry); @@ -130,8 +126,7 @@ public class TestConditionalRequestBuilder { new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)), new BasicHeader("ETag", "\"etag\""), new BasicHeader("Cache-Control","max-age=5, must-revalidate") }; - CacheEntry cacheEntry = new CacheEntry(elevenSecondsAgo, nineSecondsAgo, - new OKStatus(), cacheEntryHeaders, new ByteArrayEntity(new byte[0])); + CacheEntry cacheEntry = new CacheEntry(elevenSecondsAgo, nineSecondsAgo, cacheEntryHeaders); HttpRequest result = impl.buildConditionalRequest(request, cacheEntry); @@ -159,8 +154,7 @@ public class TestConditionalRequestBuilder { new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)), new BasicHeader("ETag", "\"etag\""), new BasicHeader("Cache-Control","max-age=5, proxy-revalidate") }; - CacheEntry cacheEntry = new CacheEntry(elevenSecondsAgo, nineSecondsAgo, - new OKStatus(), cacheEntryHeaders, new ByteArrayEntity(new byte[0])); + CacheEntry cacheEntry = new CacheEntry(elevenSecondsAgo, nineSecondsAgo, cacheEntryHeaders); HttpRequest result = impl.buildConditionalRequest(request, cacheEntry); diff --git a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolRequirements.java b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolRequirements.java index 49f2eb6df..001d5c2fa 100644 --- a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolRequirements.java +++ b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolRequirements.java @@ -53,7 +53,6 @@ import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicHttpEntityEnclosingRequest; import org.apache.http.message.BasicHttpRequest; import org.apache.http.message.BasicHttpResponse; -import org.apache.http.message.HeaderGroup; import org.apache.http.protocol.HttpContext; import org.easymock.Capture; import org.easymock.IExpectationSetters; @@ -2294,19 +2293,17 @@ public class TestProtocolRequirements { Date nineSecondsAgo = new Date(now.getTime() - 9 * 1000L); Date eightSecondsAgo = new Date(now.getTime() - 8 * 1000L); - FakeHeaderGroup headerGroup = new FakeHeaderGroup(); - - headerGroup.addHeader("Date", DateUtils.formatDate(nineSecondsAgo)); - headerGroup.addHeader("Cache-Control", "max-age=0"); - headerGroup.addHeader("ETag", "\"etag\""); - headerGroup.addHeader("Content-Length", "128"); - + Header[] hdrs = new Header[] { + new BasicHeader("Date", DateUtils.formatDate(nineSecondsAgo)), + new BasicHeader("Cache-Control", "max-age=0"), + new BasicHeader("ETag", "\"etag\""), + new BasicHeader("Content-Length", "128") + }; byte[] bytes = new byte[128]; (new Random()).nextBytes(bytes); - CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, new OKStatus(), - headerGroup.getAllHeaders(), new ByteArrayEntity(bytes)); + CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, hdrs, bytes); mockCache.putEntry(EasyMock.eq("http://foo.example.com/thing"), EasyMock.isA(HttpCacheEntry.class)); @@ -2340,18 +2337,17 @@ public class TestProtocolRequirements { Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L); Date nineSecondsAgo = new Date(now.getTime() - 9 * 1000L); Date eightSecondsAgo = new Date(now.getTime() - 8 * 1000L); - FakeHeaderGroup headerGroup = new FakeHeaderGroup(); + + Header[] hdrs = new Header[] { + new BasicHeader("Date", DateUtils.formatDate(nineSecondsAgo)), + new BasicHeader("Cache-Control", "max-age=3600"), + new BasicHeader("Content-Length", "128") + }; - - headerGroup.addHeader("Date", DateUtils.formatDate(nineSecondsAgo)); - headerGroup.addHeader("Cache-Control", "max-age=3600"); - headerGroup.addHeader("Content-Length", "128"); byte[] bytes = new byte[128]; (new Random()).nextBytes(bytes); - - CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, new OKStatus(), - headerGroup.getAllHeaders(), new ByteArrayEntity(bytes)); + CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, hdrs, bytes); impl = new CachingHttpClient(mockBackend, mockCache, MAX_BYTES); @@ -2382,19 +2378,17 @@ public class TestProtocolRequirements { Date nineSecondsAgo = new Date(now.getTime() - 9 * 1000L); Date eightSecondsAgo = new Date(now.getTime() - 8 * 1000L); - FakeHeaderGroup headerGroup = new FakeHeaderGroup(); + Header[] hdrs = new Header[] { + new BasicHeader("Date", DateUtils.formatDate(nineSecondsAgo)), + new BasicHeader("Cache-Control", "max-age=0"), + new BasicHeader("Content-Length", "128"), + new BasicHeader("Last-Modified", DateUtils.formatDate(tenSecondsAgo)) + }; - headerGroup.addHeader("Date", DateUtils.formatDate(nineSecondsAgo)); - headerGroup.addHeader("Cache-Control", "max-age=0"); - headerGroup.addHeader("Content-Length", "128"); - headerGroup.addHeader("Last-Modified", DateUtils.formatDate(tenSecondsAgo)); byte[] bytes = new byte[128]; (new Random()).nextBytes(bytes); - CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, new OKStatus(), - headerGroup.getAllHeaders(), new ByteArrayEntity(bytes)); - - + CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, hdrs, bytes); impl = new CachingHttpClient(mockBackend, mockCache, MAX_BYTES); @@ -2586,16 +2580,16 @@ public class TestProtocolRequirements { Date nineSecondsAgo = new Date(now.getTime() - 9 * 1000L); Date eightSecondsAgo = new Date(now.getTime() - 8 * 1000L); - FakeHeaderGroup headerGroup = new FakeHeaderGroup(); + Header[] hdrs = new Header[] { + new BasicHeader("Date", DateUtils.formatDate(nineSecondsAgo)), + new BasicHeader("Cache-Control", "max-age=3600"), + new BasicHeader("Content-Length", "128") + }; - headerGroup.setHeader("Date", DateUtils.formatDate(nineSecondsAgo)); - headerGroup.setHeader("Cache-Control", "max-age=3600"); - headerGroup.setHeader("Content-Length", "128"); byte[] bytes = new byte[128]; (new Random()).nextBytes(bytes); - CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, new OKStatus(), - headerGroup.getAllHeaders(), new ByteArrayEntity(bytes)); + CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, hdrs, bytes); impl = new CachingHttpClient(mockBackend, mockCache, MAX_BYTES); @@ -2629,17 +2623,17 @@ public class TestProtocolRequirements { Date requestTime = new Date(thirtySixHoursAgo.getTime() - 1000L); Date responseTime = new Date(thirtySixHoursAgo.getTime() + 1000L); - FakeHeaderGroup headerGroup = new FakeHeaderGroup(); + Header[] hdrs = new Header[] { + new BasicHeader("Date", DateUtils.formatDate(thirtySixHoursAgo)), + new BasicHeader("Cache-Control", "public"), + new BasicHeader("Last-Modified", DateUtils.formatDate(oneYearAgo)), + new BasicHeader("Content-Length", "128") + }; - headerGroup.setHeader("Date", DateUtils.formatDate(thirtySixHoursAgo)); - headerGroup.setHeader("Cache-Control", "public"); - headerGroup.setHeader("Last-Modified", DateUtils.formatDate(oneYearAgo)); - headerGroup.setHeader("Content-Length", "128"); byte[] bytes = new byte[128]; (new Random()).nextBytes(bytes); - CacheEntry entry = new CacheEntry(requestTime, responseTime, new OKStatus(), - headerGroup.getAllHeaders(), new ByteArrayEntity(bytes)); + CacheEntry entry = new CacheEntry(requestTime, responseTime, hdrs, bytes); impl = new CachingHttpClient(mockBackend, mockCache, MAX_BYTES); @@ -5252,14 +5246,4 @@ public class TestProtocolRequirements { verifyMocks(); } - private class FakeHeaderGroup extends HeaderGroup{ - - public void addHeader(String name, String value){ - this.addHeader(new BasicHeader(name,value)); - } - - public void setHeader(String name, String value){ - addHeader(name,value); - } - } } \ No newline at end of file diff --git a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseCache.java b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseCache.java index 92e591218..a42895bef 100644 --- a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseCache.java +++ b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseCache.java @@ -26,42 +26,37 @@ */ package org.apache.http.impl.client.cache; +import java.io.ByteArrayOutputStream; + import org.apache.http.client.cache.HttpCacheEntry; -import org.apache.http.client.cache.HttpCacheOperationException; import org.apache.http.client.cache.HttpCacheUpdateCallback; -import org.apache.http.entity.ByteArrayEntity; -import org.easymock.classextension.EasyMock; import org.junit.Assert; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; -import java.io.ByteArrayOutputStream; -import java.io.IOException; - public class TestResponseCache { private BasicHttpCache cache; - private HttpCacheEntry mockEntry; + private HttpCacheEntry entry; @Before public void setUp() { cache = new BasicHttpCache(5); - mockEntry = EasyMock.createMock(HttpCacheEntry.class); + entry = new CacheEntry(); } @Test public void testEntryRemainsInCacheWhenPutThere() { - cache.putEntry("foo", mockEntry); + cache.putEntry("foo", entry); HttpCacheEntry cachedEntry = cache.getEntry("foo"); - Assert.assertSame(mockEntry, cachedEntry); + Assert.assertSame(entry, cachedEntry); } @Test public void testRemovedEntriesDoNotExistAnymore() { - cache.putEntry("foo", mockEntry); + cache.putEntry("foo", entry); cache.removeEntry("foo"); @@ -74,13 +69,13 @@ public class TestResponseCache { public void testCacheHoldsNoMoreThanSpecifiedMaxEntries() { BasicHttpCache cache = new BasicHttpCache(1); - HttpCacheEntry entry1 = EasyMock.createMock(HttpCacheEntry.class); + HttpCacheEntry entry1 = new CacheEntry(); cache.putEntry("foo", entry1); - HttpCacheEntry entry2 = EasyMock.createMock(HttpCacheEntry.class); + HttpCacheEntry entry2 = new CacheEntry(); cache.putEntry("bar", entry2); - HttpCacheEntry entry3 = EasyMock.createMock(HttpCacheEntry.class); + HttpCacheEntry entry3 = new CacheEntry(); cache.putEntry("baz", entry3); HttpCacheEntry e1 = cache.getEntry("foo"); @@ -101,7 +96,7 @@ public class TestResponseCache { // fill the cache with entries for (int i = 0; i < max_size; i++) { - HttpCacheEntry entry = EasyMock.createMock(HttpCacheEntry.class); + HttpCacheEntry entry = new CacheEntry(); cache.putEntry("entry" + i, entry); } @@ -110,7 +105,7 @@ public class TestResponseCache { // add another entry, which kicks out the eldest (should be the 2nd one // created), and becomes the new MRU entry - HttpCacheEntry newMru = EasyMock.createMock(HttpCacheEntry.class); + HttpCacheEntry newMru = new CacheEntry(); cache.putEntry("newMru", newMru); // get the original second eldest @@ -128,7 +123,7 @@ public class TestResponseCache { public void testZeroMaxSizeCacheDoesNotStoreAnything() { BasicHttpCache cache = new BasicHttpCache(0); - HttpCacheEntry entry = EasyMock.createMock(HttpCacheEntry.class); + HttpCacheEntry entry = new CacheEntry(); cache.putEntry("foo", entry); HttpCacheEntry gone = cache.getEntry("foo"); @@ -137,16 +132,13 @@ public class TestResponseCache { } @Test - @Ignore - public void testCacheEntryCallbackUpdatesCacheEntry() throws HttpCacheOperationException, IOException { + public void testCacheEntryCallbackUpdatesCacheEntry() throws Exception { final byte[] expectedArray = new byte[] { 1, 2, 3, 4, 5 }; - HttpCacheEntry entry = EasyMock.createMock(HttpCacheEntry.class); - HttpCacheEntry entry2 = EasyMock.createMock(HttpCacheEntry.class); + HttpCacheEntry entry = new CacheEntry(); cache.putEntry("foo", entry); - cache.putEntry("bar", entry2); cache.updateEntry("foo", new HttpCacheUpdateCallback() { @@ -156,24 +148,18 @@ public class TestResponseCache { existing.getRequestDate(), existing.getStatusLine(), existing.getAllHeaders(), - new ByteArrayEntity(expectedArray), + new CacheEntity(expectedArray), null); - cache.removeEntry("bar"); return updated; } }); HttpCacheEntry afterUpdate = cache.getEntry("foo"); - HttpCacheEntry bar = cache.getEntry("bar"); - Assert.assertNull(bar); - - byte[] bytes; ByteArrayOutputStream stream = new ByteArrayOutputStream(); afterUpdate.getBody().writeTo(stream); - bytes = stream.toByteArray(); - - Assert.assertArrayEquals(expectedArray,bytes); + byte[] bytes = stream.toByteArray(); + Assert.assertArrayEquals(expectedArray, bytes); } }