use of android.database.sqlite.SQLiteQueryBuilder in project mobile-center-sdk-android by Microsoft.
the class DatabaseManagerTest method switchInMemory.
@Test
public void switchInMemory() throws Exception {
DatabaseManager databaseManagerMock;
/* Put. */
databaseManagerMock = getDatabaseManagerMock();
databaseManagerMock.put(new ContentValues());
verify(databaseManagerMock).switchToInMemory(eq("put"), any(RuntimeException.class));
/* Update. */
databaseManagerMock = getDatabaseManagerMock();
databaseManagerMock.update(0, new ContentValues());
verify(databaseManagerMock).switchToInMemory(eq("update"), any(RuntimeException.class));
/* Get. */
databaseManagerMock = getDatabaseManagerMock();
databaseManagerMock.get(0);
verify(databaseManagerMock).switchToInMemory(eq("get"), any(RuntimeException.class));
/* Scanner. */
{
databaseManagerMock = getDatabaseManagerMock();
databaseManagerMock.getScanner(null, null, false).iterator();
verify(databaseManagerMock).switchToInMemory(eq("scan.iterator"), any(RuntimeException.class));
}
{
databaseManagerMock = getDatabaseManagerMock();
databaseManagerMock.getScanner(null, null, false).getCount();
verify(databaseManagerMock).switchToInMemory(eq("scan.count"), any(RuntimeException.class));
}
{
/* Cursor next failing but closing working. */
databaseManagerMock = spy(new DatabaseManager(null, "database", "table", 1, null, null));
when(databaseManagerMock.getDatabase()).thenReturn(mock(SQLiteDatabase.class));
mockStatic(SQLiteUtils.class);
Cursor cursor = mock(Cursor.class);
SQLiteQueryBuilder sqLiteQueryBuilder = mock(SQLiteQueryBuilder.class, new Returns(cursor));
when(SQLiteUtils.newSQLiteQueryBuilder()).thenReturn(sqLiteQueryBuilder);
when(cursor.moveToNext()).thenThrow(new RuntimeException());
DatabaseManager.Scanner scanner = databaseManagerMock.getScanner(null, null, false);
assertFalse(scanner.iterator().hasNext());
verify(databaseManagerMock).switchToInMemory(eq("scan.hasNext"), any(RuntimeException.class));
/* We switched over in memory so closing will not switch again. Cursor is closed already. */
doThrow(new RuntimeException()).when(cursor).close();
scanner.close();
verify(databaseManagerMock, never()).switchToInMemory(eq("scan.close"), any(RuntimeException.class));
}
{
/* Cursor next failing and closing failing. */
databaseManagerMock = spy(new DatabaseManager(null, "database", "table", 1, null, null));
when(databaseManagerMock.getDatabase()).thenReturn(mock(SQLiteDatabase.class));
mockStatic(SQLiteUtils.class);
Cursor cursor = mock(Cursor.class);
SQLiteQueryBuilder sqLiteQueryBuilder = mock(SQLiteQueryBuilder.class, new Returns(cursor));
when(SQLiteUtils.newSQLiteQueryBuilder()).thenReturn(sqLiteQueryBuilder);
when(cursor.moveToNext()).thenThrow(new RuntimeException());
doThrow(new RuntimeException()).when(cursor).close();
DatabaseManager.Scanner scanner = databaseManagerMock.getScanner(null, null, false);
assertFalse(scanner.iterator().hasNext());
verify(databaseManagerMock).switchToInMemory(eq("scan.hasNext"), any(RuntimeException.class));
/* We switched over in memory so closing will not switch again. Cursor is closed already in hasNext(). */
scanner.close();
verify(databaseManagerMock, never()).switchToInMemory(eq("scan.close"), any(RuntimeException.class));
}
{
/* Cursor closing failing. */
databaseManagerMock = spy(new DatabaseManager(null, "database", "table", 1, null, null));
when(databaseManagerMock.getDatabase()).thenReturn(mock(SQLiteDatabase.class));
mockStatic(SQLiteUtils.class);
Cursor cursor = mock(Cursor.class);
SQLiteQueryBuilder sqLiteQueryBuilder = mock(SQLiteQueryBuilder.class, new Returns(cursor));
when(SQLiteUtils.newSQLiteQueryBuilder()).thenReturn(sqLiteQueryBuilder);
doThrow(new RuntimeException()).when(cursor).close();
DatabaseManager.Scanner scanner = databaseManagerMock.getScanner(null, null, false);
assertFalse(scanner.iterator().hasNext());
scanner.close();
verify(databaseManagerMock).switchToInMemory(eq("scan.close"), any(RuntimeException.class));
}
/* Delete. */
databaseManagerMock = getDatabaseManagerMock();
databaseManagerMock.delete(0);
verify(databaseManagerMock).switchToInMemory(eq("delete"), any(RuntimeException.class));
/* Delete multiple IDs. */
databaseManagerMock = getDatabaseManagerMock();
databaseManagerMock.delete(new ArrayList<Long>());
verify(databaseManagerMock, never()).switchToInMemory(eq("delete"), any(RuntimeException.class));
databaseManagerMock = getDatabaseManagerMock();
databaseManagerMock.delete(Arrays.asList(new Long[] { 0L, 1L }));
verify(databaseManagerMock).switchToInMemory(eq("delete"), any(RuntimeException.class));
/* Clear. */
databaseManagerMock = getDatabaseManagerMock();
databaseManagerMock.clear();
verify(databaseManagerMock).switchToInMemory(eq("clear"), any(RuntimeException.class));
/* Close. */
databaseManagerMock = getDatabaseManagerMock();
databaseManagerMock.close();
verify(databaseManagerMock).switchToInMemory(eq("close"), any(RuntimeException.class));
/* Row count. */
databaseManagerMock = getDatabaseManagerMock();
databaseManagerMock.getRowCount();
verify(databaseManagerMock).switchToInMemory(eq("count"), any(RuntimeException.class));
}
use of android.database.sqlite.SQLiteQueryBuilder in project android-sqlite-asset-helper by jgilfelt.
the class MyDatabase method getEmployees.
public Cursor getEmployees() {
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String[] sqlSelect = { "0 _id", "FullName" };
String sqlTables = "Employees";
qb.setTables(sqlTables);
Cursor c = qb.query(db, sqlSelect, null, null, null, null, null);
c.moveToFirst();
return c;
}
use of android.database.sqlite.SQLiteQueryBuilder in project android-jigsaw-puzzle by julesbond007.
the class ImageProvider method query.
@Override
public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(JIGSAW_TABLE);
switch(uriMatcher.match(uri)) {
case IMAGES_ID:
queryBuilder.appendWhere(ID_SELECTION + uri.getLastPathSegment());
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
use of android.database.sqlite.SQLiteQueryBuilder in project platform_packages_apps_launcher by android.
the class LauncherProvider method query.
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(args.table);
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
Cursor result = qb.query(db, projection, args.where, args.args, null, null, sortOrder);
result.setNotificationUri(getContext().getContentResolver(), uri);
return result;
}
use of android.database.sqlite.SQLiteQueryBuilder in project android-sqlite-asset-helper by jgilfelt.
the class MyDatabase method getUpgradeVersion.
public int getUpgradeVersion() {
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String[] sqlSelect = { "MAX (version)" };
String sqlTables = "upgrades";
qb.setTables(sqlTables);
Cursor c = qb.query(db, sqlSelect, null, null, null, null, null);
int v = 0;
c.moveToFirst();
if (!c.isAfterLast()) {
v = c.getInt(0);
}
c.close();
return v;
}
Aggregations