@Test(expected = IllegalArgumentException.class) public void testDeserialize__withUnknownCommandName__shouldFail() { PendingCommand pendingCommand = PendingEmptyTrash.create(); String serializedCommand = pendingCommandSerializer.serialize(pendingCommand); pendingCommandSerializer.unserialize(DATABASE_ID, "BAD_COMMAND_NAME", serializedCommand); }
public static void migratePendingCommands(SQLiteDatabase db) { List<PendingCommand> pendingCommands = new ArrayList<>(); if (columnExists(db, "pending_commands", "arguments")) { for (OldPendingCommand oldPendingCommand : getPendingCommands(db)) { PendingCommand newPendingCommand = migratePendingCommand(oldPendingCommand); pendingCommands.add(newPendingCommand); } db.execSQL("DROP TABLE IF EXISTS pending_commands"); db.execSQL("CREATE TABLE pending_commands (" + "id INTEGER PRIMARY KEY, " + "command TEXT, " + "data TEXT" + ")"); PendingCommandSerializer pendingCommandSerializer = PendingCommandSerializer.getInstance(); for (PendingCommand pendingCommand : pendingCommands) { ContentValues cv = new ContentValues(); cv.put("command", pendingCommand.getCommandName()); cv.put("data", pendingCommandSerializer.serialize(pendingCommand)); db.insert("pending_commands", "command", cv); } } }
public void addPendingCommand(PendingCommand command) throws MessagingException { final ContentValues cv = new ContentValues(); cv.put("command", command.getCommandName()); cv.put("data", pendingCommandSerializer.serialize(command)); database.execute(false, new DbCallback<Void>() { @Override public Void doDbWork(final SQLiteDatabase db) throws WrappedException { db.insert("pending_commands", "command", cv); return null; } }); }
@Override public List<PendingCommand> doDbWork(final SQLiteDatabase db) throws WrappedException { Cursor cursor = null; try { cursor = db.query("pending_commands", new String[] { "id", "command", "data" }, null, null, null, null, "id ASC"); List<PendingCommand> commands = new ArrayList<>(); while (cursor.moveToNext()) { long databaseId = cursor.getLong(0); String commandName = cursor.getString(1); String data = cursor.getString(2); PendingCommand command = pendingCommandSerializer.unserialize( databaseId, commandName, data); commands.add(command); } return commands; } finally { Utility.closeQuietly(cursor); } } });
/** * local://localhost/path/to/database/uuid.db * This constructor is only used by {@link LocalStoreProvider#getInstance(Account)} * @throws UnavailableStorageException if not {@link StorageProvider#isReady(Context)} */ private LocalStore(final Account account, final Context context) throws MessagingException { this.context = context; this.contentResolver = context.getContentResolver(); messagePreviewCreator = MessagePreviewCreator.newInstance(); messageFulltextCreator = MessageFulltextCreator.newInstance(); attachmentCounter = AttachmentCounter.newInstance(); pendingCommandSerializer = PendingCommandSerializer.getInstance(); attachmentInfoExtractor = DI.get(AttachmentInfoExtractor.class); this.account = account; SchemaDefinitionFactory schemaDefinitionFactory = DI.get(SchemaDefinitionFactory.class); RealMigrationsHelper migrationsHelper = new RealMigrationsHelper(); SchemaDefinition schemaDefinition = schemaDefinitionFactory.createSchemaDefinition(migrationsHelper); database = new LockableDatabase(context, account.getUuid(), schemaDefinition); database.setStorageProviderId(account.getLocalStorageProviderId()); database.open(); Clock clock = DI.get(Clock.class); outboxStateRepository = new OutboxStateRepository(database, clock); }
@Test public void testSerializeDeserialize__withoutArguments() { PendingCommand pendingCommand = PendingEmptyTrash.create(); String serializedCommand = pendingCommandSerializer.serialize(pendingCommand); PendingEmptyTrash unserializedCommand = (PendingEmptyTrash) pendingCommandSerializer.unserialize( DATABASE_ID, pendingCommand.getCommandName(), serializedCommand); assertEquals(DATABASE_ID, unserializedCommand.databaseId); }
@Test public void testSerializeDeserialize__withArguments() { PendingCommand pendingCommand = PendingAppend.create(SOURCE_FOLDER, UID); String serializedCommand = pendingCommandSerializer.serialize(pendingCommand); PendingAppend unserializedCommand = (PendingAppend) pendingCommandSerializer.unserialize( DATABASE_ID, pendingCommand.getCommandName(), serializedCommand); assertEquals(DATABASE_ID, unserializedCommand.databaseId); assertEquals(SOURCE_FOLDER, unserializedCommand.folder); assertEquals(UID, unserializedCommand.uid); }
@Test public void testSerializeDeserialize__withComplexArguments() { PendingCommand pendingCommand = PendingMoveOrCopy.create( SOURCE_FOLDER, DEST_FOLDER, IS_COPY, UID_MAP); String serializedCommand = pendingCommandSerializer.serialize(pendingCommand); PendingMoveOrCopy unserializedCommand = (PendingMoveOrCopy) pendingCommandSerializer.unserialize( DATABASE_ID, pendingCommand.getCommandName(), serializedCommand); assertEquals(DATABASE_ID, unserializedCommand.databaseId); assertEquals(SOURCE_FOLDER, unserializedCommand.srcFolder); assertEquals(DEST_FOLDER, unserializedCommand.destFolder); assertEquals(UID_MAP, unserializedCommand.newUidMap); }