james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [1/2] james-project git commit: MAILET-140-SenderHostIs should be covered with tests
Date Fri, 25 Nov 2016 14:24:20 GMT
Repository: james-project
Updated Branches:
  refs/heads/master def450ba2 -> e222e1c58


MAILET-140-SenderHostIs 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/e222e1c5
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/e222e1c5
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/e222e1c5

Branch: refs/heads/master
Commit: e222e1c586e18e92f431b14c0e999bd43080f430
Parents: c6855a2
Author: Laura Royet <lroyet@linagora.com>
Authored: Thu Nov 10 14:21:57 2016 +0100
Committer: Laura Royet <lroyet@linagora.com>
Committed: Fri Nov 25 10:55:06 2016 +0100

----------------------------------------------------------------------
 .../james/transport/matchers/SenderHostIs.java  |  40 ++--
 .../transport/matchers/SenderHostIsTest.java    | 186 +++++++++++++++++++
 2 files changed, 206 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/e222e1c5/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderHostIs.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderHostIs.java
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderHostIs.java
index 729a721..98fbf39 100755
--- a/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderHostIs.java
+++ b/mailet/standard/src/main/java/org/apache/james/transport/matchers/SenderHostIs.java
@@ -21,14 +21,17 @@
 
 package org.apache.james.transport.matchers;
 
