FileDocCategorySizeDatePackage
HttpResponseCacheTest.javaAPI DocAndroid 5.1 API4922Thu Mar 12 22:22:12 GMT 2015android.net.http

HttpResponseCacheTest

public final class HttpResponseCacheTest extends TestCase

Fields Summary
private File
cacheDir
private com.google.mockwebserver.MockWebServer
server
Constructors Summary
Methods Summary
public voidsetUp()


          
        super.setUp();
        String tmp = System.getProperty("java.io.tmpdir");
        cacheDir = new File(tmp, "HttpCache-" + UUID.randomUUID());
    
protected voidtearDown()

        ResponseCache.setDefault(null);
        server.shutdown();
        super.tearDown();
    
public voidtestCloseCloses()

        HttpResponseCache cache = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
        cache.close();
        try {
            cache.flush();
            fail();
        } catch (IllegalStateException expected) {
        }
    
public voidtestCloseUninstalls()

        HttpResponseCache cache = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
        cache.close();
        assertNull(ResponseCache.getDefault());
    
public voidtestDeleteUninstalls()

        HttpResponseCache cache = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
        cache.delete();
        assertNull(ResponseCache.getDefault());
    
public voidtestGetInstalledWithWrongTypeInstalled()

        ResponseCache.setDefault(new ResponseCache() {
            @Override public CacheResponse get(URI uri, String requestMethod,
                    Map<String, List<String>> requestHeaders) {
                return null;
            }
            @Override public CacheRequest put(URI uri, URLConnection connection) {
                return null;
            }
        });
        assertNull(HttpResponseCache.getInstalled());
    
public voidtestInstall()

        HttpResponseCache installed = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
        assertNotNull(installed);
        assertSame(installed, ResponseCache.getDefault());
        assertSame(installed, HttpResponseCache.getDefault());
    
public voidtestInstallClosesPreviouslyInstalled()

        HttpResponseCache first = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
        HttpResponseCache another = HttpResponseCache.install(cacheDir, 8 * 1024 * 1024);
        assertNotSame(first, another);
        try {
            first.flush();
            fail();
        } catch (IllegalStateException expected) {
        }
    
public voidtestSecondEquivalentInstallDoesNothing()

        HttpResponseCache first = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
        HttpResponseCache another = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
        assertSame(first, another);
    
public voidtestStatisticsTracking()
Make sure that statistics tracking are wired all the way through the wrapper class. http://code.google.com/p/android/issues/detail?id=25418

        HttpResponseCache cache = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);

        server.enqueue(new MockResponse()
                .addHeader("Cache-Control: max-age=60")
                .setBody("A"));
        server.play();

        URLConnection c1 = server.getUrl("/").openConnection();
        assertEquals('A", c1.getInputStream().read());
        assertEquals(1, cache.getRequestCount());
        assertEquals(1, cache.getNetworkCount());
        assertEquals(0, cache.getHitCount());

        URLConnection c2 = server.getUrl("/").openConnection();
        assertEquals('A", c2.getInputStream().read());

        URLConnection c3 = server.getUrl("/").openConnection();
        assertEquals('A", c3.getInputStream().read());
        assertEquals(3, cache.getRequestCount());
        assertEquals(1, cache.getNetworkCount());
        assertEquals(2, cache.getHitCount());