com.danikula.videocache.file.FileCache Java Examples

The following examples show how to use com.danikula.videocache.file.FileCache. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpProxyCacheTest.java    From AndriodVideoCache with Apache License 2.0 6 votes vote down vote up
@Test   // https://github.com/danikula/AndroidVideoCache/issues/43
public void testPreventClosingOriginalSourceForNewPartialRequestWithoutCache() throws Exception {
    HttpUrlSource source = new HttpUrlSource(HTTP_DATA_BIG_URL);
    FileCache fileCache = new FileCache(ProxyCacheTestUtils.newCacheFile());
    HttpProxyCache proxyCache = new HttpProxyCache(source, fileCache);
    ExecutorService executor = Executors.newFixedThreadPool(5);
    Future<Response> firstRequestFeature = processAsync(executor, proxyCache, "GET /" + HTTP_DATA_URL + " HTTP/1.1");
    Thread.sleep(100);  // wait for first request started to process

    int offset = 30000;
    String partialRequest = "GET /" + HTTP_DATA_URL + " HTTP/1.1\nRange: bytes=" + offset + "-";
    Future<Response> secondRequestFeature = processAsync(executor, proxyCache, partialRequest);

    Response secondResponse = secondRequestFeature.get();
    Response firstResponse = firstRequestFeature.get();

    byte[] responseData = loadAssetFile(ASSETS_DATA_BIG_NAME);
    assertThat(firstResponse.data).isEqualTo(responseData);

    byte[] partialData = new byte[responseData.length - offset];
    System.arraycopy(responseData, offset, partialData, 0, partialData.length);
    assertThat(secondResponse.data).isEqualTo(partialData);
}
 
Example #2
Source File: HttpProxyCacheTest.java    From AndriodVideoCache with Apache License 2.0 6 votes vote down vote up
@Test
public void testLoadEmptyFile() throws Exception {
    String zeroSizeUrl = "https://raw.githubusercontent.com/danikula/AndroidVideoCache/master/files/empty.txt";
    HttpUrlSource source = new HttpUrlSource(zeroSizeUrl);
    HttpProxyCache proxyCache = new HttpProxyCache(source, new FileCache(ProxyCacheTestUtils.newCacheFile()));
    GetRequest request = new GetRequest("GET /" + HTTP_DATA_URL + " HTTP/1.1");
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Socket socket = mock(Socket.class);
    when(socket.getOutputStream()).thenReturn(out);

    CacheListener listener = Mockito.mock(CacheListener.class);
    proxyCache.registerCacheListener(listener);
    proxyCache.processRequest(request, socket);
    proxyCache.registerCacheListener(null);
    Response response = new Response(out.toByteArray());

    Mockito.verify(listener).onCacheAvailable(Mockito.<File>any(), eq(zeroSizeUrl), eq(100));
    assertThat(response.data).isEmpty();
}
 
Example #3
Source File: HttpProxyCacheTest.java    From AndroidVideoCache with Apache License 2.0 6 votes vote down vote up
@Test   // https://github.com/danikula/AndroidVideoCache/issues/43
public void testPreventClosingOriginalSourceForNewPartialRequestWithoutCache() throws Exception {
    HttpUrlSource source = new HttpUrlSource(HTTP_DATA_BIG_URL);
    FileCache fileCache = new FileCache(ProxyCacheTestUtils.newCacheFile());
    HttpProxyCache proxyCache = new HttpProxyCache(source, fileCache);
    ExecutorService executor = Executors.newFixedThreadPool(5);
    Future<Response> firstRequestFeature = processAsync(executor, proxyCache, "GET /" + HTTP_DATA_URL + " HTTP/1.1");
    Thread.sleep(100);  // wait for first request started to process

    int offset = 30000;
    String partialRequest = "GET /" + HTTP_DATA_URL + " HTTP/1.1\nRange: bytes=" + offset + "-";
    Future<Response> secondRequestFeature = processAsync(executor, proxyCache, partialRequest);

    Response secondResponse = secondRequestFeature.get();
    Response firstResponse = firstRequestFeature.get();

    byte[] responseData = loadAssetFile(ASSETS_DATA_BIG_NAME);
    assertThat(firstResponse.data).isEqualTo(responseData);

    byte[] partialData = new byte[responseData.length - offset];
    System.arraycopy(responseData, offset, partialData, 0, partialData.length);
    assertThat(secondResponse.data).isEqualTo(partialData);
}
 
