Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Upgrade this branch to use the enhanced test program from the api-level-9 branch. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
7275688aed9541c3737a1386cfa4ddb9 |
User & Date: | dan 2017-04-27 18:00:41.995 |
Context
2017-04-29
| ||
09:24 | Update the installation instructions to reflect the new support for API level 9. And that building the native libraries is now integrated with gradle. (check-in: 985ad0e6f2 user: dan tags: trunk) | |
2017-04-27
| ||
18:00 | Upgrade this branch to use the enhanced test program from the api-level-9 branch. (check-in: 7275688aed user: dan tags: trunk) | |
17:50 | Upgrade this branch to use the build.gradle from the api-level-9 branch. To support building both the libraries and java classes in a single step (or from within Android Studio). (check-in: 3a63f88d36 user: dan tags: trunk) | |
Changes
Changes to sqlite3test/src/main/java/org/sqlite/customsqlitetest/MainActivity.java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package org.sqlite.customsqlitetest; import android.content.Context; import android.database.Cursor; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.TextView; import org.sqlite.database.DatabaseErrorHandler; import org.sqlite.database.sqlite.SQLiteDatabase; import org.sqlite.database.sqlite.SQLiteStatement; import org.sqlite.database.sqlite.SQLiteDatabaseCorruptException; import org.sqlite.database.sqlite.SQLiteOpenHelper; import java.io.File; | > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package org.sqlite.customsqlitetest; import android.content.Context; import android.database.Cursor; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.TextView; import org.json.JSONObject; import org.sqlite.database.DatabaseErrorHandler; import org.sqlite.database.sqlite.SQLiteDatabase; import org.sqlite.database.sqlite.SQLiteStatement; import org.sqlite.database.sqlite.SQLiteDatabaseCorruptException; import org.sqlite.database.sqlite.SQLiteOpenHelper; import java.io.File; |
︙ | ︙ | |||
47 48 49 50 51 52 53 54 55 56 57 58 59 | String res; db = SQLiteDatabase.openOrCreateDatabase(":memory:", null); st = db.compileStatement("SELECT sqlite_version()"); res = st.simpleQueryForString(); myTV.append("SQLite version " + res + "\n\n"); } public void test_warning(String name, String warning){ myTV.append("WARNING:" + name + ": " + warning + "\n"); } | > | > | | 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | String res; db = SQLiteDatabase.openOrCreateDatabase(":memory:", null); st = db.compileStatement("SELECT sqlite_version()"); res = st.simpleQueryForString(); myTV.append("SQLite version " + res + "\n\n"); db.close(); } public void test_warning(String name, String warning){ myTV.append("WARNING:" + name + ": " + warning + "\n"); } public void test_result(String name, String res, String expected, long t0){ long tot = (System.nanoTime() - t0) / 1000000; myTV.append(name + "... "); myNTest++; if( res.equals(expected) ){ myTV.append("ok (" + tot + "ms)\n"); } else { myNErr++; myTV.append("FAILED\n"); myTV.append(" res= \"" + res + "\"\n"); myTV.append(" expected=\"" + expected + "\"\n"); } } |
︙ | ︙ | |||
102 103 104 105 106 107 108 109 110 | String db_path2 = DB_PATH.toString() + "2"; db.execSQL("CREATE TABLE t1(x, y)"); db.execSQL("INSERT INTO t1 VALUES (1, 2), (3, 4)"); Thread t = new Thread( new Runnable() { public void run() { SQLiteStatement st = db.compileStatement("SELECT sum(x+y) FROM t1"); String res = st.simpleQueryForString(); | > | > > | 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | String db_path2 = DB_PATH.toString() + "2"; db.execSQL("CREATE TABLE t1(x, y)"); db.execSQL("INSERT INTO t1 VALUES (1, 2), (3, 4)"); Thread t = new Thread( new Runnable() { public void run() { final long t0 = System.nanoTime(); SQLiteStatement st = db.compileStatement("SELECT sum(x+y) FROM t1"); String res = st.simpleQueryForString(); test_result("thread_test_1", res, "10", t0); } }); t.start(); try { t.join(); } catch (InterruptedException e) { } db.close(); } /* ** Test that a database connection may be accessed from a second thread. */ public void thread_test_2(){ final long t0 = System.nanoTime(); SQLiteDatabase.deleteDatabase(DB_PATH); final SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH, null); db.execSQL("CREATE TABLE t1(x, y)"); db.execSQL("INSERT INTO t1 VALUES (1, 2), (3, 4)"); db.enableWriteAheadLogging(); |
︙ | ︙ | |||
148 149 150 151 152 153 154 | try { Thread.sleep(100); } catch(InterruptedException e) {} } if( t.isAlive() ){ res = "blocked"; } db.endTransaction(); try { t.join(); } catch(InterruptedException e) {} if( SQLiteDatabase.hasCodec() ){ | | | > > | 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | try { Thread.sleep(100); } catch(InterruptedException e) {} } if( t.isAlive() ){ res = "blocked"; } db.endTransaction(); try { t.join(); } catch(InterruptedException e) {} if( SQLiteDatabase.hasCodec() ){ test_result("thread_test_2", res, "blocked", t0); } else { test_result("thread_test_2", res, "concurrent", t0); } db.close(); } /* ** Use a Cursor to loop through the results of a SELECT query. */ public void csr_test_2() throws Exception { final long t0 = System.nanoTime(); SQLiteDatabase.deleteDatabase(DB_PATH); SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH, null); String res = ""; String expect = ""; int i; int nRow = 0; |
︙ | ︙ | |||
182 183 184 185 186 187 188 | for(bRes=c.moveToFirst(); bRes; bRes=c.moveToNext()){ String x = c.getString(0); res = res + "." + x; } }else{ test_warning("csr_test_1", "c==NULL"); } | | > | > | > | > | > | > | > | > | > | > | > | > | > | > | > | > | > > > > > > > > > > > > > > > > > > > | | > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 | for(bRes=c.moveToFirst(); bRes; bRes=c.moveToNext()){ String x = c.getString(0); res = res + "." + x; } }else{ test_warning("csr_test_1", "c==NULL"); } test_result("csr_test_2.1", res, expect, t0); final long t1 = System.nanoTime(); db.execSQL("BEGIN"); for(i=0; i<1000; i++){ db.execSQL("INSERT INTO t1 VALUES (X'123456'), (X'789ABC'), (X'DEF012')"); db.execSQL("INSERT INTO t1 VALUES (45), (46), (47)"); db.execSQL("INSERT INTO t1 VALUES (8.1), (8.2), (8.3)"); db.execSQL("INSERT INTO t1 VALUES (NULL), (NULL), (NULL)"); } db.execSQL("COMMIT"); c = db.rawQuery("SELECT x FROM t1", null); if( c!=null ){ boolean bRes; for(bRes=c.moveToFirst(); bRes; bRes=c.moveToNext()) nRow++; }else{ test_warning("csr_test_1", "c==NULL"); } test_result("csr_test_2.2", "" + nRow, "15000", t1); db.close(); } public String string_from_t1_x(SQLiteDatabase db){ String res = ""; Cursor c = db.rawQuery("SELECT x FROM t1", null); boolean bRes; for(bRes=c.moveToFirst(); bRes; bRes=c.moveToNext()){ String x = c.getString(0); res = res + "." + x; } return res; } public void csr_test_1() throws Exception { final long t0 = System.nanoTime(); SQLiteDatabase.deleteDatabase(DB_PATH); SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH, null); String res = ""; db.execSQL("CREATE TABLE t1(x)"); db.execSQL("INSERT INTO t1 VALUES ('one'), ('two'), ('three')"); res = string_from_t1_x(db); test_result("csr_test_1.1", res, ".one.two.three", t0); final long t1 = System.nanoTime(); db.close(); test_result("csr_test_1.2", db_is_encrypted(), "unencrypted", t1); } public void stmt_jrnl_test_1() throws Exception { final long t0 = System.nanoTime(); SQLiteDatabase.deleteDatabase(DB_PATH); SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH, null); String res = ""; db.execSQL("CREATE TABLE t1(x, y UNIQUE)"); db.execSQL("BEGIN"); db.execSQL("INSERT INTO t1 VALUES(1, 1), (2, 2), (3, 3)"); db.execSQL("UPDATE t1 SET y=y+3"); db.execSQL("COMMIT"); db.close(); test_result("stmt_jrnl_test_1.1", "did not crash", "did not crash", t0); } public void supp_char_test_1() throws Exception { final long t0 = System.nanoTime(); SQLiteDatabase.deleteDatabase(DB_PATH); SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH, null); String res = ""; String smiley = new String( Character.toChars(0x10000) ); db.execSQL("CREATE TABLE t1(x)"); db.execSQL("INSERT INTO t1 VALUES ('a" + smiley + "b')"); res = string_from_t1_x(db); test_result("supp_char_test1." + smiley, res, ".a" + smiley + "b", t0); db.close(); } /* ** If this is a SEE build, check that encrypted databases work. */ public void see_test_1() throws Exception { final long t0 = System.nanoTime(); if( !SQLiteDatabase.hasCodec() ) return; SQLiteDatabase.deleteDatabase(DB_PATH); String res = ""; SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH, null); db.execSQL("PRAGMA key = 'secretkey'"); db.execSQL("CREATE TABLE t1(x)"); db.execSQL("INSERT INTO t1 VALUES ('one'), ('two'), ('three')"); res = string_from_t1_x(db); test_result("see_test_1.1", res, ".one.two.three", t0); final long t1 = System.nanoTime(); db.close(); test_result("see_test_1.2", db_is_encrypted(), "encrypted", t1); final long t2 = System.nanoTime(); db = SQLiteDatabase.openOrCreateDatabase(DB_PATH, null); db.execSQL("PRAGMA key = 'secretkey'"); res = string_from_t1_x(db); test_result("see_test_1.3", res, ".one.two.three", t2); final long t3 = System.nanoTime(); db.close(); res = "unencrypted"; try { db = SQLiteDatabase.openOrCreateDatabase(DB_PATH.getPath(), null); string_from_t1_x(db); } catch ( SQLiteDatabaseCorruptException e ){ res = "encrypted"; } finally { db.close(); } test_result("see_test_1.4", res, "encrypted", t3); final long t4 = System.nanoTime(); res = "unencrypted"; try { db = SQLiteDatabase.openOrCreateDatabase(DB_PATH.getPath(), null); db.execSQL("PRAGMA key = 'otherkey'"); string_from_t1_x(db); } catch ( SQLiteDatabaseCorruptException e ){ res = "encrypted"; } finally { db.close(); } test_result("see_test_1.5", res, "encrypted", t4); } class MyHelper extends SQLiteOpenHelper { public MyHelper(Context ctx){ super(ctx, DB_PATH.getPath(), null, 1); } public void onConfigure(SQLiteDatabase db){ db.execSQL("PRAGMA key = 'secret'"); } public void onCreate(SQLiteDatabase db){ db.execSQL("CREATE TABLE t1(x)"); } public void onUpgrade(SQLiteDatabase db, int iOld, int iNew){ } } /* ** Check that SQLiteOpenHelper works. */ public void helper_test_1() throws Exception { final long t0 = System.nanoTime(); SQLiteDatabase.deleteDatabase(DB_PATH); MyHelper helper = new MyHelper(this); SQLiteDatabase db = helper.getWritableDatabase(); db.execSQL("INSERT INTO t1 VALUES ('x'), ('y'), ('z')"); String res = string_from_t1_x(db); test_result("helper.1", res, ".x.y.z", t0); helper.close(); } /* ** If this is a SEE build, check that SQLiteOpenHelper still works. */ public void see_test_2() throws Exception { final long t0 = System.nanoTime(); if( !SQLiteDatabase.hasCodec() ) return; SQLiteDatabase.deleteDatabase(DB_PATH); MyHelper helper = new MyHelper(this); SQLiteDatabase db = helper.getWritableDatabase(); db.execSQL("INSERT INTO t1 VALUES ('x'), ('y'), ('z')"); String res = string_from_t1_x(db); test_result("see_test_2.1", res, ".x.y.z", t0); final long t1 = System.nanoTime(); test_result("see_test_2.2", db_is_encrypted(), "encrypted", t1); final long t2 = System.nanoTime(); helper.close(); helper = new MyHelper(this); db = helper.getReadableDatabase(); test_result("see_test_2.3", res, ".x.y.z", t2); final long t3 = System.nanoTime(); db = helper.getWritableDatabase(); test_result("see_test_2.4", res, ".x.y.z", t3); final long t4 = System.nanoTime(); test_result("see_test_2.5", db_is_encrypted(), "encrypted", t4); db.close(); } private static boolean mLibIsLoaded = false; private static void loadLibrary() { if (!mLibIsLoaded) { System.loadLibrary("sqliteX"); mLibIsLoaded = true; } } public void run_the_tests(View view){ myTV.setText(""); view.post(new Runnable() { @Override public void run() { run_the_tests_really(); } }); } public void run_the_tests_really(){ loadLibrary(); DB_PATH = getApplicationContext().getDatabasePath("test.db"); DB_PATH.mkdirs(); myTV.setText(""); myNErr = 0; myNTest = 0; try { report_version(); helper_test_1(); supp_char_test_1(); csr_test_1(); csr_test_2(); thread_test_1(); thread_test_2(); see_test_1(); see_test_2(); stmt_jrnl_test_1(); json_test_1(); myTV.append("\n" + myNErr + " errors from " + myNTest + " tests\n"); } catch(Exception e) { myTV.append("Exception: " + e.toString() + "\n"); myTV.append(android.util.Log.getStackTraceString(e) + "\n"); } } public void json_test_1() throws Exception { SQLiteDatabase.deleteDatabase(DB_PATH); SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH, null); final long t0 = System.nanoTime(); db.beginTransaction(); String res = ""; db.execSQL("CREATE TABLE t1(x, y)"); JSONObject json = new JSONObject(); json.put("Foo", 1); json.put("Bar", "Gum"); db.execSQL("INSERT INTO t1 VALUES (json('" + json.toString() + "'), 1)"); final String r1 = json.toString(); json.put("Foo", 2); json.put("Bar", "Goo"); final String r2 = json.toString(); db.execSQL("INSERT INTO t1 VALUES (json('" + json.toString() + "'), 2)"); json.put("Foo", 11); json.put("Bar", "Zoo"); db.execSQL("INSERT INTO t1 VALUES (json('" + json.toString() + "'), 11)"); SQLiteStatement s = db.compileStatement("Select json_extract(x, '$.Foo') from t1 where y = 1"); res = s.simpleQueryForString(); db.setTransactionSuccessful(); db.endTransaction(); test_result("json_test_1.1", res, "1", t0); db.beginTransaction(); final long t1 = System.nanoTime(); s.close(); s = db.compileStatement("Select json_extract(x, '$.Bar') from t1 where y = 1"); res = s.simpleQueryForString(); db.setTransactionSuccessful(); db.endTransaction(); test_result("json_test_1.2", res, "Gum", t1); db.beginTransaction(); final long t2 = System.nanoTime(); s.close(); db.execSQL("Create Unique Index t1_foo on t1(json_extract(x, '$.Foo'))"); db.execSQL("Create Unique Index t1_bar on t1(json_extract(x, '$.Bar'))"); s = db.compileStatement("Select x from t1 where json_extract(x, '$.Foo') > 1 order by json_extract(x, '$.Foo') limit 1"); res = s.simpleQueryForString(); db.setTransactionSuccessful(); db.endTransaction(); test_result("json_test_1.3", res, r2, t2); s.close(); db.close(); } } |