You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2018/01/03 03:20:37 UTC

[3/9] james-project git commit: JAMES-2263 Add integration test for matcher error handling

JAMES-2263 Add integration test for matcher error handling


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

Branch: refs/heads/master
Commit: 410d92e1c45ae53d86a90e25c1bf24b54521b45f
Parents: 23353ae
Author: benwa <bt...@linagora.com>
Authored: Fri Dec 29 09:56:33 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Wed Jan 3 10:16:14 2018 +0700

----------------------------------------------------------------------
 .../apache/james/mailets/MailetErrorsTest.java  | 213 +++++++++++++++++++
 .../james/transport/mailets/ErrorMatcher.java   |  35 +++
 .../james/transport/mailets/NoopMailet.java     |  32 +++
 .../transport/mailets/RuntimeErrorMatcher.java  |  35 +++
 4 files changed, 315 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/410d92e1/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/MailetErrorsTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/MailetErrorsTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/MailetErrorsTest.java
index 40a37b9..127a2ff 100644
--- a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/MailetErrorsTest.java
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/MailetErrorsTest.java
@@ -27,8 +27,11 @@ import org.apache.james.mailets.configuration.MailetContainer;
 import org.apache.james.mailets.configuration.ProcessorConfiguration;
 import org.apache.james.probe.DataProbe;
 import org.apache.james.transport.mailets.ErrorMailet;
+import org.apache.james.transport.mailets.ErrorMatcher;
+import org.apache.james.transport.mailets.NoopMailet;
 import org.apache.james.transport.mailets.RemoveMimeHeader;
 import org.apache.james.transport.mailets.RuntimeErrorMailet;
+import org.apache.james.transport.mailets.RuntimeErrorMatcher;
 import org.apache.james.transport.mailets.ToRepository;
 import org.apache.james.transport.matchers.All;
 import org.apache.james.utils.DataProbeImpl;
@@ -293,6 +296,216 @@ public class MailetErrorsTest {
         }
     }
 
