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