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.test.AbstractODKDatabaseUtilsTest.java

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   ww  w.j a  v a2  s  . c o m

    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.test.AbstractODKDatabaseUtilsTest.java

public void testGetTableDefinitionEntry_ExpectPass() throws ActionNotAuthorizedException {
    String tableId = testTable;/*from   w  w w .  ja  v  a2 s  .  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();
    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.test.AbstractODKDatabaseUtilsTest.java

public void testUpdateTableLastSyncTime_ExpectPass() throws ActionNotAuthorizedException {
    String tableId = testTable;/*from  w  ww.  jav  a  2 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);
    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);
}

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

public void testWriteDataIntoExisitingTableWithMimeUri_ExpectPass() throws ActionNotAuthorizedException {
    String tableId = testTable;// www  . j  av  a  2 s.c om
    String testCol = "testColumn";
    String testColUriFragment = "testColumn_uriFragment";
    String testColContentType = "testColumn_contentType";
    String testColType = DataTypeNamesToRemove.MIMEURI;

    List<Column> columns = new ArrayList<Column>();
    columns.add(new Column(testCol, testCol, testColType,
            "[\"" + testColUriFragment + "\",\"" + testColContentType + "\"]"));
    columns.add(new Column(testColUriFragment, "uriFragment", ElementDataType.rowpath.name(), "[]"));
    columns.add(new Column(testColContentType, "contentType", ElementDataType.string.name(), "[]"));
    OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db, tableId,
            columns);

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

    String uuid = UUID.randomUUID().toString();

    String testUriFragment = "tables/example/instances/" + uuid + "/" + testCol + "-" + uuid + ".jpg";
    String testContentType = "image/jpg";

    ContentValues cvValues = new ContentValues();
    cvValues.put(testColUriFragment, testUriFragment);
    cvValues.put(testColContentType, testContentType);
    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 " + testColUriFragment + " = ?";
    String[] selArgs = { "" + testUriFragment };
    Cursor cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

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

        ind = cursor.getColumnIndex(testColContentType);
        type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        valContentType = cursor.getString(ind);
    }

    assertEquals(valUriFragment, testUriFragment);
    assertEquals(valContentType, testContentType);

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

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

public void testSaveAsCompleteMostRecentCheckpointDataInTableWithId_ExpectPass()
        throws ActionNotAuthorizedException {
    String tableId = testTable;//  w w w.  ja v  a2  s  .  co  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_timestamp
        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(val2, testVal2);

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

    ODKDatabaseImplUtils.get().saveAsCompleteMostRecentCheckpointRowWithId(db, tableId, rowId);
    // 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);

    val2 = null;
    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_timestamp
        ind = cursor.getColumnIndex(DataTableColumns.SAVEPOINT_TYPE);
        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(val2, testVal2);
    assertEquals(saveptType, SavepointTypeManipulator.complete());

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

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

public void testMemoryLeakCycling_ExpectPass() throws ActionNotAuthorizedException {

    LinkedList<byte[]> byteQueue = new LinkedList<byte[]>();

    String tableId = "memoryTest";
    int maxBytes = 32;
    int maxIterations = 1000;
    String testColType = ElementDataType.string.name();

    for (int j = 0; j < maxIterations; ++j) {
        if (j % 10 == 0) {
            System.out.println("iteration " + j + " of " + maxIterations);
        }//from  w ww  .  j  ava2s .c om

        int maxCols = 10 + (j % 7);
        // construct table
        List<Column> columns = new ArrayList<Column>();
        for (int i = 0; i < maxCols; ++i) {
            String testCol = "testColumn_" + Integer.toString(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);

        String rowId = LocalizationUtils.genUUID();

        ContentValues cvValues = new ContentValues();
        for (int i = 0; i < maxCols; ++i) {
            String testCol = "testColumn_" + Integer.toString(i);
            String testVal = "testVal_" + Integer.toString(i);
            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 queryCol = "testColumn_" + Integer.toString(j % maxCols);
        String queryVal = "testVal_" + Integer.toString(j % maxCols);
        String sel = "SELECT * FROM " + tableId + " WHERE " + queryCol + " = ?";
        String[] selArgs = { queryVal };
        Cursor cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

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

            ind = cursor.getColumnIndex(DataTableColumns.SAVEPOINT_TYPE);
            assertFalse(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, queryVal);
        cursor.close();

        ODKDatabaseImplUtils.get().deleteRowWithId(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);

        for (int len = 1; len < maxBytes; len += 4) {
            byte[] bytes = new byte[len];
            for (int k = 0; k < len; ++k) {
                bytes[k] = (byte) k;
            }
            byteQueue.add(bytes);
        }
        for (int len = 1; len < maxBytes; len += 4) {
            byte[] bytes = new byte[len];
            for (int k = 0; k < len; ++k) {
                bytes[k] = (byte) k;
            }
            byteQueue.add(bytes);
        }
        for (int len = 1; len < maxBytes; len += 4) {
            byte[] bytes = byteQueue.pop();
            for (int k = 0; k < len; ++k) {
                assertEquals(bytes[k], (byte) k);
            }
        }
    }
}

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

public void testSaveAsIncompleteMostRecentCheckpointDataInTableWithId_ExpectPass()
        throws ActionNotAuthorizedException {
    String tableId = testTable;/*from w  ww .  j  a v a 2s  .  c om*/
    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_timestamp
        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(val2, testVal2);

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

    ODKDatabaseImplUtils.get().saveAsIncompleteMostRecentCheckpointRowWithId(db, tableId, rowId);

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

    val2 = null;
    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_timestamp
        ind = cursor.getColumnIndex(DataTableColumns.SAVEPOINT_TYPE);
        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(val2, testVal2);
    assertEquals(saveptType, SavepointTypeManipulator.incomplete());

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

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

public void testGetChoiceList_ExpectPass() {
    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);/*  w w  w.  j  ava2s  .  c  o  m*/

    String jsonChoiceList = null;
    try {
        jsonChoiceList = ODKFileUtils.mapper.writeValueAsString(values);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }

    // Set the choice list id
    String choiceListId = ODKDatabaseImplUtils.get().setChoiceList(db, jsonChoiceList);

    // Get the choice list
    String retJsonChoiceList = ODKDatabaseImplUtils.get().getChoiceList(db, choiceListId);

    assertEquals(jsonChoiceList, retJsonChoiceList);

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

    // 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, retJsonChoiceList);
}

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

