Example usage for android.database Cursor getType

List of usage examples for android.database Cursor getType

Introduction

In this page you can find the example usage for android.database Cursor getType.

Prototype

int getType(int columnIndex);

Source Link

Document

Returns data type of the given column's value.

Usage

From source file:org.opendatakit.utilities.AbstractODKDatabaseUtilsTest.java

@Test
public void testDeleteCheckpointRowsWithValidId_ExpectPass() throws ActionNotAuthorizedException {
    String tableId = testTable;/*from ww  w  .j a  va 2 s.  com*/
    String testCol = "testColumn";
    String testColType = ElementDataType.string.name();
    String testVal = "test";
    String rowId = LocalizationUtils.genUUID();
    List<Column> columns = new ArrayList<Column>();
    columns.add(new Column(testCol, testCol, testColType, "[]"));
    OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db, tableId,
            columns);

    ODKDatabaseImplUtils.AccessContext accessContext = ODKDatabaseImplUtils.get().getAccessContext(db, tableId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST);

    ContentValues cvValues = new ContentValues();
    cvValues.put(testCol, testVal);
    ODKDatabaseImplUtils.get().insertCheckpointRowWithId(db, tableId, orderedColumns, cvValues, rowId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Select everything out of the table
    String sel = "SELECT * FROM " + tableId + " WHERE " + testCol + " = ?";
    String[] selArgs = { "" + testVal };
    Cursor cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    String val = null;
    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(testCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        val = cursor.getString(ind);

        ind = cursor.getColumnIndex(DataTableColumns.SAVEPOINT_TYPE);
        assertTrue(cursor.isNull(ind));

        // Get the conflict_type and make sure that it is null
        ind = cursor.getColumnIndex(DataTableColumns.CONFLICT_TYPE);
        assertTrue(cursor.isNull(ind));
    }

    assertEquals(val, testVal);

    ODKDatabaseImplUtils.get().deleteAllCheckpointRowsWithId(db, tableId, rowId, activeUser,
            RoleConsts.ADMIN_ROLES_LIST);
    // Select everything out of the table
    sel = "SELECT * FROM " + tableId;
    selArgs = new String[0];
    cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    assertEquals(cursor.getCount(), 0);

    // Drop the table now that the test is done
    ODKDatabaseImplUtils.get().deleteTableAndAllData(db, tableId);
}

From source file:org.opendatakit.utilities.AbstractODKDatabaseUtilsTest.java

@Test
public void testUpdateRowETagAndSyncState_ExpectPass() throws ActionNotAuthorizedException {
    String tableId = testTable;//from   w w w  . j  av a2s.  co  m
    String testCol = "testColumn";
    String testColType = ElementDataType.integer.name();
    List<Column> columns = new ArrayList<Column>();
    columns.add(new Column(testCol, testCol, testColType, "[]"));
    OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db, tableId,
            columns);

    ODKDatabaseImplUtils.AccessContext accessContext = ODKDatabaseImplUtils.get().getAccessContext(db, tableId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST);

    int testVal = 5;

    ContentValues cvValues = new ContentValues();
    String rowId = LocalizationUtils.genUUID();
    cvValues.put(testCol, testVal);
    ODKDatabaseImplUtils.get().insertRowWithId(db, tableId, orderedColumns, cvValues, rowId, activeUser,
            RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Select everything out of the table
    String sel = "SELECT * FROM " + tableId + " WHERE " + testCol + " = ?";
    String[] selArgs = { "" + testVal };
    Cursor cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    int val = 0;
    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(testCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_INTEGER);
        val = cursor.getInt(ind);
    }

    assertEquals(val, testVal);

    // Update the row ETag and sync state
    String rowETag = LocalizationUtils.genUUID();
    ODKDatabaseImplUtils.get().privilegedUpdateRowETagAndSyncState(db, tableId, rowId, rowETag,
            SyncState.synced, activeUser);

    // Run the query again and make sure that the place row in conflict worked as expected
    cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(DataTableColumns.ROW_ETAG);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        String rowETagVal = cursor.getString(ind);
        assertEquals(rowETag, rowETagVal);

        int indSyncState = cursor.getColumnIndex(DataTableColumns.SYNC_STATE);
        int typeSyncState = cursor.getType(indSyncState);
        assertEquals(typeSyncState, Cursor.FIELD_TYPE_STRING);
        String syncStateVal = cursor.getString(indSyncState);
        assertEquals(syncStateVal, SyncState.synced.name());
    }

    // Drop the table now that the test is done
    ODKDatabaseImplUtils.get().deleteTableAndAllData(db, tableId);
}

