Fyodor Kupolov | d3b0c7e | 2017-06-20 11:51:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Fyodor Kupolov | 13a4b37 | 2017-11-07 18:45:35 -0800 | [diff] [blame] | 19 | import static org.junit.Assert.assertEquals; |
Fyodor Kupolov | d3b0c7e | 2017-06-20 11:51:55 -0700 | [diff] [blame] | 20 | import static org.junit.Assert.assertNotNull; |
| 21 | import static org.junit.Assert.assertTrue; |
| 22 | import static org.junit.Assert.fail; |
| 23 | |
| 24 | import android.content.Context; |
| 25 | import android.database.sqlite.SQLiteDatabase; |
| 26 | import android.database.sqlite.SQLiteDatabaseConfiguration; |
| 27 | import android.database.sqlite.SQLiteDebug; |
| 28 | import android.database.sqlite.SQLiteOpenHelper; |
Fyodor Kupolov | d3b0c7e | 2017-06-20 11:51:55 -0700 | [diff] [blame] | 29 | import android.util.Log; |
| 30 | |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 31 | import androidx.test.InstrumentationRegistry; |
| 32 | import androidx.test.filters.SmallTest; |
| 33 | import androidx.test.runner.AndroidJUnit4; |
| 34 | |
Fyodor Kupolov | d3b0c7e | 2017-06-20 11:51:55 -0700 | [diff] [blame] | 35 | import org.junit.After; |
| 36 | import org.junit.Before; |
| 37 | import org.junit.Test; |
| 38 | import org.junit.runner.RunWith; |
| 39 | |
| 40 | import java.util.ArrayList; |
| 41 | import java.util.List; |
| 42 | |
| 43 | /** |
| 44 | * Tests for {@link SQLiteOpenHelper} |
| 45 | * |
| 46 | * <p>Run with: bit FrameworksCoreTests:android.database.SQLiteOpenHelperTest |
| 47 | */ |
| 48 | @RunWith(AndroidJUnit4.class) |
| 49 | @SmallTest |
| 50 | public class SQLiteOpenHelperTest { |
| 51 | private static final String TAG = "SQLiteOpenHelperTest"; |
| 52 | |
| 53 | private TestHelper mTestHelper; |
| 54 | private Context mContext; |
| 55 | private List<SQLiteOpenHelper> mHelpersToClose; |
| 56 | |
| 57 | private static class TestHelper extends SQLiteOpenHelper { |
| 58 | TestHelper(Context context) { // In-memory |
| 59 | super(context, null, null, 1); |
| 60 | } |
| 61 | |
| 62 | TestHelper(Context context, String name) { |
| 63 | super(context, name, null, 1); |
| 64 | } |
| 65 | |
Fyodor Kupolov | 13a4b37 | 2017-11-07 18:45:35 -0800 | [diff] [blame] | 66 | TestHelper(Context context, String name, int version, SQLiteDatabase.OpenParams params) { |
| 67 | super(context, name, version, params); |
| 68 | } |
| 69 | |
Fyodor Kupolov | d3b0c7e | 2017-06-20 11:51:55 -0700 | [diff] [blame] | 70 | @Override |
| 71 | public void onCreate(SQLiteDatabase db) { |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | @Before |
| 80 | public void setup() { |
| 81 | mContext = InstrumentationRegistry.getContext(); |
| 82 | mTestHelper = new TestHelper(mContext, "openhelper_test"); |
| 83 | mHelpersToClose = new ArrayList<>(); |
| 84 | mHelpersToClose.add(mTestHelper); |
| 85 | } |
| 86 | |
| 87 | @After |
| 88 | public void teardown() { |
| 89 | for (SQLiteOpenHelper helper : mHelpersToClose) { |
| 90 | try { |
| 91 | helper.close(); |
| 92 | if (mTestHelper.getDatabaseName() != null) { |
| 93 | SQLiteDatabase.deleteDatabase( |
| 94 | mContext.getDatabasePath(mTestHelper.getDatabaseName())); |
| 95 | } |
| 96 | } catch (RuntimeException ex) { |
| 97 | Log.w(TAG, "Error occured when closing db helper " + helper, ex); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | @Test |
| 103 | public void testLookasideDefault() throws Exception { |
| 104 | assertNotNull(mTestHelper.getWritableDatabase()); |
| 105 | verifyLookasideStats(false); |
| 106 | } |
| 107 | |
| 108 | @Test |
| 109 | public void testLookasideDisabled() throws Exception { |
| 110 | mTestHelper.setLookasideConfig(0, 0); |
| 111 | assertNotNull(mTestHelper.getWritableDatabase()); |
| 112 | verifyLookasideStats(true); |
| 113 | } |
| 114 | |
| 115 | @Test |
| 116 | public void testInMemoryLookasideDisabled() throws Exception { |
| 117 | TestHelper memHelper = new TestHelper(mContext); |
| 118 | mHelpersToClose.add(memHelper); |
| 119 | memHelper.setLookasideConfig(0, 0); |
| 120 | assertNotNull(memHelper.getWritableDatabase()); |
| 121 | verifyLookasideStats(SQLiteDatabaseConfiguration.MEMORY_DB_PATH, true); |
| 122 | } |
| 123 | |
| 124 | @Test |
| 125 | public void testInMemoryLookasideDefault() throws Exception { |
| 126 | TestHelper memHelper = new TestHelper(mContext); |
| 127 | mHelpersToClose.add(memHelper); |
| 128 | assertNotNull(memHelper.getWritableDatabase()); |
| 129 | verifyLookasideStats(SQLiteDatabaseConfiguration.MEMORY_DB_PATH, false); |
| 130 | } |
| 131 | |
| 132 | @Test |
| 133 | public void testSetLookasideConfigValidation() { |
| 134 | try { |
| 135 | mTestHelper.setLookasideConfig(-1, 0); |
| 136 | fail("Negative slot size should be rejected"); |
| 137 | } catch (IllegalArgumentException expected) { |
| 138 | } |
| 139 | try { |
| 140 | mTestHelper.setLookasideConfig(0, -10); |
| 141 | fail("Negative slot count should be rejected"); |
| 142 | } catch (IllegalArgumentException expected) { |
| 143 | } |
| 144 | try { |
| 145 | mTestHelper.setLookasideConfig(1, 0); |
| 146 | fail("Illegal config should be rejected"); |
| 147 | } catch (IllegalArgumentException expected) { |
| 148 | } |
| 149 | try { |
| 150 | mTestHelper.setLookasideConfig(0, 1); |
| 151 | fail("Illegal config should be rejected"); |
| 152 | } catch (IllegalArgumentException expected) { |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | private void verifyLookasideStats(boolean expectDisabled) { |
| 157 | verifyLookasideStats(mTestHelper.getDatabaseName(), expectDisabled); |
| 158 | } |
| 159 | |
| 160 | private static void verifyLookasideStats(String dbName, boolean expectDisabled) { |
| 161 | boolean dbStatFound = false; |
| 162 | SQLiteDebug.PagerStats info = SQLiteDebug.getDatabaseInfo(); |
| 163 | for (SQLiteDebug.DbStats dbStat : info.dbStats) { |
| 164 | if (dbStat.dbName.endsWith(dbName)) { |
| 165 | dbStatFound = true; |
| 166 | Log.i(TAG, "Lookaside for " + dbStat.dbName + " " + dbStat.lookaside); |
| 167 | if (expectDisabled) { |
| 168 | assertTrue("lookaside slots count should be zero", dbStat.lookaside == 0); |
| 169 | } else { |
| 170 | assertTrue("lookaside slots count should be greater than zero", |
| 171 | dbStat.lookaside > 0); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | assertTrue("No dbstat found for " + dbName, dbStatFound); |
| 176 | } |
Fyodor Kupolov | 13a4b37 | 2017-11-07 18:45:35 -0800 | [diff] [blame] | 177 | |
| 178 | @Test |
| 179 | public void testOpenParamsConstructor() { |
| 180 | SQLiteDatabase.OpenParams params = new SQLiteDatabase.OpenParams.Builder() |
| 181 | .setJournalMode("DELETE") |
| 182 | .setSynchronousMode("OFF") |
| 183 | .build(); |
| 184 | |
| 185 | TestHelper helper = new TestHelper(mContext, "openhelper_test_constructor", 1, params); |
| 186 | mHelpersToClose.add(helper); |
| 187 | |
| 188 | String journalMode = DatabaseUtils |
| 189 | .stringForQuery(helper.getReadableDatabase(), "PRAGMA journal_mode", null); |
| 190 | |
| 191 | assertEquals("DELETE", journalMode.toUpperCase()); |
| 192 | String syncMode = DatabaseUtils |
| 193 | .stringForQuery(helper.getReadableDatabase(), "PRAGMA synchronous", null); |
| 194 | |
| 195 | assertEquals("0", syncMode); |
| 196 | } |
| 197 | |
Fyodor Kupolov | d3b0c7e | 2017-06-20 11:51:55 -0700 | [diff] [blame] | 198 | } |