public void testUpdateTableETags() throws ActionNotAuthorizedException {
    String tableId = testTable;/*  w w w  . j  ava 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);

    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(TableDefinitionsColumns.SCHEMA_ETAG);
        assertTrue(cursor.isNull(ind));

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

    // update db schema etag and last data etag
    String newSchemaETag = LocalizationUtils.genUUID();
    String newLastDataETag = LocalizationUtils.genUUID();
    ODKDatabaseImplUtils.get().privilegedUpdateTableETags(db, tableId, newSchemaETag, newLastDataETag);

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

    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(TableDefinitionsColumns.SCHEMA_ETAG);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        assertEquals(newSchemaETag, cursor.getString(ind));

        int ind2 = cursor.getColumnIndex(TableDefinitionsColumns.LAST_DATA_ETAG);
        int type2 = cursor.getType(ind2);
        assertEquals(type2, Cursor.FIELD_TYPE_STRING);
        assertEquals(newLastDataETag, cursor.getString(ind2));
    }

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

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

public void testCreateOrOpenTableWithColumnWhenColumnIsArray_ExpectPass() {
    String tableId = testTable;/*  w  w  w.jav  a 2s  .  co  m*/
    String testCol = "testColumn";
    String itemsStr = "items";
    String testColItems = testCol + "_" + itemsStr;
    String testColType = ElementDataType.array.name();
    List<Column> columns = new ArrayList<Column>();
    columns.add(new Column(testCol, testCol, testColType, "[\"" + testColItems + "\"]"));
    columns.add(new Column(testColItems, itemsStr, ElementDataType.string.name(), "[]"));
    OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db, tableId,
            columns);

    OrderedColumns coldefs = ODKDatabaseImplUtils.get().getUserDefinedColumns(db, tableId);
    assertEquals(coldefs.getColumnDefinitions().size(), 2);
    assertEquals(coldefs.getColumnDefinitions().get(0).getElementKey(), testCol);
    assertEquals(coldefs.getColumnDefinitions().get(1).getElementKey(), testColItems);

    for (ColumnDefinition col : coldefs.getColumnDefinitions()) {
        String key = col.getElementKey();
        String name = col.getElementName();
        String type = col.getElementType();
        if (key.equals(testCol)) {
            assertTrue(key.equals(testCol));
            assertTrue(name.equals(testCol));
            assertTrue(type.equals(testColType));
        } else {
            assertTrue(key.equals(testColItems));
            assertTrue(name.equals(itemsStr));
            assertTrue(type.equals(ElementDataType.string.name()));
        }
    }

    // Select everything out of the table
    String[] selArgs = { "" + testCol };
    Cursor cursor = ODKDatabaseImplUtils.get().queryForTest(db, DatabaseConstants.COLUMN_DEFINITIONS_TABLE_NAME,
            null, elemKey + " = ?", selArgs, null, null, null, null);

    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(listChildElemKeys);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        String valStr = cursor.getString(ind);
        String testVal = "[\"" + testColItems + "\"]";
        assertEquals(valStr, testVal);
    }

    // Select everything out of the table
    String[] selArgs2 = { testColItems };
    cursor = ODKDatabaseImplUtils.get().queryForTest(db, DatabaseConstants.COLUMN_DEFINITIONS_TABLE_NAME, null,
            elemKey + " = ?", selArgs2, null, null, null, null);

    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(elemName);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        String valStr = cursor.getString(ind);
        assertEquals(valStr, itemsStr);
    }

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