From source file:org.opendatakit.utilities.AbstractODKDatabaseUtilsTest.java

@Test
public void testDeleteCheckpointRowsWithInvalidId_ExpectFail() throws ActionNotAuthorizedException {
    String tableId = testTable;/*from w w  w.j  a  v a2  s. c  o  m*/
    String testCol = "testColumn";
    String testColType = ElementDataType.string.name();
    String testVal = "test";
    String rowId = LocalizationUtils.genUUID();
    List<Column> columns = new ArrayList<Column>();
    columns.add(new Column(testCol, testCol, testColType, "[]"));
    OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db, tableId,
            columns);

    ODKDatabaseImplUtils.AccessContext accessContext = ODKDatabaseImplUtils.get().getAccessContext(db, tableId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST);

    ContentValues cvValues = new ContentValues();
    cvValues.put(testCol, testVal);
    ODKDatabaseImplUtils.get().insertCheckpointRowWithId(db, tableId, orderedColumns, cvValues, rowId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Select everything out of the table
    String sel = "SELECT * FROM " + tableId + " WHERE " + testCol + " = ?";
    String[] selArgs = { "" + testVal };
    Cursor cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    String val = null;
    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(testCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        val = cursor.getString(ind);

        ind = cursor.getColumnIndex(DataTableColumns.SAVEPOINT_TYPE);
        assertTrue(cursor.isNull(ind));

        // Get the conflict_type and make sure that it is null
        ind = cursor.getColumnIndex(DataTableColumns.CONFLICT_TYPE);
        assertTrue(cursor.isNull(ind));
    }

    assertEquals(val, testVal);

    String invalidRowId = LocalizationUtils.genUUID();
    boolean thrown = true;
    try {
        ODKDatabaseImplUtils.get().deleteAllCheckpointRowsWithId(db, tableId, invalidRowId, activeUser,
                RoleConsts.ADMIN_ROLES_LIST);
    } catch (ActionNotAuthorizedException ex) {
        throw ex;
    } catch (Exception e) {
        thrown = true;
        e.printStackTrace();
    }

    assertTrue(thrown);

    // Select everything out of the table
    sel = "SELECT * FROM " + tableId;
    selArgs = new String[0];
    cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    assertEquals(cursor.getCount(), 1);

    // Drop the table now that the test is done
    ODKDatabaseImplUtils.get().deleteTableAndAllData(db, tableId);
}

From source file:org.opendatakit.utilities.AbstractODKDatabaseUtilsTest.java

