DatabaseErrorHandlerTest.java revision e7dea4ec2479002f5ee15bc6fc98d8c8acd123a6
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;
18
19import android.content.Context;
20import android.database.sqlite.SQLiteDatabase;
21import android.database.sqlite.SQLiteDiskIOException;
22import android.database.sqlite.SQLiteException;
23import android.test.AndroidTestCase;
24import android.test.suitebuilder.annotation.Suppress;
25import android.util.Log;
26
27import java.io.BufferedWriter;
28import java.io.File;
29import java.io.FileWriter;
30import java.io.IOException;
31
32public class DatabaseErrorHandlerTest extends AndroidTestCase {
33
34    private SQLiteDatabase mDatabase;
35    private File mDatabaseFile;
36    private static final String DB_NAME = "database_test.db";
37    private File dbDir;
38
39    @Override
40    protected void setUp() throws Exception {
41        super.setUp();
42        dbDir = getContext().getDir(this.getClass().getName(), Context.MODE_PRIVATE);
43        mDatabaseFile = new File(dbDir, DB_NAME);
44        if (mDatabaseFile.exists()) {
45            mDatabaseFile.delete();
46        }
47        mDatabase = SQLiteDatabase.openOrCreateDatabase(mDatabaseFile.getPath(), null,
48                new MyDatabaseCorruptionHandler());
49        assertNotNull(mDatabase);
50    }
51
52    @Override
53    protected void tearDown() throws Exception {
54        mDatabase.close();
55        mDatabaseFile.delete();
56        super.tearDown();
57    }
58
59    public void testNoCorruptionCase() {
60        new MyDatabaseCorruptionHandler().onCorruption(mDatabase);
61        // database file should still exist
62        assertTrue(mDatabaseFile.exists());
63    }
64
65
66    public void testDatabaseIsCorrupt() throws IOException {
67        mDatabase.execSQL("create table t (i int);");
68        // write junk into the database file
69        BufferedWriter writer = new BufferedWriter(new FileWriter(mDatabaseFile.getPath()));
70        writer.write("blah");
71        writer.close();
72        assertTrue(mDatabaseFile.exists());
73        // since the database file is now corrupt, doing any sql on this database connection
74        // should trigger call to MyDatabaseCorruptionHandler.onCorruption
75        try {
76            mDatabase.execSQL("select * from t;");
77            fail("expected exception");
78        } catch (SQLiteDiskIOException e) {
79            /**
80             * this test used to produce a corrupted db. but with new sqlite it instead reports
81             * Disk I/O error. meh..
82             * need to figure out how to cause corruption in db
83             */
84            // expected
85            if (mDatabaseFile.exists()) {
86                mDatabaseFile.delete();
87            }
88        } catch (SQLiteException e) {
89
90        }
91        // database file should be gone
92        assertFalse(mDatabaseFile.exists());
93        // after corruption handler is called, the database file should be free of
94        // database corruption
95        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(mDatabaseFile.getPath(), null,
96                new MyDatabaseCorruptionHandler());
97        assertTrue(db.isDatabaseIntegrityOk());
98    }
99
100    /**
101     * An example implementation of {@link DatabaseErrorHandler} to demonstrate
102     * database corruption handler which checks to make sure database is indeed
103     * corrupt before deleting the file.
104     */
105    public class MyDatabaseCorruptionHandler implements DatabaseErrorHandler {
106        public void onCorruption(SQLiteDatabase dbObj) {
107            boolean databaseOk = dbObj.isDatabaseIntegrityOk();
108            // close the database
109            try {
110                dbObj.close();
111            } catch (SQLiteException e) {
112                /* ignore */
113            }
114            if (databaseOk) {
115                // database is just fine. no need to delete the database file
116                Log.e("MyDatabaseCorruptionHandler", "no corruption in the database: " +
117                        mDatabaseFile.getPath());
118            } else {
119                // database is corrupt. delete the database file
120                Log.e("MyDatabaseCorruptionHandler", "deleting the database file: " +
121                        mDatabaseFile.getPath());
122                new File(dbDir, DB_NAME).delete();
123            }
124        }
125    }
126}