blob: cd2005dd16ff9c8fe4767c4110ba2a16c2a10003 [file] [log] [blame]
Vasu Nori422dad02010-09-03 16:03:08 -07001/*
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.database.sqlite.SQLiteDatabaseTest.ClassToTestSqlCompilationAndCaching;
21import android.test.AndroidTestCase;
22import android.test.suitebuilder.annotation.SmallTest;
23
24import java.io.File;
25
26public class SQLiteUnfinalizedExceptionTest extends AndroidTestCase {
27 private SQLiteDatabase mDatabase;
28 private File mDatabaseFile;
29 private static final String TABLE_NAME = "testCursor";
30 @Override
31 protected void setUp() throws Exception {
32 super.setUp();
33
34 File dbDir = getContext().getDir(this.getClass().getName(), Context.MODE_PRIVATE);
35 mDatabaseFile = new File(dbDir, "UnfinalizedExceptionTest.db");
36 if (mDatabaseFile.exists()) {
37 mDatabaseFile.delete();
38 }
39 mDatabase = SQLiteDatabase.openOrCreateDatabase(mDatabaseFile.getPath(), null);
40 assertNotNull(mDatabase);
41 }
42
43 @Override
44 protected void tearDown() throws Exception {
45 mDatabase.close();
46 mDatabaseFile.delete();
47 super.tearDown();
48 }
49
50 @SmallTest
51 public void testUnfinalizedExceptionNotExcpected() {
52 mDatabase.execSQL("CREATE TABLE " + TABLE_NAME + " (i int, j int);");
53 // the above statement should be in SQLiteDatabase.mPrograms
54 // and should automatically be finalized when database is closed
55 mDatabase.lock();
56 try {
57 mDatabase.closeDatabase();
58 } finally {
59 mDatabase.unlock();
60 }
61 }
62
63 @SmallTest
64 public void testUnfinalizedException() {
65 mDatabase.execSQL("CREATE TABLE " + TABLE_NAME + " (i int, j int);");
66 mDatabase.lock();
67 mDatabase.closePendingStatements(); // clears the above from finalizer queue in mdatabase
68 mDatabase.unlock();
69 ClassToTestSqlCompilationAndCaching.create(mDatabase, "select * from " + TABLE_NAME);
70 // since the above is NOT closed, closing database should fail
71 mDatabase.lock();
72 try {
73 mDatabase.closeDatabase();
74 fail("exception expected");
75 } catch (SQLiteUnfinalizedObjectsException e) {
76 // expected
77 } finally {
78 mDatabase.unlock();
79 }
80 }
81}