Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
18084 manas 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
import org.junit.Test;
22
 
23
import java.io.ByteArrayInputStream;
24
import java.io.ByteArrayOutputStream;
25
import java.io.File;
26
import java.util.HashMap;
27
import java.util.Map;
28
 
29
import static org.junit.Assert.*;
30
 
31
public class DiskBasedCacheTest {
32
 
33
    // Simple end-to-end serialize/deserialize test.
34
    @Test public void cacheHeaderSerialization() throws Exception {
35
        Cache.Entry e = new Cache.Entry();
36
        e.data = new byte[8];
37
        e.serverDate = 1234567L;
38
        e.lastModified = 13572468L;
39
        e.ttl = 9876543L;
40
        e.softTtl = 8765432L;
41
        e.etag = "etag";
42
        e.responseHeaders = new HashMap<String, String>();
43
        e.responseHeaders.put("fruit", "banana");
44
 
45
        CacheHeader first = new CacheHeader("my-magical-key", e);
46
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
47
        first.writeHeader(baos);
48
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
49
        CacheHeader second = CacheHeader.readHeader(bais);
50
 
51
        assertEquals(first.key, second.key);
52
        assertEquals(first.serverDate, second.serverDate);
53
        assertEquals(first.lastModified, second.lastModified);
54
        assertEquals(first.ttl, second.ttl);
55
        assertEquals(first.softTtl, second.softTtl);
56
        assertEquals(first.etag, second.etag);
57
        assertEquals(first.responseHeaders, second.responseHeaders);
58
    }
59
 
60
    @Test public void serializeInt() throws Exception {
61
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
62
        DiskBasedCache.writeInt(baos, 0);
63
        DiskBasedCache.writeInt(baos, 19791214);
64
        DiskBasedCache.writeInt(baos, -20050711);
65
        DiskBasedCache.writeInt(baos, Integer.MIN_VALUE);
66
        DiskBasedCache.writeInt(baos, Integer.MAX_VALUE);
67
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
68
        assertEquals(DiskBasedCache.readInt(bais), 0);
69
        assertEquals(DiskBasedCache.readInt(bais), 19791214);
70
        assertEquals(DiskBasedCache.readInt(bais), -20050711);
71
        assertEquals(DiskBasedCache.readInt(bais), Integer.MIN_VALUE);
72
        assertEquals(DiskBasedCache.readInt(bais), Integer.MAX_VALUE);
73
    }
74
 
75
    @Test public void serializeLong() throws Exception {
76
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
77
        DiskBasedCache.writeLong(baos, 0);
78
        DiskBasedCache.writeLong(baos, 31337);
79
        DiskBasedCache.writeLong(baos, -4160);
80
        DiskBasedCache.writeLong(baos, 4295032832L);
81
        DiskBasedCache.writeLong(baos, -4314824046L);
82
        DiskBasedCache.writeLong(baos, Long.MIN_VALUE);
83
        DiskBasedCache.writeLong(baos, Long.MAX_VALUE);
84
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
85
        assertEquals(DiskBasedCache.readLong(bais), 0);
86
        assertEquals(DiskBasedCache.readLong(bais), 31337);
87
        assertEquals(DiskBasedCache.readLong(bais), -4160);
88
        assertEquals(DiskBasedCache.readLong(bais), 4295032832L);
89
        assertEquals(DiskBasedCache.readLong(bais), -4314824046L);
90
        assertEquals(DiskBasedCache.readLong(bais), Long.MIN_VALUE);
91
        assertEquals(DiskBasedCache.readLong(bais), Long.MAX_VALUE);
92
    }
93
 
94
    @Test public void serializeString() throws Exception {
95
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
96
        DiskBasedCache.writeString(baos, "");
97
        DiskBasedCache.writeString(baos, "This is a string.");
98
        DiskBasedCache.writeString(baos, "ファイカス");
99
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
100
        assertEquals(DiskBasedCache.readString(bais), "");
101
        assertEquals(DiskBasedCache.readString(bais), "This is a string.");
102
        assertEquals(DiskBasedCache.readString(bais), "ファイカス");
103
    }
104
 
105
    @Test public void serializeMap() throws Exception {
106
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
107
        Map<String, String> empty = new HashMap<String, String>();
108
        DiskBasedCache.writeStringStringMap(empty, baos);
109
        DiskBasedCache.writeStringStringMap(null, baos);
110
        Map<String, String> twoThings = new HashMap<String, String>();
111
        twoThings.put("first", "thing");
112
        twoThings.put("second", "item");
113
        DiskBasedCache.writeStringStringMap(twoThings, baos);
114
        Map<String, String> emptyKey = new HashMap<String, String>();
115
        emptyKey.put("", "value");
116
        DiskBasedCache.writeStringStringMap(emptyKey, baos);
117
        Map<String, String> emptyValue = new HashMap<String, String>();
118
        emptyValue.put("key", "");
119
        DiskBasedCache.writeStringStringMap(emptyValue, baos);
120
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
121
        assertEquals(DiskBasedCache.readStringStringMap(bais), empty);
122
        assertEquals(DiskBasedCache.readStringStringMap(bais), empty); // null reads back empty
123
        assertEquals(DiskBasedCache.readStringStringMap(bais), twoThings);
124
        assertEquals(DiskBasedCache.readStringStringMap(bais), emptyKey);
125
        assertEquals(DiskBasedCache.readStringStringMap(bais), emptyValue);
126
    }
127
 
128
    @Test
129
    public void publicMethods() throws Exception {
130
        // Catch-all test to find API-breaking changes.
131
        assertNotNull(DiskBasedCache.class.getConstructor(File.class, int.class));
132
        assertNotNull(DiskBasedCache.class.getConstructor(File.class));
133
 
134
        assertNotNull(DiskBasedCache.class.getMethod("getFileForKey", String.class));
135
    }
136
}