// Path: test/src/test/java/com/danikula/videocache/BaseTest.java
// @RunWith(RobolectricTestRunner.class)
// @Config(constants = BuildConfig.class)
// public abstract class BaseTest {
// 
//     static {
//         System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "trace");
//     }
// 
// }
// 
// Path: library/src/main/java/com/danikula/videocache/Cache.java
// public interface Cache {
// 
//     long available() throws ProxyCacheException;
// 
//     int read(byte[] buffer, long offset, int length) throws ProxyCacheException;
// 
//     void append(byte[] data, int length) throws ProxyCacheException;
// 
//     void close() throws ProxyCacheException;
// 
//     void complete() throws ProxyCacheException;
// 
//     boolean isCompleted();
// }
// 
// Path: library/src/main/java/com/danikula/videocache/ProxyCacheException.java
// public class ProxyCacheException extends Exception {
// 
//     private static final String LIBRARY_VERSION = ". Version: " + BuildConfig.VERSION_NAME;
// 
//     public ProxyCacheException(String message) {
//         super(message + LIBRARY_VERSION);
//     }
// 
//     public ProxyCacheException(String message, Throwable cause) {
//         super(message + LIBRARY_VERSION, cause);
//     }
// 
//     public ProxyCacheException(Throwable cause) {
//         super("No explanation error" + LIBRARY_VERSION, cause);
//     }
// }
// 
// Path: test/src/test/java/com/danikula/videocache/support/ProxyCacheTestUtils.java
// public static final String ASSETS_DATA_NAME = "android.jpg";
// 
// Path: test/src/test/java/com/danikula/videocache/support/ProxyCacheTestUtils.java
// public static byte[] generate(int capacity) {
//     Random random = new Random(System.currentTimeMillis());
//     byte[] result = new byte[capacity];
//     random.nextBytes(result);
//     return result;
// }
// 
// Path: test/src/test/java/com/danikula/videocache/support/ProxyCacheTestUtils.java
// public static byte[] getFileContent(File file) throws IOException {
//     return Files.asByteSource(file).read();
// }
// 
// Path: test/src/test/java/com/danikula/videocache/support/ProxyCacheTestUtils.java
// public static File getTempFile(File file) {
//     return new File(file.getParentFile(), file.getName() + ".download");
// }
// 
// Path: test/src/test/java/com/danikula/videocache/support/ProxyCacheTestUtils.java
// public static byte[] loadAssetFile(String name) throws IOException {
//     InputStream in = RuntimeEnvironment.application.getResources().getAssets().open(name);
//     ByteArrayOutputStream out = new ByteArrayOutputStream();
//     IoUtils.copy(in, out);
//     IoUtils.closeSilently(in);
//     IoUtils.closeSilently(out);
//     return out.toByteArray();
// }
// 
// Path: test/src/test/java/com/danikula/videocache/support/ProxyCacheTestUtils.java
// public static File newCacheFile() {
//     return new File(RuntimeEnvironment.application.getCacheDir(), UUID.randomUUID().toString());
// }


import com.danikula.android.garden.io.Files;
import com.danikula.videocache.BaseTest;
import com.danikula.videocache.Cache;
import com.danikula.videocache.ProxyCacheException;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import static com.danikula.videocache.support.ProxyCacheTestUtils.ASSETS_DATA_NAME;
import static com.danikula.videocache.support.ProxyCacheTestUtils.generate;
import static com.danikula.videocache.support.ProxyCacheTestUtils.getFileContent;
import static com.danikula.videocache.support.ProxyCacheTestUtils.getTempFile;
import static com.danikula.videocache.support.ProxyCacheTestUtils.loadAssetFile;
import static com.danikula.videocache.support.ProxyCacheTestUtils.newCacheFile;
import static com.google.common.io.Files.write;
import static org.fest.assertions.api.Assertions.assertThat;
package com.danikula.videocache.file;





/**
 * @author Alexey Danilov (danikula@gmail.com).
 */
public class FileCacheTest extends BaseTest {

    @Test
    public void testWriteReadDiscCache() throws Exception {
        int firstPortionLength = 10000;
        byte[] firstDataPortion = generate(firstPortionLength);
        File file = newCacheFile();
        Cache fileCache = new FileCache(file);

        fileCache.append(firstDataPortion, firstDataPortion.length);
        byte[] readData = new byte[firstPortionLength];
        fileCache.read(readData, 0, firstPortionLength);
        assertThat(readData).isEqualTo(firstDataPortion);
        byte[] fileContent = getFileContent(getTempFile(file));
        assertThat(readData).isEqualTo(fileContent);
    }

