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

Commit 2ecd7f0e authored by cketti's avatar cketti
Browse files

Add nicer API to build test messages

parent edbda6df
Loading
Loading
Loading
Loading
+36 −136
Original line number Diff line number Diff line
package com.fsck.k9.mail.helpers;


import java.io.IOException;
import java.io.OutputStream;

import com.fsck.k9.mail.Address;
import com.fsck.k9.mail.Flag;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.internet.MimeMessage;
import okio.BufferedSink;
import okio.Okio;


public class TestMessage extends MimeMessage {
    private long mId;
    private int mAttachmentCount;
    private String mSubject;
    private String mPreview = "";
    private long mThreadId;
    private long mRootId;
    private long messagePartId;
    private String mimeType;
class TestMessage extends MimeMessage {
    private final long messageSize;
    private final Address[] from;
    private final Address[] to;
    private final boolean hasAttachments;


    public long getMessagePartId() {
        return messagePartId;
    TestMessage(TestMessageBuilder builder) {
        from = toAddressArray(builder.from);
        to = toAddressArray(builder.to);
        hasAttachments = builder.hasAttachments;
        messageSize = builder.messageSize;
    }

    @Override
    public String getMimeType() {
        return mimeType;
    }


    public String getPreview() {
        return mPreview;
    public Address[] getFrom() {
        return from;
    }

    @Override
    public String getSubject() {
        return mSubject;
    public Address[] getRecipients(RecipientType type) {
        switch (type) {
            case TO:
                return to;
            case CC:
                return new Address[0];
            case BCC:
                return new Address[0];
        }


    @Override
    public void setSubject(String subject) {
        mSubject = subject;
    }


    @Override
    public void setMessageId(String messageId) {
        mMessageId = messageId;
        throw new AssertionError("Missing switch case: " + type);
    }

    @Override
    public boolean hasAttachments() {
        return (mAttachmentCount > 0);
        return hasAttachments;
    }

    public void setAttachmentCount(int i) {
        mAttachmentCount = i;
    }

    public int getAttachmentCount() {
        return mAttachmentCount;
    }

    @Override
    public void setFrom(Address from) {
        this.mFrom = new Address[] { from };
    }

    @Override
    public void setReplyTo(Address[] replyTo) {
        if (replyTo == null || replyTo.length == 0) {
            mReplyTo = null;
        } else {
            mReplyTo = replyTo;
        }
    }

    /*
     * For performance reasons, we add headers instead of setting them (see super implementation)
     * which removes (expensive) them before adding them
     */
    @Override
    public void setRecipients(RecipientType type, Address[] addresses) {
        if (type == RecipientType.TO) {
            if (addresses == null || addresses.length == 0) {
                this.mTo = null;
            } else {
                this.mTo = addresses;
            }
        } else if (type == RecipientType.CC) {
            if (addresses == null || addresses.length == 0) {
                this.mCc = null;
            } else {
                this.mCc = addresses;
            }
        } else if (type == RecipientType.BCC) {
            if (addresses == null || addresses.length == 0) {
                this.mBcc = null;
            } else {
                this.mBcc = addresses;
            }
        } else {
            throw new IllegalArgumentException("Unrecognized recipient type.");
        }
    }

    public void setFlagInternal(Flag flag, boolean set) throws MessagingException {
        super.setFlag(flag, set);
    }

    @Override
    public long getId() {
        return mId;
    }

    @Override
    public TestMessage clone() {
        TestMessage message = new TestMessage();
        super.copy(message);

        message.mId = mId;
        message.mAttachmentCount = mAttachmentCount;
        message.mSubject = mSubject;
        message.mPreview = mPreview;

        return message;
    }

    public long getThreadId() {
        return mThreadId;
    }

    public long getRootId() {
        return mRootId;
    }

    @Override
    protected void copy(MimeMessage destination) {
        super.copy(destination);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        if (!super.equals(o)) {
            return false;
        }

        return true;
    public long calculateSize() {
        return messageSize;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        return result;
    public void writeTo(OutputStream out) throws IOException, MessagingException {
        BufferedSink bufferedSink = Okio.buffer(Okio.sink(out));
        bufferedSink.writeUtf8("[message data]");
        bufferedSink.emit();
    }

    public boolean isBodyMissing() {
        return getBody() == null;
    private static Address[] toAddressArray(String email) {
        return email == null ? new Address[0] : new Address[] { new Address(email) };
    }
}
+37 −0
Original line number Diff line number Diff line
package com.fsck.k9.mail.helpers;


import com.fsck.k9.mail.Message;


public class TestMessageBuilder {
    String from;
    String to;
    boolean hasAttachments;
    long messageSize;


    public TestMessageBuilder from(String email) {
        from = email;
        return this;
    }

    public TestMessageBuilder to(String email) {
        to = email;
        return this;
    }

    public TestMessageBuilder setHasAttachments(boolean hasAttachments) {
        this.hasAttachments = hasAttachments;
        return this;
    }

    public TestMessageBuilder messageSize(long messageSize) {
        this.messageSize = messageSize;
        return this;
    }
    
    public Message build() {
        return new TestMessage(this);
    }
}
+22 −21
Original line number Diff line number Diff line
@@ -5,7 +5,6 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fsck.k9.mail.Address;
import com.fsck.k9.mail.AuthType;
import com.fsck.k9.mail.CertificateValidationException;
import com.fsck.k9.mail.ConnectionSecurity;
@@ -14,13 +13,12 @@ import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.ServerSettings;
import com.fsck.k9.mail.ServerSettings.Type;
import com.fsck.k9.mail.filter.Base64;
import com.fsck.k9.mail.helpers.TestMessageBuilder;
import com.fsck.k9.mail.helpers.TestTrustedSocketFactory;
import com.fsck.k9.mail.internet.MimeMessage;
import com.fsck.k9.mail.ssl.TrustedSocketFactory;
import com.fsck.k9.mail.store.StoreConfig;
import com.fsck.k9.mail.transport.mockServer.MockSmtpServer;
import com.fsck.k9.mail.helpers.TestMessage;
import com.fsck.k9.mailstore.BinaryMemoryBody;
import com.fsck.k9.mail.helpers.TestTrustedSocketFactory;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -346,9 +344,7 @@ public class SmtpTransportTest {

    @Test
    public void sendMessage_withToAddressToSendTo_opensConnection() throws Exception {
        TestMessage message = new TestMessage();
        message.setFrom(new Address("user@localhost"));
        message.setRecipients(Message.RecipientType.TO, new Address[] { new Address("user2@localhost") });
        Message message = getDefaultMessage();

        MockSmtpServer server = new MockSmtpServer();
        setupConnectAndPlainAuthentication(server);
@@ -358,7 +354,7 @@ public class SmtpTransportTest {
        server.output("250 OK");
        server.expect("DATA");
        server.output("354 End data with <CR><LF>.<CR><LF>");
        server.expect("");
        server.expect("[message data]");
        server.expect(".");
        server.output("250 OK: queued as 12345");
        server.expect("QUIT");
@@ -371,9 +367,7 @@ public class SmtpTransportTest {
    @Test
    public void sendMessage_with8BitEncoding_usesEncoding() throws Exception {
        extensions.add("8BITMIME");
        TestMessage message = new TestMessage();
        message.setFrom(new Address("user@localhost"));
        message.setRecipients(Message.RecipientType.TO, new Address[] { new Address("user2@localhost") });
        Message message = getDefaultMessage();

        MockSmtpServer server = new MockSmtpServer();
        setupConnectAndPlainAuthentication(server);
@@ -383,7 +377,7 @@ public class SmtpTransportTest {
        server.output("250 OK");
        server.expect("DATA");
        server.output("354 End data with <CR><LF>.<CR><LF>");
        server.expect("");
        server.expect("[message data]");
        server.expect(".");
        server.output("250 OK: queued as 12345");
        server.expect("QUIT");
@@ -396,11 +390,10 @@ public class SmtpTransportTest {
    @Test
    public void sendMessage_withMessageTooLarge_throwsException() throws Exception {
        extensions.add("SIZE 1000");
        TestMessage message = new TestMessage();
        message.setFrom(new Address("user@localhost"));
        message.setRecipients(Message.RecipientType.TO, new Address[] { new Address("user2@localhost") });
        message.setAttachmentCount(1);
        message.setBody(new BinaryMemoryBody(new byte[1001], "US-ASCII"));
        Message message = getDefaultMessageBuilder()
                .setHasAttachments(true)
                .messageSize(1234L)
                .build();

        MockSmtpServer server = new MockSmtpServer();
        setupConnectAndPlainAuthentication(server);
@@ -418,9 +411,7 @@ public class SmtpTransportTest {

    @Test
    public void sendMessage_withNegativeReply_throwsException() throws Exception {
        TestMessage message = new TestMessage();
        message.setFrom(new Address("user@localhost"));
        message.setRecipients(Message.RecipientType.TO, new Address[] { new Address("user2@localhost") });
        Message message = getDefaultMessage();

        MockSmtpServer server = new MockSmtpServer();
        setupConnectAndPlainAuthentication(server);
@@ -430,7 +421,7 @@ public class SmtpTransportTest {
        server.output("250 OK");
        server.expect("DATA");
        server.output("354 End data with <CR><LF>.<CR><LF>");
        server.expect("");
        server.expect("[message data]");
        server.expect(".");
        server.output("421 4.7.0 Temporary system problem");
        server.expect("QUIT");
@@ -476,6 +467,16 @@ public class SmtpTransportTest {
        return new SmtpTransport(storeConfig, socketFactory);
    }

    private TestMessageBuilder getDefaultMessageBuilder() {
        return new TestMessageBuilder()
                .from("user@localhost")
                .to("user2@localhost");
    }

    private Message getDefaultMessage() {
        return getDefaultMessageBuilder().build();
    }

    private void setupConnectAndPlainAuthentication(MockSmtpServer server) {
        username = "user";
        password = "password";