@Test
public void testMultithreadedMultipleDBInsertionWithSameSelect_ExpectPass()
        throws ActionNotAuthorizedException {
    int numOfThreads = 20;
    String tableId = testTable;/*from w  w  w .j  a  va 2s  . co  m*/
    String colPrefix = "testColumn";
    String testColType = ElementDataType.integer.name();
    List<Column> columns = new ArrayList<Column>();

    // Create table with the right number of columns
    for (int i = 0; i <= numOfThreads; i++) {
        String testCol = colPrefix + i;
        columns.add(new Column(testCol, testCol, testColType, "[]"));
    }

    OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db, tableId,
            columns);

    ODKDatabaseImplUtils.AccessContext accessContext = ODKDatabaseImplUtils.get().getAccessContext(db, tableId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST);

    // Insert data so that the threads can all just update
    int testVal = 0;
    String setupTestCol = colPrefix + 0;
    ContentValues cvValues = new ContentValues();
    String rowId = LocalizationUtils.genUUID();
    cvValues.put(setupTestCol, testVal);
    ODKDatabaseImplUtils.get().insertRowWithId(db, tableId, orderedColumns, cvValues, rowId, activeUser,
            RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Ensure that the row exists
    String sel = "SELECT * FROM " + tableId;
    String[] selArgs = null;
    Cursor cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(setupTestCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_INTEGER);
        int val = cursor.getInt(ind);
        assertEquals(val, testVal);
    }

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

    // Have the threads all update the corresponding column in the table
    int numOfWritesForThreads = 100;
    try {
        threadTest(numOfThreads, tableId, rowId, colPrefix, orderedColumns, true, true, numOfWritesForThreads);
    } catch (Exception e) {
        e.printStackTrace();
    }

    Cursor cursor2 = null;
    String sel2 = "SELECT * FROM " + tableId;
    String[] selArgs2 = null;
    cursor2 = ODKDatabaseImplUtils.get().rawQuery(db, sel2, selArgs2, null, accessContext);

    assertEquals(cursor2.getCount(), 1);

    System.out.println("testMultithreadedMultipleDBInsertionWithSameSelect_ExpectPass: before assert");
    OdkConnectionFactorySingleton.getOdkConnectionFactoryInterface().dumpInfo(false);

    while (cursor2.moveToNext()) {
        int indAgain = cursor2.getColumnIndex(setupTestCol);
        int typeAgain = cursor2.getType(indAgain);
        assertEquals(typeAgain, Cursor.FIELD_TYPE_INTEGER);
        int valAgain = cursor2.getInt(indAgain);
        assertEquals(valAgain, testVal);
        for (int i = 1; i <= numOfThreads; i++) {
            System.out.println("testMultithreadedMultipleDBInsertionWithSameSelect_ExpectPass: assertion "
                    + "for thread " + i);
            String columnName = colPrefix + i;
            int ind = cursor2.getColumnIndex(columnName);
            int type = cursor2.getType(ind);
            assertEquals(type, Cursor.FIELD_TYPE_INTEGER);
            int val = cursor2.getInt(ind);
            assertEquals(val, numOfWritesForThreads);
        }
    }

    if (cursor2 != null && !cursor2.isClosed()) {
        cursor2.close();
    }

    System.out.println("testMultithreadedMultipleDBInsertionWithSameSelect_ExpectPass: after assert");
    OdkConnectionFactorySingleton.getOdkConnectionFactoryInterface().dumpInfo(false);

    // Drop the table now that the test is done
    ODKDatabaseImplUtils.get().deleteTableAndAllData(db, tableId);
}

From source file:org.opendatakit.utilities.AbstractODKDatabaseUtilsTest.java

