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