Index: TestCacheEntryUpdater.java =================================================================== --- TestCacheEntryUpdater.java (revision 1002701) +++ TestCacheEntryUpdater.java (working copy) @@ -26,12 +26,23 @@ */ package org.apache.http.impl.client.cache; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotSame; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Date; + import org.apache.http.Header; +import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.HttpVersion; import org.apache.http.ProtocolVersion; import org.apache.http.client.cache.HttpCacheEntry; +import org.apache.http.client.cache.Resource; +import org.apache.http.entity.StringEntity; import org.apache.http.impl.cookie.DateUtils; import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicHttpResponse; @@ -40,12 +51,6 @@ import org.junit.Before; import org.junit.Test; -import java.io.IOException; -import java.util.Date; - -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertNotSame; - public class TestCacheEntryUpdater { private Date requestDate; @@ -182,4 +187,34 @@ Assert.fail("Header [" + name + ": " + value + "] not found in headers."); } + @Test + public void testUpdatedEntryResourceUpdated() throws IOException { + HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry("asdf".getBytes()); + + HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion( + "http", 1, 1), HttpStatus.SC_OK, "")); + response.setEntity(new StringEntity("fdsa")); + + HttpCacheEntry updatedEntry = impl.updateCacheEntry(null, cacheEntry, new Date(), new Date(), response); + + Assert.assertEquals("fdsa", new String(resourceToBytes(updatedEntry.getResource()))); + } + + private byte[] resourceToBytes(Resource res) throws IOException { + InputStream inputStream = res.getInputStream(); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + + int readBytes; + byte[] bytes = new byte[8096]; + while ((readBytes = inputStream.read(bytes)) > 0) { + outputStream.write(bytes, 0, readBytes); + } + + byte[] byteData = outputStream.toByteArray(); + + inputStream.close(); + outputStream.close(); + + return byteData; + } }