Convert IdentityHelperTest to Kotlin

This commit is contained in:
cketti 2018-01-11 19:48:58 +01:00
parent ae15e0c618
commit 11bd02a95d
2 changed files with 149 additions and 187 deletions

View file

@ -1,187 +0,0 @@
package com.fsck.k9.helper;
import android.content.Context;
import com.fsck.k9.Account;
import com.fsck.k9.Identity;
import com.fsck.k9.K9RobolectricTestRunner;
import com.fsck.k9.mail.Message;
import com.fsck.k9.mail.Address;
import com.fsck.k9.mail.Message.RecipientType;
import com.fsck.k9.mail.internet.MimeMessage;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertTrue;
@RunWith(K9RobolectricTestRunner.class)
public class IdentityHelperTest {
private Account account;
private MimeMessage msg;
@Before
public void setUp() throws Exception {
Context context = RuntimeEnvironment.application;
createDummyAccount(context);
msg = parseWithoutRecurse(toStream(
"From: <adam@example.org>\r\n" +
"Subject: Testmail\r\n" +
"MIME-Version: 1.0\r\n" +
"Content-type: text/plain\r\n" +
"Content-Transfer-Encoding: 7bit\r\n" +
"\r\n" +
"this is some test text."));
}
private static MimeMessage parseWithoutRecurse(InputStream data) throws Exception {
return MimeMessage.parseMimeMessage(data, false);
}
private static ByteArrayInputStream toStream(String rawMailData) throws Exception {
return new ByteArrayInputStream(rawMailData.getBytes("ISO-8859-1"));
}
private void createDummyAccount(Context context) {
account = new DummyAccount(context);
setIdentity();
}
private void setIdentity() {
Identity identity = new Identity();
identity.setEmail("test@example.org");
identity.setName("test");
Identity identity2 = new Identity();
identity2.setEmail("test2@example.org");
identity2.setName("test2");
Identity identity3 = new Identity();
identity3.setEmail("test3@example.org");
identity3.setName("test3");
Identity defaultIdentity = new Identity();
defaultIdentity.setEmail("default@example.org");
defaultIdentity.setName("Default");
List<Identity> identityList = new ArrayList<>();
identityList.add(defaultIdentity);
identityList.add(identity);
identityList.add(identity2);
identityList.add(identity3);
account.setIdentities(identityList);
}
@Test
public void getRecipientIdentityFromMessage_usesXOriginalTo_whenPresent() throws Exception {
Address[] addresses = {new Address("test2@example.org")};
msg.removeHeader("To");
msg.setRecipients(Message.RecipientType.X_ORIGINAL_TO, addresses);
Identity identity = IdentityHelper.getRecipientIdentityFromMessage(account, msg);
assertTrue(identity.getEmail().equalsIgnoreCase("test2@example.org"));
}
@Test
public void getRecipientIdentityFromMessage_usesTo_whenPresent() throws Exception {
Address[] addresses = {new Address("test@example.org")};
msg.setRecipients(RecipientType.TO, addresses);
Identity eva = IdentityHelper.getRecipientIdentityFromMessage(account, msg);
assertTrue(eva.getEmail().equalsIgnoreCase("test@example.org"));
}
@Test
public void getRecipientIdentityFromMessage_usesCC_whenPresent() throws Exception {
Address[] addresses = {new Address("test@example.org")};
msg.setRecipients(RecipientType.CC, addresses);
Identity eva = IdentityHelper.getRecipientIdentityFromMessage(account, msg);
assertTrue(eva.getEmail().equalsIgnoreCase("test@example.org"));
}
@Test
public void getRecipientIdentityFromMessage_usesDeliveredTo_whenPresent() throws Exception {
Address[] addresses = {new Address("test2@example.org")};
msg.setRecipients(Message.RecipientType.DELIVERED_TO, addresses);
msg.removeHeader("To");
Identity identity = IdentityHelper.getRecipientIdentityFromMessage(account, msg);
assertTrue(identity.getEmail().equalsIgnoreCase("test2@example.org"));
}
@Test
public void getRecipientIdentityFromMessage_usesXEnvelopeTo_whenPresent() throws Exception {
Address[] addresses = {new Address("test2@example.org")};
msg.setRecipients(Message.RecipientType.X_ENVELOPE_TO, addresses);
msg.removeHeader("To");
Identity identity = IdentityHelper.getRecipientIdentityFromMessage(account, msg);
assertTrue(identity.getEmail().equalsIgnoreCase("test2@example.org"));
}
@Test
public void getRecipientIdentityFromMessage_prefers1To_overCC() throws Exception {
Address[] toAddresses = {new Address("test@example.org")};
Address[] ccAddresses = {new Address("test2@example.org")};
msg.setRecipients(RecipientType.TO, toAddresses);
msg.setRecipients(RecipientType.CC, ccAddresses);
Identity identity = IdentityHelper.getRecipientIdentityFromMessage(account, msg);
assertTrue(identity.getEmail().equalsIgnoreCase("test@example.org"));
}
@Test
public void getRecipientIdentityFromMessage_prefers2CC_overXOriginalTo() throws Exception {
Address[] ccAddresses = {new Address("test@example.org")};
Address[] xOriginalToAddresses = {new Address("test2@example.org")};
msg.setRecipients(RecipientType.CC, ccAddresses);
msg.setRecipients(RecipientType.X_ORIGINAL_TO, xOriginalToAddresses);
Identity identity = IdentityHelper.getRecipientIdentityFromMessage(account, msg);
assertTrue(identity.getEmail().equalsIgnoreCase("test@example.org"));
}
@Test
public void getRecipientIdentityFromMessage_prefers3XOriginalTo_overDeliveredTo() throws Exception {
Address[] xOriginalToAddresses = {new Address("test@example.org")};
Address[] deliveredToAddresses = {new Address("test2@example.org")};
msg.setRecipients(RecipientType.X_ORIGINAL_TO, xOriginalToAddresses);
msg.setRecipients(RecipientType.DELIVERED_TO, deliveredToAddresses);
Identity identity = IdentityHelper.getRecipientIdentityFromMessage(account, msg);
assertTrue(identity.getEmail().equalsIgnoreCase("test@example.org"));
}
@Test
public void getRecipientIdentityFromMessage_prefers4DeliveredTo_overXEnvelopeTo() throws Exception {
Address[] deliveredToAddresses = {new Address("test@example.org")};
Address[] xEnvelopeTo = {new Address("test2@example.org")};
msg.setRecipients(RecipientType.DELIVERED_TO, deliveredToAddresses);
msg.setRecipients(RecipientType.X_ENVELOPE_TO, xEnvelopeTo);
Identity identity = IdentityHelper.getRecipientIdentityFromMessage(account, msg);
assertTrue(identity.getEmail().equalsIgnoreCase("test@example.org"));
}
@Test
public void getRecipientIdentityFromMessage_withNoApplicableHeaders_returnsFirstIdentity() throws Exception {
Identity identity = IdentityHelper.getRecipientIdentityFromMessage(account, msg);
assertTrue(identity.getEmail().equalsIgnoreCase("default@example.org"));
}
static class DummyAccount extends Account {
protected DummyAccount(Context context) {
super(context);
}
}
}

