Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 30f3aeba authored by Hari's avatar Hari
Browse files

Fix and add tests

parent 99a360de
Loading
Loading
Loading
Loading
+22 −24
Original line number Diff line number Diff line
package com.fsck.k9.mail.store.imap.response;
package com.fsck.k9.mail.store.imap;


import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.fsck.k9.mail.store.imap.ImapResponse;
import org.junit.Test;
import org.mockito.internal.util.collections.Sets;

@@ -19,9 +19,7 @@ public class CapabilityResponseTest {

    @Test
    public void parse_withProperResponseContainingCapabilityCode() throws Exception {
        ImapResponse response = createImapResponse("* OK [CAPABILITY IMAP4rev1 IDLE] Welcome");

        CapabilityResponse result = CapabilityResponse.parse(null, Collections.singletonList(response));
        CapabilityResponse result = parse("* OK [CAPABILITY IMAP4rev1 IDLE] Welcome");

        assertNotNull(result);
        assertEquals(Sets.newSet("IMAP4REV1", "IDLE"), result.getCapabilities());
@@ -29,36 +27,30 @@ public class CapabilityResponseTest {

    @Test
    public void parse_withTaggedResponse_shouldReturnNull() throws Exception {
        ImapResponse response = createImapResponse("1 OK");

        CapabilityResponse result = CapabilityResponse.parse(null, Collections.singletonList(response));
        CapabilityResponse result = parse("1 OK");

        assertNull(result);
    }

    @Test
    public void parse_withoutOkResponse_shouldReturnNull() throws Exception {
        ImapResponse response = createImapResponse("* BAD Go Away");

        CapabilityResponse result = CapabilityResponse.parse(null, Collections.singletonList(response));
        CapabilityResponse result = parse("* BAD Go Away");

        assertNull(result);
    }

    @Test
    public void parse_withOkResponseWithoutList_shouldReturnNull() throws Exception {
        ImapResponse response = createImapResponse("* OK Welcome");

        CapabilityResponse result = CapabilityResponse.parse(null, Collections.singletonList(response));
        CapabilityResponse result = parse("* OK Welcome");

        assertNull(result);
    }

    @Test
    public void parse_withProperCapabilityResponse() throws Exception {
        ImapResponse response = createImapResponse("* CAPABILITY IMAP4rev1 STARTTLS AUTH=GSSAPI XPIG-LATIN");
        ImapList list = createImapResponse("* CAPABILITY IMAP4rev1 STARTTLS AUTH=GSSAPI XPIG-LATIN");

        CapabilityResponse result = CapabilityResponse.parse(null, Collections.singletonList(response));
        CapabilityResponse result = CapabilityResponse.parse(list);

        assertNotNull(result);
        assertEquals(Sets.newSet("IMAP4REV1", "STARTTLS", "AUTH=GSSAPI", "XPIG-LATIN"), result.getCapabilities());
@@ -66,18 +58,18 @@ public class CapabilityResponseTest {

    @Test
    public void parse_withListInCapabilityResponse_shouldReturnNull() throws Exception {
        ImapResponse response = createImapResponse("* CAPABILITY IMAP4rev1 []");
        ImapList list = createImapResponse("* CAPABILITY IMAP4rev1 []");

        CapabilityResponse result = CapabilityResponse.parse(null, Collections.singletonList(response));
        CapabilityResponse result = CapabilityResponse.parse(list);

        assertNull(result);
    }

    @Test
    public void parse_withoutCapabilityResponse_shouldReturnNull() throws Exception {
        ImapResponse response = createImapResponse("* EXISTS 1");
        ImapList list = createImapResponse("* EXISTS 1");

        CapabilityResponse result = CapabilityResponse.parse(null, Collections.singletonList(response));
        CapabilityResponse result = CapabilityResponse.parse(list);

        assertNull(result);
    }
@@ -86,7 +78,7 @@ public class CapabilityResponseTest {
    public void parse_withEmptyResponseList_shouldReturnNull() throws Exception {
        List<ImapResponse> responses = Collections.emptyList();

        CapabilityResponse result = CapabilityResponse.parse(null, responses);
        CapabilityResponse result = CapabilityResponse.parse(responses);

        assertNull(result);
    }
@@ -95,7 +87,7 @@ public class CapabilityResponseTest {
    public void parse_withoutCapabilityResponseInResponseList_shouldReturnNull() throws Exception {
        List<ImapResponse> responses = Collections.singletonList(createImapResponse("* EXISTS 42"));

        CapabilityResponse result = CapabilityResponse.parse(null, responses);
        CapabilityResponse result = CapabilityResponse.parse(responses);

        assertNull(result);
    }
@@ -105,7 +97,7 @@ public class CapabilityResponseTest {
        ImapResponse response = createImapResponse("* CAPABILITY IMAP4rev1 LOGINDISABLED STARTTLS");
        List<ImapResponse> responses = Collections.singletonList(response);

        CapabilityResponse result = CapabilityResponse.parse(null, responses);
        CapabilityResponse result = CapabilityResponse.parse(responses);

        assertNotNull(result);
        assertEquals(Sets.newSet("IMAP4REV1", "STARTTLS", "LOGINDISABLED"), result.getCapabilities());
@@ -117,10 +109,16 @@ public class CapabilityResponseTest {
        ImapResponse responseTwo = createImapResponse("* CAPABILITY IMAP4rev1 IDLE");
        List<ImapResponse> responses = Arrays.asList(responseOne, responseTwo);

        CapabilityResponse result = CapabilityResponse.parse(null, responses);
        CapabilityResponse result = CapabilityResponse.parse(responses);

        assertNotNull(result);
        assertEquals(Sets.newSet("IMAP4REV1", "IDLE"), result.getCapabilities());
    }

    private CapabilityResponse parse(String responseText) throws IOException {
        ImapResponse response = createImapResponse(responseText);
        List<ImapResponse> responses = Collections.singletonList(response);

        return CapabilityResponse.parse(responses);
    }
}
+22 −22
Original line number Diff line number Diff line
@@ -371,7 +371,7 @@ public class ImapFolderTest {

        sourceFolder.moveMessages(messages, destinationFolder);

        verify(imapConnection).executeSimpleCommand("UID STORE 1 +FLAGS.SILENT (\\Deleted)", false);
        verify(imapConnection).executeSimpleCommand("UID STORE 1 +FLAGS.SILENT (\\Deleted)");
    }

    @Test
@@ -404,7 +404,7 @@ public class ImapFolderTest {

        folder.delete(messages, "Folder");

        verify(imapConnection).executeSimpleCommand("UID STORE 23 +FLAGS.SILENT (\\Deleted)", false);
        verify(imapConnection).executeSimpleCommand("UID STORE 23 +FLAGS.SILENT (\\Deleted)");
    }

    @Test
@@ -422,7 +422,7 @@ public class ImapFolderTest {

        folder.delete(messages, "Trash");

        verify(imapConnection).executeSimpleCommand("UID STORE 2 +FLAGS.SILENT (\\Deleted)", false);
        verify(imapConnection).executeSimpleCommand("UID STORE 2 +FLAGS.SILENT (\\Deleted)");
    }

    @Test
@@ -462,7 +462,7 @@ public class ImapFolderTest {
    public void getUnreadMessageCount_connectionThrowsIOException_shouldThrowMessagingException() throws Exception {
        ImapFolder folder = createFolder("Folder");
        prepareImapFolderForOpen(OPEN_MODE_RW);
        when(imapConnection.executeSimpleCommand("UID SEARCH 1:* NOT DELETED NOT SEEN", false)).thenThrow(new IOException());
        when(imapConnection.executeSimpleCommand("UID SEARCH 1:* NOT DELETED NOT SEEN")).thenThrow(new IOException());
        folder.open(OPEN_MODE_RW);

        try {
@@ -478,7 +478,7 @@ public class ImapFolderTest {
        ImapFolder folder = createFolder("Folder");
        prepareImapFolderForOpen(OPEN_MODE_RW);
        List<ImapResponse> imapResponses = singletonList(createImapResponse("* SEARCH 1 2 3"));
        when(imapConnection.executeSimpleCommand("UID SEARCH 1:* NOT DELETED NOT SEEN", false)).thenReturn(imapResponses);
        when(imapConnection.executeSimpleCommand("UID SEARCH 1:* NOT DELETED NOT SEEN")).thenReturn(imapResponses);
        folder.open(OPEN_MODE_RW);

        int unreadMessageCount = folder.getUnreadMessageCount();
@@ -507,7 +507,7 @@ public class ImapFolderTest {
                createImapResponse("* SEARCH 1 2"),
                createImapResponse("* SEARCH 23 42")
        );
        when(imapConnection.executeSimpleCommand("UID SEARCH 1:* FLAGGED NOT DELETED", false)).thenReturn(imapResponses);
        when(imapConnection.executeSimpleCommand("UID SEARCH 1:* FLAGGED NOT DELETED")).thenReturn(imapResponses);
        folder.open(OPEN_MODE_RW);

        int flaggedMessageCount = folder.getFlaggedMessageCount();
@@ -520,7 +520,7 @@ public class ImapFolderTest {
        ImapFolder folder = createFolder("Folder");
        prepareImapFolderForOpen(OPEN_MODE_RW);
        List<ImapResponse> imapResponses = singletonList(createImapResponse("* SEARCH 42"));
        when(imapConnection.executeSimpleCommand("UID SEARCH *:*", false)).thenReturn(imapResponses);
        when(imapConnection.executeSimpleCommand("UID SEARCH *:*")).thenReturn(imapResponses);
        folder.open(OPEN_MODE_RW);

        long highestUid = folder.getHighestUid();
@@ -544,7 +544,7 @@ public class ImapFolderTest {
    public void getHighestUid_imapConnectionThrowsIOException_shouldThrowMessagingException() throws Exception {
        ImapFolder folder = createFolder("Folder");
        prepareImapFolderForOpen(OPEN_MODE_RW);
        doThrow(IOException.class).when(imapConnection).executeSimpleCommand("UID SEARCH *:*", false);
        doThrow(IOException.class).when(imapConnection).executeSimpleCommand("UID SEARCH *:*");
        folder.open(OPEN_MODE_RW);

        try {
@@ -564,7 +564,7 @@ public class ImapFolderTest {
                createImapResponse("* SEARCH 5"),
                createImapResponse("* SEARCH 6")
        );
        when(imapConnection.executeSimpleCommand("UID SEARCH 1:10 NOT DELETED", false)).thenReturn(imapResponses);
        when(imapConnection.executeSimpleCommand("UID SEARCH 1:10 NOT DELETED")).thenReturn(imapResponses);
        folder.open(OPEN_MODE_RW);

        List<ImapMessage> messages = folder.getMessages(1, 10, null, null);
@@ -582,7 +582,7 @@ public class ImapFolderTest {
                createImapResponse("* SEARCH 47"),
                createImapResponse("* SEARCH 18")
        );
        when(imapConnection.executeSimpleCommand("UID SEARCH 1:10 SINCE 06-Feb-2016 NOT DELETED", false))
        when(imapConnection.executeSimpleCommand("UID SEARCH 1:10 SINCE 06-Feb-2016 NOT DELETED"))
                .thenReturn(imapResponses);
        folder.open(OPEN_MODE_RW);

@@ -597,7 +597,7 @@ public class ImapFolderTest {
        ImapFolder folder = createFolder("Folder");
        prepareImapFolderForOpen(OPEN_MODE_RW);
        List<ImapResponse> imapResponses = singletonList(createImapResponse("* SEARCH 99"));
        when(imapConnection.executeSimpleCommand("UID SEARCH 1:10 NOT DELETED", false)).thenReturn(imapResponses);
        when(imapConnection.executeSimpleCommand("UID SEARCH 1:10 NOT DELETED")).thenReturn(imapResponses);
        folder.open(OPEN_MODE_RW);
        MessageRetrievalListener<ImapMessage> listener = createMessageRetrievalListener();

@@ -680,7 +680,7 @@ public class ImapFolderTest {
                createImapResponse("* SEARCH 18"),
                createImapResponse("* SEARCH 49")
        );
        when(imapConnection.executeSimpleCommand("UID SEARCH 5,1:2 NOT DELETED", false)).thenReturn(imapResponses);
        when(imapConnection.executeSimpleCommand("UID SEARCH 5,1:2 NOT DELETED")).thenReturn(imapResponses);
        folder.open(OPEN_MODE_RW);

        List<ImapMessage> messages = folder.getMessages(asList(1L, 2L, 5L), false, null);
@@ -694,7 +694,7 @@ public class ImapFolderTest {
        ImapFolder folder = createFolder("Folder");
        prepareImapFolderForOpen(OPEN_MODE_RW);
        List<ImapResponse> imapResponses = singletonList(createImapResponse("* SEARCH 99"));
        when(imapConnection.executeSimpleCommand("UID SEARCH 1", false)).thenReturn(imapResponses);
        when(imapConnection.executeSimpleCommand("UID SEARCH 1")).thenReturn(imapResponses);
        folder.open(OPEN_MODE_RW);
        MessageRetrievalListener<ImapMessage> listener = createMessageRetrievalListener();

@@ -728,7 +728,7 @@ public class ImapFolderTest {
                createImapResponse("* SEARCH 22"),
                createImapResponse("* SEARCH 25")
        );
        when(imapConnection.executeSimpleCommand("UID SEARCH UID 22,25,11", false)).thenReturn(imapResponses);
        when(imapConnection.executeSimpleCommand("UID SEARCH UID 11,22,25")).thenReturn(imapResponses);
        folder.open(OPEN_MODE_RW);

        List<ImapMessage> messages = folder.getMessagesFromUids(asList("11", "22", "25"));
@@ -755,7 +755,7 @@ public class ImapFolderTest {
        ImapFolder folder = createFolder("Folder");
        prepareImapFolderForOpen(OPEN_MODE_RW);
        List<ImapResponse> imapResponses = singletonList(createImapResponse("* SEARCH 42"));
        when(imapConnection.executeSimpleCommand("UID SEARCH 1:9 NOT DELETED", false)).thenReturn(imapResponses);
        when(imapConnection.executeSimpleCommand("UID SEARCH 1:9 NOT DELETED")).thenReturn(imapResponses);
        folder.open(OPEN_MODE_RW);

        boolean areMoreMessagesAvailable = folder.areMoreMessagesAvailable(10, null);
@@ -796,8 +796,8 @@ public class ImapFolderTest {

        folder.areMoreMessagesAvailable(600, null);

        verify(imapConnection).executeSimpleCommand("UID SEARCH 100:599 NOT DELETED", false);
        verify(imapConnection).executeSimpleCommand("UID SEARCH 1:99 NOT DELETED", false);
        verify(imapConnection).executeSimpleCommand("UID SEARCH 100:599 NOT DELETED");
        verify(imapConnection).executeSimpleCommand("UID SEARCH 1:99 NOT DELETED");
    }

    @Test
@@ -1007,7 +1007,7 @@ public class ImapFolderTest {

        folder.getUidFromMessageId(message);

        verify(imapConnection).executeSimpleCommand("UID SEARCH HEADER MESSAGE-ID \"<00000000.0000000@example.org>\"", false);
        verify(imapConnection).executeSimpleCommand("UID SEARCH HEADER MESSAGE-ID \"<00000000.0000000@example.org>\"");
    }

    @Test
@@ -1017,7 +1017,7 @@ public class ImapFolderTest {
        folder.open(OPEN_MODE_RW);
        ImapMessage message = createImapMessage("2");
        when(message.getHeader("Message-ID")).thenReturn(new String[] { "<00000000.0000000@example.org>" });
        when(imapConnection.executeSimpleCommand("UID SEARCH HEADER MESSAGE-ID \"<00000000.0000000@example.org>\"", false))
        when(imapConnection.executeSimpleCommand("UID SEARCH HEADER MESSAGE-ID \"<00000000.0000000@example.org>\""))
                .thenReturn(singletonList(createImapResponse("* SEARCH 23")));

        String uid = folder.getUidFromMessageId(message);
@@ -1042,7 +1042,7 @@ public class ImapFolderTest {

        folder.setFlags(newSet(Flag.SEEN), true);

        verify(imapConnection).executeSimpleCommand("UID STORE 1:* +FLAGS.SILENT (\\Seen)", false);
        verify(imapConnection).executeSimpleCommand("UID STORE 1:* +FLAGS.SILENT (\\Seen)");
    }

    @Test
@@ -1076,7 +1076,7 @@ public class ImapFolderTest {

        folder.search("query", newSet(Flag.SEEN), Collections.<Flag>emptySet());

        verify(imapConnection).executeSimpleCommand("UID SEARCH TEXT \"query\" SEEN", false);
        verify(imapConnection).executeSimpleCommand("UID SEARCH TEXT \"query\" SEEN");
    }

    @Test
@@ -1088,7 +1088,7 @@ public class ImapFolderTest {

        folder.search("query", Collections.<Flag>emptySet(), Collections.<Flag>emptySet());

        verify(imapConnection).executeSimpleCommand("UID SEARCH OR SUBJECT \"query\" FROM \"query\"", false);
        verify(imapConnection).executeSimpleCommand("UID SEARCH OR SUBJECT \"query\" FROM \"query\"");
    }

    @Test
+0 −29
Original line number Diff line number Diff line
package com.fsck.k9.mail.store.imap.command;


import com.fsck.k9.mail.store.imap.ImapConnection;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;


public class CapabilityCommandTest {

    private ImapCommandFactory commandFactory;
    private ImapConnection connection;

    @Before
    public void setUp() {
        connection = Mockito.mock(ImapConnection.class);
        commandFactory = ImapCommandFactory.create(connection, null);
    }

    @Test
    public void executeInternal_shouldIssueProperCommand() throws Exception {
        CapabilityCommand command = commandFactory.createCapabilityCommand();

        command.executeInternal();

        Mockito.verify(connection).executeSimpleCommand("CAPABILITY", false);
    }
}
+0 −66
Original line number Diff line number Diff line
package com.fsck.k9.mail.store.imap.command;


import java.util.Collections;
import java.util.Date;
import java.util.Set;

import com.fsck.k9.mail.Flag;
import com.fsck.k9.mail.store.imap.ImapConnection;
import com.fsck.k9.mail.store.imap.ImapFolder;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;

import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.verify;


public class UidSearchCommandTest {

    private static final String TEST_QUERY_STRING = "query";
    private static final String TEST_MESSAGE_ID = "<00000000.0000000@example.org>";
    private static final Date TEST_SINCE = new Date(10000000000L);
    private static final Set<Flag> TEST_FORBIDDEN_FLAGS = Collections.singleton(Flag.DELETED);

    private static final String TEST_SEARCH_KEY_QUERY_STRING = "TEXT \"query\"";
    private static final String TEST_SEARCH_KEY_MESSAGE_ID = "HEADER MESSAGE-ID \"<00000000.0000000@example.org>\"";
    private static final String TEST_SEARCH_KEY_SINCE = "SINCE 26-Apr-1970";;
    private static final String TEST_SEARCH_KEY_FORBIDDEN_FLAGS = "NOT DELETED";

    private ImapCommandFactory commandFactory;
    private ImapConnection connection;
    private ArgumentCaptor<String> commandStringCaptor;

    @Before
    public void setUp() {
        connection = Mockito.mock(ImapConnection.class);
        commandFactory = ImapCommandFactory.create(connection, null);
        commandStringCaptor = ArgumentCaptor.forClass(String.class);
    }

    @Test
    public void execute_shouldIssueProperCommand() throws Exception {
        UidSearchCommand command = buildCommand();

        command.execute();

        verify(connection).executeSimpleCommand(commandStringCaptor.capture(), Mockito.eq(false));
        String commandString = commandStringCaptor.getValue();
        assertTrue(commandString.contains(TEST_SEARCH_KEY_QUERY_STRING));
        assertTrue(commandString.contains(TEST_SEARCH_KEY_MESSAGE_ID));
        assertTrue(commandString.contains(TEST_SEARCH_KEY_SINCE));
        assertTrue(commandString.contains(TEST_SEARCH_KEY_FORBIDDEN_FLAGS));
    }

    private UidSearchCommand buildCommand() {
        return commandFactory.createUidSearchCommandBuilder(Mockito.mock(ImapFolder.class), null)
                .performFullTextSearch(true)
                .queryString(TEST_QUERY_STRING)
                .messageId(TEST_MESSAGE_ID)
                .since(TEST_SINCE)
                .forbiddenFlags(TEST_FORBIDDEN_FLAGS)
                .build();
    }
}
+0 −62
Original line number Diff line number Diff line
package com.fsck.k9.mail.store.imap.command;


import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import com.fsck.k9.mail.Flag;
import com.fsck.k9.mail.store.imap.ImapConnection;
import com.fsck.k9.mail.store.imap.ImapFolder;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;

import static junit.framework.Assert.assertTrue;
import static org.mockito.Mockito.verify;


public class UidStoreCommandTest {

    private static final List<Long> TEST_UID_SET = Arrays.asList(1L, 2L, 3L);
    private static final boolean TEST_VALUE = true;
    private static final Set<Flag> TEST_FLAG_SET = Collections.singleton(Flag.DELETED);

    private static final String TEST_UID_SET_OUTPUT = "1:3";
    private static final String TEST_VALUE_OUTPUT = "+FLAGS";
    private static final String TEST_FLAG_SET_OUTPUT = "(\\Deleted)";

    private ImapCommandFactory commandFactory;
    private ImapConnection connection;
    private ArgumentCaptor<String> commandStringCaptor;

    @Before
    public void setUp() {
        connection = Mockito.mock(ImapConnection.class);
        commandFactory = ImapCommandFactory.create(connection, null);
        commandStringCaptor = ArgumentCaptor.forClass(String.class);
    }

    @Test
    public void execute_shouldIssueProperCommand() throws Exception {
        UidStoreCommand command = buildCommand();

        command.execute();

        verify(connection).executeSimpleCommand(commandStringCaptor.capture(), Mockito.eq(false));
        String commandString = commandStringCaptor.getValue();
        assertTrue(commandString.contains(TEST_UID_SET_OUTPUT));
        assertTrue(commandString.contains(TEST_VALUE_OUTPUT));
        assertTrue(commandString.contains(TEST_FLAG_SET_OUTPUT));
    }

    private UidStoreCommand buildCommand() {
        return commandFactory.createUidStoreCommandBuilder(Mockito.mock(ImapFolder.class))
                .idSet(TEST_UID_SET)
                .value(TEST_VALUE)
                .flagSet(TEST_FLAG_SET)
                .build();
    }
}
Loading