DatabaseManager implements Closeable { public boolean setMaxSize(long maxStorageSizeInBytes) { try { SQLiteDatabase db = getDatabase(); long newMaxSize = db.setMaximumSize(maxStorageSizeInBytes); long pageSize = db.getPageSize(); long expectedMultipleMaxSize = maxStorageSizeInBytes / pageSize; if (maxStorageSizeInBytes % pageSize != 0) { expectedMultipleMaxSize++; } expectedMultipleMaxSize *= pageSize; if (newMaxSize != expectedMultipleMaxSize) { AppCenterLog.error(LOG_TAG, "Could not change maximum database size to " + maxStorageSizeInBytes + " bytes, current maximum size is " + newMaxSize + " bytes."); return false; } if (maxStorageSizeInBytes == newMaxSize) { AppCenterLog.info(LOG_TAG, "Changed maximum database size to " + newMaxSize + " bytes."); } else { AppCenterLog.info(LOG_TAG, "Changed maximum database size to " + newMaxSize + " bytes (next multiple of page size)."); } return true; } catch (RuntimeException e) { AppCenterLog.error(LOG_TAG, "Could not change maximum database size.", e); return false; } } 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 setMaxSizeFailed() { DatabaseManager databaseManagerMock = getDatabaseManagerMock(); assertFalse(databaseManagerMock.setMaxSize(1024 * 1024)); verifyStatic(); AppCenterLog.error(eq(AppCenter.LOG_TAG), anyString(), any(RuntimeException.class)); }