Repository: thundernest/k-9
Issue: master branch crashes on start when trying to add an IMAP account

### Expected behavior
k9 should allow to create an IMAP account without crashing

### Actual behavior
After entering the details for the incoming server, the screen for outgoing server settings appears shortly, but then k9 crashes with following errors:

```
E/AndroidRuntime: FATAL EXCEPTION: MessagingController
    Process: com.fsck.k9.debug, PID: 24126
    java.lang.AssertionError: Tried to create folder 'Junk' that already exists in the database as 'Junk' (8)
        at com.fsck.k9.mailstore.LocalStore$19.doDbWork(LocalStore.java:897)
        at com.fsck.k9.mailstore.LocalStore$19.doDbWork(LocalStore.java:880)
        at com.fsck.k9.mailstore.LockableDatabase.execute(LockableDatabase.java:273)
        at com.fsck.k9.mailstore.LocalStore.createFolders(LocalStore.java:880)
        at com.fsck.k9.mailstore.K9BackendStorage.createFolders(K9BackendStorage.kt:41)
        at com.fsck.k9.backend.imap.CommandRefreshFolderList.refreshFolderList(CommandRefreshFolderList.kt:25)
        at com.fsck.k9.backend.imap.ImapBackend.refreshFolderList(ImapBackend.java:112)
        at com.fsck.k9.controller.MessagingController.refreshRemoteSynchronous(MessagingController.java:454)
        at com.fsck.k9.controller.MessagingController$5.run(MessagingController.java:444)
        at com.fsck.k9.controller.MessagingController.runInBackground(MessagingController.java:222)
        at com.fsck.k9.controller.MessagingController.access$000(MessagingController.java:115)
        at com.fsck.k9.controller.MessagingController$1.run(MessagingController.java:165)
        at java.lang.Thread.run(Thread.java:761)
```

### Steps to reproduce
1. Clone git repository, open the project in Android studio, Build, start debug, wait for the start screen ("welcome to K-9 Mail) to appear
2. Click on "Next", then the screen "Set up a new account" shows up
3. enter email address and Password
4. Click on "MANUAL SETUP", then "Account type" shows up
5. click on "IMAP", then "Incoming server settings" shows up
6. Enter your server details
7. Click on "NEXT", then a screen appears with "checking incoming server settings..."
8. When the outgoing server settings screen appears, the app crashes

### Environment
K-9 Mail version: 5.700-SNAPSHOT, master on 2019-11-12

Android version: 7.1.2

Account type (IMAP, POP3, WebDAV/Exchange): IMAP



Changed files:
mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapStore.java, mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/ImapStoreTest.java

diff --git a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapStore.java b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapStore.java
index e15b62709..b723fa9ce 100644
--- a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapStore.java
+++ b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapStore.java
@@ -179,7 +179,7 @@ public class ImapStore extends RemoteStore {
                 ListResponse.parseLsub(responses) :
                 ListResponse.parseList(responses);
 
-        List<FolderListItem> folders = new ArrayList<>(listResponses.size());
+        Map<String, FolderListItem> folderMap = new HashMap<>(listResponses.size());
         for (ListResponse listResponse : listResponses) {
             String decodedFolderName;
             try {
@@ -235,10 +235,15 @@ public class ImapStore extends RemoteStore {
                 type = FolderType.REGULAR;
             }
 
-            folders.add(new FolderListItem(folder, type));
+            FolderListItem existingItem = folderMap.get(folder);
+            if (existingItem == null || existingItem.getType() == FolderType.REGULAR) {
+                folderMap.put(folder, new FolderListItem(folder, type));
+            }
         }
 
+        List<FolderListItem> folders = new ArrayList<>(folderMap.size() + 1);
         folders.add(new FolderListItem(ImapFolder.INBOX, FolderType.INBOX));
+        folders.addAll(folderMap.values());
 
         return folders;
     }
diff --git a/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/ImapStoreTest.java b/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/ImapStoreTest.java
index d7079fa16..13ceadc6e 100644
--- a/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/ImapStoreTest.java
+++ b/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/ImapStoreTest.java
@@ -241,6 +241,31 @@ public class ImapStoreTest {
         assertEquals(Sets.newSet("INBOX", "FolderOne"), extractFolderNames(result));
     }
 
+    @Test
+    public void getPersonalNamespaces_withDuplicateFolderNames_shouldRemoveDuplicatesAndKeepFolderType()
+            throws Exception {
+        ImapConnection imapConnection = mock(ImapConnection.class);
+        when(imapConnection.hasCapability(Capabilities.LIST_EXTENDED)).thenReturn(true);
+        when(imapConnection.hasCapability(Capabilities.SPECIAL_USE)).thenReturn(true);
+        List<ImapResponse> imapResponses = Arrays.asList(
+                createImapResponse("* LIST () \\".\\" \\"INBOX\\""),
+                createImapResponse("* LIST (\\\\HasNoChildren) \\".\\" \\"Junk\\""),
+                createImapResponse("* LIST (\\\\Junk) \\".\\" \\"Junk\\""),
+                createImapResponse("* LIST (\\\\HasNoChildren) \\".\\" \\"Junk\\""),
+                createImapResponse("5 OK Success")
+        );
+        when(imapConnection.executeSimpleCommand("LIST \\"\\" \\"*\\" RETURN (SPECIAL-USE)")).thenReturn(imapResponses);
+        imapStore.enqueueImapConnection(imapConnection);
+
+        List<ImapFolder> result = imapStore.getPersonalNamespaces();
+
+        assertNotNull(result);
+        assertEquals(2, result.size());
+        ImapFolder junkFolder = getFolderByName(result, "Junk");
+        assertNotNull(junkFolder);
+        assertEquals(FolderType.SPAM, junkFolder.getType());
+    }
+
     @Test
     public void getPersonalNamespaces_withoutException_shouldLeaveImapConnectionOpen() throws Exception {
         ImapConnection imapConnection = mock(ImapConnection.class);
@@ -367,6 +392,15 @@ public class ImapStoreTest {
         return folderNames;
     }
 
+    private ImapFolder getFolderByName(List<ImapFolder> result, String folderName) {
+        for (ImapFolder imapFolder : result) {
+            if (imapFolder.getName().equals(folderName)) {
+                return imapFolder;
+            }
+        }
+        return null;
+    }
+
     private Map<String, ImapFolder> toFolderMap(List<ImapFolder> folders) {
         Map<String, ImapFolder> folderMap = new HashMap<>();
         for (ImapFolder folder : folders) {