DatabaseManager implements Closeable { public long put(@NonNull ContentValues values, @NonNull String priorityColumn) { Long id = null; Cursor cursor = null; try { while (id == null) { try { id = getDatabase().insertOrThrow(mDefaultTable, null, values); } catch (SQLiteFullException e) { AppCenterLog.debug(LOG_TAG, "Storage is full, trying to delete the oldest log that has the lowest priority which is lower or equal priority than the new log"); if (cursor == null) { String priority = values.getAsString(priorityColumn); SQLiteQueryBuilder queryBuilder = SQLiteUtils.newSQLiteQueryBuilder(); queryBuilder.appendWhere(priorityColumn + " <= ?"); cursor = getCursor(queryBuilder, SELECT_PRIMARY_KEY, new String[]{priority}, priorityColumn + " , " + PRIMARY_KEY); } if (cursor.moveToNext()) { long deletedId = cursor.getLong(0); delete(deletedId); AppCenterLog.debug(LOG_TAG, "Deleted log id=" + deletedId); } else { throw e; } } } } catch (RuntimeException e) { id = -1L; AppCenterLog.error(LOG_TAG, String.format("Failed to insert values (%s) to database %s.", values.toString(), mDatabase), e); } if (cursor != null) { try { cursor.close(); } catch (RuntimeException ignore) { } } return id; } DatabaseManager(Context context, String database, String defaultTable, int version,
                           ContentValues schema, final String sqlCreateCommand, @NonNull Listener listener); ContentValues buildValues(Cursor cursor); @Nullable ContentValues nextValues(Cursor cursor); long put(@NonNull ContentValues values, @NonNull String priorityColumn); void delete(@IntRange(from = 0) long id); int delete(@NonNull String key, @Nullable Object value); void clear(); @Override void close(); final long getRowCount(); Cursor getCursor(@Nullable SQLiteQueryBuilder queryBuilder, String[] columns, @Nullable String[] selectionArgs, @Nullable String sortOrder); boolean setMaxSize(long maxStorageSizeInBytes); long getMaxSize(); static final String PRIMARY_KEY; static final String[] SELECT_PRIMARY_KEY; }
@Test public void putFailed() { DatabaseManager databaseManagerMock = getDatabaseManagerMock(); databaseManagerMock.put(new ContentValues(), "priority"); verifyStatic(); AppCenterLog.error(eq(AppCenter.LOG_TAG), anyString(), any(RuntimeException.class)); }