From d6c50b4db6e6ab36315d4a22da1ab9c1d15dd2e0 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sat, 6 Aug 2016 14:57:45 +0200 Subject: [PATCH] add unit tests for PendingCommand serialization --- .../PendingCommandSerializerTest.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 k9mail/src/test/java/com/fsck/k9/controller/PendingCommandSerializerTest.java diff --git a/k9mail/src/test/java/com/fsck/k9/controller/PendingCommandSerializerTest.java b/k9mail/src/test/java/com/fsck/k9/controller/PendingCommandSerializerTest.java new file mode 100644 index 000000000..eccdb8f54 --- /dev/null +++ b/k9mail/src/test/java/com/fsck/k9/controller/PendingCommandSerializerTest.java @@ -0,0 +1,80 @@ +package com.fsck.k9.controller; + + +import java.util.HashMap; + +import com.fsck.k9.controller.MessagingControllerCommands.PendingAppend; +import com.fsck.k9.controller.MessagingControllerCommands.PendingCommand; +import com.fsck.k9.controller.MessagingControllerCommands.PendingEmptyTrash; +import com.fsck.k9.controller.MessagingControllerCommands.PendingMoveOrCopy; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + + + +public class PendingCommandSerializerTest { + static final int DATABASE_ID = 123; + static final String UID = "uid"; + static final String SOURCE_FOLDER = "source_folder"; + static final String DEST_FOLDER = "dest_folder"; + static final HashMap UID_MAP = new HashMap<>(); + public static final boolean IS_COPY = true; + + static { + UID_MAP.put("uid_1", "uid_other_1"); + UID_MAP.put("uid_2", "uid_other_2"); + } + + + PendingCommandSerializer pendingCommandSerializer = PendingCommandSerializer.getInstance(); + + + @Test + public void testSerializeDeserialize__withoutArguments() { + PendingCommand pendingCommand = MessagingControllerCommands.createEmptyTrash(); + + 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 = MessagingControllerCommands.createAppend(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 = MessagingControllerCommands.createMoveOrCopyBulk( + 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); + } + + @Test(expected = IllegalArgumentException.class) + public void testDeserialize__withUnknownCommandName__shouldFail() { + PendingCommand pendingCommand = MessagingControllerCommands.createEmptyTrash(); + + String serializedCommand = pendingCommandSerializer.serialize(pendingCommand); + pendingCommandSerializer.unserialize(DATABASE_ID, "BAD_COMMAND_NAME", serializedCommand); + } + +} \ No newline at end of file