DatabaseStatementTestpublic class DatabaseStatementTest extends android.test.AndroidTestCase implements android.test.PerformanceTestCase
Fields Summary |
---|
private static final String | sString1 | private static final String | sString2 | private static final String | sString3 | private static final int | CURRENT_DATABASE_VERSION | private android.database.sqlite.SQLiteDatabase | mDatabase | private File | mDatabaseFile |
Methods Summary |
---|
public boolean | isPerformanceOnly()
return false;
| private void | populateDefaultTable()
mDatabase.execSQL("CREATE TABLE test (_id INTEGER PRIMARY KEY, data TEXT);");
mDatabase.execSQL("INSERT INTO test (data) VALUES ('" + sString1 + "');");
mDatabase.execSQL("INSERT INTO test (data) VALUES ('" + sString2 + "');");
mDatabase.execSQL("INSERT INTO test (data) VALUES ('" + sString3 + "');");
| protected void | setUp()
super.setUp();
File dbDir = getContext().getDir("tests", Context.MODE_PRIVATE);
mDatabaseFile = new File(dbDir, "database_test.db");
if (mDatabaseFile.exists()) {
mDatabaseFile.delete();
}
mDatabase = SQLiteDatabase.openOrCreateDatabase(mDatabaseFile.getPath(), null);
assertNotNull(mDatabase);
mDatabase.setVersion(CURRENT_DATABASE_VERSION);
| public int | startPerformance(Intermediates intermediates)
return 1;
| protected void | tearDown()
mDatabase.close();
mDatabaseFile.delete();
super.tearDown();
| public void | testExecuteStatement()
populateDefaultTable();
SQLiteStatement statement = mDatabase.compileStatement("DELETE FROM test");
statement.execute();
Cursor c = mDatabase.query("test", null, null, null, null, null, null);
assertEquals(0, c.getCount());
c.deactivate();
statement.close();
| public void | testSimpleQuery()
mDatabase.execSQL("CREATE TABLE test (num INTEGER NOT NULL, str TEXT NOT NULL);");
mDatabase.execSQL("INSERT INTO test VALUES (1234, 'hello');");
SQLiteStatement statement1 =
mDatabase.compileStatement("SELECT num FROM test WHERE str = ?");
SQLiteStatement statement2 =
mDatabase.compileStatement("SELECT str FROM test WHERE num = ?");
try {
statement1.bindString(1, "hello");
long value = statement1.simpleQueryForLong();
assertEquals(1234, value);
statement1.bindString(1, "world");
statement1.simpleQueryForLong();
fail("shouldn't get here");
} catch (SQLiteDoneException e) {
// expected
}
try {
statement2.bindLong(1, 1234);
String value = statement1.simpleQueryForString();
assertEquals("hello", value);
statement2.bindLong(1, 5678);
statement1.simpleQueryForString();
fail("shouldn't get here");
} catch (SQLiteDoneException e) {
// expected
}
statement1.close();
statement2.close();
| public void | testSimpleStringBinding()
mDatabase.execSQL("CREATE TABLE test (num TEXT, value TEXT);");
String statement = "INSERT INTO test (num, value) VALUES (?,?)";
String[] args = new String[2];
for (int i = 0; i < 2; i++) {
args[i] = Integer.toHexString(i);
}
mDatabase.execSQL(statement, args);
Cursor c = mDatabase.query("test", null, null, null, null, null, null);
int numCol = c.getColumnIndexOrThrow("num");
int valCol = c.getColumnIndexOrThrow("value");
c.moveToFirst();
String num = c.getString(numCol);
assertEquals(Integer.toHexString(0), num);
String val = c.getString(valCol);
assertEquals(Integer.toHexString(1), val);
c.close();
| public void | testStatementClearBindings()
mDatabase.execSQL("CREATE TABLE test (num INTEGER);");
SQLiteStatement statement = mDatabase.compileStatement("INSERT INTO test (num) VALUES (?)");
for (long i = 0; i < 10; i++) {
statement.bindLong(1, i);
statement.clearBindings();
statement.execute();
}
statement.close();
Cursor c = mDatabase.query("test", null, null, null, null, null, "ROWID");
int numCol = c.getColumnIndexOrThrow("num");
assertTrue(c.moveToFirst());
for (long i = 0; i < 10; i++) {
assertTrue(c.isNull(numCol));
c.moveToNext();
}
c.close();
| public void | testStatementConstraint()
mDatabase.execSQL("CREATE TABLE test (num INTEGER NOT NULL);");
SQLiteStatement statement = mDatabase.compileStatement("INSERT INTO test (num) VALUES (?)");
// Try to insert NULL, which violates the constraint
try {
statement.clearBindings();
statement.execute();
fail("expected exception not thrown");
} catch (SQLiteConstraintException e) {
// expected
}
// Make sure the statement can still be used
statement.bindLong(1, 1);
statement.execute();
statement.close();
Cursor c = mDatabase.query("test", null, null, null, null, null, null);
int numCol = c.getColumnIndexOrThrow("num");
c.moveToFirst();
long num = c.getLong(numCol);
assertEquals(1, num);
c.close();
| public void | testStatementLongBinding()
mDatabase.execSQL("CREATE TABLE test (num INTEGER);");
SQLiteStatement statement = mDatabase.compileStatement("INSERT INTO test (num) VALUES (?)");
for (int i = 0; i < 10; i++) {
statement.bindLong(1, i);
statement.execute();
}
statement.close();
Cursor c = mDatabase.query("test", null, null, null, null, null, null);
int numCol = c.getColumnIndexOrThrow("num");
c.moveToFirst();
for (long i = 0; i < 10; i++) {
long num = c.getLong(numCol);
assertEquals(i, num);
c.moveToNext();
}
c.close();
| public void | testStatementMultiThreaded()
mDatabase.execSQL("CREATE TABLE test (num INTEGER, str TEXT);");
SQLiteStatement statement =
mDatabase.compileStatement("INSERT INTO test (num, str) VALUES (?, ?)");
StatementTestThread thread = new StatementTestThread(mDatabase, statement);
thread.start();
try {
thread.join();
} finally {
statement.close();
}
| public void | testStatementMultipleBindings()
mDatabase.execSQL("CREATE TABLE test (num INTEGER, str TEXT);");
SQLiteStatement statement =
mDatabase.compileStatement("INSERT INTO test (num, str) VALUES (?, ?)");
for (long i = 0; i < 10; i++) {
statement.bindLong(1, i);
statement.bindString(2, Long.toHexString(i));
statement.execute();
}
statement.close();
Cursor c = mDatabase.query("test", null, null, null, null, null, "ROWID");
int numCol = c.getColumnIndexOrThrow("num");
int strCol = c.getColumnIndexOrThrow("str");
assertTrue(c.moveToFirst());
for (long i = 0; i < 10; i++) {
long num = c.getLong(numCol);
String str = c.getString(strCol);
assertEquals(i, num);
assertEquals(Long.toHexString(i), str);
c.moveToNext();
}
c.close();
| public void | testStatementStringBinding()
mDatabase.execSQL("CREATE TABLE test (num TEXT);");
SQLiteStatement statement = mDatabase.compileStatement("INSERT INTO test (num) VALUES (?)");
for (long i = 0; i < 10; i++) {
statement.bindString(1, Long.toHexString(i));
statement.execute();
}
statement.close();
Cursor c = mDatabase.query("test", null, null, null, null, null, null);
int numCol = c.getColumnIndexOrThrow("num");
c.moveToFirst();
for (long i = 0; i < 10; i++) {
String num = c.getString(numCol);
assertEquals(Long.toHexString(i), num);
c.moveToNext();
}
c.close();
|
|