@Test
public void testMultithreadedDBInsertionWithDBIntPerThreadWithTxn_ExpectPass()
        throws ActionNotAuthorizedException {
    int numOfThreads = 5;
    String tableId = testTable;//w w w  . j  a  v a 2  s . com
    String colPrefix = "testColumn";
    String testColType = ElementDataType.integer.name();
    List<Column> columns = new ArrayList<Column>();

    // Create table with the right number of columns
    for (int i = 0; i <= numOfThreads; i++) {
        String testCol = colPrefix + i;
        columns.add(new Column(testCol, testCol, testColType, "[]"));
    }

    OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db, tableId,
            columns);

    ODKDatabaseImplUtils.AccessContext accessContext = ODKDatabaseImplUtils.get().getAccessContext(db, tableId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST);

    // Insert data so that the threads can all just update
    int testVal = 0;
    String setupTestCol = colPrefix + 0;
    ContentValues cvValues = new ContentValues();
    String rowId = LocalizationUtils.genUUID();
    cvValues.put(setupTestCol, testVal);
    ODKDatabaseImplUtils.get().insertRowWithId(db, tableId, orderedColumns, cvValues, rowId, activeUser,
            RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Ensure that the row exists
    String sel = "SELECT * FROM " + tableId + " WHERE " + setupTestCol + " = ?";
    String[] selArgs = { "" + testVal };
    Cursor cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(setupTestCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_INTEGER);
        int val = cursor.getInt(ind);
        assertEquals(val, testVal);
    }

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

    // Have the threads all update the corresponding column in the table
    try {
        threadTest(numOfThreads, tableId, rowId, colPrefix, orderedColumns, true, false, 0);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Ensure that the row exists
    boolean dbWithinTrxn = db.inTransaction();
    if (!dbWithinTrxn) {
        db.beginTransactionExclusive();
        String sel2 = "SELECT * FROM " + tableId;
        String[] selArgs2 = null;
        Cursor cursor2 = ODKDatabaseImplUtils.get().rawQuery(db, sel2, selArgs2, null, accessContext);

        assertEquals(cursor2.getCount(), 1);

        System.out.println("testMultithreadedDBInsertionWithDBIntPerThreadWithTxn_ExpectPass: before assert");
        OdkConnectionFactorySingleton.getOdkConnectionFactoryInterface().dumpInfo(false);

        while (cursor2.moveToNext()) {
            for (int i = 1; i <= numOfThreads; i++) {
                System.out
                        .println("testMultithreadedDBInsertionWithDBIntPerThreadWithTxn_ExpectPass: assertion "
                                + "for thread " + i);
                String columnName = colPrefix + i;
                int ind = cursor2.getColumnIndex(columnName);
                int type = cursor2.getType(ind);
                assertEquals(type, Cursor.FIELD_TYPE_INTEGER);
                int val = cursor2.getInt(ind);
                assertEquals(val, i);
            }
        }

        if (cursor2 != null && !cursor2.isClosed()) {
            cursor2.close();
        }

        db.setTransactionSuccessful();
        db.endTransaction();
    }

    System.out.println("testMultithreadedDBInsertionWithDBIntPerThreadWithTxn_ExpectPass: after assert");
    OdkConnectionFactorySingleton.getOdkConnectionFactoryInterface().dumpInfo(false);

    // Drop the table now that the test is done
    ODKDatabaseImplUtils.get().deleteTableAndAllData(db, tableId);
}

From source file:org.opendatakit.utilities.AbstractODKDatabaseUtilsTest.java

@Test
public void testMultithreadedDBInsertionWithClosingCursor_ExpectPass() throws ActionNotAuthorizedException {
    int numOfThreads = 5;
    String tableId = testTable;/*from  ww w. j a v a  2 s.  c  o m*/
    String colPrefix = "testColumn";
    String testColType = ElementDataType.integer.name();
    List<Column> columns = new ArrayList<Column>();

    // Create table with the right number of columns
    for (int i = 0; i <= numOfThreads; i++) {
        String testCol = colPrefix + i;
        columns.add(new Column(testCol, testCol, testColType, "[]"));
    }

    OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db, tableId,
            columns);

    ODKDatabaseImplUtils.AccessContext accessContext = ODKDatabaseImplUtils.get().getAccessContext(db, tableId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST);

    // Insert data so that the threads can all just update
    int testVal = 0;
    String setupTestCol = colPrefix + 0;
    ContentValues cvValues = new ContentValues();
    String rowId = LocalizationUtils.genUUID();
    cvValues.put(setupTestCol, testVal);
    ODKDatabaseImplUtils.get().insertRowWithId(db, tableId, orderedColumns, cvValues, rowId, activeUser,
            RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Ensure that the row exists
    String sel = "SELECT * FROM " + tableId + " WHERE " + setupTestCol + " = ?";
    String[] selArgs = { "" + testVal };
    Cursor cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(setupTestCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_INTEGER);
        int val = cursor.getInt(ind);
        assertEquals(val, testVal);
    }

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

    List<Long> returnedResults = null;

    // Have the threads all update the corresponding column in the table
    try {
        returnedResults = threadTest(numOfThreads, tableId, rowId, colPrefix, orderedColumns, false, false, 0);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Extra check to make sure that this has finished before
    // anything continues
    List<Long> expectedList = new ArrayList<Long>(numOfThreads);
    for (long i = 1; i <= numOfThreads; i++) {
        expectedList.add(i);
    }
    if (returnedResults != null) {
        Collections.sort(returnedResults);
    }

    assertEquals(expectedList, returnedResults);

    // Ensure that the row exists
    String sel2 = "SELECT * FROM " + tableId;
    String[] selArgs2 = null;
    Cursor cursor2 = ODKDatabaseImplUtils.get().rawQuery(db, sel2, selArgs2, null, accessContext);

    assertEquals(cursor2.getCount(), 1);

    System.out.println("testMultithreadedDBInsertionWithClosingCursor_ExpectPass: before assert");
    OdkConnectionFactorySingleton.getOdkConnectionFactoryInterface().dumpInfo(false);

    while (cursor2.moveToNext()) {
        assertEquals(cursor2.getColumnIndex(colPrefix), -1);
        for (int i = 0; i <= numOfThreads; i++) {
            String columnName = colPrefix + i;
            int ind = cursor2.getColumnIndex(columnName);
            int type = cursor2.getType(ind);
            assertEquals(type, Cursor.FIELD_TYPE_INTEGER);
            int val = cursor2.getInt(ind);
            assertEquals(val, i);
        }
    }

    if (cursor2 != null && !cursor2.isClosed()) {
        cursor2.close();
    }

    // Drop the table now that the test is done
    ODKDatabaseImplUtils.get().deleteTableAndAllData(db, tableId);
}

