This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git


The following commit(s) were added to refs/heads/master by this push:
     new 6a738002df JAMES-2434 IsFromMailingList matcher (#1606)
6a738002df is described below

commit 6a738002df687e21733ab05a3c28638e50b1abfe
Author: Maksim <[email protected]>
AuthorDate: Mon Jun 26 12:26:15 2023 +0300

    JAMES-2434 IsFromMailingList matcher (#1606)
---
 ...SingleRecipient.java => IsFromMailingList.java} | 42 +++++++++++++++++-----
 .../transport/matchers/IsSingleRecipient.java      |  5 +--
 ...cipientTest.java => IsFromMailingListTest.java} | 42 +++++++++++-----------
 .../transport/matchers/IsSingleRecipientTest.java  | 12 +++----
 4 files changed, 63 insertions(+), 38 deletions(-)

diff --git 
a/mailet/standard/src/main/java/org/apache/james/transport/matchers/IsSingleRecipient.java
 
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/IsFromMailingList.java
similarity index 51%
copy from 
mailet/standard/src/main/java/org/apache/james/transport/matchers/IsSingleRecipient.java
copy to 
mailet/standard/src/main/java/org/apache/james/transport/matchers/IsFromMailingList.java
index 56cdd0c896..581e4e3a59 100644
--- 
a/mailet/standard/src/main/java/org/apache/james/transport/matchers/IsSingleRecipient.java
+++ 
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/IsFromMailingList.java
@@ -17,28 +17,52 @@
  * under the License.                                           *
  ****************************************************************/
 
-
-
 package org.apache.james.transport.matchers;
 
 import java.util.Collection;
+import java.util.Collections;
+
+import javax.inject.Inject;
+import javax.mail.MessagingException;
 
 import org.apache.james.core.MailAddress;
 import org.apache.mailet.Mail;
+import org.apache.mailet.base.AutomaticallySentMailDetector;
 import org.apache.mailet.base.GenericMatcher;
 
 /**
- * Matches mail where the number of recipiants is exactly one.
- * @version 1.0.0, 04/12/2000
+ * <p>Matches if mail is from a mailing list.</p>
+ * <p>Implements the match method to check if the incoming mail is from a 
mailing list.
+ * If the mail is from a mailing list, then returns all the recipients of the 
mail.</p>
  */
