Fyodor Kupolov | ee90c03 | 2017-12-12 11:52:57 -0800 | [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.sqlite; |
| 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | import static org.junit.Assert.assertFalse; |
| 21 | import static org.junit.Assert.assertTrue; |
| 22 | |
| 23 | import android.content.Context; |
| 24 | import android.database.DatabaseUtils; |
| 25 | import android.support.test.InstrumentationRegistry; |
| 26 | import android.support.test.filters.SmallTest; |
| 27 | import android.support.test.runner.AndroidJUnit4; |
| 28 | |
| 29 | import org.junit.After; |
| 30 | import org.junit.Test; |
| 31 | import org.junit.runner.RunWith; |
| 32 | |
| 33 | import java.io.File; |
| 34 | |
| 35 | /** |
| 36 | * Tests for {@link SQLiteCompatibilityWalFlags} |
| 37 | */ |
| 38 | @RunWith(AndroidJUnit4.class) |
| 39 | @SmallTest |
| 40 | public class SQLiteCompatibilityWalFlagsTest { |
| 41 | |
| 42 | private SQLiteDatabase mDatabase; |
| 43 | |
| 44 | @After |
| 45 | public void tearDown() { |
| 46 | SQLiteCompatibilityWalFlags.reset(); |
| 47 | if (mDatabase != null) { |
| 48 | mDatabase.close(); |
| 49 | SQLiteDatabase.deleteDatabase(new File(mDatabase.getPath())); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | @Test |
| 54 | public void testParseConfig() { |
| 55 | SQLiteCompatibilityWalFlags.init(""); |
| 56 | assertFalse(SQLiteCompatibilityWalFlags.areFlagsSet()); |
| 57 | |
| 58 | SQLiteCompatibilityWalFlags.init(null); |
| 59 | assertFalse(SQLiteCompatibilityWalFlags.areFlagsSet()); |
| 60 | |
| 61 | SQLiteCompatibilityWalFlags.init("compatibility_wal_supported=false,wal_syncmode=OFF"); |
| 62 | assertTrue(SQLiteCompatibilityWalFlags.areFlagsSet()); |
| 63 | assertFalse(SQLiteCompatibilityWalFlags.isCompatibilityWalSupported()); |
| 64 | assertEquals("OFF", SQLiteCompatibilityWalFlags.getWALSyncMode()); |
Makoto Onuki | 0939c5a | 2018-08-24 13:38:40 -0700 | [diff] [blame] | 65 | assertEquals(-1, SQLiteCompatibilityWalFlags.getTruncateSize()); |
Fyodor Kupolov | ee90c03 | 2017-12-12 11:52:57 -0800 | [diff] [blame] | 66 | |
| 67 | SQLiteCompatibilityWalFlags.init("wal_syncmode=VALUE"); |
| 68 | assertTrue(SQLiteCompatibilityWalFlags.areFlagsSet()); |
| 69 | assertEquals(SQLiteGlobal.isCompatibilityWalSupported(), |
| 70 | SQLiteCompatibilityWalFlags.isCompatibilityWalSupported()); |
| 71 | assertEquals("VALUE", SQLiteCompatibilityWalFlags.getWALSyncMode()); |
Makoto Onuki | 0939c5a | 2018-08-24 13:38:40 -0700 | [diff] [blame] | 72 | assertEquals(-1, SQLiteCompatibilityWalFlags.getTruncateSize()); |
Fyodor Kupolov | ee90c03 | 2017-12-12 11:52:57 -0800 | [diff] [blame] | 73 | |
| 74 | SQLiteCompatibilityWalFlags.init("compatibility_wal_supported=true"); |
| 75 | assertTrue(SQLiteCompatibilityWalFlags.areFlagsSet()); |
| 76 | assertEquals(SQLiteGlobal.getWALSyncMode(), |
| 77 | SQLiteCompatibilityWalFlags.getWALSyncMode()); |
| 78 | assertTrue(SQLiteCompatibilityWalFlags.isCompatibilityWalSupported()); |
Makoto Onuki | 0939c5a | 2018-08-24 13:38:40 -0700 | [diff] [blame] | 79 | assertEquals(-1, SQLiteCompatibilityWalFlags.getTruncateSize()); |
| 80 | |
| 81 | SQLiteCompatibilityWalFlags.init("truncate_size=1024"); |
| 82 | assertEquals(1024, SQLiteCompatibilityWalFlags.getTruncateSize()); |
Fyodor Kupolov | ee90c03 | 2017-12-12 11:52:57 -0800 | [diff] [blame] | 83 | |
| 84 | SQLiteCompatibilityWalFlags.reset(); |
| 85 | SQLiteCompatibilityWalFlags.init("Invalid value"); |
| 86 | assertFalse(SQLiteCompatibilityWalFlags.areFlagsSet()); |
| 87 | } |
| 88 | |
| 89 | @Test |
| 90 | public void testApplyFlags() { |
| 91 | Context ctx = InstrumentationRegistry.getContext(); |
| 92 | |
| 93 | SQLiteCompatibilityWalFlags.init("compatibility_wal_supported=true,wal_syncmode=NORMAL"); |
| 94 | mDatabase = SQLiteDatabase |
| 95 | .openOrCreateDatabase(ctx.getDatabasePath("SQLiteCompatibilityWalFlagsTest"), null); |
| 96 | String journalMode = DatabaseUtils.stringForQuery(mDatabase, "PRAGMA journal_mode", null); |
| 97 | assertEquals("WAL", journalMode.toUpperCase()); |
| 98 | String syncMode = DatabaseUtils.stringForQuery(mDatabase, "PRAGMA synchronous", null); |
| 99 | assertEquals("Normal mode (1) is expected", "1", syncMode); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | } |