Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Merge "Call getWrappedDb in DatabaseErrorHandler."
Browse files Browse the repository at this point in the history
  • Loading branch information
TreeHugger Robot authored and Android (Google) Code Review committed Jul 18, 2018
2 parents c6c2ede + e8dbe09 commit ea1f61a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
Expand Up @@ -80,10 +80,7 @@ static class OpenHelper extends SQLiteOpenHelper {
new DatabaseErrorHandler() {
@Override
public void onCorruption(SQLiteDatabase dbObj) {
FrameworkSQLiteDatabase db = dbRef[0];
if (db != null) {
callback.onCorruption(db);
}
callback.onCorruption(getWrappedDb(dbRef, dbObj));
}
});
mCallback = callback;
Expand Down Expand Up @@ -113,12 +110,7 @@ synchronized SupportSQLiteDatabase getReadableSupportDatabase() {
}

FrameworkSQLiteDatabase getWrappedDb(SQLiteDatabase sqLiteDatabase) {
FrameworkSQLiteDatabase dbRef = mDbRef[0];
if (dbRef == null || !dbRef.isDelegate(sqLiteDatabase)) {
dbRef = new FrameworkSQLiteDatabase(sqLiteDatabase);
mDbRef[0] = dbRef;
}
return mDbRef[0];
return getWrappedDb(mDbRef, sqLiteDatabase);
}

@Override
Expand Down Expand Up @@ -156,5 +148,14 @@ public synchronized void close() {
super.close();
mDbRef[0] = null;
}

static FrameworkSQLiteDatabase getWrappedDb(FrameworkSQLiteDatabase[] refHolder,
SQLiteDatabase sqLiteDatabase) {
FrameworkSQLiteDatabase dbRef = refHolder[0];
if (dbRef == null || !dbRef.isDelegate(sqLiteDatabase)) {
refHolder[0] = new FrameworkSQLiteDatabase(sqLiteDatabase);
}
return refHolder[0];
}
}
}
Expand Up @@ -43,6 +43,9 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -148,6 +151,38 @@ public void onCreate(@NonNull SupportSQLiteDatabase db) {
assertThat(ids, is(empty()));
}

@Test
@MediumTest
public void corruptExceptionOnCreate() throws IOException {
Context context = InstrumentationRegistry.getTargetContext();

TestDatabaseCallback callback = new TestDatabaseCallback();

// Create fake DB files that will cause a SQLiteDatabaseCorruptException: SQLITE_NOTADB.
String[] dbFiles = new String[] {"corrupted", "corrupted-shm", "corrupted-wal"};
for (String fileName : dbFiles) {
File dbFile = context.getDatabasePath(fileName);
try (FileWriter fileWriter = new FileWriter(dbFile)) {
fileWriter.write(new char[]{'p', 'o', 'i', 's', 'o', 'n'});
}
}

TestDatabase db = Room.databaseBuilder(context, TestDatabase.class, "corrupted")
.addCallback(callback)
.build();

assertFalse(callback.mCreated);
assertFalse(callback.mOpened);

// Should not throw a SQLiteDatabaseCorruptException, i.e. default onCorruption() was
// executed and DB file was re-created.
List<Integer> ids = db.getUserDao().loadIds();
assertThat(ids, is(empty()));

assertTrue(callback.mCreated);
assertTrue(callback.mOpened);
}

public static class TestDatabaseCallback extends RoomDatabase.Callback {

boolean mCreated;
Expand Down

0 comments on commit ea1f61a

Please sign in to comment.