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 testGetTableHealthWhenTableHasChkpts_ExpectPass() throws ActionNotAuthorizedException {
    String tableId = testTable;/*from ww  w. j  a  v a2 s  .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);

    int testVal2 = 200;
    ContentValues updatedCvValues = new ContentValues();
    updatedCvValues.put(testCol, testVal2);
    ODKDatabaseImplUtils.get().insertCheckpointRowWithId(db, tableId, orderedColumns, updatedCvValues, rowId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // 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(), 2);

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

    int val2 = 0;
    String saveptType = null;
    while (cursor.moveToNext()) {
        // Get the actual value
        int ind = cursor.getColumnIndex(testCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_INTEGER);
        val2 = cursor.getInt(ind);

        // Get the savepoint_type
        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(val2, testVal2);

    // Also make sure that the savepoint_type
    // is empty
    assertNotSame(saveptType, SavepointTypeManipulator.incomplete());
    assertNotSame(saveptType, SavepointTypeManipulator.complete());

    // Test that the health of the table is CLEAN
    int health = ODKDatabaseImplUtils.get().getTableHealth(db, tableId);

    assertTrue(CursorUtils.getTableHealthHasChanges(health));

    // 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 testMultithreadedDBInsertionWithDBIntPerThreadWithTxnOnUpdate_ExpectPass()
        throws ActionNotAuthorizedException {
    int numOfThreads = 5;
    String tableId = testTable;//from  w w  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();
    }

    // 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();
    int testValAgain = 100;
    if (!dbWithinTrxn) {
        db.beginTransactionExclusive();
        ContentValues cvValuesAgain = new ContentValues();
        cvValuesAgain.put(setupTestCol, testValAgain);
        ODKDatabaseImplUtils.get().updateRowWithId(db, tableId, orderedColumns, cvValuesAgain, rowId,
                activeUser, RoleConsts.ADMIN_ROLES_LIST, currentLocale);
        db.setTransactionSuccessful();
        db.endTransaction();
    }

    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()) {
        int indAgain = cursor2.getColumnIndex(setupTestCol);
        int typeAgain = cursor2.getType(indAgain);
        assertEquals(typeAgain, Cursor.FIELD_TYPE_INTEGER);
        int valAgain = cursor2.getInt(indAgain);
        assertEquals(valAgain, testValAgain);
        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();
    }

    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 testInsertCheckpointRowIntoExistingTableWithIdWhenRowAlreadyExists_ExpectPass()
        throws ActionNotAuthorizedException {
    String tableId = testTable;/*ww w. ja va2  s  . c  o m*/
    String testCol = "testColumn";
    String testColType = ElementDataType.string.name();
    String testVal = "test";
    String testVal2 = "test2";
    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().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);

    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);
    }

    assertEquals(val, testVal);

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

    // 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(), 2);

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

    String val2 = null;
    String saveptType = null;
    while (cursor.moveToNext()) {
        // Get the actual value
        int ind = cursor.getColumnIndex(testCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        val2 = cursor.getString(ind);

        // Get the savepoint_type
        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(val2, testVal2);

    // Also make sure that the savepoint_type
    // is empty
    assertNotSame(saveptType, SavepointTypeManipulator.incomplete());
    assertNotSame(saveptType, SavepointTypeManipulator.complete());

    // 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 testMultithreadedDBInsertionWithClosingCursorAndOrigConn_ExpectPass()
        throws ActionNotAuthorizedException {
    int numOfThreads = 5;
    String tableId = testTable;/*  w  w  w.  j ava  2s  .  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, "[]"));
    }

    String uniqueUUID = LocalizationUtils.genUUID();
    DbHandle prevUniqueKey = new DbHandle(AbstractODKDatabaseUtilsTest.class.getSimpleName() + uniqueUUID
            + AndroidConnectFactory.INTERNAL_TYPE_SUFFIX);
    OdkConnectionInterface prevDb = OdkConnectionFactorySingleton.getOdkConnectionFactoryInterface()
            .getConnection(getAppName(), prevUniqueKey);

    OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(prevDb, 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(prevDb, 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(prevDb, 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, true, 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(prevDb, 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 testInsertCheckpointRowIntoExistingTableWithIdWhenRowIdNotProvided_ExpectPass()
        throws ActionNotAuthorizedException {
    String tableId = testTable;//from  w w w  .  j  av  a2  s  .c  o  m
    String testCol = "testColumn";
    String testColType = ElementDataType.string.name();
    String testVal = "test";
    String rowId = null;
    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;
    String saveptType = 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_TIMESTAMP);
        type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        saveptType = cursor.getString(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);

    // Also make sure that the savepoint_type
    // is empty
    assertNotSame(saveptType, SavepointTypeManipulator.incomplete());
    assertNotSame(saveptType, SavepointTypeManipulator.complete());

    // 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 testMultipleConnectionsWithTableDeletionAndCreation_ExpectPass()
        throws ActionNotAuthorizedException {
    String tableId = testTable;/*from ww w . ja va  2 s  .  com*/
    String testCol = "testColumn";
    String testColType = ElementDataType.integer.name();
    List<Column> columns = new ArrayList<Column>();
    columns.add(new Column(testCol, testCol, testColType, "[]"));

    // Create two different db connections
    String uuid1 = LocalizationUtils.genUUID();
    DbHandle uniqueKey1 = new DbHandle(AbstractODKDatabaseUtilsTest.class.getSimpleName() + uuid1
            + AndroidConnectFactory.INTERNAL_TYPE_SUFFIX);
    OdkConnectionInterface db1 = OdkConnectionFactorySingleton.getOdkConnectionFactoryInterface()
            .getConnection(getAppName(), uniqueKey1);

    String uuid2 = LocalizationUtils.genUUID();
    DbHandle uniqueKey2 = new DbHandle(AbstractODKDatabaseUtilsTest.class.getSimpleName() + uuid2
            + AndroidConnectFactory.INTERNAL_TYPE_SUFFIX);
    OdkConnectionInterface db2 = OdkConnectionFactorySingleton.getOdkConnectionFactoryInterface()
            .getConnection(getAppName(), uniqueKey2);

    // Create a table on db1
    OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db1, tableId,
            columns);

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

    // Insert a row using db1
    int testVal = 5;
    ContentValues cvValues = new ContentValues();
    String rowId = LocalizationUtils.genUUID();
    cvValues.put(testCol, testVal);
    ODKDatabaseImplUtils.get().insertRowWithId(db1, tableId, orderedColumns, cvValues, rowId, activeUser,
            RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Have both query the table
    // Query with db1
    String sel = "SELECT * FROM " + tableId;
    String[] selArgs = null;
    Cursor cursor1 = ODKDatabaseImplUtils.get().rawQuery(db1, sel, selArgs, null, accessContext);

    while (cursor1.moveToNext()) {
        int ind1 = cursor1.getColumnIndex(testCol);
        int type1 = cursor1.getType(ind1);
        assertEquals(type1, Cursor.FIELD_TYPE_INTEGER);
        int val1 = cursor1.getInt(ind1);
        assertEquals(val1, testVal);
    }

    // Query with db2
    Cursor cursor2 = ODKDatabaseImplUtils.get().rawQuery(db2, sel, selArgs, null, accessContext);

    while (cursor2.moveToNext()) {
        int ind2 = cursor2.getColumnIndex(testCol);
        int type2 = cursor2.getType(ind2);
        assertEquals(type2, Cursor.FIELD_TYPE_INTEGER);
        int val2 = cursor2.getInt(ind2);
        assertEquals(val2, testVal);
    }

    // Delete the table and recreate with a different row
    ODKDatabaseImplUtils.get().deleteTableAndAllData(db1, tableId);

    // Create a table on db1
    String newTestCol = "testColumn0";
    List<Column> newColumns = new ArrayList<Column>();
    newColumns.add(new Column(newTestCol, newTestCol, testColType, "[]"));
    orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db1, tableId, newColumns);

    // Re-create the same table with different row
    int newTestVal = 200;
    cvValues = new ContentValues();
    rowId = LocalizationUtils.genUUID();
    cvValues.put(newTestCol, newTestVal);
    ODKDatabaseImplUtils.get().insertRowWithId(db1, tableId, orderedColumns, cvValues, rowId, activeUser,
            RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Have both connections re-query the table
    // Query with db1
    cursor1 = ODKDatabaseImplUtils.get().rawQuery(db1, sel, selArgs, null, accessContext);

    while (cursor1.moveToNext()) {
        int ind3 = cursor1.getColumnIndex(newTestCol);
        int type3 = cursor1.getType(ind3);
        assertEquals(type3, Cursor.FIELD_TYPE_INTEGER);
        int val3 = cursor1.getInt(ind3);
        assertEquals(val3, newTestVal);
    }

    // Query with db2
    cursor2 = ODKDatabaseImplUtils.get().rawQuery(db2, sel, selArgs, null, accessContext);

    while (cursor2.moveToNext()) {
        int ind4 = cursor2.getColumnIndex(testCol);
        int type4 = cursor2.getType(ind4);
        assertEquals(type4, Cursor.FIELD_TYPE_INTEGER);
        int val4 = cursor2.getInt(ind4);
        assertEquals(val4, newTestVal);
    }

    // Close the cursor
    if (cursor1 != null && !cursor1.isClosed()) {
        cursor1.close();
    }

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

    System.out.println("testMultipleConnectionsWithTableDeletionAndCreation_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 testUpdateDataInExistingTableWithIdWhenIdAlreadyExists_ExpectPass()
        throws ActionNotAuthorizedException {
    String tableId = testTable;//from   ww  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;
    boolean thrown = false;

    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);

    // Try updating that row in the database
    int testVal2 = 25;
    ContentValues cvValues2 = new ContentValues();
    cvValues2.put(testCol, testVal2);

    ODKDatabaseImplUtils.get().updateRowWithId(db, tableId, orderedColumns, cvValues2, uuid, activeUser,
            RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Select everything out of the table
    String sel2 = "SELECT * FROM " + tableId;
    String[] selArgs2 = {};
    Cursor cursor2 = ODKDatabaseImplUtils.get().rawQuery(db, sel2, selArgs2, null, accessContext);
    assertEquals(cursor2.getCount(), 1);

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

    assertEquals(val2, testVal2);

    // 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 testSetChoiceList() {
    ArrayList<Object> values = new ArrayList<Object>();
    Map<String, Object> myMap = new TreeMap<String, Object>();
    Map<String, Object> displayText = new TreeMap<String, Object>();
    displayText.put("text", "displayText");
    myMap.put("choice_list_name", "test_list");
    myMap.put("data_value", "test");
    myMap.put("display", displayText);
    values.add(myMap);/*from w w  w. j  a v  a  2 s  .c om*/

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

    String jsonChoiceList = null;
    try {
        jsonChoiceList = ODKFileUtils.mapper.writeValueAsString(values);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    String choiceListId = ODKDatabaseImplUtils.get().setChoiceList(db, jsonChoiceList);

    // Select the _choice_list_id from the _choice_lists table
    String sel = "SELECT * FROM " + DatabaseConstants.CHOICE_LIST_TABLE_NAME + " WHERE "
            + ChoiceListColumns.CHOICE_LIST_ID + " = ?";
    String[] selArgs = { "" + choiceListId };
    Cursor cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);
    assertEquals(cursor.getCount(), 1);

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

    assertEquals(val, jsonChoiceList);
}

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

@Test
public void testGetTableDefinitionEntry_ExpectPass() throws ActionNotAuthorizedException {
    String tableId = testTable;//w ww .  j a v 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);
    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);

    ODKDatabaseImplUtils.AccessContext accessContextNoTableId = ODKDatabaseImplUtils.get().getAccessContext(db,
            null, activeUser, RoleConsts.ADMIN_ROLES_LIST);

    // Select everything out of the table
    String sel2 = "SELECT * FROM " + DatabaseConstants.TABLE_DEFS_TABLE_NAME + " WHERE "
            + TableDefinitionsColumns.TABLE_ID + " = ?";
    String[] selArgs2 = { "" + tableId };
    cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel2, selArgs2, null, accessContextNoTableId);
    assertEquals(cursor.getCount(), 1);

    String syncTimeVal = null;
    String schemaETagVal = null;
    String lastDataETagVal = null;
    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(TableDefinitionsColumns.LAST_SYNC_TIME);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        syncTimeVal = cursor.getString(ind);

        ind = cursor.getColumnIndex(TableDefinitionsColumns.SCHEMA_ETAG);
        assertTrue(cursor.isNull(ind));

        ind = cursor.getColumnIndex(TableDefinitionsColumns.LAST_DATA_ETAG);
        assertTrue(cursor.isNull(ind));
    }

    // Now get the table definition entry
    TableDefinitionEntry tde = ODKDatabaseImplUtils.get().getTableDefinitionEntry(db, tableId);

    // Compare the table definition entry and the raw query results
    assertEquals(syncTimeVal, tde.getLastSyncTime());
    assertEquals(schemaETagVal, tde.getSchemaETag());
    assertEquals(lastDataETagVal, tde.getLastDataETag());

    // 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 testUpdateTableLastSyncTime_ExpectPass() throws ActionNotAuthorizedException {
    String tableId = testTable;/*w  w  w . j a  v a 2  s. com*/
    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);
    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);

    ODKDatabaseImplUtils.AccessContext accessContextNoTableId = ODKDatabaseImplUtils.get().getAccessContext(db,
            null, activeUser, RoleConsts.ADMIN_ROLES_LIST);

    // Select everything out of the table
    String sel2 = "SELECT * FROM " + DatabaseConstants.TABLE_DEFS_TABLE_NAME + " WHERE "
            + TableDefinitionsColumns.TABLE_ID + " = ?";
    String[] selArgs2 = { "" + tableId };
    cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel2, selArgs2, null, accessContextNoTableId);
    assertEquals(cursor.getCount(), 1);

    String defaultSyncTime = "-1";
    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(TableDefinitionsColumns.LAST_SYNC_TIME);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        assertEquals(cursor.getString(ind), defaultSyncTime);
    }

    // udpate db table last sync time
    ODKDatabaseImplUtils.get().privilegedUpdateTableLastSyncTime(db, tableId);

    // Select everything out of the table
    cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel2, selArgs2, null, accessContextNoTableId);
    assertEquals(cursor.getCount(), 1);

    String syncTime = null;
    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(TableDefinitionsColumns.LAST_SYNC_TIME);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        syncTime = cursor.getString(ind);
    }

    // CAL: Should there be more checks here?
    assertNotSame(syncTime, defaultSyncTime);

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