Code example for SQLiteCursor

Methods: getColumnIndexOrThrow

0
 
    @Test(expected = IllegalArgumentException.class) 
    public void testGetColumnIndexOrThrowNotFoundEmpty() throws Exception { 
        setupEmptyResult(); 
 
        cursor.getColumnIndexOrThrow("Fred");
    } 
 
    @Test 
    public void testMoveToFirst() throws Exception { 
        assertThat(cursor.moveToFirst(), equalTo(true));
        assertThat(cursor.getInt(0), equalTo(1234));
        assertThat(cursor.getString(1), equalTo("Chuck"));
    } 
 
    @Test 
    public void testMoveToFirstEmpty() throws Exception { 
        setupEmptyResult(); 
 
        assertThat(cursor.moveToFirst(), equalTo(false));
    } 
 
    @Test 
    public void testMoveToNext() throws Exception { 
        cursor.moveToFirst();
 
        assertThat(cursor.moveToNext(), equalTo(true));
        assertThat(cursor.getInt(0), equalTo(1235));
        assertThat(cursor.getString(1), equalTo("Julie"));
    } 
 
    @Test 
    public void testMoveToNextPastEnd() throws Exception { 
        cursor.moveToFirst();
 
        cursor.moveToNext();
        cursor.moveToNext();
        cursor.moveToNext();
 
        assertThat(cursor.moveToNext(), equalTo(false));
    } 
     
    @Test 
    public void testMoveBackwards() throws Exception { 
    	assertThat(cursor.getPosition(), equalTo(-1));
    	 
        cursor.moveToFirst();
        assertThat(cursor.getPosition(), equalTo(0));
        cursor.moveToNext();
        assertThat(cursor.getPosition(), equalTo(1));
        cursor.moveToNext();
        assertThat(cursor.getPosition(), equalTo(2));
         
        cursor.moveToFirst();
        assertThat(cursor.getPosition(), equalTo(0));
        cursor.moveToNext();
        assertThat(cursor.getPosition(), equalTo(1));
        cursor.moveToNext();
        assertThat(cursor.getPosition(), equalTo(2));
         
        cursor.moveToPosition(1);
        assertThat(cursor.getPosition(), equalTo(1));
    } 
 
    @Test 
    public void testMoveToNextEmpty() throws Exception { 
        setupEmptyResult(); 
 
        cursor.moveToFirst();
        assertThat(cursor.moveToNext(), equalTo(false));
    } 
     
    @Test 
    public void testMoveToPrevious() throws Exception { 
    	cursor.moveToFirst();
    	cursor.moveToNext();
    	 
    	assertThat(cursor.moveToPrevious(), equalTo(true));
        assertThat(cursor.getInt(0), equalTo(1234));
        assertThat(cursor.getString(1), equalTo("Chuck"));
    } 
     
    @Test 
    public void testMoveToPreviousPastStart() throws Exception { 
    	cursor.moveToFirst();
    	 
    	// Possible to move cursor before the first item 
    	assertThat(cursor.moveToPrevious(), equalTo(true));
    	// After that, attempts to move cursor back return false 
    	assertThat(cursor.moveToPrevious(), equalTo(false));
    } 
     
    @Test 
    public void testMoveToPreviousEmpty() throws Exception { 
        setupEmptyResult(); 
    	cursor.moveToFirst();
    	 
    	assertThat(cursor.moveToPrevious(), equalTo(false));
    } 
 
    @Test 
    public void testGetPosition() throws Exception { 
        cursor.moveToFirst();
        assertThat(cursor.getPosition(), equalTo(0));
 
        cursor.moveToNext();
        assertThat(cursor.getPosition(), equalTo(1));
    } 
 
    @Test 
    public void testGetBlob() throws Exception { 
        String sql = "UPDATE table_name set blob_value=? where id=1234";
        byte[] byteData = sql.getBytes();
 
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setObject(1, byteData);
        statement.executeUpdate();
 
        setupCursor(); 
        cursor.moveToFirst();
 
        byte[] retrievedByteData = cursor.getBlob(5);
        assertThat(byteData.length, equalTo(retrievedByteData.length));
 
        for (int i = 0; i < byteData.length; i++) {
            assertThat(byteData[i], equalTo(retrievedByteData[i]));
        } 
    } 
 
    @Test 
    public void testGetClob() throws Exception { 
        String sql = "UPDATE table_name set clob_value=? where id=1234";
        String s = "Don't CLOBber my data, please. Thank you.";
 
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setObject(1, s);
        statement.executeUpdate();
 
        setupCursor(); 
        cursor.moveToFirst();
 
        String actual = cursor.getString(6);
        assertThat(s, equalTo(actual));
    } 
 
    @Test 
    public void testGetString() throws Exception { 
        cursor.moveToFirst();
 
        String[] data = {"Chuck", "Julie", "Chris"};
 
        for (String aData : data) {
            assertThat(cursor.getString(1), equalTo(aData));
            cursor.moveToNext();
        } 
    } 
 
    @Test 
    public void testGetInt() throws Exception { 
        cursor.moveToFirst();
 
        int[] data = {1234, 1235, 1236};
 
        for (int aData : data) {
            assertThat(cursor.getInt(0), equalTo(aData));
            cursor.moveToNext();
        } 
    } 
 
    @Test 
    public void testGetLong() throws Exception { 
        cursor.moveToFirst();
 
        assertThat(cursor.getLong(2), equalTo(3463L));
    } 
 
    @Test 
    public void testGetFloat() throws Exception { 
        cursor.moveToFirst();
 
        assertThat(cursor.getFloat(3), equalTo((float) 1.5));
    } 
 
    @Test 
    public void testGetDouble() throws Exception { 
        cursor.moveToFirst();
 
        assertThat(cursor.getDouble(4), equalTo(3.14159));
    } 
 
    @Test 
    public void testClose() throws Exception { 
        assertThat(cursor.isClosed(), equalTo(false));
        cursor.close();
        assertThat(cursor.isClosed(), equalTo(true));
    } 
 
    @Test 
    public void testIsNullWhenNull() throws Exception { 
        cursor.moveToFirst();
        assertThat(cursor.moveToNext(), equalTo(true));
 
        assertThat(cursor.isNull(cursor.getColumnIndex("id")), equalTo(false));
        assertThat(cursor.isNull(cursor.getColumnIndex("name")), equalTo(false));
 
        assertThat(cursor.isNull(cursor.getColumnIndex("long_value")), equalTo(true));
        assertThat(cursor.isNull(cursor.getColumnIndex("float_value")), equalTo(true));
        assertThat(cursor.isNull(cursor.getColumnIndex("double_value")), equalTo(true));
    } 
 
    @Test 
    public void testIsNullWhenNotNull() throws Exception { 
        cursor.moveToFirst();
 
        for (int i = 0; i < 5; i++) {
            assertThat(cursor.isNull(i), equalTo(false));
        } 
    } 
 
    @Test 
    public void testIsNullWhenIndexOutOfBounds() throws Exception { 
        cursor.moveToFirst();
 
        // column index 5 is out-of-bounds 
        assertThat(cursor.isNull(5), equalTo(true));
    } 
 
    private void addPeople() throws Exception { 
        String[] inserts = {
                "INSERT INTO table_name (id, name, long_value, float_value, double_value) VALUES(1234, 'Chuck', 3463, 1.5, 3.14159);", 
                "INSERT INTO table_name (id, name) VALUES(1235, 'Julie');", 
                "INSERT INTO table_name (id, name) VALUES(1236, 'Chris');" 
        }; 
 
        for (String insert : inserts) {
            connection.createStatement().executeUpdate(insert);
        } 
    } 
 
    private void setupCursor() throws Exception { 
        Statement statement = connection.createStatement(DatabaseConfig.getResultSetType(), ResultSet.CONCUR_READ_ONLY);
        String sql ="SELECT * FROM table_name;";
        resultSet = statement.executeQuery("SELECT * FROM table_name;");
        cursor = new SQLiteCursor(null, null, null, null);
        Robolectric.shadowOf(cursor).setResultSet(resultSet, sql);
    } 
 
    private void setupEmptyResult() throws Exception { 
        Statement statement = connection.createStatement();
        statement.executeUpdate("DELETE FROM table_name;");