JAMES-1976 Test for Recipients To Lower Case With many thanks to:
Nguyen Thi Mai and Le Thi Huong Lai for their work on Quotas and In memory subscriptions Tran Thi & My Linh for their work on TooMuchLines txc1996 & Van Thanh For improving readability of SMTPAuthIsSuccessfulTest thienan090196 & Tran Thi & My Linh for their tests on RecipientIsLocalTest >From Passerelle Numeriques VietNam Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/d52c5980 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/d52c5980 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/d52c5980 Branch: refs/heads/master Commit: d52c5980f83ab9edccfb8a63eb9ea86a148d2940 Parents: 9715af0 Author: Benoit Tellier <btell...@linagora.com> Authored: Wed Dec 28 08:53:27 2016 +0700 Committer: benwa <btell...@linagora.com> Committed: Wed Mar 29 08:00:16 2017 +0700 ---------------------------------------------------------------------- .../transport/mailets/RecipientToLowerCase.java | 29 +++++--- .../mailets/RecipientToLowerCaseTest.java | 73 ++++++++++++++++++++ 2 files changed, 93 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/d52c5980/mailet/standard/src/main/java/org/apache/james/transport/mailets/RecipientToLowerCase.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/main/java/org/apache/james/transport/mailets/RecipientToLowerCase.java b/mailet/standard/src/main/java/org/apache/james/transport/mailets/RecipientToLowerCase.java index 025fd67..f7c72cc 100644 --- a/mailet/standard/src/main/java/org/apache/james/transport/mailets/RecipientToLowerCase.java +++ b/mailet/standard/src/main/java/org/apache/james/transport/mailets/RecipientToLowerCase.java @@ -18,17 +18,20 @@ ****************************************************************/ package org.apache.james.transport.mailets; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; import java.util.Locale; import javax.mail.MessagingException; +import javax.mail.internet.AddressException; import org.apache.mailet.Mail; import org.apache.mailet.MailAddress; import org.apache.mailet.base.GenericMailet; +import com.google.common.base.Function; +import com.google.common.base.Throwables; +import com.google.common.collect.FluentIterable; +import com.google.common.collect.ImmutableList; + /** * {@link GenericMailet} which convert all Recipients to lowercase * @@ -36,14 +39,22 @@ import org.apache.mailet.base.GenericMailet; */ public class RecipientToLowerCase extends GenericMailet{ + public static final Function<MailAddress, MailAddress> TO_LOWERCASE = new Function<MailAddress, MailAddress>() { + @Override + public MailAddress apply(MailAddress input) { + try { + return new MailAddress(input.asString().toLowerCase(Locale.US)); + } catch (AddressException e) { + throw Throwables.propagate(e); + } + } + }; + @Override public void service(Mail mail) throws MessagingException { - Iterator<MailAddress> rcpts = mail.getRecipients().iterator(); - List<MailAddress> newRcpts = new ArrayList<MailAddress>(); - while(rcpts.hasNext()) { - newRcpts.add(new MailAddress(rcpts.next().toString().toLowerCase(Locale.US))); - } - mail.setRecipients(newRcpts); + mail.setRecipients(FluentIterable.from(mail.getRecipients()) + .transform(TO_LOWERCASE) + .toList()); } } http://git-wip-us.apache.org/repos/asf/james-project/blob/d52c5980/mailet/standard/src/test/java/org/apache/james/transport/mailets/RecipientToLowerCaseTest.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/test/java/org/apache/james/transport/mailets/RecipientToLowerCaseTest.java b/mailet/standard/src/test/java/org/apache/james/transport/mailets/RecipientToLowerCaseTest.java new file mode 100644 index 0000000..db88e69 --- /dev/null +++ b/mailet/standard/src/test/java/org/apache/james/transport/mailets/RecipientToLowerCaseTest.java @@ -0,0 +1,73 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.transport.mailets; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Collection; + +import org.apache.mailet.MailAddress; +import org.apache.mailet.base.test.FakeMail; +import org.assertj.core.api.iterable.Extractor; +import org.junit.Before; +import org.junit.Test; + +public class RecipientToLowerCaseTest { + + public static final Extractor<MailAddress, String> AS_STRING = new Extractor<MailAddress, String>() { + @Override + public String extract(MailAddress mailAddress) { + return mailAddress.asString(); + } + }; + + private RecipientToLowerCase testee; + + @Before + public void setUp() { + testee = new RecipientToLowerCase(); + } + + @Test + public void serviceShouldPutRecipientToLowerCase() throws Exception { + FakeMail fakeMail = FakeMail.builder() + .recipient(new MailAddress("thienan1...@gmail.com")) + .build(); + + testee.service(fakeMail); + + Collection<MailAddress> recipients = fakeMail.getRecipients(); + + assertThat(recipients) + .extracting(AS_STRING) + .containsOnly("thienan1...@gmail.com"); + } + + @Test + public void serviceShouldHaveNoEffectWhenNoRecipient() throws Exception { + FakeMail fakeMail = FakeMail.builder() + .build(); + + testee.service(fakeMail); + + assertThat(fakeMail.getRecipients()) + .isEmpty(); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org