From source file:org.opendatakit.utilities.AbstractODKDatabaseUtilsTest.java

@Test
public void testMultithreadedDBInsertionWithDBIntPerThreadAndForQuery_ExpectPass()
        throws ActionNotAuthorizedException {
    int numOfThreads = 5;
    String tableId = testTable;/*  w  ww .  j  a  v a2s .c  om*/
    String colPrefix = "testColumn";
    String testColType = ElementDataType.integer.name();
    List<Column> columns = new ArrayList<Column>();

    // Create table with the right number of columns
    for (int i = 0; i <= numOfThreads; i++) {
        String testCol = colPrefix + i;
        columns.add(new Column(testCol, testCol, testColType, "[]"));
    }

    OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db, tableId,
            columns);

    ODKDatabaseImplUtils.AccessContext accessContext = ODKDatabaseImplUtils.get().getAccessContext(db, tableId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST);

    // Insert data so that the threads can all just update
    int testVal = 0;
    String setupTestCol = colPrefix + 0;
    ContentValues cvValues = new ContentValues();
    String rowId = LocalizationUtils.genUUID();
    cvValues.put(setupTestCol, testVal);
    ODKDatabaseImplUtils.get().insertRowWithId(db, tableId, orderedColumns, cvValues, rowId, activeUser,
            RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Ensure that the row exists
    String sel = "SELECT * FROM " + tableId + " WHERE " + setupTestCol + " = ?";
    String[] selArgs = { "" + testVal };
    Cursor cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(setupTestCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_INTEGER);
        int val = cursor.getInt(ind);
        assertEquals(val, testVal);
    }

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

    // Have the threads all update the corresponding column in the table
    try {
        threadTest(numOfThreads, tableId, rowId, colPrefix, orderedColumns, true, false, 0);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Ensure that the row exists
    String sel2 = "SELECT * FROM " + tableId;
    String[] selArgs2 = null;

    // Query with new connection to see if this gets all recent operations
    DbHandle uniqueKey = new DbHandle(AbstractODKDatabaseUtilsTest.class.getSimpleName() + testVal
            + AndroidConnectFactory.INTERNAL_TYPE_SUFFIX);
    OdkConnectionInterface dbForQuery = OdkConnectionFactorySingleton.getOdkConnectionFactoryInterface()
            .getConnection(getAppName(), uniqueKey);

    Cursor cursor2 = ODKDatabaseImplUtils.get().rawQuery(dbForQuery, sel2, selArgs2, null, accessContext);

    assertEquals(cursor2.getCount(), 1);

    System.out.println("testMultithreadedDBInsertionWithDBIntPerThreadAndForQuery_ExpectPass: before assert");
    OdkConnectionFactorySingleton.getOdkConnectionFactoryInterface().dumpInfo(false);

    while (cursor2.moveToNext()) {
        for (int i = 1; i <= numOfThreads; i++) {
            System.out
                    .println("testMultithreadedDBInsertionWithDBIntPerThreadAndForQuery_ExpectPass: assertion "
                            + "for thread " + i);
            String columnName = colPrefix + i;
            int ind = cursor2.getColumnIndex(columnName);
            int type = cursor2.getType(ind);
            assertEquals(type, Cursor.FIELD_TYPE_INTEGER);
            int val = cursor2.getInt(ind);
            assertEquals(val, i);
        }
    }

    if (cursor2 != null && !cursor2.isClosed()) {
        cursor2.close();
    }

    System.out.println("testMultithreadedDBInsertionWithDBIntPerThreadAndForQuery_ExpectPass: after assert");
    OdkConnectionFactorySingleton.getOdkConnectionFactoryInterface().dumpInfo(false);

    // Release the OdkConnectionInterface used for the query
    dbForQuery.releaseReference();

    // Drop the table now that the test is done
    ODKDatabaseImplUtils.get().deleteTableAndAllData(db, tableId);
}

