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

Commit 524d8bac authored by Hari's avatar Hari
Browse files

Refactor and finish tests

parent f8ad003e
Loading
Loading
Loading
Loading
+16 −11
Original line number Diff line number Diff line
@@ -17,12 +17,15 @@ import com.fsck.k9.mail.store.imap.ImapUtility;
import com.fsck.k9.mail.store.imap.selectedstate.response.SelectedStateResponse;


//This is the base class for a command that is used in the "selected state" i.e when a mailbox is selected
abstract class SelectedStateCommand {
abstract class FolderSelectedStateCommand {

    /*These limits are 20 octets less than the recommended limits, in order to compensate for the length of the command
    tag, the space after the tag and the CRLF at the end of the command (these are not taken into account when
    calculating the length of the command)
    /* The below limits are 20 octets less than the recommended limits, in order to compensate for the length of the
    command tag, the space after the tag and the CRLF at the end of the command (these are not taken into account when
    calculating the length of the command). For more information, refer to section 4 of RFC 7162.

    The length limit for servers supporting the CONDSTORE extension is large in order to support the QRESYNC parameter
    to the SELECT/EXAMINE commands, which accept a list of known message sequence numbers as well as their corresponding
    UIDs.
     */
    private static final int LENGTH_LIMIT_WITHOUT_CONDSTORE = 980;
    private static final int LENGTH_LIMIT_WITH_CONDSTORE = 8172;
@@ -60,7 +63,7 @@ abstract class SelectedStateCommand {

    List<List<ImapResponse>> executeInternal(ImapConnection connection, ImapFolder folder)
            throws IOException, MessagingException {
        List<SelectedStateCommand> commands;
        List<FolderSelectedStateCommand> commands;
        String commandString = createCommandString();
        if (commandString.length() > getCommandLengthLimit(connection)) {
            commands = ImapCommandSplitter.splitCommand(this, getCommandLengthLimit(connection));
@@ -69,7 +72,7 @@ abstract class SelectedStateCommand {
        }

        List<List<ImapResponse>> responses = new ArrayList<>();
        for (SelectedStateCommand command : commands) {
        for (FolderSelectedStateCommand command : commands) {
            responses.add(folder.executeSimpleCommand(command.createCommandString()));
        }
        return responses;
@@ -100,18 +103,19 @@ abstract class SelectedStateCommand {
        }
    }

    static abstract class Builder<C extends SelectedStateCommand, B extends Builder<C, B>> {
    static abstract class Builder<C extends FolderSelectedStateCommand, B extends Builder<C, B>> {
        C command;
        B builder;

        abstract C createCommand();
        abstract B createBuilder();

        public Builder() {
            command = createCommand();
            builder = createBuilder();
        }

        abstract C createCommand();

        abstract B createBuilder();

        public B idSet(Collection<Long> idSet) {
            if (idSet != null) {
                command.idSet = new TreeSet<>(idSet);
@@ -192,6 +196,7 @@ abstract class SelectedStateCommand {

        @Override
        public String toString() {
            //The highest UID is queried as *:*, see the note in RFC 3501, page 60
            if (start == LAST_ID && end == LAST_ID) {
                return "*:*";
            }
+36 −37
Original line number Diff line number Diff line
@@ -6,16 +6,18 @@ import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import com.fsck.k9.mail.store.imap.selectedstate.command.SelectedStateCommand.Builder;
import com.fsck.k9.mail.store.imap.selectedstate.command.SelectedStateCommand.ContiguousIdGroup;
import com.fsck.k9.mail.store.imap.selectedstate.command.FolderSelectedStateCommand.Builder;
import com.fsck.k9.mail.store.imap.selectedstate.command.FolderSelectedStateCommand.ContiguousIdGroup;


class ImapCommandSplitter {

    static List<SelectedStateCommand> splitCommand(SelectedStateCommand command, int lengthLimit) {
        List<SelectedStateCommand> commands = new ArrayList<>();
    static List<FolderSelectedStateCommand> splitCommand(FolderSelectedStateCommand command, int lengthLimit) {
        if (command.getIdSet() == null && command.getIdGroups() == null) {
            throw new IllegalStateException("The constructed command is too long but does not contain ids");
        }

        if (command.getIdSet() != null || command.getIdGroups() != null) {
        List<FolderSelectedStateCommand> commands = new ArrayList<>();
        command = optimizeGroupings(command);
        Set<Long> idSet = command.getIdSet();
        List<ContiguousIdGroup> idGroups = command.getIdGroups();
@@ -52,13 +54,10 @@ class ImapCommandSplitter {
            }
            commands.add(builder.build());
        }
        } else {
            throw new IllegalStateException("The constructed command is too long but does not contain ids");
        }
        return commands;
    }

    private static SelectedStateCommand optimizeGroupings(SelectedStateCommand command) {
    static FolderSelectedStateCommand optimizeGroupings(FolderSelectedStateCommand command) {
        Set<Long> idSet = command.getIdSet();
        List<ContiguousIdGroup> idGroups = command.getIdGroups();
        if (idGroups != null && idGroups.get(0).getEnd() == ContiguousIdGroup.LAST_ID) {
@@ -90,7 +89,7 @@ class ImapCommandSplitter {
            }
        }
        checkAndAddIds(builder, idList, start, idList.size() - 1);
        SelectedStateCommand tempCommand = builder.build();
        FolderSelectedStateCommand tempCommand = builder.build();
        command.setIdSet(tempCommand.getIdSet());
        command.setIdGroups(tempCommand.getIdGroups());
        return command;
+3 −3
Original line number Diff line number Diff line
@@ -12,14 +12,14 @@ import com.fsck.k9.mail.store.imap.ImapResponse;
import com.fsck.k9.mail.store.imap.selectedstate.response.UidCopyResponse;


public class UidCopyCommand extends SelectedStateCommand {
public class UidCopyCommand extends FolderSelectedStateCommand {
    private String destinationFolderName;

    private UidCopyCommand() {
    }

    @Override
    public String createCommandString() {
    String createCommandString() {
        return String.format("%s %s%s", Commands.UID_COPY, createCombinedIdString(), destinationFolderName);
    }

@@ -38,7 +38,7 @@ public class UidCopyCommand extends SelectedStateCommand {
        return new Builder().destinationFolderName(destinationFolderName);
    }

    public static class Builder extends SelectedStateCommand.Builder<UidCopyCommand, Builder> {
    public static class Builder extends FolderSelectedStateCommand.Builder<UidCopyCommand, Builder> {

        public Builder destinationFolderName(String destinationFolderName) {
            command.destinationFolderName = destinationFolderName;
+5 −5
Original line number Diff line number Diff line
@@ -2,9 +2,9 @@ package com.fsck.k9.mail.store.imap.selectedstate.command;


import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import com.fsck.k9.mail.BodyFactory;
@@ -22,10 +22,10 @@ import com.fsck.k9.mail.store.imap.ImapResponseCallback;
import com.fsck.k9.mail.store.imap.ImapUtility;


public class UidFetchCommand extends SelectedStateCommand {
public class UidFetchCommand extends FolderSelectedStateCommand {
    private int maximumAutoDownloadMessageSize;
    private FetchProfile fetchProfile;
    private HashMap<String, Message> messageMap;
    private Map<String, Message> messageMap;
    private Part part;
    private BodyFactory bodyFactory;

@@ -115,14 +115,14 @@ public class UidFetchCommand extends SelectedStateCommand {
                .partParams(part, bodyFactory);
    }

    public static class Builder extends SelectedStateCommand.Builder<UidFetchCommand, Builder> {
    public static class Builder extends FolderSelectedStateCommand.Builder<UidFetchCommand, Builder> {

        public Builder maximumAutoDownloadMessageSize(int maximumAutoDownloadMessageSize) {
            command.maximumAutoDownloadMessageSize = maximumAutoDownloadMessageSize;
            return builder;
        }

        public Builder messageParams(FetchProfile fetchProfile, HashMap<String, Message> messageMap) {
        public Builder messageParams(FetchProfile fetchProfile, Map<String, Message> messageMap) {
            command.fetchProfile = fetchProfile;
            command.messageMap = messageMap;
            return builder;
+3 −3
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@ import com.fsck.k9.mail.store.imap.ImapUtility;
import com.fsck.k9.mail.store.imap.selectedstate.response.UidSearchResponse;


public class UidSearchCommand extends SelectedStateCommand {
public class UidSearchCommand extends FolderSelectedStateCommand {
    private boolean useUids;
    private String queryString;
    private String messageId;
@@ -33,7 +33,7 @@ public class UidSearchCommand extends SelectedStateCommand {
    }

    @Override
    public String createCommandString() {
    String createCommandString() {
        StringBuilder builder = new StringBuilder(Commands.UID_SEARCH).append(" ");
        if (useUids) {
            builder.append("UID ");
@@ -136,7 +136,7 @@ public class UidSearchCommand extends SelectedStateCommand {
                .listener(listener);
    }

    public static class Builder extends SelectedStateCommand.Builder<UidSearchCommand, Builder> {
    public static class Builder extends FolderSelectedStateCommand.Builder<UidSearchCommand, Builder> {

        public Builder useUids(boolean useUids) {
            command.useUids = useUids;
Loading