/* * Copyright (c) 2015 Denis Solonenko. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Public License v2.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html */ package ru.orangesoftware.financisto2.test.backup; import android.content.Context; import android.content.pm.PackageInfo; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.List; import java.util.zip.GZIPInputStream; import ru.orangesoftware.financisto2.backup.DatabaseExport; import ru.orangesoftware.financisto2.backup.DatabaseImport; import ru.orangesoftware.financisto2.export.Export; import ru.orangesoftware.financisto2.model.Account; import ru.orangesoftware.financisto2.model.AccountType; import ru.orangesoftware.financisto2.model.ElectronicPaymentType; import ru.orangesoftware.financisto2.model.Transaction; import ru.orangesoftware.financisto2.model.TransactionInfo; import ru.orangesoftware.financisto2.test.db.AbstractDbTest; import ru.orangesoftware.financisto2.test.export.AbstractImportExportTest; import ru.orangesoftware.financisto2.utils.Utils; public class LegacyDatabaseRestoreTest extends AbstractDbTest { public void test_should_restore_database_from_legacy_financisto1_backup_file() throws Exception { //given String backupFileContent = "PACKAGE:ru.orangesoftware.financisto\n" + "VERSION_CODE:88\n" + "VERSION_NAME:1.6.6\n" + "DATABASE_VERSION:204\n" + "#START\n" + "$ENTITY:currency\n" + "_id:3\n" + "name:EUR\n" + "title:European Euro\n" + "symbol:e\n" + "is_default:0\n" + "decimals:2\n" + "symbol_format:RS\n" + "updated_on:1363016900623\n" + "$$\n" + "$ENTITY:category\n" + "_id:0\n" + "title:No category\n" + "left:-1\n" + "right:82\n" + "last_location_id:0\n" + "last_project_id:0\n" + "sort_order:0\n" + "type:0\n" + "updated_on:0\n" + "remote_key:crebits-aaa::agxzfmZsb3d6ci1ocmRyEAsSCENhdGVnb3J5GISZPgyiAQtjcmViaXRzLWFhYQ\n" + "$$\n" + "$ENTITY:account\n" + "_id:20\n" + "title:PayPal\n" + "creation_date:1399552438975\n" + "currency_id:3\n" + "total_amount:375\n" + "type:PAYPAL\n" + "sort_order:0\n" + "is_active:1\n" + "is_include_into_totals:1\n" + "last_category_id:0\n" + "last_account_id:0\n" + "total_limit:0\n" + "card_issuer:VISA\n" + "closing_day:0\n" + "payment_day:0\n" + "last_transaction_date:1399552438985\n" + "updated_on:1399552438975\n" + "$$\n" + "$ENTITY:transactions\n" + "_id:2430\n" + "from_account_id:20\n" + "to_account_id:0\n" + "category_id:-1\n" + "project_id:0\n" + "location_id:0\n" + "from_amount:-1710\n" + "to_amount:0\n" + "datetime:1311064737834\n" + "provider:network\n" + "accuracy:1525\n" + "latitude:1.34163\n" + "longitude:103.97\n" + "is_template:0\n" + "status:UR\n" + "is_ccard_payment:0\n" + "last_recurrence:1311251678604\n" + "payee_id:0\n" + "parent_id:0\n" + "updated_on:0\n" + "original_currency_id:0\n" + "original_from_amount:0\n" + "$$\n" + "#END"; //when restoreDatabase(backupFileContent); //then Account account = getAccount(); assertEquals(AccountType.ELECTRONIC.name(), account.type); assertEquals(ElectronicPaymentType.PAYPAL.name(), account.cardIssuer); //and TransactionInfo transaction = getTransaction(account); assertTrue(transaction.isSplitParent()); } protected Account getAccount() { List<Account> accounts = db.getAllAccountsList(); assertEquals(1, accounts.size()); return accounts.get(0); } protected TransactionInfo getTransaction(Account account) { List<TransactionInfo> transactions = db.getTransactionsForAccount(account.id); assertEquals(1, transactions.size()); return transactions.get(0); } private String backupDatabase(boolean useGzip) throws Exception { Context context = getContext(); DatabaseExport databaseExport = new DatabaseExport(context, db.db(), useGzip); return databaseExport.export(); } private void restoreDatabase(String fileContent) throws IOException { Context context = getContext(); String fileName = createBackupFile(fileContent); DatabaseImport databaseImport = DatabaseImport.createFromFileBackup(context, db, categoryRepository, fileName); databaseImport.importDatabase(); deleteBackupFile(fileName); } private String createBackupFile(String fileContent) throws IOException { String fileName = "backup_" + System.currentTimeMillis() + ".backup"; FileOutputStream out = new FileOutputStream(new File(Export.getBackupFolder(getContext()), fileName)); out.write(fileContent.getBytes()); out.flush(); out.close(); return fileName; } private void deleteBackupFile(String fileName) { new File(Export.getBackupFolder(getContext()), fileName).delete(); } }