From source file:org.opendatakit.utilities.AbstractODKDatabaseUtilsTest.java

@Test
public void testWriteDataIntoExistingTableWithIdWhenIdDoesNotExist_ExpectPass()
        throws ActionNotAuthorizedException {
    String tableId = testTable;/*from w  w  w  . j  a v a2s.  c o m*/
    String testCol = "testColumn";
    String testColType = ElementDataType.integer.name();
    List<Column> columns = new ArrayList<Column>();
    columns.add(new Column(testCol, testCol, testColType, "[]"));
    OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db, tableId,
            columns);

    ODKDatabaseImplUtils.AccessContext accessContext = ODKDatabaseImplUtils.get().getAccessContext(db, tableId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST);

    int testVal = 5;

    ContentValues cvValues = new ContentValues();
    cvValues.put(testCol, testVal);

    String uuid = UUID.randomUUID().toString();
    ODKDatabaseImplUtils.get().insertRowWithId(db, tableId, orderedColumns, cvValues, uuid, activeUser,
            RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Select everything out of the table
    String sel = "SELECT * FROM " + tableId + " WHERE " + testCol + " = ?";
    String[] selArgs = { "" + testVal };
    Cursor cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);
    assertEquals(cursor.getCount(), 1);

    int val = 0;
    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(testCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_INTEGER);
        val = cursor.getInt(ind);
    }

    assertEquals(val, testVal);

    // Drop the table now that the test is done
    ODKDatabaseImplUtils.get().deleteTableAndAllData(db, tableId);
}

From source file:org.opendatakit.utilities.AbstractODKDatabaseUtilsTest.java

@Test
public void testUpdateDataInExistingTableWithIdWhenIdDoesNotExist_ExpectPass()
        throws ActionNotAuthorizedException {

    String tableId = testTable;//from  w w w  .  ja  v  a  2 s. c  om
    String testCol = "testColumn";
    String testColType = ElementDataType.integer.name();
    List<Column> columns = new ArrayList<Column>();
    columns.add(new Column(testCol, testCol, testColType, "[]"));
    OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db, tableId,
            columns);

    ODKDatabaseImplUtils.AccessContext accessContext = ODKDatabaseImplUtils.get().getAccessContext(db, tableId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST);

    int testVal = 5;

    ContentValues cvValues = new ContentValues();
    cvValues.put(testCol, testVal);

    String uuid = UUID.randomUUID().toString();
    ODKDatabaseImplUtils.get().updateRowWithId(db, tableId, orderedColumns, cvValues, uuid, activeUser,
            RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Select everything out of the table
    String sel = "SELECT * FROM " + tableId + " WHERE " + testCol + " = ?";
    String[] selArgs = { "" + testVal };
    Cursor cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);
    assertEquals(cursor.getCount(), 1);

    int val = 0;
    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(testCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_INTEGER);
        val = cursor.getInt(ind);
    }

    assertEquals(val, testVal);

    // Drop the table now that the test is done
    ODKDatabaseImplUtils.get().deleteTableAndAllData(db, tableId);
}

From source file:org.opendatakit.utilities.AbstractODKDatabaseUtilsTest.java

