SQLite Android Bindings
Check-in [40f79eca30]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add a new test that uses AndroidJUnit4. And related gradle changes.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 40f79eca30e59f72ad680a982a493c8ba08116f6
User & Date: dan 2017-05-02 19:54:22
Context
2017-05-03
18:18
Restore standard behaviours of (a) activating a connection pool in wal mode and (b) switching into wal mode automatically if the flag is set even if SQLITE_HAS_CODEC is defined (they were previously disabled in this case). Strip any URI parameters from the database name before it is included in any log messages. Always build with SQLITE_USE_URI=1 defined. check-in: e8a9b149f7 user: dan tags: trunk
2017-05-02
19:54
Add a new test that uses AndroidJUnit4. And related gradle changes. check-in: 40f79eca30 user: dan tags: trunk
2017-05-01
15:14
Define HAVE_USLEEP to avoid 1 second delays when sleep() is called check-in: efde9e0e44 user: pjw tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to gradle.properties.

    13     13   # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
    14     14   
    15     15   # When configured, Gradle will run in incubating parallel mode.
    16     16   # This option should only be used with decoupled projects. More details, visit
    17     17   # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
    18     18   # org.gradle.parallel=true
    19     19   
    20         -android.useDeprecatedNdk true

Changes to sqlite3/build.gradle.

     1      1   apply plugin: 'com.android.library'
     2      2   
     3      3   android {
     4      4       compileSdkVersion 25
     5      5       buildToolsVersion "25.0.2"
     6      6   
     7         -    publishNonDefault true
     8      7   
     9      8       defaultConfig {
    10      9           minSdkVersion 16
    11     10           targetSdkVersion 25
    12     11           versionCode 1
    13     12           versionName "1.0"
    14         -    }
    15         -
    16         -    configurations {
    17         -        debug
    18         -        release
           13  +        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    19     14       }
    20     15   
    21     16       buildTypes {
    22     17           release {
    23     18               minifyEnabled false
    24     19               proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    25     20           }
    26     21           debug {
    27     22               minifyEnabled false
    28     23               debuggable true
    29     24               jniDebuggable true
    30         -            externalNativeBuild {
    31         -                ndkBuild {
    32         -                    arguments "NDK_DEBUG:=1"
    33         -                    abiFilters "armeabi-v7a", "armeabi", "x86"
    34         -                }
           25  +            ndk {
           26  +                // Building with NDK_DEBUG=1 for mips crashes the compiler in ndk 14.
           27  +                abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64', 'mips64' // 'mips'
    35     28               }
           29  +
    36     30           }
    37     31       }
    38     32   
    39     33       //sourceSets.main.jni.srcDirs = [] //disable automatic ndk-build call
    40         -
    41     34       externalNativeBuild {
    42     35           ndkBuild {
    43     36               path 'src/main/jni/Android.mk'
    44     37           }
    45     38       }
    46     39   }
    47     40   
    48     41   dependencies {
    49     42       compile fileTree(include: ['*.jar'], dir: 'libs')
           43  +    androidTestCompile 'com.android.support:support-annotations:24.0.0'
           44  +    androidTestCompile 'com.android.support.test:runner:0.5'
           45  +    androidTestCompile 'com.android.support.test:rules:0.5'
    50     46       testCompile 'junit:junit:4.12'
    51     47   }

Added sqlite3/src/androidTest/java/org/sqlite/database/SeeTest1.java.

            1  +package org.sqlite.database;
            2  +
            3  +
            4  +import android.content.Context;
            5  +import android.database.Cursor;
            6  +import android.os.AsyncTask;
            7  +import android.support.test.InstrumentationRegistry;
            8  +import android.support.test.runner.AndroidJUnit4;
            9  +
           10  +import junit.framework.Assert;
           11  +
           12  +import org.junit.Before;
           13  +import org.junit.Test;
           14  +import org.junit.runner.RunWith;
           15  +
           16  +import org.sqlite.database.sqlite.SQLiteDatabase;
           17  +import org.sqlite.database.sqlite.SQLiteOpenHelper;
           18  +
           19  +import java.io.File;
           20  +
           21  +import static org.junit.Assert.*;
           22  +
           23  +
           24  +class MyHelper extends SQLiteOpenHelper {
           25  +
           26  +    public static final String DATABASE_NAME = "mydb.db";
           27  +
           28  +    public MyHelper(Context ctx){
           29  +        super(ctx, ctx.getDatabasePath(DATABASE_NAME).getAbsolutePath(), null, 1);
           30  +    }
           31  +    public void onConfigure(SQLiteDatabase db){
           32  +        db.execSQL("PRAGMA key = 'secret'");
           33  +
           34  +        db.enableWriteAheadLogging();
           35  +
           36  +        final Cursor pragmaCursor = db.rawQuery("PRAGMA journal_mode = WAL", null);
           37  +        pragmaCursor.moveToFirst();
           38  +        pragmaCursor.close();
           39  +    }
           40  +    public void onCreate(SQLiteDatabase db){
           41  +        db.execSQL("CREATE TABLE t1(x)");
           42  +    }
           43  +    public void onUpgrade(SQLiteDatabase db, int iOld, int iNew){
           44  +    }
           45  +}
           46  +
           47  +
           48  +/**
           49  + * Created by dan on 5/3/17.
           50  + */
           51  +@RunWith(AndroidJUnit4.class)
           52  +public class SeeTest1 {
           53  +        private Context mContext;
           54  +
           55  +    @Before
           56  +    public void setup() throws Exception {
           57  +
           58  +        System.loadLibrary("sqliteX");
           59  +
           60  +        mContext = InstrumentationRegistry.getTargetContext();
           61  +
           62  +        // delete any existing database
           63  +        File databaseFile = mContext.getDatabasePath(MyHelper.DATABASE_NAME);
           64  +        databaseFile.mkdirs();
           65  +        if (databaseFile.exists()) {
           66  +            databaseFile.delete();
           67  +        }
           68  +    }
           69  +
           70  +    @Test
           71  +    public void testAndroidDefaultWalMode() throws Exception {
           72  +        // create database
           73  +        final MyHelper helper = new MyHelper(mContext);
           74  +        helper.getWritableDatabase();
           75  +
           76  +        // verify that WAL journal mode is set
           77  +        final Cursor pragmaCursor = helper.getWritableDatabase().rawQuery("PRAGMA journal_mode", null);
           78  +        pragmaCursor.moveToFirst();
           79  +        Assert.assertEquals(pragmaCursor.getString(pragmaCursor.getColumnIndex("journal_mode")), "wal");
           80  +        pragmaCursor.close();
           81  +
           82  +        // start long running transaction
           83  +        AsyncTask.execute(new Runnable() {
           84  +            @Override
           85  +            public void run() {
           86  +                helper.getWritableDatabase().beginTransactionNonExclusive();
           87  +
           88  +                // simulate long insert
           89  +                try {
           90  +                    Thread.sleep(5000);
           91  +                } catch (InterruptedException e) {
           92  +                    e.printStackTrace();
           93  +                }
           94  +
           95  +                helper.getWritableDatabase().setTransactionSuccessful();
           96  +                helper.getWritableDatabase().endTransaction();
           97  +            }
           98  +        });
           99  +
          100  +        // wait a short time until the long transaction starts
          101  +        Thread.sleep(300);
          102  +
          103  +        long startTime = System.currentTimeMillis();
          104  +
          105  +        //try to read something from the database while the slow transaction is running
          106  +        helper.getWritableDatabase().execSQL("SELECT * FROM t1");
          107  +
          108  +        //verify that the operation didn't wait until the 3000ms long operation finished
          109  +        if (System.currentTimeMillis() - startTime > 3000) {
          110  +            throw new Exception("WAL mode isn't working corectly - read operation was blocked");
          111  +        }
          112  +    }
          113  +}