james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [james-project] 04/23: JAMES-2565 Ability to bind MockSMTP server on a fixed port
Date Fri, 06 Sep 2019 02:01:55 GMT
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

commit 47984b1e6ea6abe88e18deeeb335c575a0027350
Author: Benoit Tellier <btellier@linagora.com>
AuthorDate: Wed Aug 28 10:36:24 2019 +0700

    JAMES-2565 Ability to bind MockSMTP server on a fixed port
---
 .../org/apache/james/mock/smtp/server/MockSMTPServer.java  | 14 ++++++++++++--
 .../apache/james/mock/smtp/server/MockSMTPServerTest.java  |  8 ++++----
 2 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/MockSMTPServer.java
b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/MockSMTPServer.java
index 13a7965..8233d66 100644
--- a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/MockSMTPServer.java
+++ b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/MockSMTPServer.java
@@ -23,11 +23,21 @@ import org.apache.james.util.Port;
 import org.subethamail.smtp.server.SMTPServer;
 
 class MockSMTPServer {
+    private static final int RANDOM_PORT = 0;
+
+    public static MockSMTPServer onRandomPort(SMTPBehaviorRepository behaviorRepository,
ReceivedMailRepository mailRepository) {
+        return new MockSMTPServer(behaviorRepository, mailRepository, RANDOM_PORT);
+    }
+
+    public static MockSMTPServer onPort(SMTPBehaviorRepository behaviorRepository, ReceivedMailRepository
mailRepository, Port port) {
+        return new MockSMTPServer(behaviorRepository, mailRepository, port.getValue());
+    }
+
     private final SMTPServer server;
 
-    MockSMTPServer(SMTPBehaviorRepository behaviorRepository, ReceivedMailRepository mailRepository)
{
+    private MockSMTPServer(SMTPBehaviorRepository behaviorRepository, ReceivedMailRepository
mailRepository, int port) {
         this.server = new SMTPServer(ctx -> new MockMessageHandler(mailRepository, behaviorRepository));
-        this.server.setPort(0);
+        this.server.setPort(port);
     }
 
     void start() {
diff --git a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/MockSMTPServerTest.java
b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/MockSMTPServerTest.java
index ee8e41f..a5ceef5 100644
--- a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/MockSMTPServerTest.java
+++ b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/MockSMTPServerTest.java
@@ -74,7 +74,7 @@ class MockSMTPServerTest {
     void setUp() throws Exception {
         behaviorRepository = new SMTPBehaviorRepository();
         mailRepository = new ReceivedMailRepository();
-        mockServer = new MockSMTPServer(behaviorRepository, mailRepository);
+        mockServer = MockSMTPServer.onRandomPort(behaviorRepository, mailRepository);
 
         mimeMessage1 = MimeMessageBuilder.mimeMessageBuilder()
             .setSubject("test")
@@ -423,7 +423,7 @@ class MockSMTPServerTest {
 
     @Test
     void serverStartShouldOpenASmtpPort() {
-        MockSMTPServer mockServer = new MockSMTPServer(new SMTPBehaviorRepository(), new
ReceivedMailRepository());
+        MockSMTPServer mockServer = MockSMTPServer.onRandomPort(new SMTPBehaviorRepository(),
new ReceivedMailRepository());
         mockServer.start();
 
         assertThatCode(() -> new SMTPMessageSender(DOMAIN)
@@ -433,7 +433,7 @@ class MockSMTPServerTest {
 
     @Test
     void serverShouldBeAbleToStop() {
-        MockSMTPServer mockServer = new MockSMTPServer(new SMTPBehaviorRepository(), new
ReceivedMailRepository());
+        MockSMTPServer mockServer = MockSMTPServer.onRandomPort(new SMTPBehaviorRepository(),
new ReceivedMailRepository());
         mockServer.start();
         Port port = mockServer.getPort();
 
@@ -446,7 +446,7 @@ class MockSMTPServerTest {
 
     @Test
     void serverStartShouldBeIdempotent() {
-        MockSMTPServer mockServer = new MockSMTPServer(new SMTPBehaviorRepository(), new
ReceivedMailRepository());
+        MockSMTPServer mockServer = MockSMTPServer.onRandomPort(new SMTPBehaviorRepository(),
new ReceivedMailRepository());
         mockServer.start();
 
         assertThatCode(mockServer::start)


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


Mime
View raw message