FileDocCategorySizeDatePackage
BackupDataTest.javaAPI DocAndroid 5.1 API11469Thu Mar 12 22:22:12 GMT 2015android.app.backup

BackupDataTest

public class BackupDataTest extends android.test.AndroidTestCase

Fields Summary
private static final String
KEY1
private static final String
KEY2
private static final String
KEY3
private static final String
KEY4
private static final String[]
KEYS
private static final String
DATA1
private static final String
DATA2
private static final String
DATA3
private static final String
DATA4
private static final String[]
DATA
private static final String
TAG
private File
mFile
private android.os.ParcelFileDescriptor
mDataFile
private File
mDirectory
private android.os.Bundle
mStatusBundle
private android.content.res.AssetManager
mAssets
Constructors Summary
Methods Summary
private voidcopyAssetToFile(java.lang.String source, java.lang.String destination)

        mFile = new File(mDirectory, destination);
        openForWriting();
        FileInputStream fileInputStream = mAssets.openFd(source).createInputStream();
        FileOutputStream fileOutputStream = new FileOutputStream(mDataFile.getFileDescriptor());
        byte[] copybuffer = new byte[1024];
        int numBytes = fileInputStream.read(copybuffer);
        fileOutputStream.write(copybuffer, 0, numBytes);
        fileOutputStream.close();
    
private voiddeleteEntity(android.app.backup.BackupDataOutput bdo, java.lang.String key)

        int status = bdo.writeEntityHeader(key, -1);
        // documentation says "number of bytes written" but that's not what we get:
        assertEquals(0, status);
    
private voidopenForReading()

        mDataFile = ParcelFileDescriptor.open(mFile,
                ParcelFileDescriptor.MODE_READ_ONLY |
                        ParcelFileDescriptor.MODE_CREATE);  // Make an empty file if necessary
    
private voidopenForWriting()

        mDataFile = ParcelFileDescriptor.open(mFile,
                ParcelFileDescriptor.MODE_WRITE_ONLY |
                        ParcelFileDescriptor.MODE_CREATE |
                        ParcelFileDescriptor.MODE_TRUNCATE);  // Make an empty file if necessary
    
private voidreadAndVerifyDeletedEntity(android.app.backup.BackupDataInput bdi, java.lang.String expectedKey)

        assertEquals("Key mismatch",
                expectedKey, bdi.getKey());
        assertEquals("deletion mis-reported",
                -1, bdi.getDataSize());
    
private voidreadAndVerifyEntity(android.app.backup.BackupDataInput bdi, java.lang.String expectedKey, byte[] expectedData)

        assertEquals("Key mismatch",
                expectedKey, bdi.getKey());
        assertEquals("data size mismatch",
                expectedData.length, bdi.getDataSize());
        byte[] data = new byte[bdi.getDataSize()];
        bdi.readEntityData(data, 0, bdi.getDataSize());
        assertEquals("payload size is wrong",
                expectedData.length, data.length);
        for (int i = 0; i < data.length; i++) {
            assertEquals("payload mismatch",
                    expectedData[i], data[i]);
        }
    
protected voidsetUp()


    
         
        super.setUp();
        mDirectory = new File(Environment.getExternalStorageDirectory(), "test_data");
        mDirectory.mkdirs();
        mAssets = mContext.getAssets();
    
protected voidtearDown()

        super.tearDown();
        if (mDataFile != null) {
            mDataFile.close();
        }
    
public voidtestDelete()

        mFile = new File(mDirectory, "backup_delete_test.dat");
        openForWriting();
        BackupDataOutput bdo = new BackupDataOutput(mDataFile.getFileDescriptor());

        for(int i = 0; i < KEYS.length; i++) {
            deleteEntity(bdo, KEYS[i]);
        }

        mDataFile.close();
        openForReading();

        BackupDataInput bdi = new BackupDataInput(mDataFile.getFileDescriptor());
        int count = 0;
        while (bdi.readNextHeader()) {
            readAndVerifyDeletedEntity(bdi, KEYS[count]);
            count++;
        }
        assertEquals("four deletes in this stream", KEYS.length, count);
    
