JAMES-2366 Run WebAdminServer on a random port in tests
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/99b5cb6c Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/99b5cb6c Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/99b5cb6c Branch: refs/heads/master Commit: 99b5cb6c779a88cfd5ff39da32dbe2a4d802f5ad Parents: 686a75e Author: Antoine Duprat <adup...@linagora.com> Authored: Thu Apr 5 15:46:24 2018 +0200 Committer: Antoine Duprat <adup...@linagora.com> Committed: Fri Apr 6 15:04:50 2018 +0200 ---------------------------------------------------------------------- .../apache/james/utils/WebAdminGuiceProbe.java | 4 +- .../main/java/org/apache/james/util/Port.java | 6 ++- .../james/webadmin/RandomPortSupplier.java | 28 +++++------- .../apache/james/webadmin/WebAdminServer.java | 10 ++++- .../james/webadmin/WebAdminServerTest.java | 46 ++++++++++++++++++++ .../apache/james/webadmin/WebAdminUtils.java | 5 ++- .../webadmin/routes/MailQueueRoutesTest.java | 2 +- 7 files changed, 75 insertions(+), 26 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/99b5cb6c/server/container/guice/protocols/webadmin/src/main/java/org/apache/james/utils/WebAdminGuiceProbe.java ---------------------------------------------------------------------- diff --git a/server/container/guice/protocols/webadmin/src/main/java/org/apache/james/utils/WebAdminGuiceProbe.java b/server/container/guice/protocols/webadmin/src/main/java/org/apache/james/utils/WebAdminGuiceProbe.java index 7b0528b..844d228 100644 --- a/server/container/guice/protocols/webadmin/src/main/java/org/apache/james/utils/WebAdminGuiceProbe.java +++ b/server/container/guice/protocols/webadmin/src/main/java/org/apache/james/utils/WebAdminGuiceProbe.java @@ -21,7 +21,7 @@ package org.apache.james.utils; import javax.inject.Inject; -import org.apache.james.webadmin.PortSupplier; +import org.apache.james.util.Port; import org.apache.james.webadmin.WebAdminServer; public class WebAdminGuiceProbe implements GuiceProbe { @@ -32,7 +32,7 @@ public class WebAdminGuiceProbe implements GuiceProbe { this.webAdminServer = webAdminServer; } - public PortSupplier getWebAdminPort() { + public Port getWebAdminPort() { return webAdminServer.getPort(); } http://git-wip-us.apache.org/repos/asf/james-project/blob/99b5cb6c/server/container/util-java8/src/main/java/org/apache/james/util/Port.java ---------------------------------------------------------------------- diff --git a/server/container/util-java8/src/main/java/org/apache/james/util/Port.java b/server/container/util-java8/src/main/java/org/apache/james/util/Port.java index 2acc4b6..016c4b4 100644 --- a/server/container/util-java8/src/main/java/org/apache/james/util/Port.java +++ b/server/container/util-java8/src/main/java/org/apache/james/util/Port.java @@ -44,10 +44,14 @@ public class Port { private final int value; public Port(int value) { - assertValid(value); + validate(value); this.value = value; } + protected void validate(int port) { + assertValid(port); + } + public int getValue() { return value; } http://git-wip-us.apache.org/repos/asf/james-project/blob/99b5cb6c/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/RandomPortSupplier.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/RandomPortSupplier.java b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/RandomPortSupplier.java index 7f545b2..6289378 100644 --- a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/RandomPortSupplier.java +++ b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/RandomPortSupplier.java @@ -19,31 +19,23 @@ package org.apache.james.webadmin; -import java.io.IOException; -import java.net.ServerSocket; -import java.util.function.Supplier; - -import org.apache.james.util.MemoizedSupplier; import org.apache.james.util.Port; -import com.github.fge.lambdas.Throwing; - public class RandomPortSupplier implements PortSupplier { - public static int findFreePort() throws IOException { - try (ServerSocket socket = new ServerSocket(0)) { - return socket.getLocalPort(); - } + @Override + public Port get() { + return new RandomPort(); } - private final Supplier<Integer> portSupplier; + private static class RandomPort extends Port { - public RandomPortSupplier() { - portSupplier = MemoizedSupplier.of(Throwing.supplier(RandomPortSupplier::findFreePort)); - } + public RandomPort() { + super(0); + } - @Override - public Port get() { - return new Port(portSupplier.get()); + @Override + protected void validate(int port) { + } } } http://git-wip-us.apache.org/repos/asf/james-project/blob/99b5cb6c/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java index e544022..c21ab8c 100644 --- a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java +++ b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java @@ -28,6 +28,7 @@ import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.HierarchicalConfiguration; import org.apache.james.lifecycle.api.Configurable; import org.apache.james.metrics.api.MetricFactory; +import org.apache.james.util.Port; import org.apache.james.webadmin.authentication.AuthenticationFilter; import org.apache.james.webadmin.mdc.LoggingRequestFilter; import org.apache.james.webadmin.mdc.LoggingResponseFilter; @@ -39,6 +40,8 @@ import org.apache.james.webadmin.routes.CORSRoute; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.base.Preconditions; + import spark.Service; public class WebAdminServer implements Configurable { @@ -52,6 +55,7 @@ public class WebAdminServer implements Configurable { private final Service service; private final AuthenticationFilter authenticationFilter; private final MetricFactory metricFactory; + private Port port; // Spark do not allow to retrieve allocated port when using a random port. Thus we generate the port. @Inject @@ -76,6 +80,7 @@ public class WebAdminServer implements Configurable { configureMDC(); routesList.forEach(routes -> routes.define(service)); service.awaitInitialization(); + port = new Port(service.port()); LOGGER.info("Web admin server started"); } } @@ -123,7 +128,8 @@ public class WebAdminServer implements Configurable { service.awaitInitialization(); } - public PortSupplier getPort() { - return configuration.getPort(); + public Port getPort() { + Preconditions.checkState(port != null, "WebAdminServer should be configured."); + return port; } } http://git-wip-us.apache.org/repos/asf/james-project/blob/99b5cb6c/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminServerTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminServerTest.java b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminServerTest.java new file mode 100644 index 0000000..abb72a9 --- /dev/null +++ b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminServerTest.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.webadmin; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.apache.james.metrics.logger.DefaultMetricFactory; +import org.apache.james.util.Port; +import org.junit.Test; + +public class WebAdminServerTest { + + @Test + public void getPortShouldThrowWhenNotConfigured() throws Exception { + WebAdminServer server = WebAdminUtils.createWebAdminServer(new DefaultMetricFactory()); + assertThatThrownBy(() -> server.getPort()) + .isInstanceOf(IllegalStateException.class); + } + + @Test + public void getPortShouldReturnPortWhenConfigured() throws Exception { + WebAdminServer server = WebAdminUtils.createWebAdminServer(new DefaultMetricFactory()); + server.configure(WebAdminServer.NO_CONFIGURATION); + + Port port = server.getPort(); + + assertThat(port).isNotNull(); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/99b5cb6c/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtils.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtils.java b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtils.java index 76862b4..f4f95ff 100644 --- a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtils.java +++ b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtils.java @@ -26,6 +26,7 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import org.apache.james.metrics.api.MetricFactory; +import org.apache.james.util.Port; import org.apache.james.webadmin.authentication.NoAuthenticationFilter; import com.google.common.collect.ImmutableSet; @@ -45,12 +46,12 @@ public class WebAdminUtils { return buildRequestSpecification(webAdminServer.getPort()); } - public static RequestSpecBuilder buildRequestSpecification(PortSupplier portSupplier) { + public static RequestSpecBuilder buildRequestSpecification(Port port) { return new RequestSpecBuilder() .setContentType(ContentType.JSON) .setAccept(ContentType.JSON) .setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(StandardCharsets.UTF_8))) - .setPort(portSupplier.get().getValue()); + .setPort(port.getValue()); } } http://git-wip-us.apache.org/repos/asf/james-project/blob/99b5cb6c/server/protocols/webadmin/webadmin-mailqueue/src/test/java/org/apache/james/webadmin/routes/MailQueueRoutesTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/webadmin-mailqueue/src/test/java/org/apache/james/webadmin/routes/MailQueueRoutesTest.java b/server/protocols/webadmin/webadmin-mailqueue/src/test/java/org/apache/james/webadmin/routes/MailQueueRoutesTest.java index 3a33798..ac75c9b 100644 --- a/server/protocols/webadmin/webadmin-mailqueue/src/test/java/org/apache/james/webadmin/routes/MailQueueRoutesTest.java +++ b/server/protocols/webadmin/webadmin-mailqueue/src/test/java/org/apache/james/webadmin/routes/MailQueueRoutesTest.java @@ -81,7 +81,7 @@ public class MailQueueRoutesTest { .setContentType(ContentType.JSON) .setAccept(ContentType.JSON) .setBasePath(MailQueueRoutes.BASE_URL) - .setPort(server.getPort().get().getValue()) + .setPort(server.getPort().getValue()) .setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(StandardCharsets.UTF_8))) .build(); } --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org