use of android.database.sqlite.SQLiteOpenHelper in project mobile-center-sdk-android by Microsoft.
the class DatabaseManagerTest method failsToDeleteLogDuringPutWhenFull.
@Test
public void failsToDeleteLogDuringPutWhenFull() {
/* Mocking instances. */
Context contextMock = mock(Context.class);
SQLiteOpenHelper helperMock = mock(SQLiteOpenHelper.class);
SQLiteDatabase sqLiteDatabase = mock(SQLiteDatabase.class);
when(helperMock.getWritableDatabase()).thenReturn(sqLiteDatabase);
/* Mock the select cursor we are using to find logs to evict to fail. */
mockStatic(SQLiteUtils.class);
Cursor cursor = mock(Cursor.class);
SQLiteDiskIOException fatalException = new SQLiteDiskIOException();
when(cursor.moveToNext()).thenThrow(fatalException);
SQLiteQueryBuilder sqLiteQueryBuilder = mock(SQLiteQueryBuilder.class, new Returns(cursor));
when(SQLiteUtils.newSQLiteQueryBuilder()).thenReturn(sqLiteQueryBuilder);
/* Simulate that database is full and that deletes fail because of the cursor. */
when(sqLiteDatabase.insertOrThrow(anyString(), anyString(), any(ContentValues.class))).thenThrow(new SQLiteFullException());
/* Instantiate real instance for DatabaseManager. */
DatabaseManager databaseManager = new DatabaseManager(contextMock, "database", "table", 1, null, null, null);
databaseManager.setSQLiteOpenHelper(helperMock);
/* When we put a log, it will fail to purge. */
assertEquals(-1, databaseManager.put(mock(ContentValues.class), "priority"));
}
use of android.database.sqlite.SQLiteOpenHelper in project zxingfragmentlib by mitoyarzun.
the class HistoryManager method deleteHistoryItem.
public void deleteHistoryItem(int number) {
SQLiteOpenHelper helper = new DBHelper(activity);
SQLiteDatabase db = null;
Cursor cursor = null;
try {
db = helper.getWritableDatabase();
cursor = db.query(DBHelper.TABLE_NAME, ID_COL_PROJECTION, null, null, null, null, DBHelper.TIMESTAMP_COL + " DESC");
cursor.move(number + 1);
db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + cursor.getString(0), null);
} finally {
close(cursor, db);
}
}
use of android.database.sqlite.SQLiteOpenHelper in project zxingfragmentlib by mitoyarzun.
the class HistoryManager method buildHistoryItem.
public HistoryItem buildHistoryItem(int number) {
SQLiteOpenHelper helper = new DBHelper(activity);
SQLiteDatabase db = null;
Cursor cursor = null;
try {
db = helper.getReadableDatabase();
cursor = db.query(DBHelper.TABLE_NAME, COLUMNS, null, null, null, null, DBHelper.TIMESTAMP_COL + " DESC");
cursor.move(number + 1);
String text = cursor.getString(0);
String display = cursor.getString(1);
String format = cursor.getString(2);
long timestamp = cursor.getLong(3);
String details = cursor.getString(4);
Result result = new Result(text, null, null, BarcodeFormat.valueOf(format), timestamp);
return new HistoryItem(result, display, details);
} finally {
close(cursor, db);
}
}
use of android.database.sqlite.SQLiteOpenHelper in project zxingfragmentlib by mitoyarzun.
the class HistoryManager method deletePrevious.
private void deletePrevious(String text) {
SQLiteOpenHelper helper = new DBHelper(activity);
SQLiteDatabase db = null;
try {
db = helper.getWritableDatabase();
db.delete(DBHelper.TABLE_NAME, DBHelper.TEXT_COL + "=?", new String[] { text });
} finally {
close(null, db);
}
}
use of android.database.sqlite.SQLiteOpenHelper in project zxingfragmentlib by mitoyarzun.
the class HistoryManager method addHistoryItem.
public void addHistoryItem(Result result, ResultHandler handler) {
// considered secure.
if (!activity.getIntent().getBooleanExtra(Intents.Scan.SAVE_HISTORY, true) || handler.areContentsSecure()) {
return;
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
if (!prefs.getBoolean(PreferencesActivity.KEY_REMEMBER_DUPLICATES, false)) {
deletePrevious(result.getText());
}
ContentValues values = new ContentValues();
values.put(DBHelper.TEXT_COL, result.getText());
values.put(DBHelper.FORMAT_COL, result.getBarcodeFormat().toString());
values.put(DBHelper.DISPLAY_COL, handler.getDisplayContents().toString());
values.put(DBHelper.TIMESTAMP_COL, System.currentTimeMillis());
SQLiteOpenHelper helper = new DBHelper(activity);
SQLiteDatabase db = null;
try {
db = helper.getWritableDatabase();
// Insert the new entry into the DB.
db.insert(DBHelper.TABLE_NAME, DBHelper.TIMESTAMP_COL, values);
} finally {
close(null, db);
}
}
Aggregations