View file

@ -0,0 +1,149 @@
package com.fsck.k9.helper
import com.fsck.k9.Account
import com.fsck.k9.Identity
import com.fsck.k9.K9RobolectricTestRunner
import com.fsck.k9.mail.Address
import com.fsck.k9.mail.Message
import com.fsck.k9.mail.Message.RecipientType
import com.fsck.k9.mail.internet.MimeMessage
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RuntimeEnvironment
@RunWith(K9RobolectricTestRunner::class)
class IdentityHelperTest {
private val account = createDummyAccount()
@Test
fun getRecipientIdentityFromMessage_prefersToOverCc() {
val message = messageWithRecipients(
RecipientType.TO to IDENTITY_1_ADDRESS,
RecipientType.CC to IDENTITY_2_ADDRESS,
RecipientType.X_ORIGINAL_TO to IDENTITY_3_ADDRESS,
RecipientType.DELIVERED_TO to IDENTITY_4_ADDRESS,
RecipientType.X_ENVELOPE_TO to IDENTITY_5_ADDRESS)
val identity = IdentityHelper.getRecipientIdentityFromMessage(account, message)
assertThat(identity.email).isEqualTo(IDENTITY_1_ADDRESS)
}
@Test
fun getRecipientIdentityFromMessage_prefersCcOverXOriginalTo() {
val message = messageWithRecipients(
RecipientType.TO to "unrelated1@example.org",
RecipientType.CC to IDENTITY_2_ADDRESS,
RecipientType.X_ORIGINAL_TO to IDENTITY_3_ADDRESS,
RecipientType.DELIVERED_TO to IDENTITY_4_ADDRESS,
RecipientType.X_ENVELOPE_TO to IDENTITY_5_ADDRESS)
val identity = IdentityHelper.getRecipientIdentityFromMessage(account, message)
assertThat(identity.email).isEqualTo(IDENTITY_2_ADDRESS)
}
@Test
fun getRecipientIdentityFromMessage_prefersXOriginalToOverDeliveredTo() {
val message = messageWithRecipients(
RecipientType.TO to "unrelated1@example.org",
RecipientType.CC to "unrelated2@example.org",
RecipientType.X_ORIGINAL_TO to IDENTITY_3_ADDRESS,
RecipientType.DELIVERED_TO to IDENTITY_4_ADDRESS,
RecipientType.X_ENVELOPE_TO to IDENTITY_5_ADDRESS)
val identity = IdentityHelper.getRecipientIdentityFromMessage(account, message)
assertThat(identity.email).isEqualTo(IDENTITY_3_ADDRESS)
}
@Test
fun getRecipientIdentityFromMessage_prefersDeliveredToOverXEnvelopeTo() {
val message = messageWithRecipients(
RecipientType.TO to "unrelated1@example.org",
RecipientType.CC to "unrelated2@example.org",
RecipientType.X_ORIGINAL_TO to "unrelated3@example.org",
RecipientType.DELIVERED_TO to IDENTITY_4_ADDRESS,
RecipientType.X_ENVELOPE_TO to IDENTITY_5_ADDRESS)
val identity = IdentityHelper.getRecipientIdentityFromMessage(account, message)
assertThat(identity.email).isEqualTo(IDENTITY_4_ADDRESS)
}
@Test
fun getRecipientIdentityFromMessage_usesXEnvelopeToWhenPresent() {
val message = messageWithRecipients(
RecipientType.TO to "unrelated1@example.org",
RecipientType.CC to "unrelated2@example.org",
RecipientType.X_ORIGINAL_TO to "unrelated3@example.org",
RecipientType.DELIVERED_TO to "unrelated4@example.org",
RecipientType.X_ENVELOPE_TO to IDENTITY_5_ADDRESS)
val identity = IdentityHelper.getRecipientIdentityFromMessage(account, message)
assertThat(identity.email).isEqualTo(IDENTITY_5_ADDRESS)
}
@Test
fun getRecipientIdentityFromMessage_withoutAnyIdentityAddresses_returnsFirstIdentity() {
val message = messageWithRecipients(
RecipientType.TO to "unrelated1@example.org",
RecipientType.CC to "unrelated2@example.org",
RecipientType.X_ORIGINAL_TO to "unrelated3@example.org",
RecipientType.DELIVERED_TO to "unrelated4@example.org",
RecipientType.X_ENVELOPE_TO to "unrelated5@example.org")
val identity = IdentityHelper.getRecipientIdentityFromMessage(account, message)
assertThat(identity.email).isEqualTo(DEFAULT_ADDRESS)
}
@Test
fun getRecipientIdentityFromMessage_withNoApplicableHeaders_returnsFirstIdentity() {
val emptyMessage = MimeMessage()
val identity = IdentityHelper.getRecipientIdentityFromMessage(account, emptyMessage)
assertThat(identity.email).isEqualTo(DEFAULT_ADDRESS)
}
private fun createDummyAccount() = DummyAccount().apply {
identities = listOf(
newIdentity("Default", DEFAULT_ADDRESS),
newIdentity("Identity 1", IDENTITY_1_ADDRESS),
newIdentity("Identity 2", IDENTITY_2_ADDRESS),
newIdentity("Identity 3", IDENTITY_3_ADDRESS),
newIdentity("Identity 4", IDENTITY_4_ADDRESS),
newIdentity("Identity 5", IDENTITY_5_ADDRESS)
)
}
private fun newIdentity(name: String, email: String) = Identity().apply {
this.name = name
this.email = email
}
private fun messageWithRecipients(vararg recipients: Pair<RecipientType, String>): Message {
return MimeMessage().apply {
for ((recipientType, email) in recipients) {
setRecipients(recipientType, arrayOf(Address(email)))
}
}
}
companion object {
const val DEFAULT_ADDRESS = "default@example.org"
const val IDENTITY_1_ADDRESS = "identity1@example.org"
const val IDENTITY_2_ADDRESS = "identity2@example.org"
const val IDENTITY_3_ADDRESS = "identity3@example.org"
const val IDENTITY_4_ADDRESS = "identity4@example.org"
const val IDENTITY_5_ADDRESS = "identity5@example.org"
}
class DummyAccount : Account(RuntimeEnvironment.application)
}