This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git
commit d4d849133ac4d290f1bdb8f14f25f6b2422bfc97 Author: Robert Munteanu <[email protected]> AuthorDate: Wed Jun 5 14:13:00 2019 +0200 Allow accessing server as an instance field and stop hardcoding the local port. --- .../org/apache/sling/uca/impl/IntegrationTest.java | 5 +-- .../org/apache/sling/uca/impl/ServerControl.java | 6 ++++ .../java/org/apache/sling/uca/impl/ServerRule.java | 37 ++++++++++++++++++---- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java index d952181..aa02dd1 100644 --- a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java +++ b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java @@ -29,6 +29,7 @@ import java.net.URL; import java.net.URLConnection; import java.util.concurrent.TimeUnit; +import org.apache.sling.uca.impl.ServerRule.MisbehavingServer; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.slf4j.Logger; @@ -65,10 +66,10 @@ public class IntegrationTest { * @throws IOException various I/O problems */ @Test - public void readTimeout() throws IOException { + public void readTimeout(@MisbehavingServer ServerControl server) throws IOException { SocketTimeoutException exception = assertThrows(SocketTimeoutException.class, - () -> assertTimeout(ofSeconds(10), () -> runTest("http://localhost:" + ServerRule.getLocalPort())) + () -> assertTimeout(ofSeconds(10), () -> runTest("http://localhost:" + server.getLocalPort())) ); assertEquals("Read timed out", exception.getMessage()); } diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerControl.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerControl.java new file mode 100644 index 0000000..e73d682 --- /dev/null +++ b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerControl.java @@ -0,0 +1,6 @@ +package org.apache.sling.uca.impl; + +public interface ServerControl { + + int getLocalPort(); +} diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java index 62a6380..80d4fc4 100644 --- a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java +++ b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java @@ -17,6 +17,10 @@ package org.apache.sling.uca.impl; import java.io.IOException; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; import java.util.concurrent.TimeUnit; import javax.servlet.ServletException; @@ -31,25 +35,45 @@ import org.eclipse.jetty.server.handler.AbstractHandler; import org.junit.jupiter.api.extension.AfterAllCallback; import org.junit.jupiter.api.extension.BeforeAllCallback; import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.ParameterContext; +import org.junit.jupiter.api.extension.ParameterResolutionException; +import org.junit.jupiter.api.extension.ParameterResolver; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -class ServerRule implements BeforeAllCallback, AfterAllCallback { +class ServerRule implements BeforeAllCallback, AfterAllCallback, ParameterResolver, ServerControl { - private final Logger logger = LoggerFactory.getLogger(getClass()); + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.PARAMETER) + public @interface MisbehavingServer { } - private static final int LOCAL_PORT = 12312; + private final Logger logger = LoggerFactory.getLogger(getClass()); - public static int getLocalPort() { - return LOCAL_PORT; + public int getLocalPort() { + return ((ServerConnector) server.getConnectors()[0]).getLocalPort(); } private Server server; @Override + public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) + throws ParameterResolutionException { + return parameterContext.isAnnotated(MisbehavingServer.class); + } + + @Override + public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) + throws ParameterResolutionException { + if ( parameterContext.getParameter().getType() == ServerControl.class ) + return this; + + throw new ParameterResolutionException("Unable to get a Server instance for " + parameterContext); + } + + @Override public void beforeAll(ExtensionContext context) throws Exception { - server = new Server(LOCAL_PORT); + server = new Server(); ServerConnector connector = new ServerConnector(server) { @Override public void accept(int acceptorID) throws IOException { @@ -64,7 +88,6 @@ class ServerRule implements BeforeAllCallback, AfterAllCallback { LOG.info("Accepted"); } }; - connector.setPort(LOCAL_PORT); server.setConnectors(new Connector[] { connector }); server.setHandler(new AbstractHandler() {
