@Test public void cacheHeaderSerialization() throws Exception { Cache.Entry e = new Cache.Entry(); e.data = new byte[8]; e.serverDate = 1234567L; e.lastModified = 13572468L; e.ttl = 9876543L; e.softTtl = 8765432L; e.etag = "etag"; e.responseHeaders = new HashMap<String, String>(); e.responseHeaders.put("fruit", "banana"); CacheHeader first = new CacheHeader("my-magical-key", e); ByteArrayOutputStream baos = new ByteArrayOutputStream(); first.writeHeader(baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); CacheHeader second = CacheHeader.readHeader(bais); assertEquals(first.key, second.key); assertEquals(first.serverDate, second.serverDate); assertEquals(first.lastModified, second.lastModified); assertEquals(first.ttl, second.ttl); assertEquals(first.softTtl, second.softTtl); assertEquals(first.etag, second.etag); assertEquals(first.responseHeaders, second.responseHeaders); }
public void testCacheHeaderSerialization() throws Exception { Cache.Entry e = new Cache.Entry(); e.data = new byte[8]; e.serverDate = 1234567L; e.ttl = 9876543L; e.softTtl = 8765432L; e.etag = "etag"; e.responseHeaders = new HashMap<String, String>(); e.responseHeaders.put("fruit", "banana"); CacheHeader first = new CacheHeader("my-magical-key", e); ByteArrayOutputStream baos = new ByteArrayOutputStream(); first.writeHeader(baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); CacheHeader second = CacheHeader.readHeader(bais); assertEquals(first.key, second.key); assertEquals(first.serverDate, second.serverDate); assertEquals(first.ttl, second.ttl); assertEquals(first.softTtl, second.softTtl); assertEquals(first.etag, second.etag); assertEquals(first.responseHeaders, second.responseHeaders); }
/** deserializing the old format into the new one. */ @Test public void testCacheHeaderSerializationOldToNewFormat() throws Exception { final int CACHE_MAGIC = 0x20140623; final String key = "key"; final String etag = "etag"; final long serverDate = 1234567890l; final long ttl = 1357924680l; final long softTtl = 2468013579l; Map<String, String> responseHeaders = new HashMap<String, String>(); responseHeaders.put("first", "thing"); responseHeaders.put("second", "item"); // write old sytle header (without lastModified) ByteArrayOutputStream baos = new ByteArrayOutputStream(); DiskBasedCache.writeInt(baos, CACHE_MAGIC); DiskBasedCache.writeString(baos, key); DiskBasedCache.writeString(baos, etag == null ? "" : etag); DiskBasedCache.writeLong(baos, serverDate); DiskBasedCache.writeLong(baos, ttl); DiskBasedCache.writeLong(baos, softTtl); DiskBasedCache.writeStringStringMap(responseHeaders, baos); // read / test new style header (with lastModified) ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); CacheHeader cacheHeader = CacheHeader.readHeader(bais); assertEquals(cacheHeader.key, key); assertEquals(cacheHeader.etag, etag); assertEquals(cacheHeader.serverDate, serverDate); assertEquals(cacheHeader.ttl, ttl); assertEquals(cacheHeader.softTtl, softTtl); assertEquals(cacheHeader.responseHeaders, responseHeaders); // the old format doesn't know lastModified assertEquals(cacheHeader.lastModified, 0); }