@Test
public void testMultithreadedMultipleDBInsertionWithNewDBForQuery_ExpectPass()
        throws ActionNotAuthorizedException {
    int numOfThreads = 20;
    String tableId = testTable;/*w  w  w .ja  va2 s.co  m*/
    String colPrefix = "testColumn";
    String testColType = ElementDataType.integer.name();
    List<Column> columns = new ArrayList<Column>();

    // Create table with the right number of columns
    for (int i = 0; i <= numOfThreads; i++) {
        String testCol = colPrefix + i;
        columns.add(new Column(testCol, testCol, testColType, "[]"));
    }

    OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db, tableId,
            columns);

    ODKDatabaseImplUtils.AccessContext accessContext = ODKDatabaseImplUtils.get().getAccessContext(db, tableId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST);

    // Insert data so that the threads can all just update
    int testVal = 0;
    String setupTestCol = colPrefix + 0;
    ContentValues cvValues = new ContentValues();
    String rowId = LocalizationUtils.genUUID();
    cvValues.put(setupTestCol, testVal);
    ODKDatabaseImplUtils.get().insertRowWithId(db, tableId, orderedColumns, cvValues, rowId, activeUser,
            RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Ensure that the row exists
    String sel = "SELECT * FROM " + tableId + " WHERE " + setupTestCol + " = ?";
    String[] selArgs = { "" + testVal };
    Cursor cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(setupTestCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_INTEGER);
        int val = cursor.getInt(ind);
        assertEquals(val, testVal);
    }

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

    // Have the threads all update the corresponding column in the table
    int numOfWritesForThreads = 100;
    try {
        threadTest(numOfThreads, tableId, rowId, colPrefix, orderedColumns, true, true, numOfWritesForThreads);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Ensure that the row exists
    Cursor cursor2 = null;

    // Try this to see if it makes a difference
    // Query with new connection to see if this gets all recent operations
    String uuid = LocalizationUtils.genUUID();
    DbHandle uniqueKey = new DbHandle(AbstractODKDatabaseUtilsTest.class.getSimpleName() + uuid
            + AndroidConnectFactory.INTERNAL_TYPE_SUFFIX);
    OdkConnectionInterface dbForQuery = OdkConnectionFactorySingleton.getOdkConnectionFactoryInterface()
            .getConnection(getAppName(), uniqueKey);

    String sel2 = "SELECT * FROM " + tableId;
    String[] selArgs2 = null;
    cursor2 = ODKDatabaseImplUtils.get().rawQuery(dbForQuery, sel2, selArgs2, null, accessContext);

    assertEquals(cursor2.getCount(), 1);

    System.out.println("testMultithreadedMultipleDBInsertion_ExpectPass: before assert");
    OdkConnectionFactorySingleton.getOdkConnectionFactoryInterface().dumpInfo(false);

    while (cursor2.moveToNext()) {
        int indAgain = cursor2.getColumnIndex(setupTestCol);
        int typeAgain = cursor2.getType(indAgain);
        assertEquals(typeAgain, Cursor.FIELD_TYPE_INTEGER);
        int valAgain = cursor2.getInt(indAgain);
        assertEquals(valAgain, testVal);
        for (int i = 1; i <= numOfThreads; i++) {
            System.out
                    .println("testMultithreadedMultipleDBInsertion_ExpectPass: assertion " + "for thread " + i);
            String columnName = colPrefix + i;
            int ind = cursor2.getColumnIndex(columnName);
            int type = cursor2.getType(ind);
            assertEquals(type, Cursor.FIELD_TYPE_INTEGER);
            int val = cursor2.getInt(ind);
            assertEquals(val, numOfWritesForThreads);
        }
    }

    if (cursor2 != null && !cursor2.isClosed()) {
        cursor2.close();
    }

    System.out.println("testMultithreadedMultipleDBInsertion_ExpectPass: after assert");
    OdkConnectionFactorySingleton.getOdkConnectionFactoryInterface().dumpInfo(false);

    // Drop the table now that the test is done
    ODKDatabaseImplUtils.get().deleteTableAndAllData(db, tableId);
}