diff --git a/k9mail/src/test/java/com/fsck/k9/mailstore/migrations/MigrationTo60Test.java b/k9mail/src/test/java/com/fsck/k9/mailstore/migrations/MigrationTo60Test.java index 1d1ed7366..0f8796838 100644 --- a/k9mail/src/test/java/com/fsck/k9/mailstore/migrations/MigrationTo60Test.java +++ b/k9mail/src/test/java/com/fsck/k9/mailstore/migrations/MigrationTo60Test.java @@ -1,11 +1,7 @@ package com.fsck.k9.mailstore.migrations; -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; - import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -13,6 +9,9 @@ import java.util.List; import java.util.Map; import java.util.Set; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; + import com.fsck.k9.controller.MessagingControllerCommands.PendingAppend; import com.fsck.k9.controller.MessagingControllerCommands.PendingCommand; import com.fsck.k9.controller.MessagingControllerCommands.PendingEmptyTrash; @@ -27,11 +26,12 @@ import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; -import static org.junit.Assert.assertArrayEquals; +import static java.util.Arrays.asList; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; + @RunWith(RobolectricTestRunner.class) @Config(manifest = Config.NONE) public class MigrationTo60Test { @@ -53,51 +53,33 @@ public class MigrationTo60Test { private static final Flag FLAG = Flag.X_DESTROYED; private static final String UID = "uid"; private static final HashMap UID_MAP = new HashMap<>(); + static { UID_MAP.put("uid_1", "uid_other_1"); UID_MAP.put("uid_2", "uid_other_2"); } + @Test - public void migratePendingCommands_changesTableStructure() { - SQLiteDatabase database = SQLiteDatabase.create(null); - createV59Table(database); + public void migratePendingCommands_shouldChangeTableStructure() { + SQLiteDatabase database = createV59Table(); + MigrationTo60.migratePendingCommands(database); List columns = getColumnList(database, "pending_commands"); - - assertArrayEquals(new String[]{"id", "command", "data"}, columns.toArray()); + assertEquals(asList("id", "command", "data"), columns); } @Test - public void migratePendingCommands_isMultiRunSafe() { - SQLiteDatabase database = SQLiteDatabase.create(null); - createV59Table(database); + public void migratePendingCommands_withMultipleRuns_shouldNotThrow() { + SQLiteDatabase database = createV59Table(); MigrationTo60.migratePendingCommands(database); MigrationTo60.migratePendingCommands(database); } - private List getColumnList(SQLiteDatabase db, String table) { - List columns = new ArrayList<>(); - Cursor columnCursor = db.rawQuery("PRAGMA table_info("+table+")", null); - while (columnCursor.moveToNext()) { - columns.add(columnCursor.getString(1)); - } - return columns; - } - - private void createV59Table(SQLiteDatabase db) { - db.execSQL("CREATE TABLE pending_commands (" + - "id INTEGER PRIMARY KEY, " + - "command TEXT, " + - "arguments TEXT" + - ")"); - } - - @Test - public void testMigrateMoveOrCopy__withUidArray() { + public void migrateMoveOrCopy_withUidArray() { OldPendingCommand command = queueMoveOrCopy(SOURCE_FOLDER, DEST_FOLDER, IS_COPY, UID_ARRAY); PendingMoveOrCopy pendingCommand = (PendingMoveOrCopy) MigrationTo60.migratePendingCommand(command); @@ -105,10 +87,117 @@ public class MigrationTo60Test { assertEquals(SOURCE_FOLDER, pendingCommand.srcFolder); assertEquals(DEST_FOLDER, pendingCommand.destFolder); assertEquals(IS_COPY, pendingCommand.isCopy); - assertEquals(Arrays.asList(UID_ARRAY), pendingCommand.uids); + assertEquals(asList(UID_ARRAY), pendingCommand.uids); assertNull(pendingCommand.newUidMap); } + @Test + public void migrateMoveOrCopy_withUidMap() { + OldPendingCommand command = queueMoveOrCopy(SOURCE_FOLDER, DEST_FOLDER, IS_COPY, UID_MAP); + + PendingMoveOrCopy pendingCommand = (PendingMoveOrCopy) MigrationTo60.migratePendingCommand(command); + + assertEquals(SOURCE_FOLDER, pendingCommand.srcFolder); + assertEquals(DEST_FOLDER, pendingCommand.destFolder); + assertEquals(IS_COPY, pendingCommand.isCopy); + assertEquals(UID_MAP, pendingCommand.newUidMap); + assertNull(pendingCommand.uids); + } + + @Test + public void migrateMoveOrCopy_withOldFormat() { + OldPendingCommand command = queueMoveOrCopyOld(SOURCE_FOLDER, DEST_FOLDER, IS_COPY, UID_ARRAY); + + PendingMoveOrCopy pendingCommand = (PendingMoveOrCopy) MigrationTo60.migratePendingCommand(command); + + assertEquals(SOURCE_FOLDER, pendingCommand.srcFolder); + assertEquals(DEST_FOLDER, pendingCommand.destFolder); + assertEquals(IS_COPY, pendingCommand.isCopy); + assertEquals(asList(UID_ARRAY), pendingCommand.uids); + assertNull(pendingCommand.newUidMap); + } + + @Test + public void migrateMoveOrCopy__withEvenOlderFormat() { + OldPendingCommand command = queueMoveOrCopyEvenOlder(SOURCE_FOLDER, DEST_FOLDER, UID, IS_COPY); + + PendingMoveOrCopy pendingCommand = (PendingMoveOrCopy) MigrationTo60.migratePendingCommand(command); + + assertEquals(SOURCE_FOLDER, pendingCommand.srcFolder); + assertEquals(DEST_FOLDER, pendingCommand.destFolder); + assertEquals(IS_COPY, pendingCommand.isCopy); + assertEquals(Collections.singletonList(UID), pendingCommand.uids); + assertNull(pendingCommand.newUidMap); + } + + @Test + public void migrateSetFlag() { + OldPendingCommand command = queueSetFlagBulk(SOURCE_FOLDER, FLAG_STATE, FLAG, UID_ARRAY); + + PendingSetFlag pendingCommand = (PendingSetFlag) MigrationTo60.migratePendingCommand(command); + + assertEquals(SOURCE_FOLDER, pendingCommand.folder); + assertEquals(FLAG_STATE, pendingCommand.newState); + assertEquals(FLAG, pendingCommand.flag); + assertEquals(asList(UID_ARRAY), pendingCommand.uids); + } + + @Test + public void migrateSetFlag_oldFormat() { + OldPendingCommand command = queueSetFlagOld(SOURCE_FOLDER, FLAG_STATE, FLAG, UID); + + PendingSetFlag pendingCommand = (PendingSetFlag) MigrationTo60.migratePendingCommand(command); + + assertEquals(SOURCE_FOLDER, pendingCommand.folder); + assertEquals(FLAG_STATE, pendingCommand.newState); + assertEquals(FLAG, pendingCommand.flag); + assertEquals(Collections.singletonList(UID), pendingCommand.uids); + } + + @Test + public void migrateExpunge() { + OldPendingCommand command = queueExpunge(SOURCE_FOLDER); + + PendingExpunge pendingCommand = (PendingExpunge) MigrationTo60.migratePendingCommand(command); + + assertEquals(SOURCE_FOLDER, pendingCommand.folder); + } + + @Test + public void migrateEmptyTrash() { + OldPendingCommand command = queueEmptyTrash(); + + PendingCommand pendingCommand = MigrationTo60.migratePendingCommand(command); + + assertTrue(pendingCommand instanceof PendingEmptyTrash); + } + + @Test + public void migrateMarkAllMessagesRead() { + OldPendingCommand command = queueMarkAllMessagesRead(SOURCE_FOLDER); + + PendingMarkAllAsRead pendingCommand = (PendingMarkAllAsRead) MigrationTo60.migratePendingCommand(command); + + assertEquals(SOURCE_FOLDER, pendingCommand.folder); + } + + @Test + public void migrateAppend() { + OldPendingCommand command = queueAppend(SOURCE_FOLDER, UID); + + PendingAppend pendingCommand = (PendingAppend) MigrationTo60.migratePendingCommand(command); + + assertEquals(SOURCE_FOLDER, pendingCommand.folder); + assertEquals(UID, pendingCommand.uid); + } + + OldPendingCommand queueAppend(String srcFolder, String uid) { + OldPendingCommand command = new OldPendingCommand(); + command.command = PENDING_COMMAND_APPEND; + command.arguments = new String[] { srcFolder, uid }; + return command; + } + OldPendingCommand queueMoveOrCopy(String srcFolder, String destFolder, boolean isCopy, String uids[]) { OldPendingCommand command = new OldPendingCommand(); command.command = PENDING_COMMAND_MOVE_OR_COPY_BULK_NEW; @@ -123,22 +212,7 @@ public class MigrationTo60Test { return command; } - - @Test - public void testMigrateMoveOrCopy__withUidMap() { - OldPendingCommand command = queueMoveOrCopy(SOURCE_FOLDER, DEST_FOLDER, IS_COPY, UID_MAP); - - PendingMoveOrCopy pendingCommand = (PendingMoveOrCopy) MigrationTo60.migratePendingCommand(command); - - assertEquals(SOURCE_FOLDER, pendingCommand.srcFolder); - assertEquals(DEST_FOLDER, pendingCommand.destFolder); - assertEquals(IS_COPY, pendingCommand.isCopy); - assertEquals(UID_MAP, pendingCommand.newUidMap); - assertNull(pendingCommand.uids); - } - - OldPendingCommand queueMoveOrCopy( - String srcFolder, String destFolder, boolean isCopy, Map uidMap) { + OldPendingCommand queueMoveOrCopy(String srcFolder, String destFolder, boolean isCopy, Map uidMap) { OldPendingCommand command = new OldPendingCommand(); command.command = PENDING_COMMAND_MOVE_OR_COPY_BULK_NEW; @@ -151,24 +225,11 @@ public class MigrationTo60Test { Set strings = uidMap.keySet(); System.arraycopy(strings.toArray(new String[strings.size()]), 0, command.arguments, 4, uidMap.keySet().size()); Collection values = uidMap.values(); - System.arraycopy(values.toArray(new String[values.size()]), 0, command.arguments, 4 + uidMap.keySet().size(), uidMap.values().size()); + System.arraycopy(values.toArray(new String[values.size()]), 0, command.arguments, 4 + uidMap.keySet().size(), + uidMap.values().size()); return command; } - - @Test - public void testMigrateMoveOrCopy__withOldFormat() { - OldPendingCommand command = queueMoveOrCopyOld(SOURCE_FOLDER, DEST_FOLDER, IS_COPY, UID_ARRAY); - - PendingMoveOrCopy pendingCommand = (PendingMoveOrCopy) MigrationTo60.migratePendingCommand(command); - - assertEquals(SOURCE_FOLDER, pendingCommand.srcFolder); - assertEquals(DEST_FOLDER, pendingCommand.destFolder); - assertEquals(IS_COPY, pendingCommand.isCopy); - assertEquals(Arrays.asList(UID_ARRAY), pendingCommand.uids); - assertNull(pendingCommand.newUidMap); - } - OldPendingCommand queueMoveOrCopyOld(String srcFolder, String destFolder, boolean isCopy, String uids[]) { OldPendingCommand command = new OldPendingCommand(); command.command = PENDING_COMMAND_MOVE_OR_COPY_BULK; @@ -182,20 +243,6 @@ public class MigrationTo60Test { return command; } - - @Test - public void testMigrateMoveOrCopy__withEvenOlderFormat() { - OldPendingCommand command = queueMoveOrCopyEvenOlder(SOURCE_FOLDER, DEST_FOLDER, UID, IS_COPY); - - PendingMoveOrCopy pendingCommand = (PendingMoveOrCopy) MigrationTo60.migratePendingCommand(command); - - assertEquals(SOURCE_FOLDER, pendingCommand.srcFolder); - assertEquals(DEST_FOLDER, pendingCommand.destFolder); - assertEquals(IS_COPY, pendingCommand.isCopy); - assertEquals(Collections.singletonList(UID), pendingCommand.uids); - assertNull(pendingCommand.newUidMap); - } - OldPendingCommand queueMoveOrCopyEvenOlder(String srcFolder, String destFolder, String uid, boolean isCopy) { OldPendingCommand command = new OldPendingCommand(); command.command = PENDING_COMMAND_MOVE_OR_COPY; @@ -208,19 +255,6 @@ public class MigrationTo60Test { return command; } - - @Test - public void testMigrateSetFlag() { - OldPendingCommand command = queueSetFlagBulk(SOURCE_FOLDER, FLAG_STATE, FLAG, UID_ARRAY); - - PendingSetFlag pendingCommand = (PendingSetFlag) MigrationTo60.migratePendingCommand(command); - - assertEquals(SOURCE_FOLDER, pendingCommand.folder); - assertEquals(FLAG_STATE, pendingCommand.newState); - assertEquals(FLAG, pendingCommand.flag); - assertEquals(Arrays.asList(UID_ARRAY), pendingCommand.uids); - } - OldPendingCommand queueSetFlagBulk(String folderName, boolean newState, Flag flag, String[] uids) { OldPendingCommand command = new OldPendingCommand(); command.command = PENDING_COMMAND_SET_FLAG_BULK; @@ -233,19 +267,6 @@ public class MigrationTo60Test { return command; } - - @Test - public void testMigrateSetFlag__oldFormat() { - OldPendingCommand command = queueSetFlagOld(SOURCE_FOLDER, FLAG_STATE, FLAG, UID); - - PendingSetFlag pendingCommand = (PendingSetFlag) MigrationTo60.migratePendingCommand(command); - - assertEquals(SOURCE_FOLDER, pendingCommand.folder); - assertEquals(FLAG_STATE, pendingCommand.newState); - assertEquals(FLAG, pendingCommand.flag); - assertEquals(Collections.singletonList(UID), pendingCommand.uids); - } - OldPendingCommand queueSetFlagOld(String folderName, boolean newState, Flag flag, String uid) { OldPendingCommand command = new OldPendingCommand(); command.command = PENDING_COMMAND_SET_FLAG; @@ -257,16 +278,6 @@ public class MigrationTo60Test { return command; } - - @Test - public void testMigrateExpunge() { - OldPendingCommand command = queueExpunge(SOURCE_FOLDER); - - PendingExpunge pendingCommand = (PendingExpunge) MigrationTo60.migratePendingCommand(command); - - assertEquals(SOURCE_FOLDER, pendingCommand.folder); - } - OldPendingCommand queueExpunge(String folderName) { OldPendingCommand command = new OldPendingCommand(); command.command = PENDING_COMMAND_EXPUNGE; @@ -275,16 +286,6 @@ public class MigrationTo60Test { return command; } - - @Test - public void testMigrateEmptyTrash() { - OldPendingCommand command = queueEmptyTrash(); - - PendingCommand pendingCommand = MigrationTo60.migratePendingCommand(command); - - assertTrue(pendingCommand instanceof PendingEmptyTrash); - } - OldPendingCommand queueEmptyTrash() { OldPendingCommand command = new OldPendingCommand(); command.command = PENDING_COMMAND_EMPTY_TRASH; @@ -292,16 +293,6 @@ public class MigrationTo60Test { return command; } - - @Test - public void testMigrateMarkAllMessagesRead() { - OldPendingCommand command = queueMarkAllMessagesRead(SOURCE_FOLDER); - - PendingMarkAllAsRead pendingCommand = (PendingMarkAllAsRead) MigrationTo60.migratePendingCommand(command); - - assertEquals(SOURCE_FOLDER, pendingCommand.folder); - } - OldPendingCommand queueMarkAllMessagesRead(final String folder) { OldPendingCommand command = new OldPendingCommand(); command.command = PENDING_COMMAND_MARK_ALL_AS_READ; @@ -309,22 +300,26 @@ public class MigrationTo60Test { return command; } - - @Test - public void testMigrateAppend() { - OldPendingCommand command = queueAppend(SOURCE_FOLDER, UID); - - PendingAppend pendingCommand = (PendingAppend) MigrationTo60.migratePendingCommand(command); - - assertEquals(SOURCE_FOLDER, pendingCommand.folder); - assertEquals(UID, pendingCommand.uid); + private List getColumnList(SQLiteDatabase db, String table) { + List columns = new ArrayList<>(); + Cursor columnCursor = db.rawQuery("PRAGMA table_info(" + table + ")", null); + try { + while (columnCursor.moveToNext()) { + columns.add(columnCursor.getString(1)); + } + } finally { + columnCursor.close(); + } + return columns; } - OldPendingCommand queueAppend(String srcFolder, String uid) { - OldPendingCommand command = new OldPendingCommand(); - command.command = PENDING_COMMAND_APPEND; - command.arguments = new String[] { srcFolder, uid }; - return command; + private SQLiteDatabase createV59Table() { + SQLiteDatabase database = SQLiteDatabase.create(null); + database.execSQL("CREATE TABLE pending_commands (" + + "id INTEGER PRIMARY KEY, " + + "command TEXT, " + + "arguments TEXT" + + ")"); + return database; } - -} \ No newline at end of file +}