Example #4
Source File: HttpProxyCacheTest.java    From AndroidVideoCache with Apache License 2.0 6 votes vote down vote up
@Test
public void testLoadEmptyFile() throws Exception {
    String zeroSizeUrl = "https://raw.githubusercontent.com/danikula/AndroidVideoCache/master/files/empty.txt";
    HttpUrlSource source = new HttpUrlSource(zeroSizeUrl);
    HttpProxyCache proxyCache = new HttpProxyCache(source, new FileCache(ProxyCacheTestUtils.newCacheFile()));
    GetRequest request = new GetRequest("GET /" + HTTP_DATA_URL + " HTTP/1.1");
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Socket socket = mock(Socket.class);
    when(socket.getOutputStream()).thenReturn(out);

    CacheListener listener = Mockito.mock(CacheListener.class);
    proxyCache.registerCacheListener(listener);
    proxyCache.processRequest(request, socket);
    proxyCache.registerCacheListener(null);
    Response response = new Response(out.toByteArray());

    Mockito.verify(listener).onCacheAvailable(Mockito.<File>any(), eq(zeroSizeUrl), eq(100));
    assertThat(response.data).isEmpty();
}
 
Example #5
Source File: HttpProxyCacheTest.java    From AndriodVideoCache with Apache License 2.0 6 votes vote down vote up
@Test
public void testReuseSourceInfo() throws Exception {
    SourceInfoStorage sourceInfoStorage = SourceInfoStorageFactory.newSourceInfoStorage(RuntimeEnvironment.application);
    HttpUrlSource source = new HttpUrlSource(HTTP_DATA_URL, sourceInfoStorage);
    File cacheFile = newCacheFile();
    HttpProxyCache proxyCache = new HttpProxyCache(source, new FileCache(cacheFile));
    processRequest(proxyCache, "GET /" + HTTP_DATA_URL + " HTTP/1.1");

    HttpUrlSource notOpenableSource = ProxyCacheTestUtils.newNotOpenableHttpUrlSource(HTTP_DATA_URL, sourceInfoStorage);
    HttpProxyCache proxyCache2 = new HttpProxyCache(notOpenableSource, new FileCache(cacheFile));
    Response response = processRequest(proxyCache2, "GET /" + HTTP_DATA_URL + " HTTP/1.1");
    proxyCache.shutdown();

    assertThat(response.data).isEqualTo(loadAssetFile(ASSETS_DATA_NAME));
    assertThat(response.contentLength).isEqualTo(HTTP_DATA_SIZE);
    assertThat(response.contentType).isEqualTo("image/jpeg");
}
 
Example #6
Source File: ProxyCacheTest.java    From AndroidVideoCache with Apache License 2.0 6 votes vote down vote up
@Test
public void testReadRandomParts() throws Exception {
    int dataSize = 123456;
    byte[] sourceData = generate(dataSize);
    Source source = newPhlegmaticSource(sourceData, 300);
    File file = newCacheFile();
    Cache cache = new FileCache(file);
    ProxyCache proxyCache = new ProxyCache(source, cache);
    Random random = new Random(System.currentTimeMillis());
    for (int i = 0; i < 100; i++) {
        int offset = random.nextInt(dataSize);
        int bufferSize = random.nextInt(dataSize / 4);
        bufferSize = Math.min(bufferSize, dataSize - offset);
        byte[] buffer = new byte[bufferSize];
        proxyCache.read(buffer, offset, bufferSize);
        byte[] dataPortion = Arrays.copyOfRange(sourceData, offset, offset + bufferSize);
        assertThat(buffer).isEqualTo(dataPortion);
    }
    proxyCache.read(new byte[1], dataSize - 1, 1);
    TimeUnit.MILLISECONDS.sleep(200); // wait for completion
    assertThat(cache.isCompleted()).isTrue();
    assertThat(sourceData).isEqualTo(getFileContent(file));
}
 
