Android Open Source - android-memento Abstract Sqlite Content Provider Test






From Project

Back to project page android-memento.

License

The source code is released under:

Apache License

If you think the Android project android-memento listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * android-memento-lib https://github.com/twofortyfouram/android-memento
 * Copyright 2014 two forty four a.m. LLC
 */*w w  w .  j  av  a  2s  .  c o m*/
 * 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 com.twofortyfouram.memento.provider.sqlite;

import com.twofortyfouram.memento.debug.provider.SqliteContentProviderImpl;
import com.twofortyfouram.memento.debug.provider.TableOneContract;
import com.twofortyfouram.memento.provider.ContentProviderUtil;

import android.app.SearchManager;
import android.content.ContentProviderClient;
import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.os.RemoteException;
import android.test.MoreAsserts;
import android.test.ProviderTestCase2;
import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * Tests the {@link SqliteContentProviderImpl} in isolation as a proxy for testing
 * {@link AbstractSqliteContentProvider}.
 */
public final class AbstractSqliteContentProviderTest extends
        ProviderTestCase2<SqliteContentProviderImpl> {

    /**
     * Implements required constructor to initialize the test
     */
    public AbstractSqliteContentProviderTest() {
        super(SqliteContentProviderImpl.class,
                "com.twofortyfouram.memento.debug.provider"); //$NON-NLS-1$
    }

    @SmallTest
    public static void testIsSlowDiskAccess() {
        assertFalse(AbstractSqliteContentProvider.IS_SLOW_ACCESS_ENABLED);
    }

    @SmallTest
    public static void testNewAndIdSelection_non_null_selection() {
        final String newSelection = AbstractSqliteContentProvider.newAndIdSelection("foo = bar");

        assertNotNull(newSelection);
        assertEquals("_id = ? AND (foo = bar)", newSelection);
    }

    @SmallTest
    public static void testNewAndIdSelection_null_selection() {
        final String newSelection = AbstractSqliteContentProvider.newAndIdSelection(null);

        assertNotNull(newSelection);
        assertEquals("_id = ?", newSelection);
    }

    @SmallTest
    public static void testNewAndIdSelectionArgs_null_selection() {
        final String[] newSelectionArgs = AbstractSqliteContentProvider.newAndIdSelectionArgs("1",
                null);

        assertNotNull(newSelectionArgs);
        MoreAsserts.assertEquals(new String[]{"1"}, newSelectionArgs);
    }

    @SmallTest
    public static void testNewAndIdSelectionArgs_empty_selection() {
        final String[] newSelectionArgs = AbstractSqliteContentProvider.newAndIdSelectionArgs("1",
                new String[0]);

        assertNotNull(newSelectionArgs);
        MoreAsserts.assertEquals(new String[]{"1"}, newSelectionArgs);
    }

    @SmallTest
    public static void testNewAndIdSelectionArgs_normal_selection() {
        final String[] newSelectionArgs = AbstractSqliteContentProvider.newAndIdSelectionArgs("1",
                new String[]{"foo"});

        assertNotNull(newSelectionArgs);
        MoreAsserts.assertEquals(new String[]{"1", "foo"}, newSelectionArgs);
    }

    /*
     * This is glass-box test intended to verify the provider's lazy initialization with a query as
     * the first call.
     */
    @SmallTest
    public void testQuery_init() {
        final ContentResolver resolver = getMockContentResolver();

        Cursor cursor = null;
        try {
            cursor = resolver.query(TableOneContract.getContentUri(getContext()), null, null, null,
                    null);

            assertEquals(0, cursor.getCount());
            assertEquals(2, cursor.getColumnCount());

            final List<String> expectedColumns = new ArrayList<String>(2);
            expectedColumns.add(TableOneContract._ID);
            expectedColumns.add(TableOneContract.COLUMN_STRING_COLUMN_ONE);

            final String[] actualColumns = cursor.getColumnNames();

            MoreAsserts.assertContentsInAnyOrder(
                    expectedColumns,
                    (Object[]) actualColumns);
        } finally {
            if (null != cursor) {
                cursor.close();
                cursor = null;
            }
        }
    }

    @SmallTest
    public void testQuery_projection_single() {
        final ContentResolver resolver = getMockContentResolver();

        Cursor cursor = null;
        try {
            cursor = resolver.query(TableOneContract.getContentUri(getContext()), new String[]{
                    TableOneContract._ID
            }, null, null, null);

            assertEquals(0, cursor.getCount());
            assertEquals(1, cursor.getColumnCount());

            assertEquals(0, cursor.getColumnIndex(TableOneContract._ID));
        } finally {
            if (null != cursor) {
                cursor.close();
                cursor = null;
            }
        }
    }

    @SmallTest
    public void testQuery_sort_order() {
        final ContentResolver resolver = getMockContentResolver();

        resolver.insert(TableOneContract.getContentUri(getContext()),
                TableOneContract.getContentValues("c")); //$NON-NLS-1$
        resolver.insert(TableOneContract.getContentUri(getContext()),
                TableOneContract.getContentValues("a")); //$NON-NLS-1$
        resolver.insert(TableOneContract.getContentUri(getContext()),
                TableOneContract.getContentValues("b")); //$NON-NLS-1$
        resolver.insert(TableOneContract.getContentUri(getContext()),
                TableOneContract.getContentValues("d")); //$NON-NLS-1$

        Cursor cursor = null;

        try {
            final String sortBy = String.format(
                    "%s COLLATE LOCALIZED ASC",
                    TableOneContract.COLUMN_STRING_COLUMN_ONE); //$NON-NLS-1$

            cursor = resolver.query(TableOneContract.getContentUri(getContext()), null, null, null,
                    sortBy);

            assertEquals(4, cursor.getCount());
            assertEquals(2, cursor.getColumnCount());

            final ArrayList<String> results = new ArrayList<String>(4);

            cursor.moveToPosition(-1);
            while (cursor.moveToNext()) {
                results.add(cursor.getString(cursor
                        .getColumnIndexOrThrow(TableOneContract.COLUMN_STRING_COLUMN_ONE)));
            }

            MoreAsserts.assertContentsInOrder(results, "a", "b", "c",
                    "d"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
        } finally {
            if (null != cursor) {
                cursor.close();
                cursor = null;
            }
        }
    }

    @SmallTest
    public void testQuery_selection_with_args() {
        final ContentResolver resolver = getMockContentResolver();

        resolver.insert(TableOneContract.getContentUri(getContext()),
                TableOneContract.getContentValues("c")); //$NON-NLS-1$
        resolver.insert(TableOneContract.getContentUri(getContext()),
                TableOneContract.getContentValues("a")); //$NON-NLS-1$
        resolver.insert(TableOneContract.getContentUri(getContext()),
                TableOneContract.getContentValues("b")); //$NON-NLS-1$
        resolver.insert(TableOneContract.getContentUri(getContext()),
                TableOneContract.getContentValues("d")); //$NON-NLS-1$

        Cursor cursor = null;

        try {
            final String selection = String.format(Locale.US,
                    "%s != ?", TableOneContract.COLUMN_STRING_COLUMN_ONE); //$NON-NLS-1$
            final String[] selectionArgs = {
                    "a"}; //$NON-NLS-1$
            cursor = resolver.query(TableOneContract.getContentUri(getContext()), null, selection,
                    selectionArgs, null);

            assertEquals(3, cursor.getCount());

            cursor.moveToPosition(-1);
            while (cursor.moveToNext()) {
                MoreAsserts.assertNotEqual("a", //$NON-NLS-1$
                        cursor.getString(cursor
                                .getColumnIndexOrThrow(TableOneContract.COLUMN_STRING_COLUMN_ONE))
                );
            }
        } finally {
            if (null != cursor) {
                cursor.close();
                cursor = null;
            }
        }
    }

    @SmallTest
    public void testQuery_id_in_path() {
        final ContentResolver resolver = getMockContentResolver();

        final Uri uri = resolver.insert(TableOneContract.getContentUri(getContext()),
                TableOneContract.getContentValues("test_value_one")); //$NON-NLS-1$
        resolver.insert(TableOneContract.getContentUri(getContext()),
                TableOneContract.getContentValues("test_value_two")); //$NON-NLS-1$

        Cursor cursor = null;

        try {
            cursor = resolver.query(uri, null, null, null, null);

            assertEquals(1, cursor.getCount());

        } finally {
            if (null != cursor) {
                cursor.close();
                cursor = null;
            }
        }
    }

    @SmallTest
    public void testQuery_limit() {
        final ContentResolver resolver = getMockContentResolver();

        final ContentValues values = TableOneContract.getContentValues("test_value"); //$NON-NLS-1$
        resolver.insert(TableOneContract.getContentUri(getContext()), values);
        resolver.insert(TableOneContract.getContentUri(getContext()), values);
        resolver.insert(TableOneContract.getContentUri(getContext()), values);

        Cursor cursor = null;
        try {
            // no limit returns 3 items
            cursor = resolver.query(TableOneContract.getContentUri(getContext()), null, null, null,
                    null);
            assertEquals(3, cursor.getCount());
        } finally {
            if (null != cursor) {
                cursor.close();
                cursor = null;
            }
        }

        try {
            // limit to 1 item
            final Uri uri = TableOneContract
                    .getContentUri(getContext())
                    .buildUpon()
                    .appendQueryParameter(SearchManager.SUGGEST_PARAMETER_LIMIT,
                            Integer.toString(1)).build();

            cursor = resolver.query(uri, null, null, null, null);
            assertEquals(1, cursor.getCount());
        } finally {
            if (null != cursor) {
                cursor.close();
                cursor = null;
            }
        }
    }

    @SmallTest
    public void testQuery_count_empty() {
        final ContentResolver resolver = getMockContentResolver();

        Cursor cursor = null;
        try {
            cursor = resolver.query(TableOneContract.getContentUri(getContext()), new String[]{
                    TableOneContract._COUNT
            }, null, null, null);

            assertEquals(1, cursor.getCount());
            assertTrue(cursor.moveToFirst());
            assertEquals(0, cursor.getInt(cursor.getColumnIndexOrThrow(TableOneContract._COUNT)));
        } finally {
            if (null != cursor) {
                cursor.close();
                cursor = null;
            }
        }
    }

    @SmallTest
    public void testQuery_count_non_empty() {
        final ContentResolver resolver = getMockContentResolver();

        resolver.insert(TableOneContract.getContentUri(getContext()),
                TableOneContract.getContentValues("test_value")); //$NON-NLS-1$

        Cursor cursor = null;
        try {
            cursor = resolver.query(TableOneContract.getContentUri(getContext()), new String[]{
                    TableOneContract._COUNT
            }, null, null, null);

            assertEquals(1, cursor.getCount());
            assertTrue(cursor.moveToFirst());
            assertEquals(1, cursor.getInt(cursor.getColumnIndexOrThrow(TableOneContract._COUNT)));
        } finally {
            if (null != cursor) {
                cursor.close();
                cursor = null;
            }
        }
    }

    /*
     * This is glass-box test intended to verify the provider's lazy initialization with an insert
     * as the first call.
     */
    @SmallTest
    public void testInsert_init() {
        final ContentResolver resolver = getMockContentResolver();

        final ContentValues values = TableOneContract.getContentValues("test_value"); //$NON-NLS-1$
        final Uri resultUri = resolver.insert(TableOneContract.getContentUri(getContext()), values);
        assertNotNull(resultUri);
    }

    @SmallTest
    public void testInsert_id_in_uri() {
        final ContentResolver resolver = getMockContentResolver();

        final ContentValues values = TableOneContract.getContentValues("test_value"); //$NON-NLS-1$
        final Uri uri = ContentUris.withAppendedId(TableOneContract.getContentUri(getContext()), 1234);
        final Uri resultUri = resolver.insert(uri, values);

        assertNotNull(resultUri);
        assertEquals("1234", resultUri.getLastPathSegment());

        // Ensure values wasn't mutated by the insert method
        assertEquals(TableOneContract.getContentValues("test_value"), values); //$NON-NLS-1$
    }

    /*
     * This is glass-box test intended to verify the provider's lazy initialization with an update
     * as the first call.
     */
    @SmallTest
    public void testUpdate_init() {
        final ContentResolver resolver = getMockContentResolver();

        final ContentValues values = TableOneContract.getContentValues("test_value"); //$NON-NLS-1$
        assertEquals(0,
                resolver.update(TableOneContract.getContentUri(getContext()), values, null, null));
    }

    @SmallTest
    public void testUpdate_no_selection_success() {
        final ContentResolver resolver = getMockContentResolver();

        final ContentValues initialValues = TableOneContract
                .getContentValues("test_value"); //$NON-NLS-1$
        final Uri uri = resolver
                .insert(TableOneContract.getContentUri(getContext()), initialValues);

        final ContentValues updatedValues = TableOneContract
                .getContentValues("test_value_updated"); //$NON-NLS-1$
        assertEquals(1, resolver.update(TableOneContract.getContentUri(getContext()),
                updatedValues, null, null));

        Cursor cursor = null;
        try {
            cursor = resolver.query(uri, null, null, null, null);

            assertEquals(1, cursor.getCount());

            cursor.moveToFirst();

            assertEquals(updatedValues.getAsString(TableOneContract.COLUMN_STRING_COLUMN_ONE),
                    cursor.getString(cursor
                            .getColumnIndexOrThrow(TableOneContract.COLUMN_STRING_COLUMN_ONE))
            );
        } finally {
            if (null != cursor) {
                cursor.close();
                cursor = null;
            }
        }
    }

    @SmallTest
    public void testUpdate_with_selection_success() {
        final ContentResolver resolver = getMockContentResolver();

        final ContentValues initialValuesOne = TableOneContract
                .getContentValues("test_value_one"); //$NON-NLS-1$
        final Uri uriOne = resolver.insert(TableOneContract.getContentUri(getContext()),
                initialValuesOne);

        final ContentValues initialValuesTwo = TableOneContract
                .getContentValues("test_value_two"); //$NON-NLS-1$
        final Uri uriTwo = resolver.insert(TableOneContract.getContentUri(getContext()),
                initialValuesTwo);

        final ContentValues updatedValues = TableOneContract
                .getContentValues("test_value_updated"); //$NON-NLS-1$
        final String selection = String.format(Locale.US,
                "%s = ?", TableOneContract.COLUMN_STRING_COLUMN_ONE); //$NON-NLS-1$
        final String[] selectionArgs = {
                "test_value_one"}; //$NON-NLS-1$
        assertEquals(1, resolver.update(TableOneContract.getContentUri(getContext()),
                updatedValues, selection, selectionArgs));

        Cursor cursor = null;
        try {
            cursor = resolver.query(uriOne, null, null, null, null);

            assertEquals(1, cursor.getCount());

            cursor.moveToFirst();

            assertEquals(updatedValues.getAsString(TableOneContract.COLUMN_STRING_COLUMN_ONE),
                    cursor.getString(cursor
                            .getColumnIndexOrThrow(TableOneContract.COLUMN_STRING_COLUMN_ONE))
            );
        } finally {
            if (null != cursor) {
                cursor.close();
                cursor = null;
            }
        }

        try {
            cursor = resolver.query(uriTwo, null, null, null, null);

            assertEquals(1, cursor.getCount());

            cursor.moveToFirst();

            assertEquals(initialValuesTwo.getAsString(TableOneContract.COLUMN_STRING_COLUMN_ONE),
                    cursor.getString(cursor
                            .getColumnIndexOrThrow(TableOneContract.COLUMN_STRING_COLUMN_ONE))
            );
        } finally {
            if (null != cursor) {
                cursor.close();
                cursor = null;
            }
        }
    }

    @SmallTest
    public void testUpdate_with_selection_no_success() {
        final ContentResolver resolver = getMockContentResolver();

        final ContentValues initialValuesOne = TableOneContract
                .getContentValues("test_value_one"); //$NON-NLS-1$
        final Uri uriOne = resolver.insert(TableOneContract.getContentUri(getContext()),
                initialValuesOne);

        final ContentValues updatedValues = TableOneContract
                .getContentValues("test_value_updated"); //$NON-NLS-1$
        final String selection = String.format(Locale.US,
                "%s = ?", TableOneContract.COLUMN_STRING_COLUMN_ONE); //$NON-NLS-1$
        final String[] selectionArgs = {
                "bork_bork_bork"}; //$NON-NLS-1$
        assertEquals(0, resolver.update(TableOneContract.getContentUri(getContext()),
                updatedValues, selection, selectionArgs));

        Cursor cursor = null;
        try {
            cursor = resolver.query(uriOne, null, null, null, null);

            assertEquals(1, cursor.getCount());

            cursor.moveToFirst();

            assertEquals(initialValuesOne.getAsString(TableOneContract.COLUMN_STRING_COLUMN_ONE),
                    cursor.getString(cursor
                            .getColumnIndexOrThrow(TableOneContract.COLUMN_STRING_COLUMN_ONE))
            );
        } finally {
            if (null != cursor) {
                cursor.close();
                cursor = null;
            }
        }
    }

    @SmallTest
    public void testUpdate_with_id_success() {
        final ContentResolver resolver = getMockContentResolver();

        final ContentValues initialValuesOne = TableOneContract
                .getContentValues("test_value_one"); //$NON-NLS-1$
        final Uri uriOne = resolver.insert(TableOneContract.getContentUri(getContext()),
                initialValuesOne);

        final ContentValues initialValuesTwo = TableOneContract
                .getContentValues("test_value_two"); //$NON-NLS-1$
        final Uri uriTwo = resolver.insert(TableOneContract.getContentUri(getContext()),
                initialValuesTwo);

        final ContentValues updatedValues = TableOneContract
                .getContentValues("test_value_updated"); //$NON-NLS-1$
        assertEquals(1, resolver.update(uriOne, updatedValues, null, null));

        Cursor cursor = null;
        try {
            cursor = resolver.query(uriOne, null, null, null, null);

            assertEquals(1, cursor.getCount());

            cursor.moveToFirst();

            assertEquals(updatedValues.getAsString(TableOneContract.COLUMN_STRING_COLUMN_ONE),
                    cursor.getString(cursor
                            .getColumnIndexOrThrow(TableOneContract.COLUMN_STRING_COLUMN_ONE))
            );
        } finally {
            if (null != cursor) {
                cursor.close();
                cursor = null;
            }
        }

        try {
            // Note: This also tests that the ID of the record is unchanged
            // during the update
            cursor = resolver.query(uriTwo, null, null, null, null);

            assertEquals(1, cursor.getCount());

            cursor.moveToFirst();

            assertEquals(initialValuesTwo.getAsString(TableOneContract.COLUMN_STRING_COLUMN_ONE),
                    cursor.getString(cursor
                            .getColumnIndexOrThrow(TableOneContract.COLUMN_STRING_COLUMN_ONE))
            );
        } finally {
            if (null != cursor) {
                cursor.close();
                cursor = null;
            }
        }
    }

    /*
     * This is glass-box test intended to verify the provider's lazy initialization with a delete as
     * the first call.
     */
    @SmallTest
    public void testDelete_init() {
        final ContentResolver resolver = getMockContentResolver();

        assertEquals(0, resolver.delete(TableOneContract.getContentUri(getContext()), null, null));
    }

    @SmallTest
    public void testDelete_no_selection_success() {
        final ContentResolver resolver = getMockContentResolver();

        resolver.insert(TableOneContract.getContentUri(getContext()),
                TableOneContract.getContentValues("test_value")); //$NON-NLS-1$

        assertEquals(1, resolver.delete(TableOneContract.getContentUri(getContext()), null, null));

        assertCount(0);
    }

    @SmallTest
    public void testDelete_with_id_in_path() {
        final ContentResolver resolver = getMockContentResolver();

        final ContentValues values = TableOneContract.getContentValues("test_value"); //$NON-NLS-1$
        final Uri uri = resolver.insert(TableOneContract.getContentUri(getContext()), values);
        resolver.insert(TableOneContract.getContentUri(getContext()), values);
        resolver.insert(TableOneContract.getContentUri(getContext()), values);

        assertEquals(1, resolver.delete(uri, null, null));

        assertCount(2);
    }

    @SmallTest
    public void testDelete_with_selection() {
        final ContentResolver resolver = getMockContentResolver();

        resolver.insert(TableOneContract.getContentUri(getContext()),
                TableOneContract.getContentValues("test_value_one")); //$NON-NLS-1$
        resolver.insert(TableOneContract.getContentUri(getContext()),
                TableOneContract.getContentValues("test_value_two")); //$NON-NLS-1$
        resolver.insert(TableOneContract.getContentUri(getContext()),
                TableOneContract.getContentValues("test_value_three")); //$NON-NLS-1$

        final String selection = String.format(Locale.US,
                "%s = ?", TableOneContract.COLUMN_STRING_COLUMN_ONE); //$NON-NLS-1$
        final String[] selectionArgs = {
                "test_value_two"}; //$NON-NLS-1$
        assertEquals(1, resolver.delete(TableOneContract.getContentUri(getContext()), selection,
                selectionArgs));

        assertCount(2);
    }

    /*
     * This is glass-box test intended to verify the provider's lazy initialization with applyBatch
     * as the first call.
     */
    @SmallTest
    public void testApplyBatch_init() throws Exception {
        final ContentResolver resolver = getMockContentResolver();

        final ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
        ops.add(ContentProviderOperation
                .newAssertQuery(TableOneContract.getContentUri(getContext())).withExpectedCount(0)
                .build());

        resolver.applyBatch(SqliteContentProviderImpl.getContentAuthority(getContext()), ops);
    }

    @MediumTest
    public void testApplyBatch_abort() throws RemoteException {
        final ContentResolver resolver = getMockContentResolver();

        /*
         * When an operation is aborted, none of the operations should be performed.
         *
         * In addition, no content notification should occur.
         */

        final ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
        ops.add(ContentProviderOperation.newInsert(TableOneContract.getContentUri(getContext()))
                .withValues(TableOneContract.getContentValues("table_name")).build()); //$NON-NLS-1$

        // This query will fail, aborting the entire transaction
        ops.add(ContentProviderOperation
                .newAssertQuery(TableOneContract.getContentUri(getContext())).withExpectedCount(5)
                .build());

        try {
            resolver.applyBatch(SqliteContentProviderImpl.getContentAuthority(getContext()), ops);
            fail("Should have thrown an exception"); //$NON-NLS-1$
        } catch (final OperationApplicationException e) {
            // Expected exception
        }

        assertCount(0);
    }

    @SmallTest
    public void testApplyBatch_success() throws RemoteException, OperationApplicationException {
        final ContentResolver resolver = getMockContentResolver();

        final ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
        ops.add(ContentProviderOperation.newInsert(TableOneContract.getContentUri(getContext()))
                .withValues(TableOneContract.getContentValues("table_name")).build()); //$NON-NLS-1$
        ops.add(ContentProviderOperation.newInsert(TableOneContract.getContentUri(getContext()))
                .withValues(TableOneContract.getContentValues("table_name")).build()); //$NON-NLS-1$
        ops.add(ContentProviderOperation.newInsert(TableOneContract.getContentUri(getContext()))
                .withValues(TableOneContract.getContentValues("table_name")).build()); //$NON-NLS-1$
        ops.add(ContentProviderOperation.newUpdate(TableOneContract.getContentUri(getContext()))
                .withValues(TableOneContract.getContentValues("table_name_updated"))
                .build()); //$NON-NLS-1$

        resolver.applyBatch(SqliteContentProviderImpl.getContentAuthority(getContext()), ops);

        Cursor cursor = null;
        try {
            cursor = resolver.query(TableOneContract.getContentUri(getContext()), null, null, null,
                    null);

            assertEquals(3, cursor.getCount());
        } finally {
            if (null != cursor) {
                cursor.close();
                cursor = null;
            }
        }
    }

    /*
     * This is glass-box test intended to verify the provider's lazy initialization with a bulk
     * insert as the first call.
     */
    @SmallTest
    public void testBulkInsert_init() {
        final ContentResolver resolver = getMockContentResolver();

        final ContentValues[] values = new ContentValues[1];
        values[0] = TableOneContract.getContentValues("test_value"); //$NON-NLS-1$

        assertEquals(1, resolver.bulkInsert(TableOneContract.getContentUri(getContext()), values));
    }

    @SmallTest
    public void testBulkInsert_success() {
        final ContentResolver resolver = getMockContentResolver();

        final ContentValues[] contentValues = {
                TableOneContract.getContentValues("test_value_one"), //$NON-NLS-1$
                TableOneContract.getContentValues("test_value_two")}; //$NON-NLS-1$

        assertEquals(2,
                resolver.bulkInsert(TableOneContract.getContentUri(getContext()), contentValues));
        assertCount(2);
    }

    @SmallTest
    public void testBulkInsert_abort() {
        final ContentResolver resolver = getMockContentResolver();

        /*
         * Null violates constraints, so this will throw an exception.
         */
        final ContentValues[] contentValues = {
                TableOneContract.getContentValues("test_value_one"), //$NON-NLS-1$
                null
        };

        try {
            resolver.bulkInsert(TableOneContract.getContentUri(getContext()), contentValues);
            fail();
        } catch (final SQLiteException e) {
            // Expected exception
        }

        assertCount(0);
    }

    @SmallTest
    public void testRunInTransaction_success() {
        final ContentResolver resolver = getMockContentResolver();

        ContentProviderClient client = null;
        try {
            client = resolver.acquireContentProviderClient(SqliteContentProviderImpl
                    .getContentAuthority(getContext()));
            final SqliteContentProviderImpl provider = (SqliteContentProviderImpl) client
                    .getLocalContentProvider();

            provider.runInTransaction(new Runnable() {
                @Override
                public void run() {
                    Cursor cursor = null;
                    try {
                        cursor = resolver.query(TableOneContract.getContentUri(getContext()), null,
                                null, null, null);
                    } finally {
                        if (null != cursor) {
                            cursor.close();
                            cursor = null;
                        }
                    }

                    resolver.delete(TableOneContract.getContentUri(getContext()), null, null);
                }

            });
        } finally {
            if (null != client) {
                client.release();
                client = null;
            }
        }
    }

    @SmallTest
    public void testRunInTransaction_nested() {
        final ContentResolver resolver = getMockContentResolver();

        ContentProviderClient client = null;
        try {
            client = resolver.acquireContentProviderClient(SqliteContentProviderImpl
                    .getContentAuthority(getContext()));
            final SqliteContentProviderImpl provider = (SqliteContentProviderImpl) client
                    .getLocalContentProvider();

            provider.runInTransaction(new Runnable() {
                @Override
                public void run() {
                    final ArrayList<ContentProviderOperation> ops
                            = new ArrayList<ContentProviderOperation>(
                            1);
                    ops.add(ContentProviderOperation.newDelete(
                            TableOneContract.getContentUri(getContext())).build());

                    try {
                        resolver.applyBatch(
                                SqliteContentProviderImpl.getContentAuthority(getContext()), ops);
                    } catch (final OperationApplicationException e) {
                        throw new AssertionError(e);
                    } catch (final RemoteException e) {
                        throw new AssertionError(e);
                    }
                }

            });
        } finally {
            if (null != client) {
                client.release();
                client = null;
            }
        }
    }

    /**
     * Asserts that {@link TableOneContract} has {@code count} rows.
     *
     * @param count Number of rows to assert exist in the table.
     */
    private void assertCount(final int count) {
        ContentProviderUtil.getCountForUri(getContext(),
                TableOneContract.getContentUri(getContext()));
    }
}




Java Source Code List

com.twofortyfouram.memento.debug.provider.SqliteContentProviderImpl.java
com.twofortyfouram.memento.debug.provider.SqliteOpenHelperImpl.java
com.twofortyfouram.memento.debug.provider.SqliteUriMatcherImpl.java
com.twofortyfouram.memento.debug.provider.TableOneContract.java
com.twofortyfouram.memento.provider.ContentChangeNotificationQueueTest.java
com.twofortyfouram.memento.provider.ContentChangeNotificationQueue.java
com.twofortyfouram.memento.provider.ContentProviderOperationServiceTest.java
com.twofortyfouram.memento.provider.ContentProviderOperationService.java
com.twofortyfouram.memento.provider.ContentProviderUtilTest.java
com.twofortyfouram.memento.provider.ContentProviderUtil.java
com.twofortyfouram.memento.provider.ImmutableUriMatcherTest.java
com.twofortyfouram.memento.provider.ImmutableUriMatcher.java
com.twofortyfouram.memento.provider.sqlite.AbstractSqliteContentProviderIntegrationTest.java
com.twofortyfouram.memento.provider.sqlite.AbstractSqliteContentProviderTest.java
com.twofortyfouram.memento.provider.sqlite.AbstractSqliteContentProvider.java
com.twofortyfouram.memento.provider.sqlite.SqliteColumnBuilderTest.java
com.twofortyfouram.memento.provider.sqlite.SqliteColumnBuilder.java
com.twofortyfouram.memento.provider.sqlite.SqliteIndexBuilderTest.java
com.twofortyfouram.memento.provider.sqlite.SqliteIndexBuilder.java
com.twofortyfouram.memento.provider.sqlite.SqliteOpenHelperCompat.java
com.twofortyfouram.memento.provider.sqlite.SqliteStorageClass.java
com.twofortyfouram.memento.provider.sqlite.SqliteTableBuilderTest.java
com.twofortyfouram.memento.provider.sqlite.SqliteTableBuilder.java
com.twofortyfouram.memento.provider.sqlite.SqliteUriMatchTest.java
com.twofortyfouram.memento.provider.sqlite.SqliteUriMatch.java
com.twofortyfouram.memento.provider.sqlite.SqliteUriMatcher.java