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 {
private static final Charset UTF8 = Charset.forName("UTF-8");
private static final Charset UTF8 = Charset.forName("UTF-8");
public void testDefaultSerializer() throws Exception {
readWriteVerify(new DefaultHttpCacheEntrySerializer());
}
public void testDefaultSerializer() throws Exception {
readWriteVerify(new DefaultHttpCacheEntrySerializer());
}
public void readWriteVerify(HttpCacheEntrySerializer serializer) throws IOException {
// write the entry
HttpCacheEntry writeEntry = newCacheEntry();
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.writeTo(writeEntry, out);
public void readWriteVerify(HttpCacheEntrySerializer serializer) throws IOException {
// write the entry
HttpCacheEntry writeEntry = newCacheEntry();
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.writeTo(writeEntry, out);
// read the entry
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
HttpCacheEntry readEntry = serializer.readFrom(in);
// read the entry
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
HttpCacheEntry readEntry = serializer.readFrom(in);
// compare
assertTrue(areEqual(readEntry, writeEntry));
}
// compare
assertTrue(areEqual(readEntry, writeEntry));
}
private HttpCacheEntry newCacheEntry() 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";
private HttpCacheEntry newCacheEntry() 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");
Set<String> variants = new HashSet<String>();
variants.add("test variant 1");
variants.add("test variant 2");
ProtocolVersion pvObj = new ProtocolVersion("HTTP", 1, 1);
StatusLine slObj = new BasicStatusLine(pvObj, 200, "ok");
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);
HttpCacheEntry cacheEntry = new HttpCacheEntry(new Date(), new Date(),
slObj, headers, new HeapResource(Base64.decodeBase64(body
.getBytes(UTF8.name()))), variants);
return cacheEntry;
}
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()
.getTime() / 1000)))
return false;
if (!((one.getResponseDate().getTime() / 1000) == (two
.getResponseDate().getTime() / 1000)))
return false;
if (!one.getProtocolVersion().equals(two.getProtocolVersion()))
return false;
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()
.getTime() / 1000)))
return false;
if (!((one.getResponseDate().getTime() / 1000) == (two
.getResponseDate().getTime() / 1000)))
return false;
if (!one.getProtocolVersion().equals(two.getProtocolVersion()))
return false;
byte[] onesByteArray = resourceToBytes(one.getResource());
byte[] twosByteArray = resourceToBytes(two.getResource());
byte[] onesByteArray = resourceToBytes(one.getResource());
byte[] twosByteArray = resourceToBytes(two.getResource());
if (!Arrays.equals(onesByteArray,twosByteArray))
return false;
if (!Arrays.equals(onesByteArray,twosByteArray))
return false;
Header[] oneHeaders = one.getAllHeaders();
Header[] twoHeaders = one.getAllHeaders();
if (!(oneHeaders.length == twoHeaders.length))
return false;
for (int i = 0; i < oneHeaders.length; i++) {
if (!oneHeaders[i].getName().equals(twoHeaders[i].getName()))
return false;
if (!oneHeaders[i].getValue().equals(twoHeaders[i].getValue()))
return false;
}
Header[] oneHeaders = one.getAllHeaders();
Header[] twoHeaders = two.getAllHeaders();
if (!(oneHeaders.length == twoHeaders.length))
return false;
for (int i = 0; i < oneHeaders.length; i++) {
if (!oneHeaders[i].getName().equals(twoHeaders[i].getName()))
return false;
if (!oneHeaders[i].getValue().equals(twoHeaders[i].getValue()))
return false;
}
return true;
}
return true;
}
private byte[] resourceToBytes(Resource res) throws IOException {
InputStream inputStream = res.getInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
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);
}
int readBytes;
byte[] bytes = new byte[8096];
while ((readBytes = inputStream.read(bytes)) > 0) {
outputStream.write(bytes, 0, readBytes);
}
byte[] byteData = outputStream.toByteArray();
byte[] byteData = outputStream.toByteArray();
inputStream.close();
outputStream.close();
inputStream.close();
outputStream.close();
return byteData;
}
return byteData;
}
}