Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Add modified versions of the remainder of the Android CTS tests to this project. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | experimental |
Files: | files | file ages | folders |
SHA1: |
7820bf256b9bf6235d832930e59e550e |
User & Date: | dan 2017-11-15 10:56:58.006 |
Context
2017-11-15
| ||
18:12 | Add other CTS tests that use SQLite objects. (check-in: 796ba7d799 user: dan tags: experimental) | |
10:56 | Add modified versions of the remainder of the Android CTS tests to this project. (check-in: 7820bf256b user: dan tags: experimental) | |
2017-11-14
| ||
21:13 | Add further cts tests to this project. (check-in: 09d6816449 user: dan tags: experimental) | |
Changes
Added sqlite3/src/androidTest/java/org/sqlite/database/DatabaseStatementTest.java.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 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 180 181 182 183 184 185 186 187 188 189 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 | /* * Copyright (C) 2007 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.sqlite.database; import android.content.Context; import android.database.Cursor; import org.sqlite.database.sqlite.SQLiteConstraintException; import org.sqlite.database.sqlite.SQLiteDatabase; import org.sqlite.database.sqlite.SQLiteDoneException; import org.sqlite.database.sqlite.SQLiteStatement; import android.test.AndroidTestCase; import android.test.PerformanceTestCase; import android.test.suitebuilder.annotation.MediumTest; import java.io.File; /* * These tests were taken from * frameworks/base/tests/AndroidTests/src/com/android/unit_tests/DatabaseStatementTest.java * Modifications: * - use Context to create and delete the DB to avoid hard-coded paths */ public class DatabaseStatementTest extends AndroidTestCase implements PerformanceTestCase { private static final String sString1 = "this is a test"; private static final String sString2 = "and yet another test"; private static final String sString3 = "this string is a little longer, but still a test"; private static final String DATABASE_NAME = "database_test.db"; private static final int CURRENT_DATABASE_VERSION = 42; private SQLiteDatabase mDatabase; @Override protected void setUp() throws Exception { super.setUp(); System.loadLibrary("sqliteX"); File f = mContext.getDatabasePath(MyHelper.DATABASE_NAME); f.mkdirs(); if (f.exists()) { f.delete(); } mDatabase = SQLiteDatabase.openOrCreateDatabase(f,null); assertNotNull(mDatabase); mDatabase.setVersion(CURRENT_DATABASE_VERSION); } @Override protected void tearDown() throws Exception { mDatabase.close(); getContext().deleteDatabase(DATABASE_NAME); super.tearDown(); } public boolean isPerformanceOnly() { return false; } // These test can only be run once. public int startPerformance(Intermediates intermediates) { return 1; } private void populateDefaultTable() { mDatabase.execSQL("CREATE TABLE test (_id INTEGER PRIMARY KEY, data TEXT);"); mDatabase.execSQL("INSERT INTO test (data) VALUES ('" + sString1 + "');"); mDatabase.execSQL("INSERT INTO test (data) VALUES ('" + sString2 + "');"); mDatabase.execSQL("INSERT INTO test (data) VALUES ('" + sString3 + "');"); } @MediumTest public void testExecuteStatement() throws Exception { populateDefaultTable(); SQLiteStatement statement = mDatabase.compileStatement("DELETE FROM test"); statement.execute(); Cursor c = mDatabase.query("test", null, null, null, null, null, null); assertEquals(0, c.getCount()); c.deactivate(); statement.close(); } @MediumTest public void testSimpleQuery() throws Exception { mDatabase.execSQL("CREATE TABLE test (num INTEGER NOT NULL, str TEXT NOT NULL);"); mDatabase.execSQL("INSERT INTO test VALUES (1234, 'hello');"); SQLiteStatement statement1 = mDatabase.compileStatement("SELECT num FROM test WHERE str = ?"); SQLiteStatement statement2 = mDatabase.compileStatement("SELECT str FROM test WHERE num = ?"); try { statement1.bindString(1, "hello"); long value = statement1.simpleQueryForLong(); assertEquals(1234, value); statement1.bindString(1, "world"); statement1.simpleQueryForLong(); fail("shouldn't get here"); } catch (SQLiteDoneException e) { // expected } try { statement2.bindLong(1, 1234); String value = statement1.simpleQueryForString(); assertEquals("hello", value); statement2.bindLong(1, 5678); statement1.simpleQueryForString(); fail("shouldn't get here"); } catch (SQLiteDoneException e) { // expected } statement1.close(); statement2.close(); } @MediumTest public void testStatementLongBinding() throws Exception { mDatabase.execSQL("CREATE TABLE test (num INTEGER);"); SQLiteStatement statement = mDatabase.compileStatement("INSERT INTO test (num) VALUES (?)"); for (int i = 0; i < 10; i++) { statement.bindLong(1, i); statement.execute(); } statement.close(); Cursor c = mDatabase.query("test", null, null, null, null, null, null); int numCol = c.getColumnIndexOrThrow("num"); c.moveToFirst(); for (long i = 0; i < 10; i++) { long num = c.getLong(numCol); assertEquals(i, num); c.moveToNext(); } c.close(); } @MediumTest public void testStatementStringBinding() throws Exception { mDatabase.execSQL("CREATE TABLE test (num TEXT);"); SQLiteStatement statement = mDatabase.compileStatement("INSERT INTO test (num) VALUES (?)"); for (long i = 0; i < 10; i++) { statement.bindString(1, Long.toHexString(i)); statement.execute(); } statement.close(); Cursor c = mDatabase.query("test", null, null, null, null, null, null); int numCol = c.getColumnIndexOrThrow("num"); c.moveToFirst(); for (long i = 0; i < 10; i++) { String num = c.getString(numCol); assertEquals(Long.toHexString(i), num); c.moveToNext(); } c.close(); } @MediumTest public void testStatementClearBindings() throws Exception { mDatabase.execSQL("CREATE TABLE test (num INTEGER);"); SQLiteStatement statement = mDatabase.compileStatement("INSERT INTO test (num) VALUES (?)"); for (long i = 0; i < 10; i++) { statement.bindLong(1, i); statement.clearBindings(); statement.execute(); } statement.close(); Cursor c = mDatabase.query("test", null, null, null, null, null, "ROWID"); int numCol = c.getColumnIndexOrThrow("num"); assertTrue(c.moveToFirst()); for (long i = 0; i < 10; i++) { assertTrue(c.isNull(numCol)); c.moveToNext(); } c.close(); } @MediumTest public void testSimpleStringBinding() throws Exception { mDatabase.execSQL("CREATE TABLE test (num TEXT, value TEXT);"); String statement = "INSERT INTO test (num, value) VALUES (?,?)"; String[] args = new String[2]; for (int i = 0; i < 2; i++) { args[i] = Integer.toHexString(i); } mDatabase.execSQL(statement, args); Cursor c = mDatabase.query("test", null, null, null, null, null, null); int numCol = c.getColumnIndexOrThrow("num"); int valCol = c.getColumnIndexOrThrow("value"); c.moveToFirst(); String num = c.getString(numCol); assertEquals(Integer.toHexString(0), num); String val = c.getString(valCol); assertEquals(Integer.toHexString(1), val); c.close(); } @MediumTest public void testStatementMultipleBindings() throws Exception { mDatabase.execSQL("CREATE TABLE test (num INTEGER, str TEXT);"); SQLiteStatement statement = mDatabase.compileStatement("INSERT INTO test (num, str) VALUES (?, ?)"); for (long i = 0; i < 10; i++) { statement.bindLong(1, i); statement.bindString(2, Long.toHexString(i)); statement.execute(); } statement.close(); Cursor c = mDatabase.query("test", null, null, null, null, null, "ROWID"); int numCol = c.getColumnIndexOrThrow("num"); int strCol = c.getColumnIndexOrThrow("str"); assertTrue(c.moveToFirst()); for (long i = 0; i < 10; i++) { long num = c.getLong(numCol); String str = c.getString(strCol); assertEquals(i, num); assertEquals(Long.toHexString(i), str); c.moveToNext(); } c.close(); } private static class StatementTestThread extends Thread { private SQLiteDatabase mDatabase; private SQLiteStatement mStatement; public StatementTestThread(SQLiteDatabase db, SQLiteStatement statement) { super(); mDatabase = db; mStatement = statement; } @Override public void run() { mDatabase.beginTransaction(); for (long i = 0; i < 10; i++) { mStatement.bindLong(1, i); mStatement.bindString(2, Long.toHexString(i)); mStatement.execute(); } mDatabase.setTransactionSuccessful(); mDatabase.endTransaction(); Cursor c = mDatabase.query("test", null, null, null, null, null, "ROWID"); int numCol = c.getColumnIndexOrThrow("num"); int strCol = c.getColumnIndexOrThrow("str"); assertTrue(c.moveToFirst()); for (long i = 0; i < 10; i++) { long num = c.getLong(numCol); String str = c.getString(strCol); assertEquals(i, num); assertEquals(Long.toHexString(i), str); c.moveToNext(); } c.close(); } } @MediumTest public void testStatementMultiThreaded() throws Exception { mDatabase.execSQL("CREATE TABLE test (num INTEGER, str TEXT);"); SQLiteStatement statement = mDatabase.compileStatement("INSERT INTO test (num, str) VALUES (?, ?)"); StatementTestThread thread = new StatementTestThread(mDatabase, statement); thread.start(); try { thread.join(); } finally { statement.close(); } } @MediumTest public void testStatementConstraint() throws Exception { mDatabase.execSQL("CREATE TABLE test (num INTEGER NOT NULL);"); SQLiteStatement statement = mDatabase.compileStatement("INSERT INTO test (num) VALUES (?)"); // Try to insert NULL, which violates the constraint try { statement.clearBindings(); statement.execute(); fail("expected exception not thrown"); } catch (SQLiteConstraintException e) { // expected } // Make sure the statement can still be used statement.bindLong(1, 1); statement.execute(); statement.close(); Cursor c = mDatabase.query("test", null, null, null, null, null, null); int numCol = c.getColumnIndexOrThrow("num"); c.moveToFirst(); long num = c.getLong(numCol); assertEquals(1, num); c.close(); } } |
Added sqlite3/src/androidTest/java/org/sqlite/database/SQLiteConstraintExceptionTest.java.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.sqlite.database; import org.sqlite.database.sqlite.SQLiteConstraintException; import android.test.AndroidTestCase; public class SQLiteConstraintExceptionTest extends AndroidTestCase { public void testConstructor() { new SQLiteConstraintException(); new SQLiteConstraintException("error"); } } |
Added sqlite3/src/androidTest/java/org/sqlite/database/SQLiteDatabaseCorruptExceptionTest.java.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.database.sqlite.cts; import android.database.sqlite.SQLiteDatabaseCorruptException; import android.test.AndroidTestCase; public class SQLiteDatabaseCorruptExceptionTest extends AndroidTestCase { public void testConstructor() { new SQLiteDatabaseCorruptException(); new SQLiteDatabaseCorruptException("error"); } } |
Added sqlite3/src/androidTest/java/org/sqlite/database/SQLiteDiskIOExceptionTest.java.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.sqlite.database; import org.sqlite.database.sqlite.SQLiteDiskIOException; import android.test.AndroidTestCase; public class SQLiteDiskIOExceptionTest extends AndroidTestCase { public void testConstructor() { new SQLiteDiskIOException(); new SQLiteDiskIOException("error"); } } |
Added sqlite3/src/androidTest/java/org/sqlite/database/SQLiteDoneExceptionTest.java.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.sqlite.database; import org.sqlite.database.sqlite.SQLiteDoneException; import android.test.AndroidTestCase; public class SQLiteDoneExceptionTest extends AndroidTestCase { public void testConstructor() { new SQLiteDoneException(); new SQLiteDoneException("error"); } } |
Added sqlite3/src/androidTest/java/org/sqlite/database/SQLiteExceptionTest.java.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.sqlite.database; import org.sqlite.database.sqlite.SQLiteException; import android.test.AndroidTestCase; public class SQLiteExceptionTest extends AndroidTestCase { public void testConstructor() { new SQLiteException(); new SQLiteException("error"); } } |
Added sqlite3/src/androidTest/java/org/sqlite/database/SQLiteFullExceptionTest.java.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.sqlite.database; import org.sqlite.database.sqlite.SQLiteFullException; import android.test.AndroidTestCase; public class SQLiteFullExceptionTest extends AndroidTestCase { public void testConstructor() { new SQLiteFullException(); new SQLiteFullException("error"); } } |
Added sqlite3/src/androidTest/java/org/sqlite/database/SQLiteMisuseExceptionTest.java.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.sqlite.database; import org.sqlite.database.sqlite.SQLiteMisuseException; import android.test.AndroidTestCase; public class SQLiteMisuseExceptionTest extends AndroidTestCase { public void testConstructor() { new SQLiteMisuseException(); new SQLiteMisuseException("error"); } } |
Added sqlite3/src/androidTest/java/org/sqlite/database/SQLiteOpenHelperTest.java.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 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 139 140 141 | /* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.sqlite.database; import android.content.Context; import android.database.Cursor; import org.sqlite.database.sqlite.SQLiteCursor; import org.sqlite.database.sqlite.SQLiteCursorDriver; import org.sqlite.database.sqlite.SQLiteDatabase; import org.sqlite.database.sqlite.SQLiteOpenHelper; import org.sqlite.database.sqlite.SQLiteQuery; import org.sqlite.database.sqlite.SQLiteDatabase.CursorFactory; import android.test.AndroidTestCase; import java.io.File; /** * Test {@link SQLiteOpenHelper}. */ public class SQLiteOpenHelperTest extends AndroidTestCase { private static final String TEST_DATABASE_NAME = "database_test.db"; static String DATABASE_PATH; private static final int TEST_VERSION = 1; private static final int TEST_ILLEGAL_VERSION = 0; private MockOpenHelper mOpenHelper; private SQLiteDatabase.CursorFactory mFactory = new SQLiteDatabase.CursorFactory() { public Cursor newCursor(SQLiteDatabase db, SQLiteCursorDriver masterQuery, String editTable, SQLiteQuery query) { return new MockCursor(db, masterQuery, editTable, query); } }; @Override protected void setUp() throws Exception { super.setUp(); System.loadLibrary("sqliteX"); DATABASE_PATH = mContext.getDatabasePath(TEST_DATABASE_NAME).toString(); mOpenHelper = getOpenHelper(); } public void testConstructor() { new MockOpenHelper(mContext, DATABASE_PATH, mFactory, TEST_VERSION); // Test with illegal version number. try { new MockOpenHelper(mContext, DATABASE_PATH, mFactory, TEST_ILLEGAL_VERSION); fail("Constructor of SQLiteOpenHelp should throws a IllegalArgumentException here."); } catch (IllegalArgumentException e) { } // Test with null factory new MockOpenHelper(mContext, DATABASE_PATH, null, TEST_VERSION); } public void testGetDatabase() { SQLiteDatabase database = null; assertFalse(mOpenHelper.hasCalledOnOpen()); // Test getReadableDatabase. database = mOpenHelper.getReadableDatabase(); assertNotNull(database); assertTrue(database.isOpen()); assertTrue(mOpenHelper.hasCalledOnOpen()); // Database has been opened, so onOpen can not be invoked. mOpenHelper.resetStatus(); assertFalse(mOpenHelper.hasCalledOnOpen()); // Test getWritableDatabase. SQLiteDatabase database2 = mOpenHelper.getWritableDatabase(); assertSame(database, database2); assertTrue(database.isOpen()); assertFalse(mOpenHelper.hasCalledOnOpen()); mOpenHelper.close(); assertFalse(database.isOpen()); // After close(), onOpen() will be invoked by getWritableDatabase. mOpenHelper.resetStatus(); assertFalse(mOpenHelper.hasCalledOnOpen()); SQLiteDatabase database3 = mOpenHelper.getWritableDatabase(); assertNotNull(database); assertNotSame(database, database3); assertTrue(mOpenHelper.hasCalledOnOpen()); assertTrue(database3.isOpen()); mOpenHelper.close(); assertFalse(database3.isOpen()); } private MockOpenHelper getOpenHelper() { return new MockOpenHelper(mContext, DATABASE_PATH, mFactory, TEST_VERSION); } private class MockOpenHelper extends SQLiteOpenHelper { private boolean mHasCalledOnOpen = false; public MockOpenHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } @Override public void onOpen(SQLiteDatabase db) { mHasCalledOnOpen = true; } public boolean hasCalledOnOpen() { return mHasCalledOnOpen; } public void resetStatus() { mHasCalledOnOpen = false; } } private class MockCursor extends SQLiteCursor { public MockCursor(SQLiteDatabase db, SQLiteCursorDriver driver, String editTable, SQLiteQuery query) { super(db, driver, editTable, query); } } } |
Added sqlite3/src/androidTest/java/org/sqlite/database/SQLiteProgramTest.java.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 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 180 181 182 183 184 185 186 187 | /* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.sqlite.database; import android.content.Context; import android.database.Cursor; import org.sqlite.database.sqlite.SQLiteDatabase; import org.sqlite.database.sqlite.SQLiteDoneException; import org.sqlite.database.sqlite.SQLiteException; import org.sqlite.database.sqlite.SQLiteQuery; import org.sqlite.database.sqlite.SQLiteStatement; import android.test.AndroidTestCase; import android.test.MoreAsserts; import java.io.File; public class SQLiteProgramTest extends AndroidTestCase { private static final String DATABASE_NAME = "database_test.db"; private SQLiteDatabase mDatabase; @Override protected void setUp() throws Exception { super.setUp(); System.loadLibrary("sqliteX"); File f = mContext.getDatabasePath(DATABASE_NAME); f.mkdirs(); if (f.exists()) { f.delete(); } mDatabase = SQLiteDatabase.openOrCreateDatabase(f,null); assertNotNull(mDatabase); } @Override protected void tearDown() throws Exception { mDatabase.close(); getContext().deleteDatabase(DATABASE_NAME); super.tearDown(); } public void testBind() { mDatabase.execSQL("CREATE TABLE test (_id INTEGER PRIMARY KEY, text1 TEXT, text2 TEXT, " + "num1 INTEGER, num2 INTEGER, image BLOB);"); mDatabase.execSQL("INSERT INTO test (text1, text2, num1, num2, image) " + "VALUES ('Mike', 'Jack', 12, 30, 'abcdefg');"); mDatabase.execSQL("INSERT INTO test (text1, text2, num1, num2, image) " + "VALUES ('test1', 'test2', 213, 589, '123456789');"); SQLiteStatement statement; statement = mDatabase.compileStatement("SELECT num1 FROM test WHERE num2 = ?;"); statement.bindLong(1, 30); assertEquals(12, statement.simpleQueryForLong()); // re-bind without clearing statement.bindDouble(1, 589.0); assertEquals(213, statement.simpleQueryForLong()); statement.close(); statement = mDatabase.compileStatement("SELECT text1 FROM test WHERE text2 = ?;"); statement.bindDouble(1, 589.0); // Wrong binding try { statement.simpleQueryForString(); fail("Should throw exception (no rows found)"); } catch (SQLiteDoneException expected) { // expected } statement.bindString(1, "test2"); assertEquals("test1", statement.simpleQueryForString()); statement.clearBindings(); try { statement.simpleQueryForString(); fail("Should throw exception (unbound value)"); } catch (SQLiteDoneException expected) { // expected } statement.close(); Cursor cursor = null; try { cursor = mDatabase.query("test", new String[]{"text1"}, "where text1='a'", new String[]{"foo"}, null, null, null); fail("Should throw exception (no value to bind)"); } catch (SQLiteException expected) { // expected } finally { if (cursor != null) { cursor.close(); } } try { cursor = mDatabase.query("test", new String[]{"text1"}, "where text1='a'", new String[]{"foo", "bar"}, null, null, null); fail("Should throw exception (index too large)"); } catch (SQLiteException expected) { // expected } finally { if (cursor != null) { cursor.close(); } } // test positive case statement = mDatabase.compileStatement( "SELECT text1 FROM test WHERE text2 = ? AND num2 = ?;"); statement.bindString(1, "Jack"); statement.bindLong(2, 30); assertEquals("Mike", statement.simpleQueryForString()); statement.close(); } public void testBindNull() { mDatabase.execSQL("CREATE TABLE test (_id INTEGER PRIMARY KEY, text1 TEXT, text2 TEXT, " + "num1 INTEGER, num2 INTEGER, image BLOB);"); SQLiteStatement statement = mDatabase.compileStatement("INSERT INTO test " + "(text1,text2,num1,image) VALUES (?,?,?,?)"); statement.bindString(1, "string1"); statement.bindString(2, "string2"); statement.bindLong(3, 100); statement.bindNull(4); statement.execute(); statement.close(); final int COLUMN_TEXT1_INDEX = 0; final int COLUMN_TEXT2_INDEX = 1; final int COLUMN_NUM1_INDEX = 2; final int COLUMN_IMAGE_INDEX = 3; Cursor cursor = mDatabase.query("test", new String[] { "text1", "text2", "num1", "image" }, null, null, null, null, null); assertNotNull(cursor); assertEquals(1, cursor.getCount()); cursor.moveToFirst(); assertEquals("string1", cursor.getString(COLUMN_TEXT1_INDEX)); assertEquals("string2", cursor.getString(COLUMN_TEXT2_INDEX)); assertEquals(100, cursor.getInt(COLUMN_NUM1_INDEX)); assertNull(cursor.getString(COLUMN_IMAGE_INDEX)); cursor.close(); } public void testBindBlob() { mDatabase.execSQL("CREATE TABLE test (_id INTEGER PRIMARY KEY, text1 TEXT, text2 TEXT, " + "num1 INTEGER, num2 INTEGER, image BLOB);"); SQLiteStatement statement = mDatabase.compileStatement("INSERT INTO test " + "(text1,text2,num1,image) VALUES (?,?,?,?)"); statement.bindString(1, "string1"); statement.bindString(2, "string2"); statement.bindLong(3, 100); byte[] blob = new byte[] { '1', '2', '3' }; statement.bindBlob(4, blob); statement.execute(); statement.close(); final int COLUMN_TEXT1_INDEX = 0; final int COLUMN_TEXT2_INDEX = 1; final int COLUMN_NUM1_INDEX = 2; final int COLUMN_IMAGE_INDEX = 3; Cursor cursor = mDatabase.query("test", new String[] { "text1", "text2", "num1", "image" }, null, null, null, null, null); assertNotNull(cursor); assertEquals(1, cursor.getCount()); cursor.moveToFirst(); assertEquals("string1", cursor.getString(COLUMN_TEXT1_INDEX)); assertEquals("string2", cursor.getString(COLUMN_TEXT2_INDEX)); assertEquals(100, cursor.getInt(COLUMN_NUM1_INDEX)); byte[] value = cursor.getBlob(COLUMN_IMAGE_INDEX); MoreAsserts.assertEquals(blob, value); cursor.close(); } } |
Added sqlite3/src/androidTest/java/org/sqlite/database/SQLiteQueryBuilderTest.java.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 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 180 181 182 183 184 185 186 187 188 189 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 | /* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.sqlite.database; import android.content.Context; import android.database.Cursor; import org.sqlite.database.sqlite.SQLiteCursor; import org.sqlite.database.sqlite.SQLiteCursorDriver; import org.sqlite.database.sqlite.SQLiteDatabase; import org.sqlite.database.sqlite.SQLiteQuery; import org.sqlite.database.sqlite.SQLiteQueryBuilder; import android.os.CancellationSignal; import android.os.OperationCanceledException; import android.test.AndroidTestCase; import java.util.HashMap; import java.util.Map; import java.util.concurrent.Semaphore; import java.io.File; public class SQLiteQueryBuilderTest extends AndroidTestCase { private SQLiteDatabase mDatabase; private final String TEST_TABLE_NAME = "test"; private final String EMPLOYEE_TABLE_NAME = "employee"; private static final String DATABASE_FILE = "database_test.db"; @Override protected void setUp() throws Exception { super.setUp(); System.loadLibrary("sqliteX"); File f = mContext.getDatabasePath(MyHelper.DATABASE_NAME); f.mkdirs(); if (f.exists()) { f.delete(); } mDatabase = SQLiteDatabase.openOrCreateDatabase(f,null); assertNotNull(mDatabase); } @Override protected void tearDown() throws Exception { mDatabase.close(); getContext().deleteDatabase(DATABASE_FILE); super.tearDown(); } public void testConstructor() { new SQLiteQueryBuilder(); } public void testSetDistinct() { String expected; SQLiteQueryBuilder sqliteQueryBuilder = new SQLiteQueryBuilder(); sqliteQueryBuilder.setTables(TEST_TABLE_NAME); sqliteQueryBuilder.setDistinct(false); sqliteQueryBuilder.appendWhere("age=20"); String sql = sqliteQueryBuilder.buildQuery(new String[] { "age", "address" }, null, null, null, null, null, null); assertEquals(TEST_TABLE_NAME, sqliteQueryBuilder.getTables()); expected = "SELECT age, address FROM " + TEST_TABLE_NAME + " WHERE (age=20)"; assertEquals(expected, sql); sqliteQueryBuilder = new SQLiteQueryBuilder(); sqliteQueryBuilder.setTables(EMPLOYEE_TABLE_NAME); sqliteQueryBuilder.setDistinct(true); sqliteQueryBuilder.appendWhere("age>32"); sql = sqliteQueryBuilder.buildQuery(new String[] { "age", "address" }, null, null, null, null, null, null); assertEquals(EMPLOYEE_TABLE_NAME, sqliteQueryBuilder.getTables()); expected = "SELECT DISTINCT age, address FROM " + EMPLOYEE_TABLE_NAME + " WHERE (age>32)"; assertEquals(expected, sql); sqliteQueryBuilder = new SQLiteQueryBuilder(); sqliteQueryBuilder.setTables(EMPLOYEE_TABLE_NAME); sqliteQueryBuilder.setDistinct(true); sqliteQueryBuilder.appendWhereEscapeString("age>32"); sql = sqliteQueryBuilder.buildQuery(new String[] { "age", "address" }, null, null, null, null, null, null); assertEquals(EMPLOYEE_TABLE_NAME, sqliteQueryBuilder.getTables()); expected = "SELECT DISTINCT age, address FROM " + EMPLOYEE_TABLE_NAME + " WHERE ('age>32')"; assertEquals(expected, sql); } public void testSetProjectionMap() { String expected; Map<String, String> projectMap = new HashMap<String, String>(); projectMap.put("EmployeeName", "name"); projectMap.put("EmployeeAge", "age"); projectMap.put("EmployeeAddress", "address"); SQLiteQueryBuilder sqliteQueryBuilder = new SQLiteQueryBuilder(); sqliteQueryBuilder.setTables(TEST_TABLE_NAME); sqliteQueryBuilder.setDistinct(false); sqliteQueryBuilder.setProjectionMap(projectMap); String sql = sqliteQueryBuilder.buildQuery(new String[] { "EmployeeName", "EmployeeAge" }, null, null, null, null, null, null); expected = "SELECT name, age FROM " + TEST_TABLE_NAME; assertEquals(expected, sql); sql = sqliteQueryBuilder.buildQuery(null, // projectionIn is null null, null, null, null, null, null); assertTrue(sql.matches("SELECT (age|name|address), (age|name|address), (age|name|address) " + "FROM " + TEST_TABLE_NAME)); assertTrue(sql.contains("age")); assertTrue(sql.contains("name")); assertTrue(sql.contains("address")); sqliteQueryBuilder.setProjectionMap(null); sql = sqliteQueryBuilder.buildQuery(new String[] { "name", "address" }, null, null, null, null, null, null); assertTrue(sql.matches("SELECT (name|address), (name|address) " + "FROM " + TEST_TABLE_NAME)); assertTrue(sql.contains("name")); assertTrue(sql.contains("address")); } public void testSetCursorFactory() { mDatabase.execSQL("CREATE TABLE test (_id INTEGER PRIMARY KEY, " + "name TEXT, age INTEGER, address TEXT);"); mDatabase.execSQL("INSERT INTO test (name, age, address) VALUES ('Mike', '20', 'LA');"); mDatabase.execSQL("INSERT INTO test (name, age, address) VALUES ('jack', '40', 'LA');"); SQLiteQueryBuilder sqliteQueryBuilder = new SQLiteQueryBuilder(); sqliteQueryBuilder.setTables(TEST_TABLE_NAME); Cursor cursor = sqliteQueryBuilder.query(mDatabase, new String[] { "name", "age" }, null, null, null, null, null); assertNotNull(cursor); assertTrue(cursor instanceof SQLiteCursor); SQLiteDatabase.CursorFactory factory = new SQLiteDatabase.CursorFactory() { public Cursor newCursor(SQLiteDatabase db, SQLiteCursorDriver masterQuery, String editTable, SQLiteQuery query) { return new MockCursor(db, masterQuery, editTable, query); } }; sqliteQueryBuilder.setCursorFactory(factory); cursor = sqliteQueryBuilder.query(mDatabase, new String[] { "name", "age" }, null, null, null, null, null); assertNotNull(cursor); assertTrue(cursor instanceof MockCursor); } private static class MockCursor extends SQLiteCursor { public MockCursor(SQLiteDatabase db, SQLiteCursorDriver driver, String editTable, SQLiteQuery query) { super(db, driver, editTable, query); } } public void testBuildQueryString() { String expected; final String[] DEFAULT_TEST_PROJECTION = new String [] { "name", "age", "sum(salary)" }; final String DEFAULT_TEST_WHERE = "age > 25"; final String DEFAULT_HAVING = "sum(salary) > 3000"; String sql = SQLiteQueryBuilder.buildQueryString(false, "Employee", DEFAULT_TEST_PROJECTION, DEFAULT_TEST_WHERE, "name", DEFAULT_HAVING, "name", "100"); expected = "SELECT name, age, sum(salary) FROM Employee WHERE " + DEFAULT_TEST_WHERE + " GROUP BY name " + "HAVING " + DEFAULT_HAVING + " " + "ORDER BY name " + "LIMIT 100"; assertEquals(expected, sql); } public void testBuildQuery() { final String[] DEFAULT_TEST_PROJECTION = new String[] { "name", "sum(salary)" }; final String DEFAULT_TEST_WHERE = "age > 25"; final String DEFAULT_HAVING = "sum(salary) > 2000"; SQLiteQueryBuilder sqliteQueryBuilder = new SQLiteQueryBuilder(); sqliteQueryBuilder.setTables(TEST_TABLE_NAME); sqliteQueryBuilder.setDistinct(false); String sql = sqliteQueryBuilder.buildQuery(DEFAULT_TEST_PROJECTION, DEFAULT_TEST_WHERE, null, "name", DEFAULT_HAVING, "name", "2"); String expected = "SELECT name, sum(salary) FROM " + TEST_TABLE_NAME + " WHERE (" + DEFAULT_TEST_WHERE + ") " + "GROUP BY name HAVING " + DEFAULT_HAVING + " ORDER BY name LIMIT 2"; assertEquals(expected, sql); } public void testAppendColumns() { StringBuilder sb = new StringBuilder(); String[] columns = new String[] { "name", "age" }; assertEquals("", sb.toString()); SQLiteQueryBuilder.appendColumns(sb, columns); assertEquals("name, age ", sb.toString()); } public void testQuery() { createEmployeeTable(); SQLiteQueryBuilder sqliteQueryBuilder = new SQLiteQueryBuilder(); sqliteQueryBuilder.setTables("Employee"); Cursor cursor = sqliteQueryBuilder.query(mDatabase, new String[] { "name", "sum(salary)" }, null, null, "name", "sum(salary)>1000", "name"); assertNotNull(cursor); assertEquals(3, cursor.getCount()); final int COLUMN_NAME_INDEX = 0; final int COLUMN_SALARY_INDEX = 1; cursor.moveToFirst(); assertEquals("Jim", cursor.getString(COLUMN_NAME_INDEX)); assertEquals(4500, cursor.getInt(COLUMN_SALARY_INDEX)); cursor.moveToNext(); assertEquals("Mike", cursor.getString(COLUMN_NAME_INDEX)); assertEquals(4000, cursor.getInt(COLUMN_SALARY_INDEX)); cursor.moveToNext(); assertEquals("jack", cursor.getString(COLUMN_NAME_INDEX)); assertEquals(3500, cursor.getInt(COLUMN_SALARY_INDEX)); sqliteQueryBuilder = new SQLiteQueryBuilder(); sqliteQueryBuilder.setTables(EMPLOYEE_TABLE_NAME); cursor = sqliteQueryBuilder.query(mDatabase, new String[] { "name", "sum(salary)" }, null, null, "name", "sum(salary)>1000", "name", "2" // limit is 2 ); assertNotNull(cursor); assertEquals(2, cursor.getCount()); cursor.moveToFirst(); assertEquals("Jim", cursor.getString(COLUMN_NAME_INDEX)); assertEquals(4500, cursor.getInt(COLUMN_SALARY_INDEX)); cursor.moveToNext(); assertEquals("Mike", cursor.getString(COLUMN_NAME_INDEX)); assertEquals(4000, cursor.getInt(COLUMN_SALARY_INDEX)); } public void testUnionQuery() { String expected; String[] innerProjection = new String[] {"name", "age", "location"}; SQLiteQueryBuilder employeeQueryBuilder = new SQLiteQueryBuilder(); SQLiteQueryBuilder peopleQueryBuilder = new SQLiteQueryBuilder(); employeeQueryBuilder.setTables("employee"); peopleQueryBuilder.setTables("people"); String employeeSubQuery = employeeQueryBuilder.buildUnionSubQuery( "_id", innerProjection, null, 2, "employee", "age=25", null, null, null); String peopleSubQuery = peopleQueryBuilder.buildUnionSubQuery( "_id", innerProjection, null, 2, "people", "location=LA", null, null, null); expected = "SELECT name, age, location FROM employee WHERE (age=25)"; assertEquals(expected, employeeSubQuery); expected = "SELECT name, age, location FROM people WHERE (location=LA)"; assertEquals(expected, peopleSubQuery); SQLiteQueryBuilder unionQueryBuilder = new SQLiteQueryBuilder(); unionQueryBuilder.setDistinct(true); String unionQuery = unionQueryBuilder.buildUnionQuery( new String[] { employeeSubQuery, peopleSubQuery }, null, null); expected = "SELECT name, age, location FROM employee WHERE (age=25) " + "UNION SELECT name, age, location FROM people WHERE (location=LA)"; assertEquals(expected, unionQuery); } public void testCancelableQuery_WhenNotCanceled_ReturnsResultSet() { createEmployeeTable(); CancellationSignal cancellationSignal = new CancellationSignal(); SQLiteQueryBuilder sqliteQueryBuilder = new SQLiteQueryBuilder(); sqliteQueryBuilder.setTables("Employee"); Cursor cursor = sqliteQueryBuilder.query(mDatabase, new String[] { "name", "sum(salary)" }, null, null, "name", "sum(salary)>1000", "name", null, cancellationSignal); assertEquals(3, cursor.getCount()); } public void testCancelableQuery_WhenCanceledBeforeQuery_ThrowsImmediately() { createEmployeeTable(); CancellationSignal cancellationSignal = new CancellationSignal(); SQLiteQueryBuilder sqliteQueryBuilder = new SQLiteQueryBuilder(); sqliteQueryBuilder.setTables("Employee"); cancellationSignal.cancel(); try { sqliteQueryBuilder.query(mDatabase, new String[] { "name", "sum(salary)" }, null, null, "name", "sum(salary)>1000", "name", null, cancellationSignal); fail("Expected OperationCanceledException"); } catch (OperationCanceledException ex) { // expected } } public void testCancelableQuery_WhenCanceledAfterQuery_ThrowsWhenExecuted() { createEmployeeTable(); CancellationSignal cancellationSignal = new CancellationSignal(); SQLiteQueryBuilder sqliteQueryBuilder = new SQLiteQueryBuilder(); sqliteQueryBuilder.setTables("Employee"); Cursor cursor = sqliteQueryBuilder.query(mDatabase, new String[] { "name", "sum(salary)" }, null, null, "name", "sum(salary)>1000", "name", null, cancellationSignal); cancellationSignal.cancel(); try { cursor.getCount(); // force execution fail("Expected OperationCanceledException"); } catch (OperationCanceledException ex) { // expected } } public void testCancelableQuery_WhenCanceledDueToContention_StopsWaitingAndThrows() { createEmployeeTable(); for (int i = 0; i < 5; i++) { final CancellationSignal cancellationSignal = new CancellationSignal(); final Semaphore barrier1 = new Semaphore(0); final Semaphore barrier2 = new Semaphore(0); Thread contentionThread = new Thread() { @Override public void run() { mDatabase.beginTransaction(); // acquire the only available connection barrier1.release(); // release query to start running try { barrier2.acquire(); // wait for test to end } catch (InterruptedException e) { } mDatabase.endTransaction(); // release the connection } }; Thread cancellationThread = new Thread() { @Override public void run() { try { Thread.sleep(300); } catch (InterruptedException ex) { } cancellationSignal.cancel(); } }; try { SQLiteQueryBuilder sqliteQueryBuilder = new SQLiteQueryBuilder(); sqliteQueryBuilder.setTables("Employee"); contentionThread.start(); cancellationThread.start(); try { barrier1.acquire(); // wait for contention thread to start transaction } catch (InterruptedException e) { } final long startTime = System.nanoTime(); try { Cursor cursor = sqliteQueryBuilder.query(mDatabase, new String[] { "name", "sum(salary)" }, null, null, "name", "sum(salary)>1000", "name", null, cancellationSignal); cursor.getCount(); // force execution fail("Expected OperationCanceledException"); } catch (OperationCanceledException ex) { // expected } // We want to confirm that the query really was blocked trying to acquire a // connection for a certain amount of time before it was freed by cancel. final long waitTime = System.nanoTime() - startTime; if (waitTime > 150 * 1000000L) { return; // success! } } finally { barrier1.release(); barrier2.release(); try { contentionThread.join(); cancellationThread.join(); } catch (InterruptedException e) { } } } // Occasionally we might miss the timing deadline due to factors in the // environment, but if after several trials we still couldn't demonstrate // that the query was blocked, then the test must be broken. fail("Could not prove that the query actually blocked before cancel() was called."); } public void testCancelableQuery_WhenCanceledDuringLongRunningQuery_CancelsQueryAndThrows() { // Populate a table with a bunch of integers. mDatabase.execSQL("CREATE TABLE x (v INTEGER);"); for (int i = 0; i < 100; i++) { mDatabase.execSQL("INSERT INTO x VALUES (?)", new Object[] { i }); } for (int i = 0; i < 5; i++) { final CancellationSignal cancellationSignal = new CancellationSignal(); Thread cancellationThread = new Thread() { @Override public void run() { try { Thread.sleep(300); } catch (InterruptedException ex) { } cancellationSignal.cancel(); } }; try { // Build an unsatisfiable 5-way cross-product query over 100 values but // produces no output. This should force SQLite to loop for a long time // as it tests 10^10 combinations. SQLiteQueryBuilder sqliteQueryBuilder = new SQLiteQueryBuilder(); sqliteQueryBuilder.setTables("x AS a, x AS b, x AS c, x AS d, x AS e"); cancellationThread.start(); final long startTime = System.nanoTime(); try { Cursor cursor = sqliteQueryBuilder.query(mDatabase, null, "a.v + b.v + c.v + d.v + e.v > 1000000", null, null, null, null, null, cancellationSignal); cursor.getCount(); // force execution fail("Expected OperationCanceledException"); } catch (OperationCanceledException ex) { // expected } // We want to confirm that the query really was running and then got // canceled midway. final long waitTime = System.nanoTime() - startTime; if (waitTime > 150 * 1000000L && waitTime < 600 * 1000000L) { return; // success! } } finally { try { cancellationThread.join(); } catch (InterruptedException e) { } } } // Occasionally we might miss the timing deadline due to factors in the // environment, but if after several trials we still couldn't demonstrate // that the query was canceled, then the test must be broken. fail("Could not prove that the query actually canceled midway during execution."); } private void createEmployeeTable() { mDatabase.execSQL("CREATE TABLE employee (_id INTEGER PRIMARY KEY, " + "name TEXT, month INTEGER, salary INTEGER);"); mDatabase.execSQL("INSERT INTO employee (name, month, salary) " + "VALUES ('Mike', '1', '1000');"); mDatabase.execSQL("INSERT INTO employee (name, month, salary) " + "VALUES ('Mike', '2', '3000');"); mDatabase.execSQL("INSERT INTO employee (name, month, salary) " + "VALUES ('jack', '1', '2000');"); mDatabase.execSQL("INSERT INTO employee (name, month, salary) " + "VALUES ('jack', '3', '1500');"); mDatabase.execSQL("INSERT INTO employee (name, month, salary) " + "VALUES ('Jim', '1', '1000');"); mDatabase.execSQL("INSERT INTO employee (name, month, salary) " + "VALUES ('Jim', '3', '3500');"); } } |
Added sqlite3/src/androidTest/java/org/sqlite/database/SQLiteQueryTest.java.
> > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.sqlite.database; import junit.framework.TestCase; public class SQLiteQueryTest extends TestCase { public void testMethods() { // cannot obtain an instance of SQLiteQuery } } |