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
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());
65
66        SQLiteCompatibilityWalFlags.init("wal_syncmode=VALUE");
67        assertTrue(SQLiteCompatibilityWalFlags.areFlagsSet());
68        assertEquals(SQLiteGlobal.isCompatibilityWalSupported(),
69                SQLiteCompatibilityWalFlags.isCompatibilityWalSupported());
70        assertEquals("VALUE", SQLiteCompatibilityWalFlags.getWALSyncMode());
71
72        SQLiteCompatibilityWalFlags.init("compatibility_wal_supported=true");
73        assertTrue(SQLiteCompatibilityWalFlags.areFlagsSet());
74        assertEquals(SQLiteGlobal.getWALSyncMode(),
75                SQLiteCompatibilityWalFlags.getWALSyncMode());
76        assertTrue(SQLiteCompatibilityWalFlags.isCompatibilityWalSupported());
77
78        SQLiteCompatibilityWalFlags.reset();
79        SQLiteCompatibilityWalFlags.init("Invalid value");
80        assertFalse(SQLiteCompatibilityWalFlags.areFlagsSet());
81    }
82
83    @Test
84    public void testApplyFlags() {
85        Context ctx = InstrumentationRegistry.getContext();
86
87        SQLiteCompatibilityWalFlags.init("compatibility_wal_supported=true,wal_syncmode=NORMAL");
88        mDatabase = SQLiteDatabase
89                .openOrCreateDatabase(ctx.getDatabasePath("SQLiteCompatibilityWalFlagsTest"), null);
90        String journalMode = DatabaseUtils.stringForQuery(mDatabase, "PRAGMA journal_mode", null);
91        assertEquals("WAL", journalMode.toUpperCase());
92        String syncMode = DatabaseUtils.stringForQuery(mDatabase, "PRAGMA synchronous", null);
93        assertEquals("Normal mode (1) is expected", "1", syncMode);
94    }
95
96
97}
98