Repository: james-project
Updated Branches:
  refs/heads/master 5d3bedee1 -> a281f59fd


MAILET-152 Add instructions for implementing TooMuchRecipients


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/398663e7
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/398663e7
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/398663e7

Branch: refs/heads/master
Commit: 398663e7bc575c5e95fb6f470a81b49a27394717
Parents: 5d3bede
Author: benwa <btell...@linagora.com>
Authored: Tue Mar 21 09:16:03 2017 +0700
Committer: benwa <btell...@linagora.com>
Committed: Mon Apr 3 18:09:24 2017 +0700

----------------------------------------------------------------------
 .../transport/matchers/TooManyRecipients.java   |  62 +++++++
 .../matchers/TooManyRecipientsTest.java         | 172 +++++++++++++++++++
 2 files changed, 234 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/398663e7/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyRecipients.java
----------------------------------------------------------------------
diff --git 
a/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyRecipients.java
 
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyRecipients.java
new file mode 100644
index 0000000..51895a3
--- /dev/null
+++ 
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyRecipients.java
@@ -0,0 +1,62 @@
+/****************************************************************
+ * 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 java.util.Collection;
+
+import javax.mail.MessagingException;
+
+import org.apache.mailet.Mail;
+import org.apache.mailet.MailAddress;
+import org.apache.mailet.base.GenericMatcher;
+
+import com.google.common.collect.ImmutableList;
+
+public class TooManyRecipients extends GenericMatcher {
+
+    private int maximumRecipientCount;
+
+    @Override
+    public void init() throws MessagingException {
+        String condition = getCondition();
+
+        if (condition == null) {
+            throw new MessagingException("it should have a condition");
+        }
+
+        try {
+            maximumRecipientCount = Integer.parseInt(condition);
+        } catch (Exception e) {
+            throw new MessagingException("Condition should be a number");
+        }
+
+        if (maximumRecipientCount < 1) {
+            throw new MessagingException("it should be positive condition");
+        }
+    }
+
+    @Override
+    public Collection<MailAddress> match(Mail mail) throws MessagingException {
+        if (mail.getRecipients().size() > maximumRecipientCount) {
+            return mail.getRecipients();
+        }
+        return ImmutableList.of();
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/398663e7/mailet/standard/src/test/java/org/apache/james/transport/matchers/TooManyRecipientsTest.java
----------------------------------------------------------------------
diff --git 
a/mailet/standard/src/test/java/org/apache/james/transport/matchers/TooManyRecipientsTest.java
 
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/TooManyRecipientsTest.java
new file mode 100644
index 0000000..d399d11
--- /dev/null
+++ 
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/TooManyRecipientsTest.java
@@ -0,0 +1,172 @@
+/****************************************************************
+ * 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.assertj.core.api.Assertions.assertThat;
+
+import java.util.Collection;
+
+import javax.mail.MessagingException;
+
+import org.apache.mailet.MailAddress;
+import org.apache.mailet.base.test.FakeMail;
+import org.apache.mailet.base.test.FakeMatcherConfig;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import com.google.common.collect.ImmutableList;
+
+public class TooManyRecipientsTest {
+
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
+
+    private TooManyRecipients testee;
+
+    @Before
+    public void setUp() {
+        testee = new TooManyRecipients();
+    }
+
+    @Test
+    public void initShouldThrowOnAbsentCondition() throws Exception {
+        expectedException.expect(MessagingException.class);
+
+        testee.init(FakeMatcherConfig.builder()
+            .matcherName("matcherName")
+            .build());
+    }
+
+    @Test
+    public void initShouldThrowOnInvalidCondition() throws Exception{
+        expectedException.expect(MessagingException.class);
+
+        testee.init(FakeMatcherConfig.builder()
+            .condition("a")
+            .matcherName("matcherName")
+            .build());
+    }
+
+    @Test
+    public void initShouldThrowOnEmptyCondition() throws Exception {
+        expectedException.expect(MessagingException.class);
+
+        testee.init(FakeMatcherConfig.builder()
+            .condition("")
+            .matcherName("matcherName")
+            .build());
+    }
+
+    @Test
+    public void initShouldThrowOnZeroCondition() throws Exception {
+        expectedException.expect(MessagingException.class);
+
+        testee.init(FakeMatcherConfig.builder()
+            .condition("0")
+            .matcherName("matcherName")
+            .build());
+    }
+
+    @Test
+    public void initShouldThrowOnNegativeCondition() throws Exception {
+        expectedException.expect(MessagingException.class);
+
+        testee.init(FakeMatcherConfig.builder()
+            .condition("-10")
+            .matcherName("matcherName")
+            .build());
+    }
+
+    @Test
+    public void matchShouldReturnNoRecipientWhenMailHaveNoRecipient() throws 
Exception {
+        testee.init(FakeMatcherConfig.builder()
+            .condition("3")
+            .matcherName("matcherName")
+            .build());
+
+        Collection<MailAddress> result = 
testee.match(FakeMail.builder().build());
+
+        assertThat(result).isEmpty();
+    }
+
+    @Test
+    public void matchShouldAcceptMailsUnderLimit() throws Exception {
+        testee.init(FakeMatcherConfig.builder()
+            .condition("3")
+            .matcherName("matcherName")
+            .build());
+
+        FakeMail fakeMail = FakeMail.builder()
+            .recipient(new MailAddress("cuong.t...@gmail.com"))
+            .build();
+
+        Collection<MailAddress> result = testee.match(fakeMail);
+
+        assertThat(result).isEmpty();
+    }
+
+
+    @Test
+    public void matchShouldAcceptMailsAtLimit() throws Exception {
+        testee.init(FakeMatcherConfig.builder()
+            .condition("3")
+            .matcherName("matcherName")
+            .build());
+
+        ImmutableList<MailAddress> mailAddresses = ImmutableList.of(
+            new MailAddress("cuong.t...@gmail.com"),
+            new MailAddress("suu.t...@gmail.com"),
+            new MailAddress("tuan.t...@gmail.com"));
+
+        FakeMail fakeMail = FakeMail.builder()
+            .recipients(mailAddresses)
+            .build();
+
+        Collection<MailAddress> result = testee.match(fakeMail);
+
+        assertThat(result).isEmpty();
+    }
+
+    @Test
+    public void matchShouldRejectMailsOverLimit() throws Exception {
+        testee.init(FakeMatcherConfig.builder()
+            .condition("3")
+            .matcherName("matcherName")
+            .build());
+
+        ImmutableList<MailAddress> mailAddresses = ImmutableList.of(
+            new MailAddress("cuong.t...@gmail.com"),
+            new MailAddress("suu.t...@gmail.com"),
+            new MailAddress("tuan.t...@gmail.com"),
+            new MailAddress("sang.t...@gmail.com"));
+
+
+        FakeMail fakeMail = FakeMail.builder()
+            .recipients(mailAddresses)
+            .build();
+
+        Collection<MailAddress> result = testee.match(fakeMail);
+
+        assertThat(result).isEqualTo(mailAddresses);
+    }
+
+}


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

Reply via email to