blob: 551a58ed7cb53cc6253a32a048e4e7ccd4d873fe [file] [log] [blame]
Fyodor Kupolovee90c032017-12-12 11:52:57 -08001/*
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
17package android.database.sqlite;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertFalse;
21import static org.junit.Assert.assertTrue;
22
23import android.content.Context;
24import android.database.DatabaseUtils;
25import android.support.test.InstrumentationRegistry;
26import android.support.test.filters.SmallTest;
27import android.support.test.runner.AndroidJUnit4;
28
29import org.junit.After;
30import org.junit.Test;
31import org.junit.runner.RunWith;
32
33import java.io.File;
34
35/**
36 * Tests for {@link SQLiteCompatibilityWalFlags}
37 */
38@RunWith(AndroidJUnit4.class)
39@SmallTest
40public 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 Onuki0939c5a2018-08-24 13:38:40 -070065 assertEquals(-1, SQLiteCompatibilityWalFlags.getTruncateSize());
Fyodor Kupolovee90c032017-12-12 11:52:57 -080066
67 SQLiteCompatibilityWalFlags.init("wal_syncmode=VALUE");
68 assertTrue(SQLiteCompatibilityWalFlags.areFlagsSet());
69 assertEquals(SQLiteGlobal.isCompatibilityWalSupported(),
70 SQLiteCompatibilityWalFlags.isCompatibilityWalSupported());
71 assertEquals("VALUE", SQLiteCompatibilityWalFlags.getWALSyncMode());
Makoto Onuki0939c5a2018-08-24 13:38:40 -070072 assertEquals(-1, SQLiteCompatibilityWalFlags.getTruncateSize());
Fyodor Kupolovee90c032017-12-12 11:52:57 -080073
74 SQLiteCompatibilityWalFlags.init("compatibility_wal_supported=true");
75 assertTrue(SQLiteCompatibilityWalFlags.areFlagsSet());
76 assertEquals(SQLiteGlobal.getWALSyncMode(),
77 SQLiteCompatibilityWalFlags.getWALSyncMode());
78 assertTrue(SQLiteCompatibilityWalFlags.isCompatibilityWalSupported());
Makoto Onuki0939c5a2018-08-24 13:38:40 -070079 assertEquals(-1, SQLiteCompatibilityWalFlags.getTruncateSize());
80
81 SQLiteCompatibilityWalFlags.init("truncate_size=1024");
82 assertEquals(1024, SQLiteCompatibilityWalFlags.getTruncateSize());
Fyodor Kupolovee90c032017-12-12 11:52:57 -080083
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}