### Question:
IdentityHelper { public static Identity getRecipientIdentityFromMessage(Account account, Message message) { Identity recipient = null; for (Address address : message.getRecipients(Message.RecipientType.X_ORIGINAL_TO)) { Identity identity = account.findIdentity(address); if (identity != null) { recipient = identity; break; } } if (recipient == null) { for (Address address : message.getRecipients(Message.RecipientType.DELIVERED_TO)) { Identity identity = account.findIdentity(address); if (identity != null) { recipient = identity; break; } } } if (recipient == null) { for (Address address : message.getRecipients(Message.RecipientType.X_ENVELOPE_TO)) { Identity identity = account.findIdentity(address); if (identity != null) { recipient = identity; break; } } } if (recipient == null) { for (Address address : message.getRecipients(Message.RecipientType.TO)) { Identity identity = account.findIdentity(address); if (identity != null) { recipient = identity; break; } } } if (recipient == null) { Address[] ccAddresses = message.getRecipients(Message.RecipientType.CC); if (ccAddresses.length > 0) { for (Address address : ccAddresses) { Identity identity = account.findIdentity(address); if (identity != null) { recipient = identity; break; } } } } if (recipient == null) { recipient = account.getIdentity(0); } return recipient; }  static Identity getRecipientIdentityFromMessage(Account account, Message message);  }### Answer:
@Test public void testXOriginalTo() throws Exception { Address[] addresses = {new Address("test2@mail.com")}; msg.setRecipients(Message.RecipientType.X_ORIGINAL_TO, addresses); Identity identity = IdentityHelper.getRecipientIdentityFromMessage(account, msg); assertTrue(identity.getEmail().equalsIgnoreCase("test2@mail.com")); }
@Test public void testTo_withoutXOriginalTo() throws Exception { Identity eva = IdentityHelper.getRecipientIdentityFromMessage(account, msg); assertTrue(eva.getEmail().equalsIgnoreCase("eva@example.org")); }
@Test public void testDeliveredTo() throws Exception { Address[] addresses = {new Address("test2@mail.com")}; msg.setRecipients(Message.RecipientType.DELIVERED_TO, addresses); msg.removeHeader("X-Original-To"); Identity identity = IdentityHelper.getRecipientIdentityFromMessage(account, msg); assertTrue(identity.getEmail().equalsIgnoreCase("test2@mail.com")); }
@Test public void testXEnvelopeTo() throws Exception { Address[] addresses = {new Address("test@mail.com")}; msg.setRecipients(Message.RecipientType.X_ENVELOPE_TO, addresses); msg.removeHeader("X-Original-To"); msg.removeHeader("Delivered-To"); Identity identity = IdentityHelper.getRecipientIdentityFromMessage(account, msg); assertTrue(identity.getEmail().equalsIgnoreCase("test@mail.com")); }
@Test public void testXEnvelopeTo_withXOriginalTo() throws Exception { Address[] addresses = {new Address("test@mail.com")}; Address[] xoriginaltoaddresses = {new Address("test2@mail.com")}; msg.setRecipients(Message.RecipientType.X_ENVELOPE_TO, addresses); msg.setRecipients(Message.RecipientType.X_ORIGINAL_TO, xoriginaltoaddresses); Identity identity = IdentityHelper.getRecipientIdentityFromMessage(account, msg); assertTrue(identity.getEmail().equalsIgnoreCase("test2@mail.com")); }