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

Commit af59c529 authored by Hari's avatar Hari
Browse files

Fix tests

parent a13d30de
Loading
Loading
Loading
Loading
+6 −4
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ import static com.fsck.k9.mail.Folder.OPEN_MODE_RO;
import static com.fsck.k9.mail.Folder.OPEN_MODE_RW;
import static com.fsck.k9.mail.store.imap.ImapResponseHelper.createImapResponse;
import static java.util.Arrays.asList;
import static java.util.Collections.singleton;
import static java.util.Collections.singletonList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -666,7 +667,7 @@ public class ImapFolderTest {
        when(imapStore.getConnection()).thenReturn(imapConnection);

        try {
            folder.getMessages(asList(1L, 2L, 5L), false, null);
            folder.getMessages(new HashSet<>(asList(1L, 2L, 5L)), false, null);
            fail("Expected exception");
        } catch (MessagingException e) {
            assertCheckOpenErrorMessage("Folder", e);
@@ -682,10 +683,11 @@ public class ImapFolderTest {
                createImapResponse("* SEARCH 18"),
                createImapResponse("* SEARCH 49")
        );
        when(imapConnection.executeSimpleCommand("UID SEARCH 1,2,5 NOT DELETED")).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);
        List<ImapMessage> messages = folder.getMessages(newSet(1L, 2L, 5L), false, null);

        assertNotNull(messages);
        assertEquals(newSet("17", "18", "49"), extractMessageUids(messages));
@@ -700,7 +702,7 @@ public class ImapFolderTest {
        folder.open(OPEN_MODE_RW);
        MessageRetrievalListener<ImapMessage> listener = createMessageRetrievalListener();

        List<ImapMessage> messages = folder.getMessages(singletonList(1L), true, listener);
        List<ImapMessage> messages = folder.getMessages(singleton(1L), true, listener);

        ImapMessage message = messages.get(0);
        verify(listener).messageStarted("99", 0, 1);
+4 −2
Original line number Diff line number Diff line
@@ -4,7 +4,9 @@ package com.fsck.k9.mail.store.imap;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.fsck.k9.mail.filter.PeekableInputStream;

@@ -36,8 +38,8 @@ public class ImapResponseHelper {
        return parser.readResponse();
    }

    public static List<Long> createNonContiguousIdSet(long start, long end, int interval) {
        List<Long> ids = new ArrayList<>();
    public static Set<Long> createNonContiguousIdSet(long start, long end, int interval) {
        Set<Long> ids = new HashSet<>();
        for (long i = start;i <= end;i += interval) {
            ids.add(i);
        }
+15 −24
Original line number Diff line number Diff line
@@ -7,7 +7,8 @@ import com.fsck.k9.mail.store.imap.ImapResponseHelper;
import org.junit.Before;
import org.junit.Test;

import static java.util.Arrays.asList;
import java.util.Collections;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.atLeast;
@@ -15,6 +16,7 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.internal.util.collections.Sets.newSet;


public class FolderSelectedStateCommandTest {
@@ -31,20 +33,16 @@ public class FolderSelectedStateCommandTest {

    @Test
    public void createCombinedIdString_withIdSet_shouldCreateRespectiveString() {
        TestCommand command = new TestCommand.Builder()
                .idSet(asList(1L, 2L, 3L))
                .build();
        TestCommand command = TestCommand.createWithIdSet(newSet(1L, 3L, 5L));

        String idString = command.createCombinedIdString();

        assertEquals(idString, "1,2,3 ");
        assertEquals(idString, "1,3,5 ");
    }

    @Test
    public void createCombinedIdString_withIdGroup_shouldCreateRespectiveString() {
        TestCommand command = new TestCommand.Builder()
                .addIdGroup(1L, 10L)
                .build();
        TestCommand command = TestCommand.createWithIdGroup(1L, 10L);

        String idString = command.createCombinedIdString();

@@ -53,9 +51,8 @@ public class FolderSelectedStateCommandTest {

    @Test
    public void createCombinedIdString_withAllIds_shouldCreateRespectiveString() {
        TestCommand command = new TestCommand.Builder()
                .allIds(true)
                .build();
        TestCommand command = TestCommand.createWithIdSet(Collections.<Long>emptySet());
        command.useAllIds(true);

        String idString = command.createCombinedIdString();

@@ -64,9 +61,8 @@ public class FolderSelectedStateCommandTest {

    @Test
    public void createCombinedIdString_withOnlyHighestId_shouldCreateRespectiveString() {
        TestCommand command = new TestCommand.Builder()
                .onlyHighestId(true)
                .build();
        TestCommand command = TestCommand.createWithIdSet(Collections.<Long>emptySet());
        command.useOnlyHighestId(true);

        String idString = command.createCombinedIdString();

@@ -75,10 +71,8 @@ public class FolderSelectedStateCommandTest {

    @Test
    public void createCombinedIdString_withIdSetAndGroup_shouldCreateRespectiveString() {
        TestCommand command = new TestCommand.Builder()
                .idSet(asList(1L, 2L, 3L))
                .addIdGroup(5L, 10L)
                .build();
        TestCommand command = TestCommand.createWithIdSetAndGroup(newSet(1L, 2L, 3L),
                5L, 10L);

        String idString = command.createCombinedIdString();

@@ -87,9 +81,7 @@ public class FolderSelectedStateCommandTest {

    @Test
    public void executeInternal_withShortCommand_shouldNotSplitCommand() throws Exception {
        TestCommand command = new TestCommand.Builder()
                .idSet(asList(1L, 2L, 3L))
                .build();
        TestCommand command = TestCommand.createWithIdSet(newSet(1L, 2L, 3L));

        command.executeInternal(imapConnection, imapFolder);

@@ -98,9 +90,8 @@ public class FolderSelectedStateCommandTest {

    @Test
    public void executeInternal_withLongCommand_shouldSplitCommand() throws Exception {
        TestCommand command = new TestCommand.Builder()
                .idSet(ImapResponseHelper.createNonContiguousIdSet(10000L, 10500L, 2))
                .build();
        TestCommand command = TestCommand.createWithIdSet(ImapResponseHelper
                .createNonContiguousIdSet(10000L, 10500L, 2));

        command.executeInternal(imapConnection, imapFolder);

+32 −37
Original line number Diff line number Diff line
package com.fsck.k9.mail.store.imap.selectedstate.command;


import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import com.fsck.k9.mail.store.imap.selectedstate.command.FolderSelectedStateCommand.ContiguousIdGroup;
import org.junit.Test;
@@ -18,41 +16,44 @@ public class ImapCommandSplitterTest {

    @Test
    public void splitCommand_withManyNonContiguousIds_shouldSplitCommand() throws Exception {
        List<Long> ids = createNonContiguousIdSet(10000, 10500, 2);
        TestCommand command = createTestCommand(ids);
        Set<Long> ids = createNonContiguousIdSet(10000, 10500, 2);
        TestCommand command = TestCommand.createWithIdSet(ids);

        List<FolderSelectedStateCommand> commands = ImapCommandSplitter.splitCommand(command, 980);
        List<String> commands = ImapCommandSplitter.splitCommand(command, 980);

        assertEquals(commands.get(0).getIdSet(), new TreeSet<>(createNonContiguousIdSet(10000, 10324, 2)));
        assertEquals(commands.get(1).getIdSet(), new TreeSet<>(createNonContiguousIdSet(10326, 10500, 2)));
        assertEquals(commands.size(), 2);
        verifyTestCommandString(commands.get(0), createNonContiguousIdSet(10000, 10322, 2), null,
                null);
        verifyTestCommandString(commands.get(1), createNonContiguousIdSet(10324, 10500, 2), null,
                null);
    }

    @Test
    public void splitCommand_withContiguousAndNonContiguousIds_shouldGroupIdsAndSplitCommand() throws Exception {
        List<Long> firstIdSet = createNonContiguousIdSet(10000, 10300, 2);
        List<Long> secondIdSet = createNonContiguousIdSet(10301, 10399, 1);
        List<Long> thirdIdSet = createNonContiguousIdSet(10400, 10500, 2);
        List<Long> idSet = new ArrayList<>(firstIdSet.size() + secondIdSet.size() + thirdIdSet.size());
    public void splitCommand_withContiguousAndNonContiguousIds_shouldGroupIdsAndSplitCommand()
            throws Exception {
        Set<Long> firstIdSet = createNonContiguousIdSet(10000, 10300, 2);
        Set<Long> secondIdSet = createNonContiguousIdSet(10301, 10399, 1);
        Set<Long> thirdIdSet = createNonContiguousIdSet(10400, 10500, 2);
        Set<Long> idSet = new HashSet<>(firstIdSet.size() + secondIdSet.size() + thirdIdSet.size());
        idSet.addAll(firstIdSet);
        idSet.addAll(secondIdSet);
        idSet.addAll(thirdIdSet);
        TestCommand command = createTestCommand(idSet);
        TestCommand command = TestCommand.createWithIdSet(idSet);
        ImapCommandSplitter.optimizeGroupings(command);

        List<FolderSelectedStateCommand> commands = ImapCommandSplitter.splitCommand(command, 980);
        List<String> commands = ImapCommandSplitter.splitCommand(command, 980);

        List<Long> firstCommandIds = createNonContiguousIdSet(10000, 10298, 2);
        firstCommandIds.addAll(createNonContiguousIdSet(10402, 10426, 2));
        assertEquals(commands.get(0).getIdSet(), new TreeSet<>(firstCommandIds));
        assertEquals(commands.get(1).getIdSet(), new TreeSet<>(createNonContiguousIdSet(10428, 10500, 2)));
        assertEquals(commands.get(1).getIdGroups().get(0).getStart().longValue(), 10300L);
        assertEquals(commands.get(1).getIdGroups().get(0).getEnd().longValue(), 10400L);
        assertEquals(commands.size(), 2);
        Set<Long> firstCommandIds = createNonContiguousIdSet(10000, 10298, 2);
        firstCommandIds.addAll(createNonContiguousIdSet(10402, 10424, 2));
        verifyTestCommandString(commands.get(0), firstCommandIds, null, null);
        Set<Long> secondCommandIds = createNonContiguousIdSet(10426, 10500, 2);
        verifyTestCommandString(commands.get(1), secondCommandIds, 10300L, 10400L);
    }

    @Test
    public void optimizeGroupings_withContiguousIds_shouldGroupIdsCorrectly() {
        TestCommand command = new TestCommand.Builder()
                .idSet(createNonContiguousIdSet(1, 100, 1))
                .build();
        TestCommand command = TestCommand.createWithIdSet(createNonContiguousIdSet(1, 100, 1));

        ImapCommandSplitter.optimizeGroupings(command);

@@ -61,13 +62,10 @@ public class ImapCommandSplitterTest {

    @Test
    public void optimizeGroupings_withContiguousAndNonContiguousIds_shouldGroupIdsCorrectly() {
        List<Long> idSet = createNonContiguousIdSet(1, 100, 1);
        Set<Long> finalIdSet = new HashSet<>(createNonContiguousIdSet(300, 400, 2));
        Set<Long> idSet = createNonContiguousIdSet(1, 100, 1);
        Set<Long> finalIdSet = createNonContiguousIdSet(300, 400, 2);
        idSet.addAll(finalIdSet);
        TestCommand command = new TestCommand.Builder()
                .idSet(idSet)
                .addIdGroup(101L, 115L)
                .build();
        TestCommand command = TestCommand.createWithIdSetAndGroup(idSet, 101L, 115L);

        ImapCommandSplitter.optimizeGroupings(command);

@@ -75,14 +73,6 @@ public class ImapCommandSplitterTest {
        verifyIdGroup(command, 1, 115);
    }

    private TestCommand createTestCommand(List<Long> ids) {
        TestCommand.Builder builder = new TestCommand.Builder();
        if (ids != null) {
            builder.idSet(ids);
        }
        return builder.build();
    }

    private void verifyIdGroup(TestCommand command, long start, long end) {
        List<ContiguousIdGroup> idGroups = command.getIdGroups();
        assertEquals(idGroups.size(), 1);
@@ -91,4 +81,9 @@ public class ImapCommandSplitterTest {
        assertEquals(idGroup.getStart(), Long.valueOf(start));
        assertEquals(idGroup.getEnd(), Long.valueOf(end));
    }

    private void verifyTestCommandString(String commandString, Set<Long> ids, Long start, Long end) {
        TestCommand tempCommand = TestCommand.createWithIdSetAndGroup(ids, start, end);
        assertEquals(commandString, tempCommand.createCommandString());
    }
}
+15 −15
Original line number Diff line number Diff line
package com.fsck.k9.mail.store.imap.selectedstate.command;

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

class TestCommand extends FolderSelectedStateCommand {

    private TestCommand() {
    private TestCommand(Set<Long> ids) {
        super(ids);
    }

    @Override
    String createCommandString() {
        return createCombinedIdString().trim();
        return String.format("TEST %s", createCombinedIdString()).trim();
    }

    @Override
    Builder newBuilder() {
        return new Builder();
    static TestCommand createWithIdSetAndGroup(Set<Long> ids, Long start, Long end) {
        TestCommand command = new TestCommand(ids);
        command.addIdGroup(start, end);
        return command;
    }

    static class Builder extends FolderSelectedStateCommand.Builder<TestCommand, Builder> {

        @Override
        TestCommand createCommand() {
            return new TestCommand();
    static TestCommand createWithIdSet(Set<Long> ids) {
        return createWithIdSetAndGroup(ids, null, null);
    }

        @Override
        Builder createBuilder() {
            return this;
        }
    static TestCommand createWithIdGroup(long start, long end) {
        return createWithIdSetAndGroup(Collections.<Long>emptySet(), start, end);
    }
}
 No newline at end of file
Loading