HttpResponseCacheTest.java revision a7284f0e72745d66155e1e282fc07113332790fa
1/*
2 * Copyright (C) 2011 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
17package android.net.http;
18
19import java.io.File;
20import java.net.CacheRequest;
21import java.net.CacheResponse;
22import java.net.ResponseCache;
23import java.net.URI;
24import java.net.URLConnection;
25import java.util.List;
26import java.util.Map;
27import java.util.UUID;
28import junit.framework.TestCase;
29
30public final class HttpResponseCacheTest extends TestCase {
31
32    private File cacheDir;
33
34    @Override public void setUp() throws Exception {
35        super.setUp();
36        String tmp = System.getProperty("java.io.tmpdir");
37        cacheDir = new File(tmp, "HttpCache-" + UUID.randomUUID());
38    }
39
40    @Override protected void tearDown() throws Exception {
41        ResponseCache.setDefault(null);
42        super.tearDown();
43    }
44
45    public void testInstall() throws Exception {
46        HttpResponseCache installed = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
47        assertNotNull(installed);
48        assertSame(installed, ResponseCache.getDefault());
49        assertSame(installed, HttpResponseCache.getDefault());
50    }
51
52    public void testSecondEquivalentInstallDoesNothing() throws Exception {
53        HttpResponseCache first = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
54        HttpResponseCache another = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
55        assertSame(first, another);
56    }
57
58    public void testInstallClosesPreviouslyInstalled() throws Exception {
59        HttpResponseCache first = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
60        HttpResponseCache another = HttpResponseCache.install(cacheDir, 8 * 1024 * 1024);
61        assertNotSame(first, another);
62        try {
63            first.flush();
64            fail();
65        } catch (IllegalStateException expected) {
66        }
67    }
68
69    public void testGetInstalledWithWrongTypeInstalled() {
70        ResponseCache.setDefault(new ResponseCache() {
71            @Override public CacheResponse get(URI uri, String requestMethod,
72                    Map<String, List<String>> requestHeaders) {
73                return null;
74            }
75            @Override public CacheRequest put(URI uri, URLConnection connection) {
76                return null;
77            }
78        });
79        assertNull(HttpResponseCache.getInstalled());
80    }
81
82    public void testCloseCloses() throws Exception {
83        HttpResponseCache cache = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
84        cache.close();
85        try {
86            cache.flush();
87            fail();
88        } catch (IllegalStateException expected) {
89        }
90    }
91
92    public void testCloseUninstalls() throws Exception {
93        HttpResponseCache cache = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
94        cache.close();
95        assertNull(ResponseCache.getDefault());
96    }
97
98    public void testDeleteUninstalls() throws Exception {
99        HttpResponseCache cache = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
100        cache.delete();
101        assertNull(ResponseCache.getDefault());
102    }
103}
104