Search in sources :

Example 71 with Cursor

use of android.database.Cursor in project platform_frameworks_base by android.

the class DatabaseCursorTest method testCursorWindowFailureWhenTooManyCursorWindowsLeftOpen.

/**
     * sometimes CursorWindow creation fails due to non-availability of memory create
     * another CursorWindow object. One of the scenarios of its occurrence is when
     * there are too many CursorWindow objects already opened by the process.
     * This test is for that scenario.
     */
@LargeTest
// Failing.
@Suppress
public void testCursorWindowFailureWhenTooManyCursorWindowsLeftOpen() {
    mDatabase.execSQL("CREATE TABLE test (_id INTEGER PRIMARY KEY, data TEXT);");
    mDatabase.execSQL("INSERT INTO test values(1, 'test');");
    int N = 1024;
    ArrayList<Cursor> cursorList = new ArrayList<Cursor>();
    // open many cursors until a failure occurs
    for (int i = 0; i < N; i++) {
        try {
            Cursor cursor = mDatabase.rawQuery("select * from test", null);
            cursor.getCount();
            cursorList.add(cursor);
        } catch (CursorWindowAllocationException e) {
            // got the exception we wanted
            break;
        } catch (Exception e) {
            fail("unexpected exception: " + e.getMessage());
            e.printStackTrace();
            break;
        }
    }
    for (Cursor c : cursorList) {
        c.close();
    }
}
Also used : ArrayList(java.util.ArrayList) SQLiteCursor(android.database.sqlite.SQLiteCursor) Cursor(android.database.Cursor) CursorIndexOutOfBoundsException(android.database.CursorIndexOutOfBoundsException) Suppress(android.test.suitebuilder.annotation.Suppress) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 72 with Cursor

use of android.database.Cursor in project platform_frameworks_base by android.

the class DatabaseCursorTest method testRealColumns.

@MediumTest
public void testRealColumns() throws Exception {
    mDatabase.execSQL("CREATE TABLE test (_id INTEGER PRIMARY KEY, data REAL);");
    ContentValues values = new ContentValues();
    values.put("data", 42.11);
    long id = mDatabase.insert("test", "data", values);
    assertTrue(id > 0);
    Cursor c = mDatabase.rawQuery("SELECT data FROM test", null);
    assertNotNull(c);
    assertTrue(c.moveToFirst());
    assertEquals(42.11, c.getDouble(0));
    c.close();
}
Also used : ContentValues(android.content.ContentValues) SQLiteCursor(android.database.sqlite.SQLiteCursor) Cursor(android.database.Cursor) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Example 73 with Cursor

use of android.database.Cursor in project platform_frameworks_base by android.

the class DatabaseCursorTest method testManyRowsTxtLong.

@LargeTest
public void testManyRowsTxtLong() throws Exception {
    mDatabase.execSQL("CREATE TABLE test (_id INTEGER PRIMARY KEY, txt TEXT, data INT);");
    Random random = new Random(System.currentTimeMillis());
    StringBuilder randomString = new StringBuilder(1979);
    for (int i = 0; i < 1979; i++) {
        randomString.append((random.nextInt() & 0xf) % 10);
    }
    // if cursor window size changed, adjust this value too  
    final int count = 600;
    mDatabase.execSQL("BEGIN Transaction;");
    for (int i = 0; i < count; i++) {
        StringBuilder sql = new StringBuilder(2100);
        sql.append("INSERT INTO test (txt, data) VALUES ('");
        sql.append(randomString);
        sql.append("','");
        sql.append(i);
        sql.append("');");
        mDatabase.execSQL(sql.toString());
    }
    mDatabase.execSQL("COMMIT;");
    Cursor c = mDatabase.query("test", new String[] { "txt", "data" }, null, null, null, null, null);
    assertNotNull(c);
    int i = 0;
    while (c.moveToNext()) {
        assertEquals(randomString.toString(), c.getString(0));
        assertEquals(i, c.getInt(1));
        i++;
    }
    assertEquals(count, i);
    assertEquals(count, c.getCount());
    c.close();
}
Also used : Random(java.util.Random) SQLiteCursor(android.database.sqlite.SQLiteCursor) Cursor(android.database.Cursor) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 74 with Cursor

use of android.database.Cursor in project platform_frameworks_base by android.

the class DatabaseCursorTest method testCursor2.

@MediumTest
public void testCursor2() throws Exception {
    populateDefaultTable();
    Cursor c = mDatabase.query("test", null, "_id > 1000", null, null, null, null);
    assertEquals(0, c.getCount());
    assertTrue(c.isBeforeFirst());
    try {
        c.getInt(0);
        fail("CursorIndexOutOfBoundsException expected");
    } catch (CursorIndexOutOfBoundsException ex) {
    // expected
    }
    int i;
    for (c.moveToFirst(), i = 0; !c.isAfterLast(); c.moveToNext(), i++) {
        c.getInt(0);
    }
    assertEquals(0, i);
    try {
        c.getInt(0);
        fail("CursorIndexOutOfBoundsException expected");
    } catch (CursorIndexOutOfBoundsException ex) {
    // expected
    }
    c.close();
}
Also used : CursorIndexOutOfBoundsException(android.database.CursorIndexOutOfBoundsException) SQLiteCursor(android.database.sqlite.SQLiteCursor) Cursor(android.database.Cursor) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Example 75 with Cursor

use of android.database.Cursor in project platform_frameworks_base by android.

the class DatabaseLocaleTest method testHoge.

@SmallTest
public void testHoge() throws Exception {
    Cursor cursor = null;
    try {
        String expectedString = new String(new int[] { 0xFE000 }, 0, 1);
        mDatabase.execSQL("INSERT INTO test(id, data) VALUES(1, '" + expectedString + "')");
        cursor = mDatabase.rawQuery("SELECT data FROM test WHERE id = 1", null);
        assertNotNull(cursor);
        assertTrue(cursor.moveToFirst());
        String actualString = cursor.getString(0);
        assertEquals(expectedString.length(), actualString.length());
        for (int i = 0; i < expectedString.length(); i++) {
            assertEquals((int) expectedString.charAt(i), (int) actualString.charAt(i));
        }
        assertEquals(expectedString, actualString);
    } finally {
        if (cursor != null)
            cursor.close();
    }
}
Also used : Cursor(android.database.Cursor) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Aggregations

Cursor (android.database.Cursor)4002 ArrayList (java.util.ArrayList)547 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)527 Uri (android.net.Uri)467 ContentValues (android.content.ContentValues)334 ContentResolver (android.content.ContentResolver)193 Test (org.junit.Test)183 RemoteException (android.os.RemoteException)182 File (java.io.File)170 IOException (java.io.IOException)159 MatrixCursor (android.database.MatrixCursor)154 Intent (android.content.Intent)140 SQLException (android.database.SQLException)126 MediumTest (android.test.suitebuilder.annotation.MediumTest)116 HashMap (java.util.HashMap)108 SQLiteException (android.database.sqlite.SQLiteException)94 SQLiteQueryBuilder (android.database.sqlite.SQLiteQueryBuilder)93 SQLiteCursor (android.database.sqlite.SQLiteCursor)88 Query (android.app.DownloadManager.Query)76 MergeCursor (android.database.MergeCursor)75