summaryrefslogtreecommitdiffstats
path: root/core/tests
diff options
context:
space:
mode:
authorVasu Nori <vnori@google.com>2010-07-16 15:14:08 -0700
committerVasu Nori <vnori@google.com>2010-07-21 16:04:04 -0700
commit65a8883f0e605bb8a73a692987b47ce5da632e72 (patch)
tree7224e456ae309020f5c48d7a0203823b6262a59b /core/tests
parent71cbae81b05c7fff8e27d54168c24e38693ff2f3 (diff)
downloadframeworks_base-65a8883f0e605bb8a73a692987b47ce5da632e72.zip
frameworks_base-65a8883f0e605bb8a73a692987b47ce5da632e72.tar.gz
frameworks_base-65a8883f0e605bb8a73a692987b47ce5da632e72.tar.bz2
don't store mDatabase in SQLiteCursor as it is already in SQLiteQuery
SQLiteCursor has two members: mQuery, mDatabase but mQuery already has mDatabase. there is no need for SQLiteCursor.mDatabase. and everytime SQLiteQuery.mDatabase is to be used, try to use a pooled database connection handle, if possible. Change-Id: I42b2376d714a1a4091c843e245a45b882bb7fee6
Diffstat (limited to 'core/tests')
-rw-r--r--core/tests/coretests/src/android/database/sqlite/DatabaseConnectionPoolTest.java10
-rw-r--r--core/tests/coretests/src/android/database/sqlite/SQLiteCursorTest.java94
-rw-r--r--core/tests/coretests/src/android/database/sqlite/SQLiteDatabaseTest.java54
3 files changed, 153 insertions, 5 deletions
diff --git a/core/tests/coretests/src/android/database/sqlite/DatabaseConnectionPoolTest.java b/core/tests/coretests/src/android/database/sqlite/DatabaseConnectionPoolTest.java
index bb5e024..525dd2d 100644
--- a/core/tests/coretests/src/android/database/sqlite/DatabaseConnectionPoolTest.java
+++ b/core/tests/coretests/src/android/database/sqlite/DatabaseConnectionPoolTest.java
@@ -17,6 +17,7 @@
package android.database.sqlite;
import android.content.Context;
+import android.database.sqlite.SQLiteDatabaseTest.ClassToTestSqlCompilationAndCaching;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Log;
@@ -73,6 +74,7 @@ public class DatabaseConnectionPoolTest extends AndroidTestCase {
SQLiteDatabase db = mTestPool.get(TEST_SQL);
// pool size should be one - since only one should be allocated for the above get()
assertEquals(1, mTestPool.getSize());
+ assertEquals(mDatabase, db.mParentConnObj);
// no free connections should be available
assertEquals(0, mTestPool.getFreePoolSize());
assertFalse(mTestPool.isDatabaseObjFree(db));
@@ -104,6 +106,7 @@ public class DatabaseConnectionPoolTest extends AndroidTestCase {
SQLiteDatabase db = mTestPool.get(TEST_SQL);
assertFalse(dbObjs.contains(db));
dbObjs.add(db);
+ assertEquals(mDatabase, db.mParentConnObj);
}
assertEquals(0, mTestPool.getFreePoolSize());
assertEquals(MAX_CONN, mTestPool.getSize());
@@ -197,11 +200,12 @@ public class DatabaseConnectionPoolTest extends AndroidTestCase {
}
private void executeSqlOnDatabaseConn(SQLiteDatabase db, String sql) {
- // execute the given SQL on the given database connection so that the prepared
- // statement for SQL is cached by the given database connection
+ // get the given sql be compiled on the given database connection.
// this will help DatabaseConenctionPool figure out if a given SQL statement
// is already cached by a database connection.
- db.execSQL(sql, new String[]{1+""});
+ ClassToTestSqlCompilationAndCaching c =
+ ClassToTestSqlCompilationAndCaching.create(db, sql);
+ c.close();
}
/**
diff --git a/core/tests/coretests/src/android/database/sqlite/SQLiteCursorTest.java b/core/tests/coretests/src/android/database/sqlite/SQLiteCursorTest.java
new file mode 100644
index 0000000..3c3ff3f
--- /dev/null
+++ b/core/tests/coretests/src/android/database/sqlite/SQLiteCursorTest.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.database.sqlite;
+
+import android.content.Context;
+import android.test.AndroidTestCase;
+import android.test.suitebuilder.annotation.SmallTest;
+
+import java.io.File;
+
+public class SQLiteCursorTest extends AndroidTestCase {
+ private SQLiteDatabase mDatabase;
+ private File mDatabaseFile;
+ private static final String TABLE_NAME = "testCursor";
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ File dbDir = getContext().getDir(this.getClass().getName(), Context.MODE_PRIVATE);
+ mDatabaseFile = new File(dbDir, "sqlitecursor_test.db");
+ if (mDatabaseFile.exists()) {
+ mDatabaseFile.delete();
+ }
+ mDatabase = SQLiteDatabase.openOrCreateDatabase(mDatabaseFile.getPath(), null);
+ assertNotNull(mDatabase);
+ // create a test table
+ mDatabase.execSQL("CREATE TABLE " + TABLE_NAME + " (i int, j int);");
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ mDatabase.close();
+ mDatabaseFile.delete();
+ super.tearDown();
+ }
+
+ @SmallTest
+ public void testQueryObjReassignment() {
+ mDatabase.enableWriteAheadLogging();
+ // have a few connections in the database connection pool
+ DatabaseConnectionPool pool = mDatabase.mConnectionPool;
+ pool.setMaxPoolSize(5);
+ SQLiteCursor cursor =
+ (SQLiteCursor) mDatabase.rawQuery("select * from " + TABLE_NAME, null);
+ assertNotNull(cursor);
+ // it should use a pooled database connection
+ SQLiteDatabase db = cursor.getDatabase();
+ assertTrue(db.mConnectionNum > 0);
+ assertFalse(mDatabase.equals(db));
+ assertEquals(mDatabase, db.mParentConnObj);
+ assertTrue(pool.getConnectionList().contains(db));
+ assertTrue(db.isOpen());
+ // do a requery. cursor should continue to use the above pooled connection
+ cursor.requery();
+ SQLiteDatabase dbAgain = cursor.getDatabase();
+ assertEquals(db, dbAgain);
+ // disable WAL so that the pooled connection held by the above cursor is closed
+ mDatabase.disableWriteAheadLogging();
+ assertFalse(db.isOpen());
+ assertNull(mDatabase.mConnectionPool);
+ // requery - which should make the cursor use mDatabase connection since the pooled
+ // connection is no longer available
+ cursor.requery();
+ SQLiteDatabase db1 = cursor.getDatabase();
+ assertTrue(db1.mConnectionNum == 0);
+ assertEquals(mDatabase, db1);
+ assertNull(mDatabase.mConnectionPool);
+ assertTrue(db1.isOpen());
+ assertFalse(mDatabase.equals(db));
+ // enable WAL and requery - this time a pooled connection should be used
+ mDatabase.enableWriteAheadLogging();
+ cursor.requery();
+ db = cursor.getDatabase();
+ assertTrue(db.mConnectionNum > 0);
+ assertFalse(mDatabase.equals(db));
+ assertEquals(mDatabase, db.mParentConnObj);
+ assertTrue(mDatabase.mConnectionPool.getConnectionList().contains(db));
+ assertTrue(db.isOpen());
+ }
+}
diff --git a/core/tests/coretests/src/android/database/sqlite/SQLiteDatabaseTest.java b/core/tests/coretests/src/android/database/sqlite/SQLiteDatabaseTest.java
index 91ef0b7..662ba97 100644
--- a/core/tests/coretests/src/android/database/sqlite/SQLiteDatabaseTest.java
+++ b/core/tests/coretests/src/android/database/sqlite/SQLiteDatabaseTest.java
@@ -17,6 +17,7 @@
package android.database.sqlite;
import android.content.Context;
+import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
@@ -78,6 +79,55 @@ public class SQLiteDatabaseTest extends AndroidTestCase {
}
@SmallTest
+ public void testDisableWriteAheadLogging() {
+ mDatabase.execSQL("create table test (i int);");
+ mDatabase.enableWriteAheadLogging();
+ assertNotNull(mDatabase.mConnectionPool);
+ // get a pooled database connection
+ SQLiteDatabase db = mDatabase.getDbConnection("select * from test");
+ assertNotNull(db);
+ assertFalse(mDatabase.equals(db));
+ assertTrue(db.isOpen());
+ // disable WAL - which should close connection pool and all pooled connections
+ mDatabase.disableWriteAheadLogging();
+ assertNull(mDatabase.mConnectionPool);
+ assertFalse(db.isOpen());
+ }
+
+ @SmallTest
+ public void testCursorsWithClosedDbConnAfterDisableWriteAheadLogging() {
+ mDatabase.disableWriteAheadLogging();
+ mDatabase.beginTransactionNonExclusive();
+ mDatabase.execSQL("create table test (i int);");
+ mDatabase.execSQL("insert into test values(1);");
+ mDatabase.setTransactionSuccessful();
+ mDatabase.endTransaction();
+ mDatabase.enableWriteAheadLogging();
+ assertNotNull(mDatabase.mConnectionPool);
+ assertEquals(0, mDatabase.mConnectionPool.getSize());
+ assertEquals(0, mDatabase.mConnectionPool.getFreePoolSize());
+ // get a cursor which should use pooled database connection
+ Cursor c = mDatabase.rawQuery("select * from test", null);
+ assertEquals(1, c.getCount());
+ assertEquals(1, mDatabase.mConnectionPool.getSize());
+ assertEquals(1, mDatabase.mConnectionPool.getFreePoolSize());
+ SQLiteDatabase db = mDatabase.mConnectionPool.getConnectionList().get(0);
+ assertTrue(mDatabase.mConnectionPool.isDatabaseObjFree(db));
+ // disable WAL - which should close connection pool and all pooled connections
+ mDatabase.disableWriteAheadLogging();
+ assertNull(mDatabase.mConnectionPool);
+ assertFalse(db.isOpen());
+ // cursor data should still be accessible because it is fetching data from CursorWindow
+ c.moveToNext();
+ assertEquals(1, c.getInt(0));
+ c.requery();
+ assertEquals(1, c.getCount());
+ c.moveToNext();
+ assertEquals(1, c.getInt(0));
+ c.close();
+ }
+
+ @SmallTest
public void testSetConnectionPoolSize() {
mDatabase.enableWriteAheadLogging();
// can't set pool size to zero
@@ -279,11 +329,11 @@ public class SQLiteDatabaseTest extends AndroidTestCase {
}
}
- private static class ClassToTestSqlCompilationAndCaching extends SQLiteProgram {
+ public static class ClassToTestSqlCompilationAndCaching extends SQLiteProgram {
private ClassToTestSqlCompilationAndCaching(SQLiteDatabase db, String sql) {
super(db, sql);
}
- private static ClassToTestSqlCompilationAndCaching create(SQLiteDatabase db, String sql) {
+ public static ClassToTestSqlCompilationAndCaching create(SQLiteDatabase db, String sql) {
db.lock();
try {
return new ClassToTestSqlCompilationAndCaching(db, sql);