SQLiteCursorTest.java revision 65a8883f0e605bb8a73a692987b47ce5da632e72
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.database.sqlite;
18
19import android.content.Context;
20import android.test.AndroidTestCase;
21import android.test.suitebuilder.annotation.SmallTest;
22
23import java.io.File;
24
25public class SQLiteCursorTest extends AndroidTestCase {
26    private SQLiteDatabase mDatabase;
27    private File mDatabaseFile;
28    private static final String TABLE_NAME = "testCursor";
29    @Override
30    protected void setUp() throws Exception {
31        super.setUp();
32
33        File dbDir = getContext().getDir(this.getClass().getName(), Context.MODE_PRIVATE);
34        mDatabaseFile = new File(dbDir, "sqlitecursor_test.db");
35        if (mDatabaseFile.exists()) {
36            mDatabaseFile.delete();
37        }
38        mDatabase = SQLiteDatabase.openOrCreateDatabase(mDatabaseFile.getPath(), null);
39        assertNotNull(mDatabase);
40        // create a test table
41        mDatabase.execSQL("CREATE TABLE " + TABLE_NAME + " (i int, j int);");
42    }
43
44    @Override
45    protected void tearDown() throws Exception {
46        mDatabase.close();
47        mDatabaseFile.delete();
48        super.tearDown();
49    }
50
51    @SmallTest
52    public void testQueryObjReassignment() {
53        mDatabase.enableWriteAheadLogging();
54        // have a few connections in the database connection pool
55        DatabaseConnectionPool pool = mDatabase.mConnectionPool;
56        pool.setMaxPoolSize(5);
57        SQLiteCursor cursor =
58                (SQLiteCursor) mDatabase.rawQuery("select * from " + TABLE_NAME, null);
59        assertNotNull(cursor);
60        // it should use a pooled database connection
61        SQLiteDatabase db = cursor.getDatabase();
62        assertTrue(db.mConnectionNum > 0);
63        assertFalse(mDatabase.equals(db));
64        assertEquals(mDatabase, db.mParentConnObj);
65        assertTrue(pool.getConnectionList().contains(db));
66        assertTrue(db.isOpen());
67        // do a requery. cursor should continue to use the above pooled connection
68        cursor.requery();
69        SQLiteDatabase dbAgain = cursor.getDatabase();
70        assertEquals(db, dbAgain);
71        // disable WAL so that the pooled connection held by the above cursor is closed
72        mDatabase.disableWriteAheadLogging();
73        assertFalse(db.isOpen());
74        assertNull(mDatabase.mConnectionPool);
75        // requery - which should make the cursor use mDatabase connection since the pooled
76        // connection is no longer available
77        cursor.requery();
78        SQLiteDatabase db1 = cursor.getDatabase();
79        assertTrue(db1.mConnectionNum == 0);
80        assertEquals(mDatabase, db1);
81        assertNull(mDatabase.mConnectionPool);
82        assertTrue(db1.isOpen());
83        assertFalse(mDatabase.equals(db));
84        // enable WAL and requery - this time a pooled connection should be used
85        mDatabase.enableWriteAheadLogging();
86        cursor.requery();
87        db = cursor.getDatabase();
88        assertTrue(db.mConnectionNum > 0);
89        assertFalse(mDatabase.equals(db));
90        assertEquals(mDatabase, db.mParentConnObj);
91        assertTrue(mDatabase.mConnectionPool.getConnectionList().contains(db));
92        assertTrue(db.isOpen());
93    }
94}
95