DiskBasedCacheTest.java revision e5a344749f100325cd8ef0d78cd7db5221b5e628
1e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick/*
2e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick * Copyright (C) 2013 The Android Open Source Project
3e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick *
4e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick * Licensed under the Apache License, Version 2.0 (the "License");
5e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick * you may not use this file except in compliance with the License.
6e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick * You may obtain a copy of the License at
7e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick *
8e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick *      http://www.apache.org/licenses/LICENSE-2.0
9e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick *
10e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick * Unless required by applicable law or agreed to in writing, software
11e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick * distributed under the License is distributed on an "AS IS" BASIS,
12e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick * See the License for the specific language governing permissions and
14e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick * limitations under the License.
15e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick */
16e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick
17e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrickpackage com.android.volley.toolbox;
18e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick
19e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrickimport android.test.AndroidTestCase;
20e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick
21e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrickimport com.android.volley.Cache;
22e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrickimport com.android.volley.toolbox.DiskBasedCache.CacheHeader;
23e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick
24e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrickimport java.io.ByteArrayInputStream;
25e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrickimport java.io.ByteArrayOutputStream;
26e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrickimport java.util.HashMap;
27e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick
28e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrickpublic class DiskBasedCacheTest extends AndroidTestCase {
29e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick
30e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick    // Simple end-to-end serialize/deserialize test.
31e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick    public void testCacheHeaderSerialization() throws Exception {
32e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick        Cache.Entry e = new Cache.Entry();
33e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick        e.data = new byte[8];
34e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick        e.serverDate = 1234567L;
35e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick        e.ttl = 9876543L;
36e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick        e.softTtl = 8765432L;
37e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick        e.etag = "etag";
38e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick        e.responseHeaders = new HashMap<String, String>();
39e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick        e.responseHeaders.put("fruit", "banana");
40e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick
41e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick        CacheHeader first = new CacheHeader("my-magical-key", e);
42e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick        ByteArrayOutputStream baos = new ByteArrayOutputStream();
43e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick        first.writeHeader(baos);
44e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
45e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick        CacheHeader second = CacheHeader.readHeader(bais);
46e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick
47e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick        assertEquals(first.key, second.key);
48e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick        assertEquals(first.serverDate, second.serverDate);
49e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick        assertEquals(first.ttl, second.ttl);
50e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick        assertEquals(first.softTtl, second.softTtl);
51e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick        assertEquals(first.etag, second.etag);
52e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick        assertEquals(first.responseHeaders, second.responseHeaders);
53e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick    }
54e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick
55e5a344749f100325cd8ef0d78cd7db5221b5e628Ficus Kirkpatrick}
56