-import org.apache.mailet.base.GenericMatcher;
+import java.util.Collection;
+import java.util.Locale;
+
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
+import org.apache.mailet.base.GenericMatcher;
 
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Locale;
-import java.util.StringTokenizer;
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Splitter;
+import com.google.common.collect.ImmutableSet;
 
 /**
  * <p>Checkes the sender's displayed domain name against a supplied list.</p>
@@ -43,24 +46,21 @@ import java.util.StringTokenizer;
  * @version 1.0.0, 2002-09-10
  */
 public class SenderHostIs extends GenericMatcher {
-    /**
-     * The collection of host names to match against.
-     */
-    private Collection<String> senderHosts;
 
-    /**
-     * Initialize the mailet.
-     */
+    private Collection<String> senderHosts;
+    
     public void init()  {
-        //Parse the condition...
-        StringTokenizer st = new StringTokenizer(getCondition(), ", ", false);
+        String condition = getCondition();
+        Preconditions.checkNotNull(condition, "'condition' should not be null");
 
-        //..into a vector of domain names.
-        senderHosts = new java.util.HashSet<String>();
-        while (st.hasMoreTokens()) {
-            senderHosts.add(st.nextToken().toLowerCase(Locale.US));
-        }
-        senderHosts = Collections.unmodifiableCollection(senderHosts);
+        senderHosts = parseDomainsList(condition);
+    }
+
+    @VisibleForTesting Collection<String> parseDomainsList(String condition) {
+        return ImmutableSet
+                .copyOf(Splitter.onPattern("(, |,| )")
+                        .omitEmptyStrings()
+                        .split(condition));
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/james-project/blob/e222e1c5/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderHostIsTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderHostIsTest.java
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderHostIsTest.java
new file mode 100644
index 0000000..9546906
--- /dev/null
+++ b/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderHostIsTest.java
@@ -0,0 +1,186 @@
+/****************************************************************
+ * 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_JAMES2;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+
+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.base.test.FakeMail;
+import org.apache.mailet.base.test.FakeMatcherConfig;
+import org.junit.Before;
+import org.junit.Test;
+
+public class SenderHostIsTest {
+
+    private SenderHostIs matcher;
+    private MailetContext mailContext;
+
+    @Before
+    public void setUp() throws MessagingException {
+        mailContext = mock(MailetContext.class);
+        matcher = new SenderHostIs();
+
+    }
+
+    @Test
+    public void shouldMatchWhenSenderHostIsKnown() throws MessagingException {
+        //Given
+        FakeMatcherConfig mci = FakeMatcherConfig.builder()
+                .matcherName("SenderHostIs")
+                .mailetContext(mailContext)
+                .condition("james.apache.org, james3.apache.org, james2.apache.org, james4.apache.org,
james5.apache.org")
+                .build();
+
+        matcher.init(mci);
+
+        Mail mail = FakeMail.builder()
+                .sender(ANY_AT_JAMES2)
+                .recipient(ANY_AT_JAMES2)
+                .build();
+        //When
+        Collection<MailAddress> actual = matcher.match(mail);
+        //Then
+        assertThat(actual).containsExactly(ANY_AT_JAMES2);
+    }
+
+    @Test
+    public void shouldNotMatchWhenSenderHostIsUnknown() throws MessagingException {
+        //Given
+        matcher.init(FakeMatcherConfig.builder()
+                .matcherName("SenderHostIs")
+                .mailetContext(mailContext)
+                .condition("james.apache.org, james3.apache.org, james4.apache.org")
+                .build());
+
+        Mail mail = FakeMail.builder()
+                .sender(ANY_AT_JAMES2)
+                .recipient(ANY_AT_JAMES2)
+                .build();
+        //When
+        Collection<MailAddress> actual = matcher.match(mail);
+        //Then
+        assertThat(actual).isNull();
+
+    }
+
+    @Test
+    public void shouldNotMatchWhenEmptyList() throws MessagingException {
+        //Given
+        matcher.init(FakeMatcherConfig.builder()
+                .matcherName("SenderHostIs")
+                .mailetContext(mailContext)
+                .condition("")
+                .build());
+
+        Mail mail = FakeMail.builder()
+                .sender(ANY_AT_JAMES2)
+                .recipient(ANY_AT_JAMES2)
+                .build();
+        //When
+        Collection<MailAddress> actual = matcher.match(mail);
+        //Then
+        assertThat(actual).isNull();
+    }
+
+    @Test
+    public void shouldNotMatchWhenNullSender() throws MessagingException {
+        //Given
+        matcher.init(FakeMatcherConfig.builder()
+                .matcherName("SenderHostIs")
+                .mailetContext(mailContext)
+                .condition("")
+                .build());
+
+        Mail mail = FakeMail.builder()
+                .recipient(ANY_AT_JAMES2)
+                .build();
+        //When
+        Collection<MailAddress> actual = matcher.match(mail);
+        //Then
+        assertThat(actual).isNull();
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void shouldThrowWhenNullCondition() throws Exception {
+        //When
+        matcher.init(FakeMatcherConfig.builder()
+                .matcherName("SenderHostIs")
+                .mailetContext(mailContext)
+                .condition(null)
+                .build());
+    }
+
+    @Test
+    public void parseDomainsListShouldParseWhenOnlyOneDomain() {
+        //When
+        Collection<String> senderHosts = matcher.parseDomainsList("james.apache.org");
+        //Then
+        assertThat(senderHosts).containsOnly("james.apache.org");
+    }
+
+    @Test
+    public void parseDomainsListShouldParseWhenCommaSpacePattern() {
+        //When
+        Collection<String> senderHosts = matcher.parseDomainsList("james.apache.org,
james2.apache.org, james3.apache.org, james4.apache.org, james5.apache.org");
+        //Then
+        assertThat(senderHosts).containsOnly("james.apache.org", "james2.apache.org", "james3.apache.org",
"james4.apache.org", "james5.apache.org");
+    }
+
+    @Test
+    public void parseDomainsListShouldParseWhenCommaPattern() {
+        //When
+        Collection<String> senderHosts = matcher.parseDomainsList("james.apache.org,james2.apache.org,james3.apache.org,james4.apache.org,james5.apache.org");
+        //Then
+        assertThat(senderHosts).containsOnly("james.apache.org", "james2.apache.org", "james3.apache.org",
"james4.apache.org", "james5.apache.org");
+    }
+
+    @Test
+    public void parseDomainsListShouldParseWhenSpacePattern() {
+        //When
+        Collection<String> senderHosts = matcher.parseDomainsList("james.apache.org
james2.apache.org james3.apache.org james4.apache.org james5.apache.org");
+        //Then
+        assertThat(senderHosts).containsOnly("james.apache.org", "james2.apache.org", "james3.apache.org",
"james4.apache.org", "james5.apache.org");
+    }
+
+    @Test
+    public void parseDomainsListShouldParseWhenMixedPatterns() {
+        //When
+        Collection<String> senderHosts = matcher.parseDomainsList("james.apache.org
james2.apache.org,james3.apache.org, james4.apache.org james5.apache.org");
+        //Then
+        assertThat(senderHosts).containsOnly("james.apache.org", "james2.apache.org", "james3.apache.org",
"james4.apache.org", "james5.apache.org");
+    }
+
+    @Test
+    public void parseDomainsListShouldIgnoreEmptyDomains() {
+        //When
+        Collection<String> senderHosts = matcher.parseDomainsList("james.apache.org
  james2.apache.org james3.apache.org , james4.apache.org,,,james5.apache.org");
+        //Then
+        assertThat(senderHosts).containsOnly("james.apache.org", "james2.apache.org", "james3.apache.org",
"james4.apache.org", "james5.apache.org");
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message