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 42d7d55d5380d13648533160844d30f765a2c6f1 Author: Tran Tien Duc <dt...@linagora.com> AuthorDate: Tue Sep 10 15:29:59 2019 +0700 JAMES-2879 Prevent ConfigurationClient connects with a non-started server Eliminate run-time checking by passing new type into ConfigurationClient.from() --- .../mock/smtp/server/ConfigurationClient.java | 2 +- .../mock/smtp/server/HTTPConfigurationServer.java | 24 +++++++++++++++++++--- .../mock/smtp/server/ConfigurationClientTest.java | 6 +++--- .../smtp/server/HTTPConfigurationServerTest.java | 10 ++++----- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/ConfigurationClient.java b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/ConfigurationClient.java index 0bc85d9..a93c369 100644 --- a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/ConfigurationClient.java +++ b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/ConfigurationClient.java @@ -157,7 +157,7 @@ public interface ConfigurationClient { } @VisibleForTesting - static ConfigurationClient fromServer(HTTPConfigurationServer server) { + static ConfigurationClient fromServer(HTTPConfigurationServer.RunningStage server) { return from(Host.from("localhost", server.getPort().getValue())); } diff --git a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/HTTPConfigurationServer.java b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/HTTPConfigurationServer.java index b3963ed..83f6ac0 100644 --- a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/HTTPConfigurationServer.java +++ b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/HTTPConfigurationServer.java @@ -43,6 +43,23 @@ import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; import com.github.steveash.guavate.Guavate; public class HTTPConfigurationServer { + + public static class RunningStage { + private final HTTPConfigurationServer underlyingServer; + + private RunningStage(HTTPConfigurationServer underlyingServer) { + this.underlyingServer = underlyingServer; + } + + public Port getPort() { + return underlyingServer.getPort(); + } + + public void stop() throws Exception { + underlyingServer.stop(); + } + } + static class SMTPBehaviorsServlet extends HttpServlet { private final SMTPBehaviorRepository smtpBehaviorRepository; @@ -131,15 +148,16 @@ public class HTTPConfigurationServer { .build()); } - public void start() throws Exception { + public RunningStage start() throws Exception { jettyHttpServer.start(); + return new RunningStage(this); } - public Port getPort() { + private Port getPort() { return new Port(jettyHttpServer.getPort()); } - public void stop() throws Exception { + private void stop() throws Exception { jettyHttpServer.stop(); } } diff --git a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/ConfigurationClientTest.java b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/ConfigurationClientTest.java index 21128ec..c1d9c1f 100644 --- a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/ConfigurationClientTest.java +++ b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/ConfigurationClientTest.java @@ -29,7 +29,7 @@ import org.junit.jupiter.api.Test; class ConfigurationClientTest { private ConfigurationClient testee; - private HTTPConfigurationServer server; + private HTTPConfigurationServer.RunningStage server; private SMTPBehaviorRepository behaviorRepository; private ReceivedMailRepository mailRepository; @@ -37,8 +37,8 @@ class ConfigurationClientTest { void setUp() throws Exception { behaviorRepository = new SMTPBehaviorRepository(); mailRepository = new ReceivedMailRepository(); - server = HTTPConfigurationServer.onRandomPort(behaviorRepository, mailRepository); - server.start(); + server = HTTPConfigurationServer.onRandomPort(behaviorRepository, mailRepository) + .start(); testee = ConfigurationClient.fromServer(server); } diff --git a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/HTTPConfigurationServerTest.java b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/HTTPConfigurationServerTest.java index d15f607..4cc18aa 100644 --- a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/HTTPConfigurationServerTest.java +++ b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/HTTPConfigurationServerTest.java @@ -47,14 +47,14 @@ import net.javacrumbs.jsonunit.core.Option; import net.javacrumbs.jsonunit.core.internal.Options; class HTTPConfigurationServerTest { - private HTTPConfigurationServer server; + private HTTPConfigurationServer.RunningStage server; @Nested class SMTPBehaviorsTest { @BeforeEach void setUp() throws Exception { - server = HTTPConfigurationServer.onRandomPort(new SMTPBehaviorRepository(), new ReceivedMailRepository()); - server.start(); + server = HTTPConfigurationServer.onRandomPort(new SMTPBehaviorRepository(), new ReceivedMailRepository()) + .start(); RestAssured.requestSpecification = new RequestSpecBuilder() .setContentType(ContentType.JSON) @@ -147,8 +147,8 @@ class HTTPConfigurationServerTest { void setUp() throws Exception { mailRepository = new ReceivedMailRepository(); - server = HTTPConfigurationServer.onRandomPort(new SMTPBehaviorRepository(), mailRepository); - server.start(); + server = HTTPConfigurationServer.onRandomPort(new SMTPBehaviorRepository(), mailRepository) + .start(); RestAssured.requestSpecification = new RequestSpecBuilder() .setContentType(ContentType.JSON) --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org