JAMES-2262 Use a Test rule for Mock SpamD test rule

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

Branch: refs/heads/master
Commit: d11a7e406642aaaa12436ebd9c46b46c802b63af
Parents: 1ef7daf
Author: benwa <btell...@linagora.com>
Authored: Mon Dec 18 08:40:41 2017 +0700
Committer: benwa <btell...@linagora.com>
Committed: Mon Dec 25 11:24:28 2017 +0700

----------------------------------------------------------------------
 .../transport/mailets/SpamAssassinTest.java     | 16 +++----
 .../smtpserver/SpamAssassinHandlerTest.java     | 14 +++---
 .../smtpserver/mock/util/MockSpamdTestRule.java | 46 ++++++++++++++++++++
 3 files changed, 57 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/d11a7e40/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/SpamAssassinTest.java
----------------------------------------------------------------------
diff --git 
a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/SpamAssassinTest.java
 
b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/SpamAssassinTest.java
index 82279c0..71831d9 100644
--- 
a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/SpamAssassinTest.java
+++ 
b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/SpamAssassinTest.java
@@ -24,15 +24,20 @@ import static 
org.assertj.core.api.Assertions.assertThatThrownBy;
 
 import javax.mail.MessagingException;
 
-import org.apache.james.smtpserver.mock.util.MockSpamd;
+import org.apache.james.smtpserver.mock.util.MockSpamdTestRule;
 import org.apache.james.util.scanner.SpamAssassinInvoker;
 import org.apache.mailet.Mail;
 import org.apache.mailet.base.test.FakeMail;
 import org.apache.mailet.base.test.FakeMailetConfig;
 import org.apache.mailet.base.test.MimeMessageBuilder;
+import org.junit.Rule;
 import org.junit.Test;
 
 public class SpamAssassinTest {
+
+    @Rule
+    public MockSpamdTestRule spamd = new MockSpamdTestRule();
+
     private SpamAssassin mailet = new SpamAssassin();
 
     @Test
@@ -129,9 +134,6 @@ public class SpamAssassinTest {
 
     @Test
     public void serviceShouldWriteSpamAttributeOnMail() throws Exception {
-        MockSpamd spamd = new MockSpamd();
-        new Thread(spamd).start();
-
         FakeMailetConfig mailetConfiguration = FakeMailetConfig.builder()
             .mailetName("SpamAssassin")
             .setProperty(SpamAssassin.SPAMD_HOST, "localhost")
@@ -156,9 +158,6 @@ public class SpamAssassinTest {
 
     @Test
     public void serviceShouldWriteMessageAsNotSpamWhenNotSpam() throws 
Exception {
-        MockSpamd spamd = new MockSpamd();
-        new Thread(spamd).start();
-
         FakeMailetConfig mailetConfiguration = FakeMailetConfig.builder()
             .mailetName("SpamAssassin")
             .setProperty(SpamAssassin.SPAMD_HOST, "localhost")
@@ -182,9 +181,6 @@ public class SpamAssassinTest {
 
     @Test
     public void serviceShouldWriteMessageAsSpamWhenSpam() throws Exception {
-        MockSpamd spamd = new MockSpamd();
-        new Thread(spamd).start();
-
         FakeMailetConfig mailetConfiguration = FakeMailetConfig.builder()
             .mailetName("SpamAssassin")
             .setProperty(SpamAssassin.SPAMD_HOST, "localhost")

http://git-wip-us.apache.org/repos/asf/james-project/blob/d11a7e40/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SpamAssassinHandlerTest.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SpamAssassinHandlerTest.java
 
b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SpamAssassinHandlerTest.java
index bc94b6a..cf1b7cc 100644
--- 
a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SpamAssassinHandlerTest.java
+++ 
b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SpamAssassinHandlerTest.java
@@ -35,8 +35,10 @@ import 
org.apache.james.smtpserver.fastfail.SpamAssassinHandler;
 import org.apache.james.smtpserver.mock.MockMimeMessage;
 import org.apache.james.smtpserver.mock.mailet.MockMail;
 import org.apache.james.smtpserver.mock.util.MockSpamd;
+import org.apache.james.smtpserver.mock.util.MockSpamdTestRule;
 import org.apache.james.util.scanner.SpamAssassinInvoker;
 import org.apache.mailet.Mail;
+import org.junit.Rule;
 import org.junit.Test;
 
 public class SpamAssassinHandlerTest {
@@ -91,6 +93,9 @@ public class SpamAssassinHandlerTest {
 
     }
 
+    @Rule
+    public MockSpamdTestRule spamd = new MockSpamdTestRule();
+
     private Mail setupMockedMail(MimeMessage message) {
         MockMail mail = new MockMail();
         mail.setMessage(message);
@@ -107,9 +112,6 @@ public class SpamAssassinHandlerTest {
 
     @Test
     public void testNonSpam() throws IOException, MessagingException {
-        MockSpamd spamd = new MockSpamd();
-        new Thread(spamd).start();
-
         SMTPSession session = 
setupMockedSMTPSession(setupMockedMail(setupMockedMimeMessage("test")));
 
         SpamAssassinHandler handler = new SpamAssassinHandler();
@@ -127,9 +129,6 @@ public class SpamAssassinHandlerTest {
 
     @Test
     public void testSpam() throws IOException, MessagingException {
-        MockSpamd spamd = new MockSpamd();
-        new Thread(spamd).start();
-
         SMTPSession session = 
setupMockedSMTPSession(setupMockedMail(setupMockedMimeMessage(MockSpamd.GTUBE)));
 
         SpamAssassinHandler handler = new SpamAssassinHandler();
@@ -146,9 +145,6 @@ public class SpamAssassinHandlerTest {
 
     @Test
     public void testSpamReject() throws IOException, MessagingException {
-        MockSpamd spamd = new MockSpamd();
-        new Thread(spamd).start();
-
         SMTPSession session = 
setupMockedSMTPSession(setupMockedMail(setupMockedMimeMessage(MockSpamd.GTUBE)));
 
         SpamAssassinHandler handler = new SpamAssassinHandler();

http://git-wip-us.apache.org/repos/asf/james-project/blob/d11a7e40/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/mock/util/MockSpamdTestRule.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/mock/util/MockSpamdTestRule.java
 
b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/mock/util/MockSpamdTestRule.java
new file mode 100644
index 0000000..4822b8f
--- /dev/null
+++ 
b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/mock/util/MockSpamdTestRule.java
@@ -0,0 +1,46 @@
+/****************************************************************
+ * 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.smtpserver.mock.util;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+import org.junit.rules.ExternalResource;
+
+public class MockSpamdTestRule extends ExternalResource {
+
+    private ExecutorService executor = Executors.newSingleThreadExecutor();
+    private MockSpamd spamd = new MockSpamd();
+
+    @Override
+    protected void before() throws Throwable {
+        spamd.bind();
+        executor.execute(spamd);
+    }
+
+    public int getPort() {
+        return spamd.getPort();
+    }
+
+    @Override
+    protected void after() {
+        executor.shutdownNow();
+    }
+}


---------------------------------------------------------------------
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