1 package com.xtremelabs.robolectric.shadows; 2 3 4 import android.content.Context; 5 import android.database.sqlite.SQLiteDatabase; 6 import android.database.sqlite.SQLiteDatabase.CursorFactory; 7 import android.database.sqlite.SQLiteOpenHelper; 8 import com.xtremelabs.robolectric.WithTestDefaultsRunner; 9 import org.junit.Before; 10 import org.junit.Test; 11 import org.junit.runner.RunWith; 12 13 import static org.hamcrest.CoreMatchers.equalTo; 14 import static org.hamcrest.CoreMatchers.notNullValue; 15 import static org.hamcrest.CoreMatchers.sameInstance; 16 import static org.junit.Assert.assertThat; 17 18 @RunWith(WithTestDefaultsRunner.class) 19 public class SQLiteOpenHelperTest { 20 21 private TestOpenHelper helper; 22 23 @Before setUp()24 public void setUp() throws Exception { 25 helper = new TestOpenHelper(null, "path", null, 1); 26 } 27 28 @Test testInitialGetReadableDatabase()29 public void testInitialGetReadableDatabase() throws Exception { 30 SQLiteDatabase database = helper.getReadableDatabase(); 31 assertInitialDB(database); 32 } 33 34 @Test testSubsequentGetReadableDatabase()35 public void testSubsequentGetReadableDatabase() throws Exception { 36 SQLiteDatabase database = helper.getReadableDatabase(); 37 helper.reset(); 38 database = helper.getReadableDatabase(); 39 40 assertSubsequentDB(database); 41 } 42 43 @Test testSameDBInstanceSubsequentGetReadableDatabase()44 public void testSameDBInstanceSubsequentGetReadableDatabase() throws Exception { 45 SQLiteDatabase db1 = helper.getReadableDatabase(); 46 SQLiteDatabase db2 = helper.getReadableDatabase(); 47 48 assertThat(db1, sameInstance(db2)); 49 } 50 51 @Test testInitialGetWritableDatabase()52 public void testInitialGetWritableDatabase() throws Exception { 53 SQLiteDatabase database = helper.getWritableDatabase(); 54 assertInitialDB(database); 55 } 56 57 @Test testSubsequentGetWritableDatabase()58 public void testSubsequentGetWritableDatabase() throws Exception { 59 helper.getWritableDatabase(); 60 helper.reset(); 61 62 assertSubsequentDB(helper.getWritableDatabase()); 63 } 64 65 @Test testSameDBInstanceSubsequentGetWritableDatabase()66 public void testSameDBInstanceSubsequentGetWritableDatabase() throws Exception { 67 SQLiteDatabase db1 = helper.getWritableDatabase(); 68 SQLiteDatabase db2 = helper.getWritableDatabase(); 69 70 assertThat(db1, sameInstance(db2)); 71 } 72 73 @Test testClose()74 public void testClose() throws Exception { 75 SQLiteDatabase database = helper.getWritableDatabase(); 76 77 assertThat(database.isOpen(), equalTo(true)); 78 helper.close(); 79 assertThat(database.isOpen(), equalTo(false)); 80 } 81 assertInitialDB(SQLiteDatabase database)82 private void assertInitialDB(SQLiteDatabase database) { 83 assertDatabaseOpened(database); 84 assertThat(helper.onCreateCalled, equalTo(true)); 85 } 86 assertSubsequentDB(SQLiteDatabase database)87 private void assertSubsequentDB(SQLiteDatabase database) { 88 assertDatabaseOpened(database); 89 assertThat(helper.onCreateCalled, equalTo(false)); 90 } 91 assertDatabaseOpened(SQLiteDatabase database)92 private void assertDatabaseOpened(SQLiteDatabase database) { 93 assertThat(database, notNullValue()); 94 assertThat(database.isOpen(), equalTo(true)); 95 assertThat(helper.onOpenCalled, equalTo(true)); 96 assertThat(helper.onUpgradeCalled, equalTo(false)); 97 } 98 99 private class TestOpenHelper extends SQLiteOpenHelper { 100 101 public boolean onCreateCalled; 102 public boolean onUpgradeCalled; 103 public boolean onOpenCalled; 104 TestOpenHelper(Context context, String name, CursorFactory factory, int version)105 public TestOpenHelper(Context context, String name, 106 CursorFactory factory, int version) { 107 super(context, name, factory, version); 108 reset(); 109 } 110 111 @Override onCreate(SQLiteDatabase database)112 public void onCreate(SQLiteDatabase database) { 113 onCreateCalled = true; 114 } 115 116 @Override onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion)117 public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) { 118 onUpgradeCalled = true; 119 } 120 121 @Override onOpen(SQLiteDatabase database)122 public void onOpen(SQLiteDatabase database) { 123 onOpenCalled = true; 124 } 125 reset()126 public void reset() { 127 onCreateCalled = false; 128 onUpgradeCalled = false; 129 onOpenCalled = false; 130 } 131 } 132 } 133