MAILET-122 Improve RecipientIs code quality and add missing 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/cb0650f5
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/cb0650f5
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/cb0650f5

Branch: refs/heads/master
Commit: cb0650f565bdc7a631b62dcb9d333637dc3abaec
Parents: 9c9de6d
Author: Benoit Tellier <[email protected]>
Authored: Wed Aug 31 14:08:12 2016 +0700
Committer: Benoit Tellier <[email protected]>
Committed: Mon Oct 10 11:35:56 2016 +0200

----------------------------------------------------------------------
 .../james/transport/matchers/RecipientIs.java   | 24 ++++++-------
 .../transport/matchers/RecipientIsTest.java     | 37 +++++++++++++++++---
 2 files changed, 45 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/cb0650f5/mailet/standard/src/main/java/org/apache/james/transport/matchers/RecipientIs.java
----------------------------------------------------------------------
diff --git 
a/mailet/standard/src/main/java/org/apache/james/transport/matchers/RecipientIs.java
 
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/RecipientIs.java
index 9fdc591..3dce311 100644
--- 
a/mailet/standard/src/main/java/org/apache/james/transport/matchers/RecipientIs.java
+++ 
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/RecipientIs.java
@@ -17,29 +17,29 @@
  * under the License.                                           *
  ****************************************************************/
 
+package org.apache.james.transport.matchers;
 
+import java.util.Collection;
 
-package org.apache.james.transport.matchers;
+import javax.mail.MessagingException;
 
-import org.apache.mailet.base.GenericRecipientMatcher;
+import org.apache.james.transport.matchers.utils.MailAddressCollectionReader;
 import org.apache.mailet.MailAddress;
+import org.apache.mailet.base.GenericRecipientMatcher;
 
-import java.util.Collection;
-import java.util.StringTokenizer;
+import com.google.common.base.Strings;
 
-/**
- * Matches mail where the recipent is one of a configurable list.
- * @version 1.0.0, 24/04/1999
- */
 public class RecipientIs extends GenericRecipientMatcher {
 
     private Collection<MailAddress> recipients;
 
     public void init() throws javax.mail.MessagingException {
-        StringTokenizer st = new StringTokenizer(getCondition(), ", \t", 
false);
-        recipients = new java.util.HashSet<MailAddress>();
-        while (st.hasMoreTokens()) {
-            recipients.add(new MailAddress(st.nextToken()));
+        if (Strings.isNullOrEmpty(getCondition())) {
+            throw new MessagingException("RecipientIs should have a condition  
composed of a list of mail addresses");
+        }
+        recipients = MailAddressCollectionReader.read(getCondition());
+        if (recipients.isEmpty()) {
+            throw new MessagingException("RecipientIs should have at least one 
address passed as a condition");
         }
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/cb0650f5/mailet/standard/src/test/java/org/apache/james/transport/matchers/RecipientIsTest.java
----------------------------------------------------------------------
diff --git 
a/mailet/standard/src/test/java/org/apache/james/transport/matchers/RecipientIsTest.java
 
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/RecipientIsTest.java
index c8bfa68..774f455 100644
--- 
a/mailet/standard/src/test/java/org/apache/james/transport/matchers/RecipientIsTest.java
+++ 
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/RecipientIsTest.java
@@ -20,19 +20,25 @@
 
 package org.apache.james.transport.matchers;
 
-import static org.assertj.core.api.Assertions.assertThat;
 import static org.apache.mailet.base.MailAddressFixture.ANY_AT_JAMES;
-import static org.apache.mailet.base.MailAddressFixture.OTHER_AT_JAMES;
 import static org.apache.mailet.base.MailAddressFixture.ANY_AT_JAMES2;
+import static org.apache.mailet.base.MailAddressFixture.OTHER_AT_JAMES;
+import static org.assertj.core.api.Assertions.assertThat;
 
+import javax.mail.MessagingException;
 import org.apache.mailet.base.test.FakeMail;
 import org.apache.mailet.base.test.FakeMailContext;
 import org.apache.mailet.base.test.FakeMatcherConfig;
 import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 
 public class RecipientIsTest {
 
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
+
     private RecipientIs matcher;
 
     @Before
@@ -48,7 +54,7 @@ public class RecipientIsTest {
             .recipient(ANY_AT_JAMES)
             .build();
 
-       assertThat(matcher.match(fakeMail)).containsExactly(ANY_AT_JAMES);
+       assertThat(matcher.match(fakeMail)).containsOnly(ANY_AT_JAMES);
     }
 
     @Test
@@ -59,7 +65,7 @@ public class RecipientIsTest {
             .recipients(ANY_AT_JAMES, OTHER_AT_JAMES)
             .build();
 
-        assertThat(matcher.match(fakeMail)).containsExactly(ANY_AT_JAMES);
+        assertThat(matcher.match(fakeMail)).containsOnly(ANY_AT_JAMES);
     }
 
     @Test
@@ -72,4 +78,27 @@ public class RecipientIsTest {
 
         assertThat(matcher.match(fakeMail)).isEmpty();
     }
+
+    @Test
+    public void initShouldThrowOnMissingCondition() throws Exception {
+        expectedException.expect(MessagingException.class);
+        matcher.init(new FakeMatcherConfig("RecipientIs", 
FakeMailContext.defaultContext()));
+    }
+
+    @Test
+    public void initShouldThrowOnEmptyCondition() throws Exception {
+        expectedException.expect(MessagingException.class);
+        matcher.init(new FakeMatcherConfig("RecipientIs=", 
FakeMailContext.defaultContext()));
+    }
+
+    @Test
+    public void shouldBeAbleToMatchSeveralAddresses() throws Exception {
+        matcher.init(new FakeMatcherConfig("RecipientIs=" + 
ANY_AT_JAMES.toString() + ", " + ANY_AT_JAMES2.toString(), 
FakeMailContext.defaultContext()));
+
+        FakeMail fakeMail = FakeMail.builder()
+            .recipients(ANY_AT_JAMES, OTHER_AT_JAMES, ANY_AT_JAMES2)
+            .build();
+
+        assertThat(matcher.match(fakeMail)).containsOnly(ANY_AT_JAMES, 
ANY_AT_JAMES2);
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to