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

private void internalTestMemoryLeakCycling_ExpectPass(int maxIterations) throws ActionNotAuthorizedException {

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

    String tableId = "memoryTest";
    int maxBytes = 32;
    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 w  w . j  av a 2  s.  co  m

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

@Test
public void testWriteDataIntoExistingTableWithMimeUri_ExpectPass() throws ActionNotAuthorizedException {
    String tableId = testTable;//from  www . ja v a 2s .  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.AbstractODKDatabaseUtilsTest.java

@Test
public void testSaveAsCompleteMostRecentCheckpointDataInTableWithId_ExpectPass()
        throws ActionNotAuthorizedException {
    String tableId = testTable;//from   w w  w .  j a  v a2s  . 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_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.AbstractODKDatabaseUtilsTest.java

@Test
public void testSaveAsIncompleteMostRecentCheckpointDataInTableWithId_ExpectPass()
        throws ActionNotAuthorizedException {
    String tableId = testTable;/*from  w  w w .ja  va 2 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_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.AbstractODKDatabaseUtilsTest.java

@Test
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);/*from w  w  w . j  a va 2  s. 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.AbstractODKDatabaseUtilsTest.java

@Test
public void testUpdateTableETags() 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);

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

@Test
public void testCreateOrOpenTableWithColumnWhenColumnIsArray_ExpectPass() {
    String tableId = testTable;/*from  ww w . java2  s  .  c o  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);
}

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

@Test
public void testRawQueryWithData_ExpectPass() {
    String tableId = testTable;//w  ww. j  av a 2  s .  co m
    String query = "SELECT * FROM " + tableId;
    List<Column> columns = new ArrayList<Column>();
    columns.add(new Column("col1", "col1", "string", "[]"));
    OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db, tableId,
            columns);

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

    // Check that the user defined rows are in the table
    Cursor cursor = ODKDatabaseImplUtils.get().rawQuery(db, query, null, null, accessContext);
    Cursor refCursor = db.rawQuery(query, null);

    if (cursor != null && refCursor != null) {
        int index = 0;
        while (cursor.moveToNext() && refCursor.moveToNext()) {
            int testType = cursor.getType(index);
            int refType = refCursor.getType(index);
            assertEquals(testType, refType);

            switch (refType) {
            case Cursor.FIELD_TYPE_BLOB:
                byte[] byteArray = cursor.getBlob(index);
                byte[] refByteArray = refCursor.getBlob(index);
                assertEquals(byteArray, refByteArray);
                break;
            case Cursor.FIELD_TYPE_FLOAT:
                float valueFloat = cursor.getFloat(index);
                float refValueFloat = refCursor.getFloat(index);
                assertEquals(valueFloat, refValueFloat, 0.0);
                break;
            case Cursor.FIELD_TYPE_INTEGER:
                int valueInt = cursor.getInt(index);
                int refValueInt = refCursor.getInt(index);
                assertEquals(valueInt, refValueInt);
                break;
            case Cursor.FIELD_TYPE_STRING:
                String valueStr = cursor.getString(index);
                String refValueStr = refCursor.getString(index);
                assertEquals(valueStr, refValueStr);
                break;
            case Cursor.FIELD_TYPE_NULL:
            default:
                break;
            }
        }
    }

    // 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 testWriteDataIntoExistingTableWithGeopoint_ExpectPass() throws ActionNotAuthorizedException {
    String tableId = testTable;//w w w  .  j av a2s. co m
    String testCol = "testColumn";
    String testColLat = "testColumn_latitude";
    String testColLong = "testColumn_longitude";
    String testColAlt = "testColumn_altitude";
    String testColAcc = "testColumn_accuracy";
    double pos_lat = 5.55;
    double pos_long = 6.6;
    double pos_alt = 7.77;
    double pos_acc = 8.88;
    String testColType = ElementType.GEOPOINT;
    List<Column> columns = new ArrayList<Column>();
    columns.add(new Column(testCol, testCol, testColType,
            "[\"" + testColLat + "\",\"" + testColLong + "\",\"" + testColAlt + "\",\"" + testColAcc + "\"]"));
    columns.add(new Column(testColLat, "latitude", ElementDataType.number.name(), "[]"));
    columns.add(new Column(testColLong, "longitude", ElementDataType.number.name(), "[]"));
    columns.add(new Column(testColAlt, "altitude", ElementDataType.number.name(), "[]"));
    columns.add(new Column(testColAcc, "accuracy", ElementDataType.number.name(), "[]"));
    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(testColLat, pos_lat);
    cvValues.put(testColLong, pos_long);
    cvValues.put(testColAlt, pos_alt);
    cvValues.put(testColAcc, pos_acc);

    ODKDatabaseImplUtils.get().insertRowWithId(db, tableId, orderedColumns, cvValues,
            LocalizationUtils.genUUID(), activeUser, RoleConsts.ADMIN_ROLES_LIST, currentLocale);

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

    double valLat = 0;
    double valLong = 0;
    double valAlt = 0;
    double valAcc = 0;
    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(testColLat);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_FLOAT);
        valLat = cursor.getDouble(ind);

        ind = cursor.getColumnIndex(testColLong);
        type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_FLOAT);
        valLong = cursor.getDouble(ind);

        ind = cursor.getColumnIndex(testColAlt);
        type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_FLOAT);
        valAlt = cursor.getDouble(ind);

        ind = cursor.getColumnIndex(testColAcc);
        type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_FLOAT);
        valAcc = cursor.getDouble(ind);
    }

    assertEquals(valLat, pos_lat, 0.0);
    assertEquals(valLong, pos_long, 0.0);
    assertEquals(valAlt, pos_alt, 0.0);
    assertEquals(valAcc, pos_acc, 0.0);

    //cursor.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 testCreateOrOpenTableWithColumnWhenColumnIsMimeUri_ExpectPass() {
    String tableId = testTable;//from   ww  w  . j a  va2s  .  c o  m
    String testCol = "testColumn";
    String uriFrag = "uriFragment";
    String conType = "contentType";
    String testColUriFrag = testCol + "_" + uriFrag;
    String testColContType = testCol + "_" + conType;
    String testColType = DataTypeNamesToRemove.MIMEURI;

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

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

    List<String> cols = new ArrayList<String>();
    cols.add(uriFrag);
    cols.add(conType);

    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(testCol + "_" + name));
            assertTrue(cols.contains(name));
            if (name.equals(uriFrag)) {
                assertTrue(type.equals(ElementDataType.rowpath.name()));
            } else {
                assertTrue(type.equals(ElementDataType.string.name()));
            }
        }
    }

    // Select everything out of the table for element key
    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 = "[\"" + testColUriFrag + "\",\"" + testColContType + "\"]";
        assertEquals(valStr, testVal);
    }

    // Select everything out of the table for uriFragment
    String[] selArgs2 = { testColUriFrag };
    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, uriFrag);
    }

    // Select everything out of the table for contentType
    String[] selArgs3 = { testColContType };
    cursor = ODKDatabaseImplUtils.get().queryForTest(db, DatabaseConstants.COLUMN_DEFINITIONS_TABLE_NAME, null,
            elemKey + " = ?", selArgs3, 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, conType);
    }

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