Example #7
Source File: ProxyCacheTest.java    From AndriodVideoCache with Apache License 2.0 6 votes vote down vote up
@Test
public void testReadRandomParts() throws Exception {
    int dataSize = 123456;
    byte[] sourceData = generate(dataSize);
    Source source = newPhlegmaticSource(sourceData, 300);
    File file = newCacheFile();
    Cache cache = new FileCache(file);
    ProxyCache proxyCache = new ProxyCache(source, cache);
    Random random = new Random(System.currentTimeMillis());
    for (int i = 0; i < 100; i++) {
        int offset = random.nextInt(dataSize);
        int bufferSize = random.nextInt(dataSize / 4);
        bufferSize = Math.min(bufferSize, dataSize - offset);
        byte[] buffer = new byte[bufferSize];
        proxyCache.read(buffer, offset, bufferSize);
        byte[] dataPortion = Arrays.copyOfRange(sourceData, offset, offset + bufferSize);
        assertThat(buffer).isEqualTo(dataPortion);
    }
    proxyCache.read(new byte[1], dataSize - 1, 1);
    TimeUnit.MILLISECONDS.sleep(200); // wait for completion
    assertThat(cache.isCompleted()).isTrue();
    assertThat(sourceData).isEqualTo(getFileContent(file));
}
 
Example #8
Source File: HttpProxyCacheTest.java    From AndroidVideoCache with Apache License 2.0 6 votes vote down vote up
@Test
public void testReuseSourceInfo() throws Exception {
    SourceInfoStorage sourceInfoStorage = SourceInfoStorageFactory.newSourceInfoStorage(RuntimeEnvironment.application);
    HttpUrlSource source = new HttpUrlSource(HTTP_DATA_URL, sourceInfoStorage);
    File cacheFile = newCacheFile();
    HttpProxyCache proxyCache = new HttpProxyCache(source, new FileCache(cacheFile));
    processRequest(proxyCache, "GET /" + HTTP_DATA_URL + " HTTP/1.1");

    HttpUrlSource notOpenableSource = ProxyCacheTestUtils.newNotOpenableHttpUrlSource(HTTP_DATA_URL, sourceInfoStorage);
    HttpProxyCache proxyCache2 = new HttpProxyCache(notOpenableSource, new FileCache(cacheFile));
    Response response = processRequest(proxyCache2, "GET /" + HTTP_DATA_URL + " HTTP/1.1");
    proxyCache.shutdown();

    assertThat(response.data).isEqualTo(loadAssetFile(ASSETS_DATA_NAME));
    assertThat(response.contentLength).isEqualTo(HTTP_DATA_SIZE);
    assertThat(response.contentType).isEqualTo("image/jpeg");
}
 
Example #9
Source File: HttpProxyCacheServerClients.java    From GSYVideoPlayer with Apache License 2.0 5 votes vote down vote up
private HttpProxyCache newHttpProxyCache() throws ProxyCacheException {
    HttpUrlSource source = new HttpUrlSource(url, config.sourceInfoStorage, config.headerInjector);
    FileCache cache = new FileCache(config.generateCacheFile(url), config.diskUsage);
    HttpProxyCache httpProxyCache = new HttpProxyCache(source, cache);
    httpProxyCache.registerCacheListener(uiCacheListener);
    return httpProxyCache;
}
 
Example #10
Source File: HttpProxyCacheServerClients.java    From AndroidVideoCache with Apache License 2.0 5 votes vote down vote up
private HttpProxyCache newHttpProxyCache() throws ProxyCacheException {
    HttpUrlSource source = new HttpUrlSource(url, config.sourceInfoStorage, config.headerInjector);
    FileCache cache = new FileCache(config.generateCacheFile(url), config.diskUsage);
    HttpProxyCache httpProxyCache = new HttpProxyCache(source, cache);
    httpProxyCache.registerCacheListener(uiCacheListener);
    return httpProxyCache;
}
 
