BackupManager { static void backupAllBooks() { BooksDbAdapter booksDbAdapter = BooksDbAdapter.getInstance(); List<String> bookUIDs = booksDbAdapter.getAllBookUIDs(); Context context = GnuCashApplication.getAppContext(); for (String bookUID : bookUIDs) { String backupFile = getBookBackupFileUri(bookUID); if (backupFile == null){ backupBook(bookUID); continue; } try (BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(context.getContentResolver().openOutputStream(Uri.parse(backupFile)))){ GZIPOutputStream gzipOutputStream = new GZIPOutputStream(bufferedOutputStream); OutputStreamWriter writer = new OutputStreamWriter(gzipOutputStream); ExportParams params = new ExportParams(ExportFormat.XML); new GncXmlExporter(params).generateExport(writer); writer.close(); } catch (IOException ex) { Log.e(LOG_TAG, "Auto backup failed for book " + bookUID); ex.printStackTrace(); Crashlytics.logException(ex); } } }  static boolean backupActiveBook(); static boolean backupBook(String bookUID); @Nullable static String getBookBackupFileUri(String bookUID); static List<File> getBackupList(String bookUID); static void schedulePeriodicBackups(Context context); static final String KEY_BACKUP_FILE; }
@Test public void backupAllBooks() throws Exception { String activeBookUID = createNewBookWithDefaultAccounts(); BookUtils.activateBook(activeBookUID); createNewBookWithDefaultAccounts(); assertThat(mBooksDbAdapter.getRecordsCount()).isEqualTo(2); BackupManager.backupAllBooks(); for (String bookUID : mBooksDbAdapter.getAllBookUIDs()) { assertThat(BackupManager.getBackupList(bookUID).size()).isEqualTo(1); } }