1package com.xtremelabs.robolectric.shadows;
2
3
4import android.content.Context;
5import android.database.sqlite.SQLiteDatabase;
6import android.database.sqlite.SQLiteDatabase.CursorFactory;
7import android.database.sqlite.SQLiteOpenHelper;
8import com.xtremelabs.robolectric.WithTestDefaultsRunner;
9import org.junit.Before;
10import org.junit.Test;
11import org.junit.runner.RunWith;
12
13import static org.hamcrest.CoreMatchers.equalTo;
14import static org.hamcrest.CoreMatchers.notNullValue;
15import static org.hamcrest.CoreMatchers.sameInstance;
16import static org.junit.Assert.assertThat;
17
18@RunWith(WithTestDefaultsRunner.class)
19public class SQLiteOpenHelperTest {
20
21    private TestOpenHelper helper;
22
23    @Before
24    public void setUp() throws Exception {
25        helper = new TestOpenHelper(null, "path", null, 1);
26    }
27
28    @Test
29    public void testInitialGetReadableDatabase() throws Exception {
30        SQLiteDatabase database = helper.getReadableDatabase();
31        assertInitialDB(database);
32    }
33
34    @Test
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
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
52    public void testInitialGetWritableDatabase() throws Exception {
53        SQLiteDatabase database = helper.getWritableDatabase();
54        assertInitialDB(database);
55    }
56
57    @Test
58    public void testSubsequentGetWritableDatabase() throws Exception {
59        helper.getWritableDatabase();
60        helper.reset();
61
62        assertSubsequentDB(helper.getWritableDatabase());
63    }
64
65    @Test
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
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
82    private void assertInitialDB(SQLiteDatabase database) {
83        assertDatabaseOpened(database);
84        assertThat(helper.onCreateCalled, equalTo(true));
85    }
86
87    private void assertSubsequentDB(SQLiteDatabase database) {
88        assertDatabaseOpened(database);
89        assertThat(helper.onCreateCalled, equalTo(false));
90    }
91
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
105        public TestOpenHelper(Context context, String name,
106                              CursorFactory factory, int version) {
107            super(context, name, factory, version);
108            reset();
109        }
110
111        @Override
112        public void onCreate(SQLiteDatabase database) {
113            onCreateCalled = true;
114        }
115
116        @Override
117        public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
118            onUpgradeCalled = true;
119        }
120
121        @Override
122        public void onOpen(SQLiteDatabase database) {
123            onOpenCalled = true;
124        }
125
126        public void reset() {
127            onCreateCalled = false;
128            onUpgradeCalled = false;
129            onOpenCalled = false;
130        }
131    }
132}
133