public voidtestMixed()

        mFile = new File(mDirectory, "backup_mixed_test.dat");
        openForWriting();

        BackupDataOutput bdo = new BackupDataOutput(mDataFile.getFileDescriptor());

        int i = 0;
        deleteEntity(bdo, KEYS[i]); i++;
        writeEntity(bdo, KEYS[i], DATA[i].getBytes()); i++;
        writeEntity(bdo, KEYS[i], DATA[i].getBytes()); i++;
        deleteEntity(bdo, KEYS[i]); i++;

        mDataFile.close();
        openForReading();

        BackupDataInput bdi = new BackupDataInput(mDataFile.getFileDescriptor());
        int out = 0;
        assertTrue(bdi.readNextHeader());
        readAndVerifyDeletedEntity(bdi, KEYS[out]); out++;
        assertTrue(bdi.readNextHeader());
        readAndVerifyEntity(bdi, KEYS[out], DATA[out].getBytes()); out++;
        assertTrue(bdi.readNextHeader());
        readAndVerifyEntity(bdi, KEYS[out], DATA[out].getBytes()); out++;
        assertTrue(bdi.readNextHeader());
        readAndVerifyDeletedEntity(bdi, KEYS[out]); out++;
        assertFalse("four items in this stream",
                bdi.readNextHeader());
    
public voidtestMultiple()

        mFile = new File(mDirectory, "backup_multiple_test.dat");
        openForWriting();
        BackupDataOutput bdo = new BackupDataOutput(mDataFile.getFileDescriptor());

        for(int i = 0; i < KEYS.length; i++) {
            writeEntity(bdo, KEYS[i], DATA[i].getBytes());
        }

        mDataFile.close();
        openForReading();

        BackupDataInput bdi = new BackupDataInput(mDataFile.getFileDescriptor());
        int count = 0;
        while (bdi.readNextHeader()) {
            readAndVerifyEntity(bdi, KEYS[count], DATA[count].getBytes());
            count++;
        }
        assertEquals("four entities in this stream", KEYS.length, count);
    
public voidtestReadMockData()

        copyAssetToFile("backup_mock.dat", "backup_read_mock_test.dat");

        openForReading();
        BackupDataInput bdi = new BackupDataInput(mDataFile.getFileDescriptor());
        BufferedReader truth = new BufferedReader(new InputStreamReader(
                mAssets.openFd("backup_mock.gld").createInputStream()));
        while( bdi.readNextHeader()) {
            String[] expected = truth.readLine().split(":");
            byte[] expectedBytes = null;
            if (expected.length > 1) {
                expectedBytes = Base64.decode(expected[1], Base64.DEFAULT);
            }
            String key = bdi.getKey();
            int dataSize = bdi.getDataSize();

            assertEquals("wrong key", expected[0], key);
            assertEquals("wrong length for key " + key,
                    (expectedBytes == null ? -1: expectedBytes.length), dataSize);
            if (dataSize != -1) {
                byte[] buffer = new byte[dataSize];
                bdi.readEntityData(buffer, 0, dataSize);
                assertEquals("wrong data for key " + key, expected[1],
                        Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP));
            }
        }
        assertNull("there are unused entries in the golden file", truth.readLine());
    
public voidtestReadRealData()

        copyAssetToFile("backup_real.dat", "backup_read_real_test.dat");

        openForReading();
        BackupDataInput bdi = new BackupDataInput(mDataFile.getFileDescriptor());
        BufferedReader truth = new BufferedReader(new InputStreamReader(
                mAssets.openFd("backup_real.gld").createInputStream()));

        while(bdi.readNextHeader()) {
            String[] expected = truth.readLine().split(":");
            byte[] expectedBytes = null;
            if (expected.length > 1) {
                expectedBytes = Base64.decode(expected[1], Base64.DEFAULT);
            }
            String key = bdi.getKey();
            int dataSize = bdi.getDataSize();

            assertEquals("wrong key", expected[0], key);
            assertEquals("wrong length for key " + key,
                    (expectedBytes == null ? -1: expectedBytes.length), dataSize);
            if (dataSize != -1) {
                byte[] buffer = new byte[dataSize];
                bdi.readEntityData(buffer, 0, dataSize);
                assertEquals("wrong data for key " + key, expected[1],
                        Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP));
            }
        }
        assertNull("there are unused entries in the golden file", truth.readLine());
    
public voidtestSingle()

        mFile = new File(mDirectory, "backup_mixed_sinlge.dat");
        openForWriting();
        BackupDataOutput bdo = new BackupDataOutput(mDataFile.getFileDescriptor());

        writeEntity(bdo, KEY1, DATA1.getBytes());

        mDataFile.close();
        openForReading();

        BackupDataInput bdi = new BackupDataInput(mDataFile.getFileDescriptor());
        int count = 0;
        while (bdi.readNextHeader()) {
            readAndVerifyEntity(bdi, KEY1, DATA1.getBytes());
            count++;
        }
        assertEquals("only one entity in this stream", 1, count);
    
private voidwriteEntity(android.app.backup.BackupDataOutput bdo, java.lang.String key, byte[] data)

        int status = bdo.writeEntityHeader(key, data.length);
        // documentation says "number of bytes written" but that's not what we get:
        assertEquals(0, status);

        status = bdo.writeEntityData(data, data.length);
        // documentation says "number of bytes written" but that's not what we get:
        assertEquals(0, status);