TosUtils { public List<MailAddress> getTo(Mail originalMail) throws MessagingException { List<InternetAddress> apparentlyTo = mailet.getTo(); if (!apparentlyTo.isEmpty()) { if (containsOnlyUnalteredOrTo(apparentlyTo)) { return ImmutableList.of(); } return SpecialAddressesUtils.from(mailet).replaceInternetAddresses(originalMail, apparentlyTo); } return ImmutableList.of(); } private  TosUtils(RedirectNotify mailet); static TosUtils from(RedirectNotify mailet); List<MailAddress> getTo(Mail originalMail);  }
@Test public void getToShouldReturnEmptyWhenMailetToIsEmpty() throws Exception { when(mailet.getTo()) .thenReturn(ImmutableList.<InternetAddress>of()); FakeMail fakeMail = FakeMail.defaultFakeMail(); List<MailAddress> to = testee.getTo(fakeMail); assertThat(to).isEmpty(); }
@Test public void getToShouldReturnEmptyWhenMailetToContainsOnlyUnaltered() throws Exception { when(mailet.getTo()) .thenReturn(ImmutableList.of(SpecialAddress.UNALTERED.toInternetAddress())); FakeMail fakeMail = FakeMail.defaultFakeMail(); List<MailAddress> to = testee.getTo(fakeMail); assertThat(to).isEmpty(); }
@Test public void getToShouldReturnEmptyWhenMailetToContainsOnlyRecipients() throws Exception { when(mailet.getTo()) .thenReturn(ImmutableList.of(SpecialAddress.RECIPIENTS.toInternetAddress())); FakeMail fakeMail = FakeMail.defaultFakeMail(); List<MailAddress> to = testee.getTo(fakeMail); assertThat(to).isEmpty(); }
@Test public void getToShouldReturnToWhenMailetToAreCommon() throws Exception { MailAddress mailAddress = new MailAddress("test", "james.org"); MailAddress mailAddress2 = new MailAddress("test2", "james.org"); when(mailet.getTo()) .thenReturn(ImmutableList.of(mailAddress.toInternetAddress(), mailAddress2.toInternetAddress())); FakeMail fakeMail = FakeMail.defaultFakeMail(); List<MailAddress> to = testee.getTo(fakeMail); ImmutableList<MailAddress> expectedTo = ImmutableList.of(mailAddress, mailAddress2); assertThat(to).containsOnlyElementsOf(expectedTo); }
@Test public void getToShouldReturnAddressesFromOriginalMailWhenMailetToAreSpecialAddresses() throws Exception { when(mailet.getTo()) .thenReturn(ImmutableList.of(SpecialAddress.FROM.toInternetAddress(), SpecialAddress.TO.toInternetAddress())); MailAddress from = new MailAddress("from", "james.org"); MailAddress toMailAddress = new MailAddress("to", "james.org"); MailAddress toMailAddress2 = new MailAddress("to2", "james.org"); FakeMail fakeMail = FakeMail.builder() .name("name") .sender(from) .recipients(toMailAddress, toMailAddress2) .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() .addToRecipient("to@james.org", "to2@james.org")) .build(); List<MailAddress> to = testee.getTo(fakeMail); ImmutableList<MailAddress> expectedTo = ImmutableList.of(from, toMailAddress, toMailAddress2); assertThat(to).containsOnlyElementsOf(expectedTo); }