MAILET-115 Refactor getSender & getReversePath
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/6f52c81a Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/6f52c81a Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/6f52c81a Branch: refs/heads/master Commit: 6f52c81a1d9d5c149e209bff65ca2fc05c1b4c81 Parents: 734a656 Author: Antoine Duprat <[email protected]> Authored: Tue Nov 15 10:48:56 2016 +0100 Committer: Benoit Tellier <[email protected]> Committed: Wed Jan 11 10:03:29 2017 +0700 ---------------------------------------------------------------------- .../apache/james/transport/mailets/Bounce.java | 9 +- .../james/transport/mailets/DSNBounce.java | 9 +- .../transport/mailets/NotifyPostmaster.java | 9 +- .../james/transport/mailets/NotifySender.java | 9 +- .../james/transport/mailets/Redirect.java | 16 +-- .../apache/james/transport/mailets/Resend.java | 9 +- .../mailets/redirect/AbstractRedirect.java | 4 + .../james/transport/util/ReversePathUtils.java | 57 -------- .../james/transport/util/SenderUtils.java | 57 -------- .../transport/util/SpecialAddressesUtils.java | 19 +++ .../transport/util/ReversePathUtilsTest.java | 129 ------------------- .../james/transport/util/SenderUtilsTest.java | 118 ----------------- .../util/SpecialAddressesUtilsTest.java | 61 ++++++++- 13 files changed, 113 insertions(+), 393 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/6f52c81a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Bounce.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Bounce.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Bounce.java index 9429bf5..b5e7ca7 100644 --- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Bounce.java +++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Bounce.java @@ -30,8 +30,7 @@ import org.apache.james.transport.mailets.redirect.NotifyMailetInitParameters; import org.apache.james.transport.mailets.redirect.NotifyMailetsMessage; import org.apache.james.transport.mailets.redirect.SpecialAddress; import org.apache.james.transport.mailets.utils.MimeMessageModifier; -import org.apache.james.transport.util.ReversePathUtils; -import org.apache.james.transport.util.SenderUtils; +import org.apache.james.transport.util.SpecialAddressesUtils; import org.apache.mailet.Mail; import org.apache.mailet.MailAddress; @@ -160,12 +159,14 @@ public class Bounce extends AbstractRedirect { @Override protected MailAddress getReversePath() throws MessagingException { - return ReversePathUtils.from(this).getReversePath(); + return SpecialAddressesUtils.from(this) + .getFirstSpecialAddressIfMatchingOrGivenAddress(getInitParameters().getReversePath(), AbstractRedirect.REVERSE_PATH_ALLOWED_SPECIALS); } @Override protected MailAddress getSender() throws MessagingException { - return SenderUtils.from(this).getSender(); + return SpecialAddressesUtils.from(this) + .getFirstSpecialAddressIfMatchingOrGivenAddress(getInitParameters().getSender(), AbstractRedirect.SENDER_ALLOWED_SPECIALS); } @Override http://git-wip-us.apache.org/repos/asf/james-project/blob/6f52c81a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/DSNBounce.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/DSNBounce.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/DSNBounce.java index 0d0b795..6e7197e 100755 --- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/DSNBounce.java +++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/DSNBounce.java @@ -40,8 +40,7 @@ import org.apache.james.transport.mailets.redirect.SpecialAddress; import org.apache.james.transport.mailets.redirect.TypeCode; import org.apache.james.transport.mailets.utils.MimeMessageModifier; import org.apache.james.transport.util.Patterns; -import org.apache.james.transport.util.ReversePathUtils; -import org.apache.james.transport.util.SenderUtils; +import org.apache.james.transport.util.SpecialAddressesUtils; import org.apache.mailet.Mail; import org.apache.mailet.MailAddress; import org.apache.mailet.base.DateFormats; @@ -147,7 +146,8 @@ public class DSNBounce extends AbstractRedirect { @Override protected MailAddress getReversePath() throws MessagingException { - return ReversePathUtils.from(this).getReversePath(); + return SpecialAddressesUtils.from(this) + .getFirstSpecialAddressIfMatchingOrGivenAddress(getInitParameters().getReversePath(), AbstractRedirect.REVERSE_PATH_ALLOWED_SPECIALS); } @Override @@ -157,7 +157,8 @@ public class DSNBounce extends AbstractRedirect { @Override protected MailAddress getSender() throws MessagingException { - return SenderUtils.from(this).getSender(); + return SpecialAddressesUtils.from(this) + .getFirstSpecialAddressIfMatchingOrGivenAddress(getInitParameters().getSender(), AbstractRedirect.SENDER_ALLOWED_SPECIALS); } @Override http://git-wip-us.apache.org/repos/asf/james-project/blob/6f52c81a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifyPostmaster.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifyPostmaster.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifyPostmaster.java index 4b90ac8..14144fc 100644 --- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifyPostmaster.java +++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifyPostmaster.java @@ -31,8 +31,7 @@ import org.apache.james.transport.mailets.redirect.NotifyMailetInitParameters; import org.apache.james.transport.mailets.redirect.NotifyMailetsMessage; import org.apache.james.transport.mailets.redirect.SpecialAddress; import org.apache.james.transport.mailets.utils.MimeMessageModifier; -import org.apache.james.transport.util.ReversePathUtils; -import org.apache.james.transport.util.SenderUtils; +import org.apache.james.transport.util.SpecialAddressesUtils; import org.apache.mailet.Mail; import org.apache.mailet.MailAddress; import org.apache.mailet.MailetConfig; @@ -172,7 +171,8 @@ public class NotifyPostmaster extends AbstractRedirect { @Override protected MailAddress getReversePath() throws MessagingException { - return ReversePathUtils.from(this).getReversePath(); + return SpecialAddressesUtils.from(this) + .getFirstSpecialAddressIfMatchingOrGivenAddress(getInitParameters().getReversePath(), AbstractRedirect.REVERSE_PATH_ALLOWED_SPECIALS); } @Override @@ -182,7 +182,8 @@ public class NotifyPostmaster extends AbstractRedirect { @Override protected MailAddress getSender() throws MessagingException { - return SenderUtils.from(this).getSender(); + return SpecialAddressesUtils.from(this) + .getFirstSpecialAddressIfMatchingOrGivenAddress(getInitParameters().getSender(), AbstractRedirect.SENDER_ALLOWED_SPECIALS); } @Override http://git-wip-us.apache.org/repos/asf/james-project/blob/6f52c81a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifySender.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifySender.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifySender.java index 0299a06..2d35948 100644 --- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifySender.java +++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifySender.java @@ -31,8 +31,7 @@ import org.apache.james.transport.mailets.redirect.NotifyMailetInitParameters; import org.apache.james.transport.mailets.redirect.NotifyMailetsMessage; import org.apache.james.transport.mailets.redirect.SpecialAddress; import org.apache.james.transport.mailets.utils.MimeMessageModifier; -import org.apache.james.transport.util.ReversePathUtils; -import org.apache.james.transport.util.SenderUtils; +import org.apache.james.transport.util.SpecialAddressesUtils; import org.apache.mailet.Mail; import org.apache.mailet.MailAddress; import org.apache.mailet.MailetConfig; @@ -172,7 +171,8 @@ public class NotifySender extends AbstractRedirect { @Override protected MailAddress getReversePath() throws MessagingException { - return ReversePathUtils.from(this).getReversePath(); + return SpecialAddressesUtils.from(this) + .getFirstSpecialAddressIfMatchingOrGivenAddress(getInitParameters().getReversePath(), AbstractRedirect.REVERSE_PATH_ALLOWED_SPECIALS); } @Override @@ -182,7 +182,8 @@ public class NotifySender extends AbstractRedirect { @Override protected MailAddress getSender() throws MessagingException { - return SenderUtils.from(this).getSender(); + return SpecialAddressesUtils.from(this) + .getFirstSpecialAddressIfMatchingOrGivenAddress(getInitParameters().getSender(), AbstractRedirect.SENDER_ALLOWED_SPECIALS); } @Override http://git-wip-us.apache.org/repos/asf/james-project/blob/6f52c81a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Redirect.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Redirect.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Redirect.java index b31f609..22fddc6 100644 --- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Redirect.java +++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Redirect.java @@ -31,7 +31,7 @@ import org.apache.james.transport.mailets.redirect.RedirectMailetInitParameters; import org.apache.james.transport.mailets.redirect.TypeCode; import org.apache.james.transport.mailets.utils.MimeMessageModifier; import org.apache.james.transport.util.MailAddressUtils; -import org.apache.james.transport.util.SenderUtils; +import org.apache.james.transport.util.SpecialAddressesUtils; import org.apache.mailet.Mail; import org.apache.mailet.MailAddress; @@ -377,15 +377,8 @@ public class Redirect extends AbstractRedirect { @Override protected MailAddress getReversePath() throws MessagingException { - String addressString = getInitParameter("reversePath"); - if (addressString == null) { - return null; - } - - List<MailAddress> mailAddress = AddressExtractor.withContext(getMailetContext()) - .allowedSpecials(ImmutableList.of("postmaster", "sender", "null")) - .extract(addressString); - return mailAddress.get(0); + return SpecialAddressesUtils.from(this) + .getFirstSpecialAddressIfMatchingOrGivenAddress(getInitParameters().getReversePath(), ImmutableList.of("postmaster", "sender", "null")); } @Override @@ -409,7 +402,8 @@ public class Redirect extends AbstractRedirect { @Override protected MailAddress getSender() throws MessagingException { - return SenderUtils.from(this).getSender(); + return SpecialAddressesUtils.from(this) + .getFirstSpecialAddressIfMatchingOrGivenAddress(getInitParameters().getSender(), AbstractRedirect.SENDER_ALLOWED_SPECIALS); } @Override http://git-wip-us.apache.org/repos/asf/james-project/blob/6f52c81a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Resend.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Resend.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Resend.java index d2aa272..9fd24ff 100644 --- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Resend.java +++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Resend.java @@ -30,8 +30,7 @@ import org.apache.james.transport.mailets.redirect.InitParameters; import org.apache.james.transport.mailets.redirect.RedirectMailetInitParameters; import org.apache.james.transport.mailets.utils.MimeMessageModifier; import org.apache.james.transport.util.MailAddressUtils; -import org.apache.james.transport.util.ReversePathUtils; -import org.apache.james.transport.util.SenderUtils; +import org.apache.james.transport.util.SpecialAddressesUtils; import org.apache.mailet.Mail; import org.apache.mailet.MailAddress; @@ -348,7 +347,8 @@ public class Resend extends AbstractRedirect { @Override protected MailAddress getReversePath() throws MessagingException { - return ReversePathUtils.from(this).getReversePath(); + return SpecialAddressesUtils.from(this) + .getFirstSpecialAddressIfMatchingOrGivenAddress(getInitParameters().getReversePath(), AbstractRedirect.REVERSE_PATH_ALLOWED_SPECIALS); } @Override @@ -364,7 +364,8 @@ public class Resend extends AbstractRedirect { @Override protected MailAddress getSender() throws MessagingException { - return SenderUtils.from(this).getSender(); + return SpecialAddressesUtils.from(this) + .getFirstSpecialAddressIfMatchingOrGivenAddress(getInitParameters().getSender(), AbstractRedirect.SENDER_ALLOWED_SPECIALS); } @Override http://git-wip-us.apache.org/repos/asf/james-project/blob/6f52c81a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/AbstractRedirect.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/AbstractRedirect.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/AbstractRedirect.java index faf359f..8a12293 100644 --- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/AbstractRedirect.java +++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/AbstractRedirect.java @@ -48,6 +48,8 @@ import org.apache.mailet.base.DateFormats; import org.apache.mailet.base.GenericMailet; import org.apache.mailet.base.RFC2822Headers; +import com.google.common.collect.ImmutableList; + /** * <p> * Abstract mailet providing configurable redirection services.<br> @@ -150,6 +152,8 @@ import org.apache.mailet.base.RFC2822Headers; public abstract class AbstractRedirect extends GenericMailet { private static final char LINE_BREAK = '\n'; + public static final List<String> REVERSE_PATH_ALLOWED_SPECIALS = ImmutableList.of("postmaster", "sender", "null", "unaltered"); + public static final List<String> SENDER_ALLOWED_SPECIALS = ImmutableList.of("postmaster", "sender", "unaltered"); public abstract InitParameters getInitParameters(); http://git-wip-us.apache.org/repos/asf/james-project/blob/6f52c81a/server/mailet/mailets/src/main/java/org/apache/james/transport/util/ReversePathUtils.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/util/ReversePathUtils.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/util/ReversePathUtils.java deleted file mode 100644 index 651f8e1..0000000 --- a/server/mailet/mailets/src/main/java/org/apache/james/transport/util/ReversePathUtils.java +++ /dev/null @@ -1,57 +0,0 @@ -/**************************************************************** - * 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.util; - -import java.util.List; - -import javax.mail.MessagingException; - -import org.apache.james.transport.mailets.redirect.AbstractRedirect; -import org.apache.james.transport.mailets.redirect.AddressExtractor; -import org.apache.mailet.MailAddress; - -import com.google.common.base.Strings; -import com.google.common.collect.FluentIterable; -import com.google.common.collect.ImmutableList; - -public class ReversePathUtils { - - public static ReversePathUtils from(AbstractRedirect mailet) { - return new ReversePathUtils(mailet); - } - - private final AbstractRedirect mailet; - - private ReversePathUtils(AbstractRedirect mailet) { - this.mailet = mailet; - } - - public MailAddress getReversePath() throws MessagingException { - String reversePath = mailet.getInitParameters().getReversePath(); - if (Strings.isNullOrEmpty(reversePath)) { - return null; - } - - List<MailAddress> extractAddresses = AddressExtractor - .withContext(mailet.getMailetContext()) - .allowedSpecials(ImmutableList.of("postmaster", "sender", "null", "unaltered")) - .extract(reversePath); - return FluentIterable.from(extractAddresses).first().orNull(); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/6f52c81a/server/mailet/mailets/src/main/java/org/apache/james/transport/util/SenderUtils.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/util/SenderUtils.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/util/SenderUtils.java deleted file mode 100644 index 20c73ea..0000000 --- a/server/mailet/mailets/src/main/java/org/apache/james/transport/util/SenderUtils.java +++ /dev/null @@ -1,57 +0,0 @@ -/**************************************************************** - * 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.util; - -import java.util.List; - -import javax.mail.MessagingException; - -import org.apache.james.transport.mailets.redirect.AbstractRedirect; -import org.apache.james.transport.mailets.redirect.AddressExtractor; -import org.apache.mailet.MailAddress; - -import com.google.common.base.Strings; -import com.google.common.collect.FluentIterable; -import com.google.common.collect.ImmutableList; - -public class SenderUtils { - - public static SenderUtils from(AbstractRedirect mailet) { - return new SenderUtils(mailet); - } - - private final AbstractRedirect mailet; - - private SenderUtils(AbstractRedirect mailet) { - this.mailet = mailet; - } - - public MailAddress getSender() throws MessagingException { - String sender = mailet.getInitParameters().getSender(); - if (Strings.isNullOrEmpty(sender)) { - return null; - } - - List<MailAddress> extractAddresses = AddressExtractor - .withContext(mailet.getMailetContext()) - .allowedSpecials(ImmutableList.of("postmaster", "sender", "unaltered")) - .extract(sender); - return FluentIterable.from(extractAddresses).first().orNull(); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/6f52c81a/server/mailet/mailets/src/main/java/org/apache/james/transport/util/SpecialAddressesUtils.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/util/SpecialAddressesUtils.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/util/SpecialAddressesUtils.java index a6e6dbe..707925d 100644 --- a/server/mailet/mailets/src/main/java/org/apache/james/transport/util/SpecialAddressesUtils.java +++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/util/SpecialAddressesUtils.java @@ -26,6 +26,7 @@ import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.ParseException; +import org.apache.james.transport.mailets.redirect.AddressExtractor; import org.apache.james.transport.mailets.redirect.SpecialAddress; import org.apache.james.transport.mailets.redirect.SpecialAddressKind; import org.apache.mailet.Mail; @@ -33,6 +34,8 @@ import org.apache.mailet.MailAddress; import org.apache.mailet.base.GenericMailet; import org.apache.mailet.base.RFC2822Headers; +import com.google.common.base.Strings; +import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; @@ -254,4 +257,20 @@ public class SpecialAddressesUtils { return ImmutableList.of(); } } + + /** + * If the givenAddress matches one of the allowedSpecials SpecialAddresses, then it's returned + * else the givenAddress is returned. + */ + public MailAddress getFirstSpecialAddressIfMatchingOrGivenAddress(String givenAddress, List<String> allowedSpecials) throws MessagingException { + if (Strings.isNullOrEmpty(givenAddress)) { + return null; + } + + List<MailAddress> extractAddresses = AddressExtractor + .withContext(genericMailet.getMailetContext()) + .allowedSpecials(allowedSpecials) + .extract(givenAddress); + return FluentIterable.from(extractAddresses).first().orNull(); + } } http://git-wip-us.apache.org/repos/asf/james-project/blob/6f52c81a/server/mailet/mailets/src/test/java/org/apache/james/transport/util/ReversePathUtilsTest.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/util/ReversePathUtilsTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/util/ReversePathUtilsTest.java deleted file mode 100644 index 4ceff65..0000000 --- a/server/mailet/mailets/src/test/java/org/apache/james/transport/util/ReversePathUtilsTest.java +++ /dev/null @@ -1,129 +0,0 @@ -/**************************************************************** - * 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.util; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import org.apache.james.transport.mailets.redirect.AbstractRedirect; -import org.apache.james.transport.mailets.redirect.InitParameters; -import org.apache.james.transport.mailets.redirect.SpecialAddress; -import org.apache.mailet.MailAddress; -import org.apache.mailet.MailetContext; -import org.junit.Before; -import org.junit.Test; - -public class ReversePathUtilsTest { - - private InitParameters initParameters; - private MailAddress postmaster; - private ReversePathUtils testee; - - @Before - public void setup() throws Exception { - AbstractRedirect mailet = mock(AbstractRedirect.class); - initParameters = mock(InitParameters.class); - when(mailet.getInitParameters()) - .thenReturn(initParameters); - - MailetContext mailetContext = mock(MailetContext.class); - postmaster = new MailAddress("[email protected]"); - when(mailetContext.getPostmaster()) - .thenReturn(postmaster); - when(mailet.getMailetContext()) - .thenReturn(mailetContext); - - testee = ReversePathUtils.from(mailet); - } - - @Test - public void getReversePathShouldReturnNullWhenReversePathInitParameterIsNull() throws Exception { - when(initParameters.getReversePath()) - .thenReturn(null); - - MailAddress reversePath = testee.getReversePath(); - - assertThat(reversePath).isNull(); - } - - @Test - public void getReversePathShouldReturnNullWhenReversePathInitParameterIsEmpty() throws Exception { - when(initParameters.getReversePath()) - .thenReturn(""); - - MailAddress reversePath = testee.getReversePath(); - - assertThat(reversePath).isNull(); - } - - @Test - public void getReversePathShouldReturnFirstMailAddressWhenMultipleAddresses() throws Exception { - when(initParameters.getReversePath()) - .thenReturn("[email protected], [email protected]"); - - MailAddress reversePath = testee.getReversePath(); - - MailAddress expectedMailAddress = new MailAddress("test", "james.org"); - assertThat(reversePath).isEqualTo(expectedMailAddress); - } - - @Test - public void getReversePathShouldReturnPostmasterSpecialAddressWhenFirstAddressIsSpecialPostmaster() throws Exception { - when(initParameters.getReversePath()) - .thenReturn("postmaster, [email protected]"); - - MailAddress reversePath = testee.getReversePath(); - - assertThat(reversePath).isEqualTo(postmaster); - } - - @Test - public void getReversePathShouldReturnSenderSpecialAddressWhenFirstAddressIsSpecialSender() throws Exception { - when(initParameters.getReversePath()) - .thenReturn("sender, [email protected]"); - - MailAddress reversePath = testee.getReversePath(); - - MailAddress expectedMailAddress = SpecialAddress.SENDER; - assertThat(reversePath).isEqualTo(expectedMailAddress); - } - - @Test - public void getReversePathShouldReturnNullSpecialAddressWhenFirstAddressIsSpecialNull() throws Exception { - when(initParameters.getReversePath()) - .thenReturn("null, [email protected]"); - - MailAddress reversePath = testee.getReversePath(); - - MailAddress expectedMailAddress = SpecialAddress.NULL; - assertThat(reversePath).isEqualTo(expectedMailAddress); - } - - @Test - public void getReversePathShouldReturnUnalteredSpecialAddressWhenFirstAddressIsSpecialUnaltered() throws Exception { - when(initParameters.getReversePath()) - .thenReturn("unaltered, [email protected]"); - - MailAddress reversePath = testee.getReversePath(); - - MailAddress expectedMailAddress = SpecialAddress.UNALTERED; - assertThat(reversePath).isEqualTo(expectedMailAddress); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/6f52c81a/server/mailet/mailets/src/test/java/org/apache/james/transport/util/SenderUtilsTest.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/util/SenderUtilsTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/util/SenderUtilsTest.java deleted file mode 100644 index 50af2ce..0000000 --- a/server/mailet/mailets/src/test/java/org/apache/james/transport/util/SenderUtilsTest.java +++ /dev/null @@ -1,118 +0,0 @@ -/**************************************************************** - * 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.util; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import org.apache.james.transport.mailets.redirect.AbstractRedirect; -import org.apache.james.transport.mailets.redirect.InitParameters; -import org.apache.james.transport.mailets.redirect.SpecialAddress; -import org.apache.mailet.MailAddress; -import org.apache.mailet.MailetContext; -import org.junit.Before; -import org.junit.Test; - -public class SenderUtilsTest { - - private InitParameters initParameters; - private MailAddress postmaster; - private SenderUtils testee; - - @Before - public void setup() throws Exception { - AbstractRedirect mailet = mock(AbstractRedirect.class); - initParameters = mock(InitParameters.class); - when(mailet.getInitParameters()) - .thenReturn(initParameters); - - MailetContext mailetContext = mock(MailetContext.class); - postmaster = new MailAddress("[email protected]"); - when(mailetContext.getPostmaster()) - .thenReturn(postmaster); - when(mailet.getMailetContext()) - .thenReturn(mailetContext); - - testee = SenderUtils.from(mailet); - } - - @Test - public void getSenderShouldReturnNullWhenSenderInitParameterIsNull() throws Exception { - when(initParameters.getSender()) - .thenReturn(null); - - MailAddress sender = testee.getSender(); - - assertThat(sender).isNull(); - } - - @Test - public void getSenderShouldReturnNullWhenSenderInitParameterIsEmpty() throws Exception { - when(initParameters.getSender()) - .thenReturn(""); - - MailAddress sender = testee.getSender(); - - assertThat(sender).isNull(); - } - - @Test - public void getSenderShouldReturnFirstMailAddressWhenMultipleAddresses() throws Exception { - when(initParameters.getSender()) - .thenReturn("[email protected], [email protected]"); - - MailAddress sender = testee.getSender(); - - MailAddress expectedMailAddress = new MailAddress("test", "james.org"); - assertThat(sender).isEqualTo(expectedMailAddress); - } - - @Test - public void getSenderShouldReturnPostmasterSpecialAddressWhenFirstAddressIsSpecialPostmaster() throws Exception { - when(initParameters.getSender()) - .thenReturn("postmaster, [email protected]"); - - MailAddress sender = testee.getSender(); - - assertThat(sender).isEqualTo(postmaster); - } - - @Test - public void getSenderShouldReturnSenderSpecialAddressWhenFirstAddressIsSpecialSender() throws Exception { - when(initParameters.getSender()) - .thenReturn("sender, [email protected]"); - - MailAddress sender = testee.getSender(); - - MailAddress expectedMailAddress = SpecialAddress.SENDER; - assertThat(sender).isEqualTo(expectedMailAddress); - } - - @Test - public void getSenderShouldReturnUnalteredSpecialAddressWhenFirstAddressIsSpecialUnaltered() throws Exception { - when(initParameters.getSender()) - .thenReturn("unaltered, [email protected]"); - - MailAddress sender = testee.getSender(); - - MailAddress expectedMailAddress = SpecialAddress.UNALTERED; - assertThat(sender).isEqualTo(expectedMailAddress); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/6f52c81a/server/mailet/mailets/src/test/java/org/apache/james/transport/util/SpecialAddressesUtilsTest.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/util/SpecialAddressesUtilsTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/util/SpecialAddressesUtilsTest.java index 78b724c..5388998 100644 --- a/server/mailet/mailets/src/test/java/org/apache/james/transport/util/SpecialAddressesUtilsTest.java +++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/util/SpecialAddressesUtilsTest.java @@ -19,6 +19,8 @@ package org.apache.james.transport.util; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import java.util.Collection; import java.util.List; @@ -32,6 +34,7 @@ import javax.mail.internet.MimeMessage; import org.apache.james.transport.mailets.redirect.SpecialAddress; import org.apache.mailet.Mail; import org.apache.mailet.MailAddress; +import org.apache.mailet.MailetContext; import org.apache.mailet.base.GenericMailet; import org.apache.mailet.base.MailAddressFixture; import org.apache.mailet.base.RFC2822Headers; @@ -43,13 +46,24 @@ import com.google.common.collect.ImmutableList; public class SpecialAddressesUtilsTest { + private MailAddress postmaster; private SpecialAddressesUtils testee; @Before - public void setup() { + public void setup() throws Exception { + final MailetContext mailetContext = mock(MailetContext.class); + postmaster = new MailAddress("[email protected]"); + when(mailetContext.getPostmaster()) + .thenReturn(postmaster); + testee = SpecialAddressesUtils.from(new GenericMailet() { @Override + public MailetContext getMailetContext() { + return mailetContext; + } + + @Override public void service(Mail mail) throws MessagingException { } }); @@ -440,4 +454,49 @@ public class SpecialAddressesUtilsTest { MailAddress expected = new MailAddress("[email protected]"); assertThat(addresses).containsOnly(expected); } + + @Test + public void getFirstSpecialAddressIfMatchingOrGivenAddressShouldReturnNullWhenSenderInitParameterIsNull() throws Exception { + MailAddress sender = testee.getFirstSpecialAddressIfMatchingOrGivenAddress(null, ImmutableList.of("postmaster", "sender", "unaltered")); + + assertThat(sender).isNull(); + } + + @Test + public void getFirstSpecialAddressIfMatchingOrGivenAddressShouldReturnNullWhenSenderInitParameterIsEmpty() throws Exception { + MailAddress sender = testee.getFirstSpecialAddressIfMatchingOrGivenAddress("", ImmutableList.of("postmaster", "sender", "unaltered")); + + assertThat(sender).isNull(); + } + + @Test + public void getFirstSpecialAddressIfMatchingOrGivenAddressShouldReturnGivenAddressWhenNoSpecialAddressMatches() throws Exception { + MailAddress sender = testee.getFirstSpecialAddressIfMatchingOrGivenAddress("[email protected]", ImmutableList.of("postmaster", "sender", "unaltered")); + + MailAddress expectedMailAddress = new MailAddress("test", "james.org"); + assertThat(sender).isEqualTo(expectedMailAddress); + } + + @Test + public void getFirstSpecialAddressIfMatchingOrGivenAddressShouldReturnFirstSpecialAddressWhenMatching() throws Exception { + MailAddress sender = testee.getFirstSpecialAddressIfMatchingOrGivenAddress("postmaster", ImmutableList.of("postmaster", "sender", "unaltered")); + + assertThat(sender).isEqualTo(postmaster); + } + + @Test + public void getFirstSpecialAddressIfMatchingOrGivenAddressShouldReturnSecondSpecialAddressWhenMatching() throws Exception { + MailAddress sender = testee.getFirstSpecialAddressIfMatchingOrGivenAddress("sender", ImmutableList.of("postmaster", "sender", "unaltered")); + + MailAddress expectedMailAddress = SpecialAddress.SENDER; + assertThat(sender).isEqualTo(expectedMailAddress); + } + + @Test + public void getFirstSpecialAddressIfMatchingOrGivenAddressShouldReturnLastSpecialAddressWhenMatching() throws Exception { + MailAddress sender = testee.getFirstSpecialAddressIfMatchingOrGivenAddress("unaltered", ImmutableList.of("postmaster", "sender", "unaltered")); + + MailAddress expectedMailAddress = SpecialAddress.UNALTERED; + assertThat(sender).isEqualTo(expectedMailAddress); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
