io.nuclei.splice.ApplicationTest.java Source code

Java tutorial

Introduction

Here is the source code for io.nuclei.splice.ApplicationTest.java

Source

/**
 * Copyright 2015 Kyle J Brock
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.nuclei.splice;

import android.app.Application;
import android.support.v4.util.AtomicFile;
import android.test.ApplicationTestCase;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.util.HashSet;
import java.util.Set;

import io.nuclei.splice.internal.DbManager;
import io.nuclei.splice.internal.FileManager;
import io.nuclei.splice.internal.FileRef;

public class ApplicationTest extends ApplicationTestCase<Application> {

    static final String CONTENT = "TEST";

    Set<String> cleanup = new HashSet<>();

    public ApplicationTest() {
        super(Application.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        DbManager.initialize(getContext());
    }

    String writeFile() throws Exception {
        String id = Splice.with(getContext()).store(new ByteArrayInputStream(CONTENT.getBytes("UTF-8")));
        cleanup.add(id);
        SpliceFile file = Splice.with(getContext(), id).get();
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            file.copyTo(out);
            assertTrue(CONTENT.equals(out.toString("UTF-8")));
        } finally {
            file.close();
        }
        validateFile(id, FileManager.TYPE_LOCAL_FILE);
        return id;
    }

    public void testWriteFile() throws Exception {
        writeFile();
    }

    public void testWriteFileCache() throws Exception {
        String id = Splice.with(getContext()).asCache().store(new ByteArrayInputStream(CONTENT.getBytes("UTF-8")));
        cleanup.add(id);
        SpliceFile file = Splice.with(getContext(), id).get();
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            file.copyTo(out);
            assertTrue(CONTENT.equals(out.toString("UTF-8")));
        } finally {
            file.close();
        }
        validateFile(id, FileManager.TYPE_CACHE_FILE);
    }

    public void testWriteFileExternal() throws Exception {
        String id = Splice.with(getContext()).asExternal()
                .store(new ByteArrayInputStream(CONTENT.getBytes("UTF-8")));
        cleanup.add(id);
        SpliceFile file = Splice.with(getContext(), id).get();
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            file.copyTo(out);
            assertTrue(CONTENT.equals(out.toString("UTF-8")));
        } finally {
            file.close();
        }
        validateFile(id, FileManager.TYPE_EXTERNAL_FILE);
    }

    public void testDeleteFile() throws Exception {
        String id = writeFile();
        cleanup.add(id);
        Splice.with(getContext(), id).delete();
        FileRef ref = FileManager.newRef(id);
        AtomicFile file = FileManager.getAtomicFile(getContext(), ref);
        assertTrue(!file.getBaseFile().exists());
    }

    public void testExistsLocal() throws Exception {
        String id = Splice.with(getContext()).store(new ByteArrayInputStream(CONTENT.getBytes("UTF-8")));
        cleanup.add(id);
        assertTrue(Splice.with(getContext(), id).existsLocal());
        Splice.with(getContext(), id).delete();
        assertTrue(!Splice.with(getContext(), id).existsLocal());
    }

    void validateFile(String id, int type) throws Exception {
        FileRef ref = FileManager.newRef(id);
        assertTrue(ref.type == type);

        AtomicFile file = FileManager.getAtomicFile(getContext(), ref);
        assertTrue(file.getBaseFile().exists());

        FileInputStream in = file.openRead();
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            FileManager.copy(in, out);
            assertTrue(CONTENT.equals(out.toString("UTF-8")));
        } finally {
            in.close();
        }
    }

    @Override
    protected void tearDown() throws Exception {
        for (String id : cleanup) {
            Splice splice = Splice.with(getContext(), id);
            if (splice.existsLocal())
                splice.delete();
        }
        cleanup.clear();
        super.tearDown();
    }
}