MAILET-139 SenderHostIsLocal should be covered with tests
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/977ae9e0 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/977ae9e0 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/977ae9e0 Branch: refs/heads/master Commit: 977ae9e0e60d159c83f480fe944f4e67ccb5a58b Parents: ff57437 Author: Laura Royet <lro...@linagora.com> Authored: Tue Nov 8 14:31:46 2016 +0100 Committer: Laura Royet <lro...@linagora.com> Committed: Thu Nov 17 17:18:28 2016 +0100 ---------------------------------------------------------------------- .../transport/matchers/SenderHostIsLocal.java | 16 ++-- .../matchers/SenderHostIsLocalTest.java | 85 ++++++++++++++++++++ 2 files changed, 95 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/977ae9e0/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderHostIsLocal.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderHostIsLocal.java b/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderHostIsLocal.java index 2092908..6200383 100644 --- a/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderHostIsLocal.java +++ b/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderHostIsLocal.java @@ -20,11 +20,11 @@ package org.apache.james.transport.matchers; -import org.apache.mailet.base.GenericMatcher; +import java.util.Collection; + import org.apache.mailet.Mail; import org.apache.mailet.MailAddress; - -import java.util.Collection; +import org.apache.mailet.base.GenericMatcher; /** * Checks the sender's displayed domain name against a the hosts serviced by @@ -38,12 +38,16 @@ import java.util.Collection; */ public class SenderHostIsLocal extends GenericMatcher { public Collection<MailAddress> match(Mail mail) { - if (mail.getSender() != null - && this.getMailetContext().isLocalServer( - mail.getSender().getDomain().toLowerCase())) { + if (mail.getSender() != null && isLocalServer(mail)) { return mail.getRecipients(); } return null; + + } + private boolean isLocalServer(Mail mail) { + return this.getMailetContext().isLocalServer( + mail.getSender().getDomain().toLowerCase()); } + } http://git-wip-us.apache.org/repos/asf/james-project/blob/977ae9e0/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderHostIsLocalTest.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderHostIsLocalTest.java b/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderHostIsLocalTest.java new file mode 100644 index 0000000..e52fc4c --- /dev/null +++ b/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderHostIsLocalTest.java @@ -0,0 +1,85 @@ +/**************************************************************** + * 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.matchers; + +import static org.apache.mailet.base.MailAddressFixture.ANY_AT_JAMES; +import static org.apache.mailet.base.MailAddressFixture.ANY_AT_JAMES2; +import static org.apache.mailet.base.MailAddressFixture.JAMES2_APACHE_ORG; +import static org.apache.mailet.base.MailAddressFixture.JAMES_APACHE_ORG; +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 javax.mail.MessagingException; + +import org.apache.mailet.Mail; +import org.apache.mailet.MailAddress; +import org.apache.mailet.MailetContext; +import org.apache.mailet.Matcher; +import org.apache.mailet.base.test.FakeMail; +import org.apache.mailet.base.test.FakeMatcherConfig; +import org.junit.Before; +import org.junit.Test; + +public class SenderHostIsLocalTest { + + private Matcher matcher; + + @Before + public void setUp() throws MessagingException { + MailetContext mailContext = mock(MailetContext.class); + when(mailContext.isLocalServer(JAMES_APACHE_ORG)).thenReturn(true); + when(mailContext.isLocalServer(JAMES2_APACHE_ORG)).thenReturn(false); + + matcher = new SenderHostIsLocal(); + FakeMatcherConfig mci= new FakeMatcherConfig("SenderHostIsLocal", mailContext); + matcher.init(mci); + } + + @Test + public void shouldMatchWhenSenderHostIsLocal() throws MessagingException { + //Given + Mail mail = FakeMail.builder() + .sender(ANY_AT_JAMES) + .recipient(ANY_AT_JAMES2) + .build(); + //When + Collection<MailAddress> actual = matcher.match(mail); + //Then + assertThat(actual).containsExactly(ANY_AT_JAMES2); + } + + @Test + public void shouldNotMatchWhenSenderHostIsNotLocal() throws MessagingException { + //Given + Mail mail = FakeMail.builder() + .sender(ANY_AT_JAMES2) + .recipient(ANY_AT_JAMES) + .build(); + //When + Collection<MailAddress> actual = matcher.match(mail); + //Then + assertThat(actual).isNull(); + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org