Example #11
Source File: HttpProxyCacheTest.java    From AndroidVideoCache with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcessPartialRequestWithoutCache() throws Exception {
    FileCache fileCache = new FileCache(ProxyCacheTestUtils.newCacheFile());
    FileCache spyFileCache = Mockito.spy(fileCache);
    doThrow(new RuntimeException()).when(spyFileCache).read(any(byte[].class), anyLong(), anyInt());

    String httpRequest = "GET /" + HTTP_DATA_URL + " HTTP/1.1\nRange: bytes=2000-";
    Response response = processRequest(HTTP_DATA_URL, httpRequest, spyFileCache);

    byte[] fullData = loadTestData();
    byte[] partialData = new byte[fullData.length - 2000];
    System.arraycopy(fullData, 2000, partialData, 0, partialData.length);
    assertThat(response.data).isEqualTo(partialData);
    assertThat(response.code).isEqualTo(206);
}
 
Example #12
Source File: HttpProxyCacheTest.java    From AndroidVideoCache with Apache License 2.0 5 votes vote down vote up
@Test
public void testCacheListenerCalledAtTheEnd() throws Exception {
    File file = ProxyCacheTestUtils.newCacheFile();
    File tempFile = ProxyCacheTestUtils.getTempFile(file);
    HttpProxyCache proxyCache = new HttpProxyCache(new HttpUrlSource(HTTP_DATA_URL), new FileCache(file));
    CacheListener listener = Mockito.mock(CacheListener.class);
    proxyCache.registerCacheListener(listener);
    processRequest(proxyCache, "GET /" + HTTP_DATA_URL + " HTTP/1.1");

    Mockito.verify(listener).onCacheAvailable(tempFile, HTTP_DATA_URL, 100);    // must be called for temp file ...
    Mockito.verify(listener).onCacheAvailable(file, HTTP_DATA_URL, 100);        // .. and for original file too
}
 
Example #13
Source File: HttpProxyCacheTest.java    From AndroidVideoCache with Apache License 2.0 5 votes vote down vote up
@Test(expected = ProxyCacheException.class)
public void testTouchSourceForAbsentSourceInfoAndCache() throws Exception {
    SourceInfoStorage sourceInfoStorage = SourceInfoStorageFactory.newEmptySourceInfoStorage();
    HttpUrlSource source = ProxyCacheTestUtils.newNotOpenableHttpUrlSource(HTTP_DATA_URL, sourceInfoStorage);
    HttpProxyCache proxyCache = new HttpProxyCache(source, new FileCache(newCacheFile()));
    processRequest(proxyCache, "GET /" + HTTP_DATA_URL + " HTTP/1.1");
    proxyCache.shutdown();
    fail("Angry source should throw error! There is no file and caches source info");
}
 
Example #14
Source File: HttpProxyCacheTest.java    From AndroidVideoCache with Apache License 2.0 5 votes vote down vote up
@Test(expected = ProxyCacheException.class)
public void testTouchSourceForExistedSourceInfoAndAbsentCache() throws Exception {
    SourceInfoStorage sourceInfoStorage = SourceInfoStorageFactory.newSourceInfoStorage(RuntimeEnvironment.application);
    sourceInfoStorage.put(HTTP_DATA_URL, new SourceInfo(HTTP_DATA_URL, HTTP_DATA_SIZE, "image/jpg"));
    HttpUrlSource source = ProxyCacheTestUtils.newNotOpenableHttpUrlSource(HTTP_DATA_URL, sourceInfoStorage);
    HttpProxyCache proxyCache = new HttpProxyCache(source, new FileCache(newCacheFile()));
    processRequest(proxyCache, "GET /" + HTTP_DATA_URL + " HTTP/1.1");
    proxyCache.shutdown();
    fail("Angry source should throw error! There is no cache file");
}
 