+    @Test
+    public void matcherProcessorsShouldHandleMessagingException() throws Exception {
+        jamesServer = TemporaryJamesServer.builder()
+            .withBase(SMTP_ONLY_MODULE)
+            .build(temporaryFolder,
+                MailetContainer.builder()
+                    .threads(2)
+                    .postmaster("postmaster@localhost")
+                    .addProcessor(emptyTransport())
+                    .addProcessor(errorProcessor())
+                    .addProcessor(ProcessorConfiguration.builder()
+                        .state(Mail.DEFAULT)
+                        .addMailet(MailetConfiguration.builder()
+                            .matcher(ErrorMatcher.class)
+                            .mailet(NoopMailet.class)
+                            .build())
+                        .build())
+                    .build());
+        dataProbe = jamesServer.getProbe(DataProbeImpl.class);
+        MailRepositoryProbeImpl probe = jamesServer.getProbe(MailRepositoryProbeImpl.class);
+
+        dataProbe.addDomain(JAMES_APACHE_ORG);
+        dataProbe.addUser(FROM, PASSWORD);
+
+        try (SMTPMessageSender messageSender = SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG)) {
+            messageSender.sendMessage(FROM, RECIPIENT);
+
+            calmlyAwait.atMost(Duration.TEN_SECONDS)
+                .until(() -> probe.getRepositoryMailCount(ERROR_REPOSITORY) == 1);
+        }
+    }
+
+    @Test
+    public void matcherProcessorsShouldHandleRuntimeException() throws Exception {
+        jamesServer = TemporaryJamesServer.builder()
+            .withBase(SMTP_ONLY_MODULE)
+            .build(temporaryFolder,
+                MailetContainer.builder()
+                    .threads(2)
+                    .postmaster("postmaster@localhost")
+                    .addProcessor(emptyTransport())
+                    .addProcessor(errorProcessor())
+                    .addProcessor(ProcessorConfiguration.builder()
+                        .state(Mail.DEFAULT)
+                        .addMailet(MailetConfiguration.builder()
+                            .matcher(RuntimeErrorMatcher.class)
+                            .mailet(NoopMailet.class)
+                            .build())
+                        .build())
+                    .build());
+        dataProbe = jamesServer.getProbe(DataProbeImpl.class);
+        MailRepositoryProbeImpl probe = jamesServer.getProbe(MailRepositoryProbeImpl.class);
+
+        dataProbe.addDomain(JAMES_APACHE_ORG);
+        dataProbe.addUser(FROM, PASSWORD);
+
+        try (SMTPMessageSender messageSender = SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG)) {
+            messageSender.sendMessage(FROM, RECIPIENT);
+
+            calmlyAwait.atMost(Duration.TEN_SECONDS)
+                .until(() -> probe.getRepositoryMailCount(ERROR_REPOSITORY) == 1);
+        }
+    }
+
+    @Test
+    public void matcherProcessorsShouldHandleMessagingExceptionWhenSpecificErrorHandlingSpecified() throws Exception {
+        jamesServer = TemporaryJamesServer.builder()
+            .withBase(SMTP_ONLY_MODULE)
+            .build(temporaryFolder,
+                MailetContainer.builder()
+                    .threads(2)
+                    .postmaster("postmaster@localhost")
+                    .addProcessor(emptyTransport())
+                    .addProcessor(errorProcessor())
+                    .addProcessor(customProcessor())
+                    .addProcessor(ProcessorConfiguration.builder()
+                        .state(Mail.DEFAULT)
+                        .addMailet(MailetConfiguration.builder()
+                            .matcher(ErrorMatcher.class)
+                            .mailet(NoopMailet.class)
+                            .addProperty("onMailetException", CUSTOM_PROCESSOR)
+                            .build())
+                        .build())
+                    .build());
+        dataProbe = jamesServer.getProbe(DataProbeImpl.class);
+        MailRepositoryProbeImpl probe = jamesServer.getProbe(MailRepositoryProbeImpl.class);
+
+        dataProbe.addDomain(JAMES_APACHE_ORG);
+        dataProbe.addUser(FROM, PASSWORD);
+
+        try (SMTPMessageSender messageSender = SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG)) {
+            messageSender.sendMessage(FROM, RECIPIENT);
+
+            calmlyAwait.atMost(Duration.TEN_SECONDS)
+                .until(() -> probe.getRepositoryMailCount(CUSTOM_REPOSITORY) == 1);
+        }
+    }
+
+    @Test
+    public void matcherProcessorsShouldHandleRuntimeExceptionWhenSpecificErrorHandlingSpecified() throws Exception {
+        jamesServer = TemporaryJamesServer.builder()
+            .withBase(SMTP_ONLY_MODULE)
+            .build(temporaryFolder,
+                MailetContainer.builder()
+                    .threads(2)
+                    .postmaster("postmaster@localhost")
+                    .addProcessor(emptyTransport())
+                    .addProcessor(errorProcessor())
+                    .addProcessor(customProcessor())
+                    .addProcessor(ProcessorConfiguration.builder()
+                        .state(Mail.DEFAULT)
+                        .addMailet(MailetConfiguration.builder()
+                            .matcher(RuntimeErrorMatcher.class)
+                            .mailet(NoopMailet.class)
+                            .addProperty("onMailetException", CUSTOM_PROCESSOR)
+                            .build())
+                        .build())
+                    .build());
+        dataProbe = jamesServer.getProbe(DataProbeImpl.class);
+        MailRepositoryProbeImpl probe = jamesServer.getProbe(MailRepositoryProbeImpl.class);
+
+        dataProbe.addDomain(JAMES_APACHE_ORG);
+        dataProbe.addUser(FROM, PASSWORD);
+
+        try (SMTPMessageSender messageSender = SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG)) {
+            messageSender.sendMessage(FROM, RECIPIENT);
+
+            calmlyAwait.atMost(Duration.TEN_SECONDS)
+                .until(() -> probe.getRepositoryMailCount(CUSTOM_REPOSITORY) == 1);
+        }
+    }
+
+    @Test
+    public void onMatcherExceptionIgnoreShouldContinueProcessingWhenRuntimeException() throws Exception {
+        jamesServer = TemporaryJamesServer.builder()
+            .withBase(SMTP_ONLY_MODULE)
+            .build(temporaryFolder,
+                MailetContainer.builder()
+                    .threads(2)
+                    .postmaster("postmaster@localhost")
+                    .addProcessor(emptyTransport())
+                    .addProcessor(errorProcessor())
+                    .addProcessor(customProcessor())
+                    .addProcessor(ProcessorConfiguration.builder()
+                        .state(Mail.DEFAULT)
+                        .addMailet(MailetConfiguration.builder()
+                            .matcher(RuntimeErrorMatcher.class)
+                            .mailet(NoopMailet.class)
+                            .addProperty("onMailetException", "ignore")
+                            .build())
+                        .addMailet(MailetConfiguration.builder()
+                            .matcher(All.class)
+                            .mailet(ToRepository.class)
+                            .addProperty("repositoryPath", CUSTOM_REPOSITORY)
+                            .build())
+                        .build())
+                    .build());
+        dataProbe = jamesServer.getProbe(DataProbeImpl.class);
+        MailRepositoryProbeImpl probe = jamesServer.getProbe(MailRepositoryProbeImpl.class);
+
+        dataProbe.addDomain(JAMES_APACHE_ORG);
+        dataProbe.addUser(FROM, PASSWORD);
+
+        try (SMTPMessageSender messageSender = SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG)) {
+            messageSender.sendMessage(FROM, RECIPIENT);
+
+            calmlyAwait.atMost(Duration.TEN_SECONDS)
+                .until(() -> probe.getRepositoryMailCount(CUSTOM_REPOSITORY) == 1);
+        }
+    }
+
+    @Test
+    public void onMatcherExceptionIgnoreShouldContinueProcessingWhenMessagingException() throws Exception {
+        jamesServer = TemporaryJamesServer.builder()
+            .withBase(SMTP_ONLY_MODULE)
+            .build(temporaryFolder,
+                MailetContainer.builder()
+                    .threads(2)
+                    .postmaster("postmaster@localhost")
+                    .addProcessor(emptyTransport())
+                    .addProcessor(errorProcessor())
+                    .addProcessor(customProcessor())
+                    .addProcessor(ProcessorConfiguration.builder()
+                        .state(Mail.DEFAULT)
+                        .addMailet(MailetConfiguration.builder()
+                            .matcher(ErrorMatcher.class)
+                            .mailet(NoopMailet.class)
+                            .addProperty("onMailetException", "ignore")
+                            .build())
+                        .addMailet(MailetConfiguration.builder()
+                            .matcher(All.class)
+                            .mailet(ToRepository.class)
+                            .addProperty("repositoryPath", CUSTOM_REPOSITORY)
+                            .build())
+                        .build())
+                    .build());
+        dataProbe = jamesServer.getProbe(DataProbeImpl.class);
+        MailRepositoryProbeImpl probe = jamesServer.getProbe(MailRepositoryProbeImpl.class);
+
+        dataProbe.addDomain(JAMES_APACHE_ORG);
+        dataProbe.addUser(FROM, PASSWORD);
+
+        try (SMTPMessageSender messageSender = SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG)) {
+            messageSender.sendMessage(FROM, RECIPIENT);
+
+            calmlyAwait.atMost(Duration.TEN_SECONDS)
+                .until(() -> probe.getRepositoryMailCount(CUSTOM_REPOSITORY) == 1);
+        }
+    }
+
     private ProcessorConfiguration errorProcessor() {
         return ProcessorConfiguration.builder()
             .state(Mail.ERROR)

http://git-wip-us.apache.org/repos/asf/james-project/blob/410d92e1/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/ErrorMatcher.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/ErrorMatcher.java b/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/ErrorMatcher.java
new file mode 100644
index 0000000..aef2398
--- /dev/null
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/ErrorMatcher.java
@@ -0,0 +1,35 @@
+/****************************************************************
+ * 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.mailets;
+
+import java.util.Collection;
+
+import javax.mail.MessagingException;
+
+import org.apache.james.core.MailAddress;
+import org.apache.mailet.Mail;
+import org.apache.mailet.base.GenericMatcher;
+
+public class ErrorMatcher extends GenericMatcher {
+    @Override
+    public Collection<MailAddress> match(Mail mail) throws MessagingException {
+        throw new MessagingException();
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/410d92e1/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/NoopMailet.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/NoopMailet.java b/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/NoopMailet.java
new file mode 100644
index 0000000..0f0905e
--- /dev/null
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/NoopMailet.java
@@ -0,0 +1,32 @@
+/****************************************************************
+ * 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.mailets;
+
+import javax.mail.MessagingException;
+
+import org.apache.mailet.Mail;
+import org.apache.mailet.base.GenericMailet;
+
+public class NoopMailet extends GenericMailet {
+    @Override
+    public void service(Mail mail) throws MessagingException {
+
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/410d92e1/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/RuntimeErrorMatcher.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/RuntimeErrorMatcher.java b/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/RuntimeErrorMatcher.java
new file mode 100644
index 0000000..abf63d2
--- /dev/null
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/RuntimeErrorMatcher.java
@@ -0,0 +1,35 @@
+/****************************************************************
+ * 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.mailets;
+
+import java.util.Collection;
+
+import javax.mail.MessagingException;
+
+import org.apache.james.core.MailAddress;
+import org.apache.mailet.Mail;
+import org.apache.mailet.base.GenericMatcher;
+
+public class RuntimeErrorMatcher extends GenericMatcher {
+    @Override
+    public Collection<MailAddress> match(Mail mail) throws MessagingException {
+        throw new RuntimeException();
+    }
+}


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