    @Test
    public void testFileCacheCompletion() throws Exception {
        File file = newCacheFile();
        File tempFile = getTempFile(file);
        Cache fileCache = new FileCache(file);
        assertThat(file.exists()).isFalse();
        assertThat(tempFile.exists()).isTrue();

        int dataSize = 345;
        fileCache.append(generate(dataSize), dataSize);
        fileCache.complete();

        assertThat(file.exists()).isTrue();
        assertThat(tempFile.exists()).isFalse();
        assertThat(file.length()).isEqualTo(dataSize);
    }

// Path: test/src/test/java/com/danikula/videocache/BaseTest.java
// @RunWith(RobolectricTestRunner.class)
// @Config(constants = BuildConfig.class)
// public abstract class BaseTest {
// 
//     static {
//         System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "trace");
//     }
// 
// }
// 
// Path: library/src/main/java/com/danikula/videocache/Cache.java
// public interface Cache {
// 
//     long available() throws ProxyCacheException;
// 
//     int read(byte[] buffer, long offset, int length) throws ProxyCacheException;
// 
//     void append(byte[] data, int length) throws ProxyCacheException;
// 
//     void close() throws ProxyCacheException;
// 
//     void complete() throws ProxyCacheException;
// 
//     boolean isCompleted();
// }
// 
// Path: library/src/main/java/com/danikula/videocache/ProxyCacheException.java
// public class ProxyCacheException extends Exception {
// 
//     private static final String LIBRARY_VERSION = ". Version: " + BuildConfig.VERSION_NAME;
// 
//     public ProxyCacheException(String message) {
//         super(message + LIBRARY_VERSION);
//     }
// 
//     public ProxyCacheException(String message, Throwable cause) {
//         super(message + LIBRARY_VERSION, cause);
//     }
// 
//     public ProxyCacheException(Throwable cause) {
//         super("No explanation error" + LIBRARY_VERSION, cause);
//     }
// }
// 
// Path: test/src/test/java/com/danikula/videocache/support/ProxyCacheTestUtils.java
// public static final String ASSETS_DATA_NAME = "android.jpg";
// 
// Path: test/src/test/java/com/danikula/videocache/support/ProxyCacheTestUtils.java
// public static byte[] generate(int capacity) {
//     Random random = new Random(System.currentTimeMillis());
//     byte[] result = new byte[capacity];
//     random.nextBytes(result);
//     return result;
// }
// 
// Path: test/src/test/java/com/danikula/videocache/support/ProxyCacheTestUtils.java
// public static byte[] getFileContent(File file) throws IOException {
//     return Files.asByteSource(file).read();
// }
// 
// Path: test/src/test/java/com/danikula/videocache/support/ProxyCacheTestUtils.java
// public static File getTempFile(File file) {
//     return new File(file.getParentFile(), file.getName() + ".download");
// }
// 
// Path: test/src/test/java/com/danikula/videocache/support/ProxyCacheTestUtils.java
// public static byte[] loadAssetFile(String name) throws IOException {
//     InputStream in = RuntimeEnvironment.application.getResources().getAssets().open(name);
//     ByteArrayOutputStream out = new ByteArrayOutputStream();
//     IoUtils.copy(in, out);
//     IoUtils.closeSilently(in);
//     IoUtils.closeSilently(out);
//     return out.toByteArray();
// }
// 
// Path: test/src/test/java/com/danikula/videocache/support/ProxyCacheTestUtils.java
// public static File newCacheFile() {
//     return new File(RuntimeEnvironment.application.getCacheDir(), UUID.randomUUID().toString());
// }


// Path: test/src/test/java/com/danikula/videocache/file/FileCacheTest.java
import com.danikula.android.garden.io.Files;
import com.danikula.videocache.BaseTest;
import com.danikula.videocache.Cache;
import com.danikula.videocache.ProxyCacheException;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import static com.danikula.videocache.support.ProxyCacheTestUtils.ASSETS_DATA_NAME;
import static com.danikula.videocache.support.ProxyCacheTestUtils.generate;
import static com.danikula.videocache.support.ProxyCacheTestUtils.getFileContent;
import static com.danikula.videocache.support.ProxyCacheTestUtils.getTempFile;
import static com.danikula.videocache.support.ProxyCacheTestUtils.loadAssetFile;
import static com.danikula.videocache.support.ProxyCacheTestUtils.newCacheFile;
import static com.google.common.io.Files.write;
import static org.fest.assertions.api.Assertions.assertThat;

package com.danikula.videocache.file;





/**
 * @author Alexey Danilov (danikula@gmail.com).
 */
public class FileCacheTest extends BaseTest {

    @Test
    public void testWriteReadDiscCache() throws Exception {
        int firstPortionLength = 10000;
        byte[] firstDataPortion = generate(firstPortionLength);
        File file = newCacheFile();
        Cache fileCache = new FileCache(file);

        fileCache.append(firstDataPortion, firstDataPortion.length);
        byte[] readData = new byte[firstPortionLength];
        fileCache.read(readData, 0, firstPortionLength);
        assertThat(readData).isEqualTo(firstDataPortion);
        byte[] fileContent = getFileContent(getTempFile(file));
        assertThat(readData).isEqualTo(fileContent);
    }

    @Test
    public void testFileCacheCompletion() throws Exception {
        File file = newCacheFile();
        File tempFile = getTempFile(file);
        Cache fileCache = new FileCache(file);
        assertThat(file.exists()).isFalse();
        assertThat(tempFile.exists()).isTrue();

        int dataSize = 345;
        fileCache.append(generate(dataSize), dataSize);
        fileCache.complete();

        assertThat(file.exists()).isTrue();
        assertThat(tempFile.exists()).isFalse();
        assertThat(file.length()).isEqualTo(dataSize);
    }

    @Test(expected = ProxyCacheException.class)