Example #15
Source File: HttpProxyCacheTest.java    From AndroidVideoCache with Apache License 2.0 5 votes vote down vote up
@Test
public void testTouchSourceForExistedSourceInfoAndCache() throws Exception {
    SourceInfoStorage sourceInfoStorage = SourceInfoStorageFactory.newSourceInfoStorage(RuntimeEnvironment.application);
    sourceInfoStorage.put(HTTP_DATA_URL, new SourceInfo(HTTP_DATA_URL, HTTP_DATA_SIZE, "cached/mime"));
    HttpUrlSource source = ProxyCacheTestUtils.newNotOpenableHttpUrlSource(HTTP_DATA_URL, sourceInfoStorage);
    File file = newCacheFile();
    IoUtils.saveToFile(loadAssetFile(ASSETS_DATA_NAME), file);
    HttpProxyCache proxyCache = new HttpProxyCache(source, new FileCache(file));
    Response response = processRequest(proxyCache, "GET /" + HTTP_DATA_URL + " HTTP/1.1");
    proxyCache.shutdown();
    assertThat(response.data).isEqualTo(loadAssetFile(ASSETS_DATA_NAME));
    assertThat(response.contentLength).isEqualTo(HTTP_DATA_SIZE);
    assertThat(response.contentType).isEqualTo("cached/mime");
}
 
Example #16
Source File: ProxyCacheTest.java    From AndroidVideoCache with Apache License 2.0 5 votes vote down vote up
@Test
public void testProxyWithPhlegmaticSource() throws Exception {
    int dataSize = 100000;
    byte[] sourceData = generate(dataSize);
    Source source = newPhlegmaticSource(sourceData, 200);
    ProxyCache proxyCache = new ProxyCache(source, new FileCache(newCacheFile()));
    byte[] readData = new byte[dataSize];
    proxyCache.read(readData, 0, dataSize);
    assertThat(readData).isEqualTo(sourceData);
}
 
Example #17
Source File: ProxyCacheTest.java    From AndroidVideoCache with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadEnd() throws Exception {
    int capacity = 5323;
    Source source = newPhlegmaticSource(generate(capacity), 200);
    Cache cache = new FileCache(newCacheFile());
    ProxyCache proxyCache = new ProxyCache(source, cache);
    proxyCache.read(new byte[1], capacity - 1, 1);
    TimeUnit.MILLISECONDS.sleep(200); // wait for completion
    assertThat(cache.isCompleted()).isTrue();
}
 
Example #18
Source File: ProxyCacheTest.java    From AndroidVideoCache with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadingHttpData() throws Exception {
    Source source = new HttpUrlSource(HTTP_DATA_URL);
    ProxyCache proxyCache = new ProxyCache(source, new FileCache(newCacheFile()));
    byte[] remoteData = new byte[HTTP_DATA_SIZE];
    proxyCache.read(remoteData, 0, HTTP_DATA_SIZE);
    proxyCache.shutdown();

    assertThat(remoteData).isEqualTo(loadAssetFile(ASSETS_DATA_NAME));
}
 
Example #19
Source File: ProxyCacheTest.java    From AndroidVideoCache with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadMoreThanAvailable() throws Exception {
    byte[] data = generate(20000);
    Cache fileCache = new FileCache(newCacheFile());
    ProxyCache proxyCache = new ProxyCache(new ByteArraySource(data), fileCache);

    byte[] buffer = new byte[15000];
    proxyCache.read(buffer, 18000, buffer.length);
    byte[] expectedData = new byte[15000];
    System.arraycopy(data, 18000, expectedData, 0, 2000);
    assertThat(buffer).isEqualTo(expectedData);
}
 
Example #20
Source File: ProxyCacheTest.java    From AndroidVideoCache with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompletion() throws Exception {
    Cache cache = new FileCache(newCacheFile());
    ProxyCache proxyCache = new ProxyCache(new ByteArraySource(generate(20000)), cache);
    proxyCache.read(new byte[5], 19999, 5);
    assertThat(cache.isCompleted()).isTrue();
}
 
