TABS -> SPACES; fixed bug in a test case

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@988878 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2010-08-25 08:33:41 +00:00
parent 672221fc5f
commit 68c8b4b1ee
1 changed files with 78 additions and 78 deletions

View File

@ -25,92 +25,92 @@ import org.apache.http.message.BasicStatusLine;
public class TestHttpCacheEntrySerializers extends TestCase { public class TestHttpCacheEntrySerializers extends TestCase {
private static final Charset UTF8 = Charset.forName("UTF-8"); private static final Charset UTF8 = Charset.forName("UTF-8");
public void testDefaultSerializer() throws Exception { public void testDefaultSerializer() throws Exception {
readWriteVerify(new DefaultHttpCacheEntrySerializer()); readWriteVerify(new DefaultHttpCacheEntrySerializer());
} }
public void readWriteVerify(HttpCacheEntrySerializer serializer) throws IOException { public void readWriteVerify(HttpCacheEntrySerializer serializer) throws IOException {
// write the entry // write the entry
HttpCacheEntry writeEntry = newCacheEntry(); HttpCacheEntry writeEntry = newCacheEntry();
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.writeTo(writeEntry, out); serializer.writeTo(writeEntry, out);
// read the entry // read the entry
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
HttpCacheEntry readEntry = serializer.readFrom(in); HttpCacheEntry readEntry = serializer.readFrom(in);
// compare // compare
assertTrue(areEqual(readEntry, writeEntry)); assertTrue(areEqual(readEntry, writeEntry));
} }
private HttpCacheEntry newCacheEntry() throws UnsupportedEncodingException { private HttpCacheEntry newCacheEntry() throws UnsupportedEncodingException {
Header[] headers = new Header[5]; Header[] headers = new Header[5];
for (int i = 0; i < headers.length; i++) { for (int i = 0; i < headers.length; i++) {
headers[i] = new BasicHeader("header" + i, "value" + i); headers[i] = new BasicHeader("header" + i, "value" + i);
} }
String body = "Lorem ipsum dolor sit amet"; String body = "Lorem ipsum dolor sit amet";
ProtocolVersion pvObj = new ProtocolVersion("HTTP", 1, 1); ProtocolVersion pvObj = new ProtocolVersion("HTTP", 1, 1);
StatusLine slObj = new BasicStatusLine(pvObj, 200, "ok"); StatusLine slObj = new BasicStatusLine(pvObj, 200, "ok");
Set<String> variants = new HashSet<String>(); Set<String> variants = new HashSet<String>();
variants.add("test variant 1"); variants.add("test variant 1");
variants.add("test variant 2"); variants.add("test variant 2");
HttpCacheEntry cacheEntry = new HttpCacheEntry(new Date(), new Date(), HttpCacheEntry cacheEntry = new HttpCacheEntry(new Date(), new Date(),
slObj, headers, new HeapResource(Base64.decodeBase64(body slObj, headers, new HeapResource(Base64.decodeBase64(body
.getBytes(UTF8.name()))), variants); .getBytes(UTF8.name()))), variants);
return cacheEntry; return cacheEntry;
} }
private boolean areEqual(HttpCacheEntry one, HttpCacheEntry two) throws IOException { private boolean areEqual(HttpCacheEntry one, HttpCacheEntry two) throws IOException {
// dates are only stored with second precision, so scrub milliseconds // dates are only stored with second precision, so scrub milliseconds
if (!((one.getRequestDate().getTime() / 1000) == (two.getRequestDate() if (!((one.getRequestDate().getTime() / 1000) == (two.getRequestDate()
.getTime() / 1000))) .getTime() / 1000)))
return false; return false;
if (!((one.getResponseDate().getTime() / 1000) == (two if (!((one.getResponseDate().getTime() / 1000) == (two
.getResponseDate().getTime() / 1000))) .getResponseDate().getTime() / 1000)))
return false; return false;
if (!one.getProtocolVersion().equals(two.getProtocolVersion())) if (!one.getProtocolVersion().equals(two.getProtocolVersion()))
return false; return false;
byte[] onesByteArray = resourceToBytes(one.getResource()); byte[] onesByteArray = resourceToBytes(one.getResource());
byte[] twosByteArray = resourceToBytes(two.getResource()); byte[] twosByteArray = resourceToBytes(two.getResource());
if (!Arrays.equals(onesByteArray,twosByteArray)) if (!Arrays.equals(onesByteArray,twosByteArray))
return false; return false;
Header[] oneHeaders = one.getAllHeaders(); Header[] oneHeaders = one.getAllHeaders();
Header[] twoHeaders = one.getAllHeaders(); Header[] twoHeaders = two.getAllHeaders();
if (!(oneHeaders.length == twoHeaders.length)) if (!(oneHeaders.length == twoHeaders.length))
return false; return false;
for (int i = 0; i < oneHeaders.length; i++) { for (int i = 0; i < oneHeaders.length; i++) {
if (!oneHeaders[i].getName().equals(twoHeaders[i].getName())) if (!oneHeaders[i].getName().equals(twoHeaders[i].getName()))
return false; return false;
if (!oneHeaders[i].getValue().equals(twoHeaders[i].getValue())) if (!oneHeaders[i].getValue().equals(twoHeaders[i].getValue()))
return false; return false;
} }
return true; return true;
} }
private byte[] resourceToBytes(Resource res) throws IOException { private byte[] resourceToBytes(Resource res) throws IOException {
InputStream inputStream = res.getInputStream(); InputStream inputStream = res.getInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int readBytes; int readBytes;
byte[] bytes = new byte[8096]; byte[] bytes = new byte[8096];
while ((readBytes = inputStream.read(bytes)) > 0) { while ((readBytes = inputStream.read(bytes)) > 0) {
outputStream.write(bytes, 0, readBytes); outputStream.write(bytes, 0, readBytes);
} }
byte[] byteData = outputStream.toByteArray(); byte[] byteData = outputStream.toByteArray();
inputStream.close(); inputStream.close();
outputStream.close(); outputStream.close();
return byteData; return byteData;
} }
} }