This is an automated email from the ASF dual-hosted git repository. orpiske pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new ad596a5092c CAMEL-18456: converted camel-http to camel-test-infra-jetty ad596a5092c is described below commit ad596a5092ccb59f63aa85269e9d6d8d270e4478 Author: Otavio Rodolfo Piske <angusyo...@gmail.com> AuthorDate: Mon Sep 12 15:04:43 2022 +0200 CAMEL-18456: converted camel-http to camel-test-infra-jetty --- components/camel-http/pom.xml | 19 ++++- .../component/http/HttpProducerSessionTest.java | 38 ++++----- .../infra/jetty/services/JettyConfiguration.java | 43 +++++++--- .../jetty/services/JettyConfigurationBuilder.java | 94 +++++++++++++++++++--- 4 files changed, 152 insertions(+), 42 deletions(-) diff --git a/components/camel-http/pom.xml b/components/camel-http/pom.xml index 62ab84be4e4..b5b5f16ee7d 100644 --- a/components/camel-http/pom.xml +++ b/components/camel-http/pom.xml @@ -101,12 +101,25 @@ <classifier>tests</classifier> <scope>test</scope> </dependency> + + + <!-- test infra --> <dependency> - <groupId>org.eclipse.jetty</groupId> - <artifactId>jetty-server</artifactId> - <version>${jetty-version}</version> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-infra-common</artifactId> + <version>${project.version}</version> + <type>test-jar</type> <scope>test</scope> </dependency> + + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-infra-jetty</artifactId> + <version>${project.version}</version> + <type>test-jar</type> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProducerSessionTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProducerSessionTest.java index 145cc74ee62..f839a57124d 100644 --- a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProducerSessionTest.java +++ b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProducerSessionTest.java @@ -16,25 +16,33 @@ */ package org.apache.camel.component.http; -import java.net.InetSocketAddress; - import org.apache.camel.BindToRegistry; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.http.handler.SessionReflectionHandler; import org.apache.camel.http.base.cookie.ExchangeCookieHandler; import org.apache.camel.http.base.cookie.InstanceCookieHandler; import org.apache.camel.test.AvailablePortFinder; +import org.apache.camel.test.infra.jetty.services.JettyConfiguration; +import org.apache.camel.test.infra.jetty.services.JettyConfigurationBuilder; +import org.apache.camel.test.infra.jetty.services.JettyEmbeddedService; import org.apache.camel.test.junit5.CamelTestSupport; -import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.handler.ContextHandler; import org.eclipse.jetty.server.session.SessionHandler; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; public class HttpProducerSessionTest extends CamelTestSupport { - private static volatile int port; - private static Server localServer; + private static final int PORT = AvailablePortFinder.getNextAvailable(); + + private final JettyConfiguration jettyConfiguration = JettyConfigurationBuilder + .emptyTemplate() + .withPort(PORT) + .withContextPath("/session") + .withContextHandlerConfiguration().withCustomizer(HttpProducerSessionTest::customizer) + .build().build(); + + @RegisterExtension + public JettyEmbeddedService service = new JettyEmbeddedService(jettyConfiguration); @BindToRegistry("instanceCookieHandler") private InstanceCookieHandler instanceHandler = new InstanceCookieHandler(); @@ -45,22 +53,10 @@ public class HttpProducerSessionTest extends CamelTestSupport { @BindToRegistry("noopCookieStore") private NoopCookieStore cookieStore = new NoopCookieStore(); - @BeforeAll - public static void initServer() throws Exception { - port = AvailablePortFinder.getNextAvailable(); - localServer = new Server(new InetSocketAddress("127.0.0.1", port)); - ContextHandler contextHandler = new ContextHandler(); - contextHandler.setContextPath("/session"); + private static void customizer(ContextHandler contextHandler) { SessionHandler sessionHandler = new SessionHandler(); sessionHandler.setHandler(new SessionReflectionHandler()); contextHandler.setHandler(sessionHandler); - localServer.setHandler(contextHandler); - localServer.start(); - } - - @AfterAll - public static void shutdownServer() throws Exception { - localServer.stop(); } @Test @@ -89,7 +85,7 @@ public class HttpProducerSessionTest extends CamelTestSupport { private String getTestServerEndpointSessionUrl() { // session handling will not work for localhost - return "http://127.0.0.1:" + port + "/session/"; + return "http://127.0.0.1:" + PORT + "/session/"; } @Override diff --git a/test-infra/camel-test-infra-jetty/src/test/java/org/apache/camel/test/infra/jetty/services/JettyConfiguration.java b/test-infra/camel-test-infra-jetty/src/test/java/org/apache/camel/test/infra/jetty/services/JettyConfiguration.java index 72ec45ff643..0390d02dade 100644 --- a/test-infra/camel-test-infra-jetty/src/test/java/org/apache/camel/test/infra/jetty/services/JettyConfiguration.java +++ b/test-infra/camel-test-infra-jetty/src/test/java/org/apache/camel/test/infra/jetty/services/JettyConfiguration.java @@ -37,6 +37,7 @@ import org.eclipse.jetty.security.UserStore; import org.eclipse.jetty.security.authentication.BasicAuthenticator; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.handler.ContextHandler; +import org.eclipse.jetty.server.handler.ErrorHandler; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.util.security.Constraint; @@ -52,6 +53,7 @@ public class JettyConfiguration { public abstract static class AbstractContextHandlerConfiguration<T> { protected final String contextPath; + protected Consumer<T> customizer; public AbstractContextHandlerConfiguration(String contextPath) { this.contextPath = contextPath; @@ -61,40 +63,59 @@ public class JettyConfiguration { return contextPath; } - public void customize(Consumer<T> customizer, T handler) { - customizer.accept(handler); + public void customize(Consumer<T> customizer) { + this.customizer = customizer; } abstract T resolve(); } public static class ContextHandlerConfiguration extends AbstractContextHandlerConfiguration<ContextHandler> { + private final ContextHandler contextHandler; + public ContextHandlerConfiguration(String contextPath) { super(contextPath); + + contextHandler = new ContextHandler(contextPath); + } + + public void setErrorHandler(ErrorHandler errorHandler) { + contextHandler.setErrorHandler(errorHandler); + } + + public void setHandler(Handler handler) { + contextHandler.setHandler(handler); } @Override ContextHandler resolve() { - ContextHandler contextHandler = new ContextHandler(contextPath); + if (customizer != null) { + customizer.accept(contextHandler); + } return contextHandler; } } public static class WebContextConfiguration extends AbstractContextHandlerConfiguration<WebAppContext> { - private final String webApp; + private String webApp; - public WebContextConfiguration(String webApp, String contextPath) { + public WebContextConfiguration(String contextPath) { super(contextPath); - this.webApp = webApp; } - public String getWebApp() { - return webApp; + public void setWebApp(String webApp) { + this.webApp = webApp; } public WebAppContext resolve() { - return new WebAppContext(webApp, super.getContextPath()); + final WebAppContext webAppContext = new WebAppContext(webApp, super.getContextPath()); + + if (customizer != null) { + customizer.accept(webAppContext); + } + + return webAppContext; } } @@ -259,6 +280,10 @@ public class JettyConfiguration { contextHandler.addServlet(servletConfiguration.buildServletHolder(), servletConfiguration.getPathSpec()); } + if (customizer != null) { + customizer.accept(contextHandler); + } + return contextHandler; } } diff --git a/test-infra/camel-test-infra-jetty/src/test/java/org/apache/camel/test/infra/jetty/services/JettyConfigurationBuilder.java b/test-infra/camel-test-infra-jetty/src/test/java/org/apache/camel/test/infra/jetty/services/JettyConfigurationBuilder.java index 8dbf524b8a7..9edad30fbd9 100644 --- a/test-infra/camel-test-infra-jetty/src/test/java/org/apache/camel/test/infra/jetty/services/JettyConfigurationBuilder.java +++ b/test-infra/camel-test-infra-jetty/src/test/java/org/apache/camel/test/infra/jetty/services/JettyConfigurationBuilder.java @@ -23,13 +23,20 @@ import java.util.function.Supplier; import javax.net.ssl.SSLContext; import org.apache.camel.util.KeyValueHolder; +import org.eclipse.jetty.server.Handler; +import org.eclipse.jetty.server.handler.ContextHandler; +import org.eclipse.jetty.server.handler.ErrorHandler; import org.eclipse.jetty.servlet.ServletContextHandler; /** * This builder can be used to build and configure a configuration holder for embedded Jetty instances */ public final class JettyConfigurationBuilder { - public static class ServletConfigurationBuilder { + private interface ConfigurationBuilderDelegate { + JettyConfigurationBuilder build(); + } + + public static class ServletConfigurationBuilder implements ConfigurationBuilderDelegate { private final JettyConfiguration jettyConfiguration; private final JettyConfigurationBuilder jettyConfigurationBuilder; @@ -43,9 +50,8 @@ public final class JettyConfigurationBuilder { = new JettyConfiguration.ServletHandlerConfiguration(jettyConfiguration.getContextPath()); } - public ServletConfigurationBuilder customize( - Consumer<ServletContextHandler> customizer, ServletContextHandler handler) { - servletHandlerConfiguration.customize(customizer, handler); + public ServletConfigurationBuilder customize(Consumer<ServletContextHandler> customizer) { + servletHandlerConfiguration.customize(customizer); return this; } @@ -69,12 +75,80 @@ public final class JettyConfigurationBuilder { return this; } + @Override public JettyConfigurationBuilder build() { jettyConfiguration.setContextHandlerConfiguration(servletHandlerConfiguration); return jettyConfigurationBuilder; } } + public static class WebAppContextConfigurationBuilder implements ConfigurationBuilderDelegate { + private final JettyConfiguration jettyConfiguration; + private final JettyConfigurationBuilder jettyConfigurationBuilder; + + private final JettyConfiguration.WebContextConfiguration webContextConfiguration; + + public WebAppContextConfigurationBuilder(JettyConfigurationBuilder jettyConfigurationBuilder, + JettyConfiguration jettyConfiguration) { + this.jettyConfigurationBuilder = jettyConfigurationBuilder; + this.jettyConfiguration = jettyConfiguration; + + this.webContextConfiguration = new JettyConfiguration.WebContextConfiguration(jettyConfiguration.getContextPath()); + } + + public String getContextPath() { + return webContextConfiguration.getContextPath(); + } + + @Override + public JettyConfigurationBuilder build() { + jettyConfiguration.setContextHandlerConfiguration(webContextConfiguration); + + return jettyConfigurationBuilder; + } + } + + public static class ContextHandlerConfigurationBuilder implements ConfigurationBuilderDelegate { + private final JettyConfiguration jettyConfiguration; + private final JettyConfigurationBuilder jettyConfigurationBuilder; + + private final JettyConfiguration.ContextHandlerConfiguration contextHandlerConfiguration; + private Consumer<ContextHandler> contextHandlerCustomizer; + + public ContextHandlerConfigurationBuilder(JettyConfigurationBuilder jettyConfigurationBuilder, + JettyConfiguration jettyConfiguration) { + this.jettyConfiguration = jettyConfiguration; + this.jettyConfigurationBuilder = jettyConfigurationBuilder; + + contextHandlerConfiguration + = new JettyConfiguration.ContextHandlerConfiguration(jettyConfiguration.getContextPath()); + } + + public ContextHandlerConfigurationBuilder withErrorHandler(ErrorHandler errorHandler) { + contextHandlerConfiguration.setErrorHandler(errorHandler); + + return this; + } + + public ContextHandlerConfigurationBuilder withHandler(Handler handler) { + contextHandlerConfiguration.setHandler(handler); + + return this; + } + + public ContextHandlerConfigurationBuilder withCustomizer(Consumer<ContextHandler> contextHandlerCustomizer) { + contextHandlerConfiguration.customize(contextHandlerCustomizer); + return this; + } + + @Override + public JettyConfigurationBuilder build() { + jettyConfiguration.setContextHandlerConfiguration(contextHandlerConfiguration); + + return jettyConfigurationBuilder; + } + } + private JettyConfiguration jettyConfiguration = new JettyConfiguration(); private JettyConfigurationBuilder() { @@ -100,14 +174,16 @@ public final class JettyConfigurationBuilder { return new ServletConfigurationBuilder(this, jettyConfiguration); } - public JettyConfigurationBuilder withContextPath(String contextPath) { - jettyConfiguration.setContextPath(contextPath); + public WebAppContextConfigurationBuilder withWebAppContextConfiguration() { + return new WebAppContextConfigurationBuilder(this, jettyConfiguration); + } - return this; + public ContextHandlerConfigurationBuilder withContextHandlerConfiguration() { + return new ContextHandlerConfigurationBuilder(this, jettyConfiguration); } - public JettyConfigurationBuilder withWebAppContext(JettyConfiguration.WebContextConfiguration webContextConfiguration) { - jettyConfiguration.setWebContextConfiguration(webContextConfiguration); + public JettyConfigurationBuilder withContextPath(String contextPath) { + jettyConfiguration.setContextPath(contextPath); return this; }