Example #21
Source File: ProxyCacheTest.java    From AndroidVideoCache with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoTouchSource() throws Exception {
    int dataSize = 2000;
    byte[] data = generate(dataSize);
    File file = newCacheFile();
    IoUtils.saveToFile(data, file);

    Source source = ProxyCacheTestUtils.newAngryHttpUrlSource();
    ProxyCache proxyCache = new ProxyCache(source, new FileCache(file));
    byte[] readData = new byte[dataSize];
    proxyCache.read(readData, 0, dataSize);

    assertThat(readData).isEqualTo(data);
}
 
Example #22
Source File: HttpProxyCacheTest.java    From AndriodVideoCache with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcessPartialRequestWithoutCache() throws Exception {
    FileCache fileCache = new FileCache(ProxyCacheTestUtils.newCacheFile());
    FileCache spyFileCache = Mockito.spy(fileCache);
    doThrow(new RuntimeException()).when(spyFileCache).read(any(byte[].class), anyLong(), anyInt());

    String httpRequest = "GET /" + HTTP_DATA_URL + " HTTP/1.1\nRange: bytes=2000-";
    Response response = processRequest(HTTP_DATA_URL, httpRequest, spyFileCache);

    byte[] fullData = loadTestData();
    byte[] partialData = new byte[fullData.length - 2000];
    System.arraycopy(fullData, 2000, partialData, 0, partialData.length);
    assertThat(response.data).isEqualTo(partialData);
    assertThat(response.code).isEqualTo(206);
}
 
Example #23
Source File: HttpProxyCacheServerClients.java    From DKVideoPlayer with Apache License 2.0 5 votes vote down vote up
private HttpProxyCache newHttpProxyCache() throws ProxyCacheException {
    HttpUrlSource source = new HttpUrlSource(url, config.sourceInfoStorage, config.headerInjector);
    FileCache cache = new FileCache(config.generateCacheFile(url), config.diskUsage);
    HttpProxyCache httpProxyCache = new HttpProxyCache(source, cache);
    httpProxyCache.registerCacheListener(uiCacheListener);
    return httpProxyCache;
}
 
Example #24
Source File: HttpProxyCacheTest.java    From AndriodVideoCache with Apache License 2.0 5 votes vote down vote up
@Test
public void testCacheListenerCalledAtTheEnd() throws Exception {
    File file = ProxyCacheTestUtils.newCacheFile();
    File tempFile = ProxyCacheTestUtils.getTempFile(file);
    HttpProxyCache proxyCache = new HttpProxyCache(new HttpUrlSource(HTTP_DATA_URL), new FileCache(file));
    CacheListener listener = Mockito.mock(CacheListener.class);
    proxyCache.registerCacheListener(listener);
    processRequest(proxyCache, "GET /" + HTTP_DATA_URL + " HTTP/1.1");

    Mockito.verify(listener).onCacheAvailable(tempFile, HTTP_DATA_URL, 100);    // must be called for temp file ...
    Mockito.verify(listener).onCacheAvailable(file, HTTP_DATA_URL, 100);        // .. and for original file too
}
 
Example #25
Source File: HttpProxyCacheTest.java    From AndriodVideoCache with Apache License 2.0 5 votes vote down vote up
@Test(expected = ProxyCacheException.class)
public void testTouchSourceForAbsentSourceInfoAndCache() throws Exception {
    SourceInfoStorage sourceInfoStorage = SourceInfoStorageFactory.newEmptySourceInfoStorage();
    HttpUrlSource source = ProxyCacheTestUtils.newNotOpenableHttpUrlSource(HTTP_DATA_URL, sourceInfoStorage);
    HttpProxyCache proxyCache = new HttpProxyCache(source, new FileCache(newCacheFile()));
    processRequest(proxyCache, "GET /" + HTTP_DATA_URL + " HTTP/1.1");
    proxyCache.shutdown();
    fail("Angry source should throw error! There is no file and caches source info");
}
 