-public class IsSingleRecipient extends GenericMatcher {
 
+public class IsFromMailingList extends GenericMatcher {
+
+    /**
+     * Used to detect automatically sent mails.
+     */
+    private final AutomaticallySentMailDetector automaticallySentMailDetector;
+
+    /**
+     * Constructor for IsFromMailingList.
+     * @param automaticallySentMailDetector Mail detector.
+     */
+    @Inject
+    public IsFromMailingList(AutomaticallySentMailDetector 
automaticallySentMailDetector) {
+        this.automaticallySentMailDetector = automaticallySentMailDetector;
+    }
+
+    /**
+     * Checks if the incoming mail is from a mailing list and returns all the 
recipients of the mail.
+     * @param mail Mail to be matched.
+     * @throws MessagingException if there is a problem while matching the 
mail.
+     * @return Collection of MailAddress if matches, else an empty Collection.
+     */
     @Override
-    public Collection<MailAddress> match(Mail mail) {
-        if (mail.getRecipients().size() == 1) {
+    public Collection<MailAddress> match(Mail mail) throws MessagingException {
+        if (automaticallySentMailDetector.isMailingList(mail)) {
             return mail.getRecipients();
-        } else {
-            return null;
         }
+        return Collections.emptyList();
     }
 }
diff --git 
a/mailet/standard/src/main/java/org/apache/james/transport/matchers/IsSingleRecipient.java
 
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/IsSingleRecipient.java
index 56cdd0c896..16f6e2993b 100644
--- 
a/mailet/standard/src/main/java/org/apache/james/transport/matchers/IsSingleRecipient.java
+++ 
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/IsSingleRecipient.java
@@ -22,13 +22,14 @@
 package org.apache.james.transport.matchers;
 
 import java.util.Collection;
+import java.util.Collections;
 
 import org.apache.james.core.MailAddress;
 import org.apache.mailet.Mail;
 import org.apache.mailet.base.GenericMatcher;
 
 /**
- * Matches mail where the number of recipiants is exactly one.
+ * Matches mail where the number of recipients is exactly one.
  * @version 1.0.0, 04/12/2000
  */
 public class IsSingleRecipient extends GenericMatcher {
@@ -38,7 +39,7 @@ public class IsSingleRecipient extends GenericMatcher {
         if (mail.getRecipients().size() == 1) {
             return mail.getRecipients();
         } else {
-            return null;
+            return Collections.emptyList();
         }
     }
 }
diff --git 
a/mailet/standard/src/test/java/org/apache/james/transport/matchers/IsSingleRecipientTest.java
 
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/IsFromMailingListTest.java
similarity index 70%
copy from 
mailet/standard/src/test/java/org/apache/james/transport/matchers/IsSingleRecipientTest.java
copy to 
mailet/standard/src/test/java/org/apache/james/transport/matchers/IsFromMailingListTest.java
index ffcaf44692..96c63c1871 100644
--- 
a/mailet/standard/src/test/java/org/apache/james/transport/matchers/IsSingleRecipientTest.java
+++ 
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/IsFromMailingListTest.java
@@ -17,53 +17,53 @@
  * 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.OTHER_AT_JAMES;
-import static org.assertj.core.api.Assertions.assertThat;
-
-import javax.mail.MessagingException;
-
 import org.apache.mailet.Matcher;
+import org.apache.mailet.base.AutomaticallySentMailDetector;
 import org.apache.mailet.base.test.FakeMail;
 import org.apache.mailet.base.test.FakeMatcherConfig;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
-public class IsSingleRecipientTest {
+import javax.mail.MessagingException;
+
+import static org.apache.mailet.base.MailAddressFixture.ANY_AT_JAMES;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class IsFromMailingListTest {
 
     private Matcher matcher;
+    private AutomaticallySentMailDetector automaticallySentMailDetector;
 
     @BeforeEach
     public void setUp() throws MessagingException {
-        matcher = new IsSingleRecipient();
+        automaticallySentMailDetector = 
mock(AutomaticallySentMailDetector.class);
+        matcher = new IsFromMailingList(automaticallySentMailDetector);
         FakeMatcherConfig matcherConfig = FakeMatcherConfig.builder()
-                .matcherName("IsSingleRecipient")
+                .matcherName("IsFromMailingList")
                 .build();
         matcher.init(matcherConfig);
     }
 
     @Test
-    public void matchShouldMatchOneRecipientsEmails() throws 
MessagingException {
+    void matchShouldMatchFromMailingListEmails() throws MessagingException {
         FakeMail fakeMail = 
FakeMail.builder().name("mail").recipient(ANY_AT_JAMES).build();
 
+        
when(automaticallySentMailDetector.isMailingList(fakeMail)).thenReturn(true);
+
         assertThat(matcher.match(fakeMail)).containsExactly(ANY_AT_JAMES);
     }
 
     @Test
-    public void matchShouldNotMatchMultiRecipientsEMail() throws 
MessagingException {
-        FakeMail fakeMail = 
FakeMail.builder().name("mail").recipients(ANY_AT_JAMES, 
OTHER_AT_JAMES).build();
-
-        assertThat(matcher.match(fakeMail)).isNull();
-    }
+    void matchShouldNotMatchIfNotFromMailingListEmails() throws 
MessagingException {
+        FakeMail fakeMail = 
FakeMail.builder().name("mail").recipient(ANY_AT_JAMES).build();
 
-    @Test
-    public void matchShouldNotMatchMailWithNotRecipients() throws 
MessagingException {
-        FakeMail fakeMail = FakeMail.defaultFakeMail();
+        
when(automaticallySentMailDetector.isMailingList(fakeMail)).thenReturn(false);
 
-        assertThat(matcher.match(fakeMail)).isNull();
+        assertThat(matcher.match(fakeMail)).isEmpty();
     }
 
-}
+}
\ No newline at end of file
diff --git 
a/mailet/standard/src/test/java/org/apache/james/transport/matchers/IsSingleRecipientTest.java
 
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/IsSingleRecipientTest.java
index ffcaf44692..b9f453310b 100644
--- 
a/mailet/standard/src/test/java/org/apache/james/transport/matchers/IsSingleRecipientTest.java
+++ 
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/IsSingleRecipientTest.java
@@ -32,7 +32,7 @@ import org.apache.mailet.base.test.FakeMatcherConfig;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
-public class IsSingleRecipientTest {
+class IsSingleRecipientTest {
 
     private Matcher matcher;
 
@@ -46,24 +46,24 @@ public class IsSingleRecipientTest {
     }
 
     @Test
-    public void matchShouldMatchOneRecipientsEmails() throws 
MessagingException {
+    void matchShouldMatchOneRecipientsEmail() throws MessagingException {
         FakeMail fakeMail = 
FakeMail.builder().name("mail").recipient(ANY_AT_JAMES).build();
 
         assertThat(matcher.match(fakeMail)).containsExactly(ANY_AT_JAMES);
     }
 
     @Test
-    public void matchShouldNotMatchMultiRecipientsEMail() throws 
MessagingException {
+    void matchShouldNotMatchMultiRecipientsEmail() throws MessagingException {
         FakeMail fakeMail = 
FakeMail.builder().name("mail").recipients(ANY_AT_JAMES, 
OTHER_AT_JAMES).build();
 
-        assertThat(matcher.match(fakeMail)).isNull();
+        assertThat(matcher.match(fakeMail)).isEmpty();
     }
 
     @Test
-    public void matchShouldNotMatchMailWithNotRecipients() throws 
MessagingException {
+    void matchShouldNotMatchMailWithNotRecipients() throws MessagingException {
         FakeMail fakeMail = FakeMail.defaultFakeMail();
 
-        assertThat(matcher.match(fakeMail)).isNull();
+        assertThat(matcher.match(fakeMail)).isEmpty();
     }
 
 }


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

Reply via email to