| 21478 |
rajender |
1 |
/*
|
|
|
2 |
* Copyright (C) 2013 The Android Open Source Project
|
|
|
3 |
*
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
|
6 |
* You may obtain a copy of the License at
|
|
|
7 |
*
|
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
*
|
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13 |
* See the License for the specific language governing permissions and
|
|
|
14 |
* limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
package com.android.volley.toolbox;
|
|
|
18 |
|
|
|
19 |
import com.android.volley.Cache;
|
|
|
20 |
import com.android.volley.toolbox.DiskBasedCache.CacheHeader;
|
|
|
21 |
|
|
|
22 |
import org.junit.Test;
|
|
|
23 |
|
|
|
24 |
import java.io.ByteArrayInputStream;
|
|
|
25 |
import java.io.ByteArrayOutputStream;
|
|
|
26 |
import java.io.File;
|
|
|
27 |
import java.util.HashMap;
|
|
|
28 |
import java.util.Map;
|
|
|
29 |
|
|
|
30 |
import static org.junit.Assert.assertEquals;
|
|
|
31 |
import static org.junit.Assert.assertNotNull;
|
|
|
32 |
|
|
|
33 |
public class DiskBasedCacheTest {
|
|
|
34 |
|
|
|
35 |
// Simple end-to-end serialize/deserialize test.
|
|
|
36 |
@Test public void cacheHeaderSerialization() throws Exception {
|
|
|
37 |
Cache.Entry e = new Cache.Entry();
|
|
|
38 |
e.data = new byte[8];
|
|
|
39 |
e.serverDate = 1234567L;
|
|
|
40 |
e.lastModified = 13572468L;
|
|
|
41 |
e.ttl = 9876543L;
|
|
|
42 |
e.softTtl = 8765432L;
|
|
|
43 |
e.etag = "etag";
|
|
|
44 |
e.responseHeaders = new HashMap<String, String>();
|
|
|
45 |
e.responseHeaders.put("fruit", "banana");
|
|
|
46 |
|
|
|
47 |
CacheHeader first = new CacheHeader("my-magical-key", e);
|
|
|
48 |
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
49 |
first.writeHeader(baos);
|
|
|
50 |
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
|
|
|
51 |
CacheHeader second = CacheHeader.readHeader(bais);
|
|
|
52 |
|
|
|
53 |
assertEquals(first.key, second.key);
|
|
|
54 |
assertEquals(first.serverDate, second.serverDate);
|
|
|
55 |
assertEquals(first.lastModified, second.lastModified);
|
|
|
56 |
assertEquals(first.ttl, second.ttl);
|
|
|
57 |
assertEquals(first.softTtl, second.softTtl);
|
|
|
58 |
assertEquals(first.etag, second.etag);
|
|
|
59 |
assertEquals(first.responseHeaders, second.responseHeaders);
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
@Test public void serializeInt() throws Exception {
|
|
|
63 |
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
64 |
DiskBasedCache.writeInt(baos, 0);
|
|
|
65 |
DiskBasedCache.writeInt(baos, 19791214);
|
|
|
66 |
DiskBasedCache.writeInt(baos, -20050711);
|
|
|
67 |
DiskBasedCache.writeInt(baos, Integer.MIN_VALUE);
|
|
|
68 |
DiskBasedCache.writeInt(baos, Integer.MAX_VALUE);
|
|
|
69 |
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
|
|
|
70 |
assertEquals(DiskBasedCache.readInt(bais), 0);
|
|
|
71 |
assertEquals(DiskBasedCache.readInt(bais), 19791214);
|
|
|
72 |
assertEquals(DiskBasedCache.readInt(bais), -20050711);
|
|
|
73 |
assertEquals(DiskBasedCache.readInt(bais), Integer.MIN_VALUE);
|
|
|
74 |
assertEquals(DiskBasedCache.readInt(bais), Integer.MAX_VALUE);
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
@Test public void serializeLong() throws Exception {
|
|
|
78 |
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
79 |
DiskBasedCache.writeLong(baos, 0);
|
|
|
80 |
DiskBasedCache.writeLong(baos, 31337);
|
|
|
81 |
DiskBasedCache.writeLong(baos, -4160);
|
|
|
82 |
DiskBasedCache.writeLong(baos, 4295032832L);
|
|
|
83 |
DiskBasedCache.writeLong(baos, -4314824046L);
|
|
|
84 |
DiskBasedCache.writeLong(baos, Long.MIN_VALUE);
|
|
|
85 |
DiskBasedCache.writeLong(baos, Long.MAX_VALUE);
|
|
|
86 |
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
|
|
|
87 |
assertEquals(DiskBasedCache.readLong(bais), 0);
|
|
|
88 |
assertEquals(DiskBasedCache.readLong(bais), 31337);
|
|
|
89 |
assertEquals(DiskBasedCache.readLong(bais), -4160);
|
|
|
90 |
assertEquals(DiskBasedCache.readLong(bais), 4295032832L);
|
|
|
91 |
assertEquals(DiskBasedCache.readLong(bais), -4314824046L);
|
|
|
92 |
assertEquals(DiskBasedCache.readLong(bais), Long.MIN_VALUE);
|
|
|
93 |
assertEquals(DiskBasedCache.readLong(bais), Long.MAX_VALUE);
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
@Test public void serializeString() throws Exception {
|
|
|
97 |
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
98 |
DiskBasedCache.writeString(baos, "");
|
|
|
99 |
DiskBasedCache.writeString(baos, "This is a string.");
|
|
|
100 |
DiskBasedCache.writeString(baos, "ファイカス");
|
|
|
101 |
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
|
|
|
102 |
assertEquals(DiskBasedCache.readString(bais), "");
|
|
|
103 |
assertEquals(DiskBasedCache.readString(bais), "This is a string.");
|
|
|
104 |
assertEquals(DiskBasedCache.readString(bais), "ファイカス");
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
@Test public void serializeMap() throws Exception {
|
|
|
108 |
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
109 |
Map<String, String> empty = new HashMap<String, String>();
|
|
|
110 |
DiskBasedCache.writeStringStringMap(empty, baos);
|
|
|
111 |
DiskBasedCache.writeStringStringMap(null, baos);
|
|
|
112 |
Map<String, String> twoThings = new HashMap<String, String>();
|
|
|
113 |
twoThings.put("first", "thing");
|
|
|
114 |
twoThings.put("second", "item");
|
|
|
115 |
DiskBasedCache.writeStringStringMap(twoThings, baos);
|
|
|
116 |
Map<String, String> emptyKey = new HashMap<String, String>();
|
|
|
117 |
emptyKey.put("", "value");
|
|
|
118 |
DiskBasedCache.writeStringStringMap(emptyKey, baos);
|
|
|
119 |
Map<String, String> emptyValue = new HashMap<String, String>();
|
|
|
120 |
emptyValue.put("key", "");
|
|
|
121 |
DiskBasedCache.writeStringStringMap(emptyValue, baos);
|
|
|
122 |
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
|
|
|
123 |
assertEquals(DiskBasedCache.readStringStringMap(bais), empty);
|
|
|
124 |
assertEquals(DiskBasedCache.readStringStringMap(bais), empty); // null reads back empty
|
|
|
125 |
assertEquals(DiskBasedCache.readStringStringMap(bais), twoThings);
|
|
|
126 |
assertEquals(DiskBasedCache.readStringStringMap(bais), emptyKey);
|
|
|
127 |
assertEquals(DiskBasedCache.readStringStringMap(bais), emptyValue);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
@Test
|
|
|
131 |
public void publicMethods() throws Exception {
|
|
|
132 |
// Catch-all test to find API-breaking changes.
|
|
|
133 |
assertNotNull(DiskBasedCache.class.getConstructor(File.class, int.class));
|
|
|
134 |
assertNotNull(DiskBasedCache.class.getConstructor(File.class));
|
|
|
135 |
|
|
|
136 |
assertNotNull(DiskBasedCache.class.getMethod("getFileForKey", String.class));
|
|
|
137 |
}
|
|
|
138 |
}
|