Example #26
Source File: HttpProxyCacheTest.java    From AndriodVideoCache with Apache License 2.0 5 votes vote down vote up
@Test(expected = ProxyCacheException.class)
public void testTouchSourceForExistedSourceInfoAndAbsentCache() throws Exception {
    SourceInfoStorage sourceInfoStorage = SourceInfoStorageFactory.newSourceInfoStorage(RuntimeEnvironment.application);
    sourceInfoStorage.put(HTTP_DATA_URL, new SourceInfo(HTTP_DATA_URL, HTTP_DATA_SIZE, "image/jpg"));
    HttpUrlSource source = ProxyCacheTestUtils.newNotOpenableHttpUrlSource(HTTP_DATA_URL, sourceInfoStorage);
    HttpProxyCache proxyCache = new HttpProxyCache(source, new FileCache(newCacheFile()));
    processRequest(proxyCache, "GET /" + HTTP_DATA_URL + " HTTP/1.1");
    proxyCache.shutdown();
    fail("Angry source should throw error! There is no cache file");
}
 
Example #27
Source File: HttpProxyCacheTest.java    From AndriodVideoCache with Apache License 2.0 5 votes vote down vote up
@Test
public void testTouchSourceForExistedSourceInfoAndCache() throws Exception {
    SourceInfoStorage sourceInfoStorage = SourceInfoStorageFactory.newSourceInfoStorage(RuntimeEnvironment.application);
    sourceInfoStorage.put(HTTP_DATA_URL, new SourceInfo(HTTP_DATA_URL, HTTP_DATA_SIZE, "cached/mime"));
    HttpUrlSource source = ProxyCacheTestUtils.newNotOpenableHttpUrlSource(HTTP_DATA_URL, sourceInfoStorage);
    File file = newCacheFile();
    IoUtils.saveToFile(loadAssetFile(ASSETS_DATA_NAME), file);
    HttpProxyCache proxyCache = new HttpProxyCache(source, new FileCache(file));
    Response response = processRequest(proxyCache, "GET /" + HTTP_DATA_URL + " HTTP/1.1");
    proxyCache.shutdown();
    assertThat(response.data).isEqualTo(loadAssetFile(ASSETS_DATA_NAME));
    assertThat(response.contentLength).isEqualTo(HTTP_DATA_SIZE);
    assertThat(response.contentType).isEqualTo("cached/mime");
}
 
Example #28
Source File: ProxyCacheTest.java    From AndriodVideoCache with Apache License 2.0 5 votes vote down vote up
@Test
public void testProxyWithPhlegmaticSource() throws Exception {
    int dataSize = 100000;
    byte[] sourceData = generate(dataSize);
    Source source = newPhlegmaticSource(sourceData, 200);
    ProxyCache proxyCache = new ProxyCache(source, new FileCache(newCacheFile()));
    byte[] readData = new byte[dataSize];
    proxyCache.read(readData, 0, dataSize);
    assertThat(readData).isEqualTo(sourceData);
}
 
Example #29
Source File: ProxyCacheTest.java    From AndriodVideoCache with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadEnd() throws Exception {
    int capacity = 5323;
    Source source = newPhlegmaticSource(generate(capacity), 200);
    Cache cache = new FileCache(newCacheFile());
    ProxyCache proxyCache = new ProxyCache(source, cache);
    proxyCache.read(new byte[1], capacity - 1, 1);
    TimeUnit.MILLISECONDS.sleep(200); // wait for completion
    assertThat(cache.isCompleted()).isTrue();
}
 
Example #30
Source File: ProxyCacheTest.java    From AndriodVideoCache with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadingHttpData() throws Exception {
    Source source = new HttpUrlSource(HTTP_DATA_URL);
    ProxyCache proxyCache = new ProxyCache(source, new FileCache(newCacheFile()));
    byte[] remoteData = new byte[HTTP_DATA_SIZE];
    proxyCache.read(remoteData, 0, HTTP_DATA_SIZE);
    proxyCache.shutdown();

    assertThat(remoteData).isEqualTo(loadAssetFile(ASSETS_DATA_NAME));
}