You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ds...@apache.org on 2025/09/09 16:08:50 UTC
(tomcat) 03/08: Create a helper in TomcatBaseTest to injects a LifecycleListener to a Context of a Container that sends code ADD_CHILD_EVENT and utilize this helper in TestManagerWebapp.
This is an automated email from the ASF dual-hosted git repository.
dsoumis pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit 497e94d82cd3c2d917de08dc26eb7926401c1473
Author: Dimitris Soumis <ji...@gmail.com>
AuthorDate: Mon Sep 8 17:21:00 2025 +0300
Create a helper in TomcatBaseTest to injects a LifecycleListener to a Context of a Container that sends code ADD_CHILD_EVENT and utilize this helper in TestManagerWebapp.
---
.../apache/catalina/manager/TestManagerWebapp.java | 126 ++++++++++-----------
.../apache/catalina/startup/TomcatBaseTest.java | 48 ++++++++
2 files changed, 106 insertions(+), 68 deletions(-)
diff --git a/test/org/apache/catalina/manager/TestManagerWebapp.java b/test/org/apache/catalina/manager/TestManagerWebapp.java
index f3a11be8cf..693350a144 100644
--- a/test/org/apache/catalina/manager/TestManagerWebapp.java
+++ b/test/org/apache/catalina/manager/TestManagerWebapp.java
@@ -29,8 +29,6 @@ import org.junit.Assert;
import org.junit.Test;
import static org.apache.catalina.startup.SimpleHttpClient.CRLF;
-import org.apache.catalina.Container;
-import org.apache.catalina.ContainerListener;
import org.apache.catalina.Context;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleEvent;
@@ -532,74 +530,66 @@ public class TestManagerWebapp extends TomcatBaseTest {
HostConfig hostConfig = new HostConfig();
ctx.getParent().addLifecycleListener(hostConfig);
- ContainerListener containerListener = event -> {
- if (Container.ADD_CHILD_EVENT.equals(event.getType())) {
- Container child = (Container) event.getData();
- if (child instanceof Context c) {
- if ("/examples".equals(c.getPath())) {
- c.addLifecycleListener(new FailOnceListener());
- }
- }
- }
- };
- ctx.getParent().addContainerListener(containerListener);
-
- tomcat.start();
- SimpleHttpClient client = new SimpleHttpClient() {
- @Override
- public void connect() throws IOException {
- connect(30000, 30000);
- }
-
- @Override
- public boolean isResponseBodyOK() {
- return true;
- }
- };
-
- appDir = new File(webappDir, "examples");
- client.setPort(getPort());
- String basicHeader = (new BasicAuthHeader("Basic", "admin", "sekr3t")).getHeader().toString();
+ try (TomcatBaseTest.ContainerInjector ignored =
+ TomcatBaseTest.ContainerInjector.inject(ctx.getParent(), "/examples", new FailOnceListener())) {
- client.setRequest(new String[] {
- "GET /manager/text/deploy?war=" + URLEncoder.QUERY.encode(appDir.getAbsolutePath(), StandardCharsets.UTF_8) + " HTTP/1.1" + CRLF +
- "Host: localhost" + CRLF +
- "Authorization: " + basicHeader + CRLF +
- "Connection: Close" + CRLF + CRLF
- });
- client.connect();
- client.processRequest(true);
- Assert.assertEquals(HttpServletResponse.SC_OK, client.getStatusCode());
-
- client.setRequest(new String[] {
- "GET /examples/servlets/servlet/RequestInfoExample HTTP/1.1" + CRLF +
- "Host: localhost" + CRLF +
- "Connection: Close" + CRLF + CRLF
- });
- client.connect();
- client.processRequest(true);
- Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, client.getStatusCode());
-
- client.setRequest(new String[] {
- "GET /manager/text/start?path=/examples HTTP/1.1" + CRLF +
- "Host: localhost" + CRLF +
- "Authorization: " + basicHeader + CRLF +
- "Connection: Close" + CRLF + CRLF
- });
- client.connect();
- client.processRequest(true);
- Assert.assertEquals(HttpServletResponse.SC_OK, client.getStatusCode());
-
- client.setRequest(new String[] {
- "GET /examples/servlets/servlet/RequestInfoExample HTTP/1.1" + CRLF +
- "Host: localhost" + CRLF +
- "Connection: Close" + CRLF + CRLF
- });
- client.connect();
- client.processRequest(true);
- Assert.assertEquals(HttpServletResponse.SC_OK, client.getStatusCode());
+ tomcat.start();
+ SimpleHttpClient client = new SimpleHttpClient() {
+ @Override
+ public void connect() throws IOException {
+ connect(30000, 30000);
+ }
- tomcat.stop();
+ @Override
+ public boolean isResponseBodyOK() {
+ return true;
+ }
+ };
+
+ appDir = new File(webappDir, "examples");
+ client.setPort(getPort());
+ String basicHeader = (new BasicAuthHeader("Basic", "admin", "sekr3t")).getHeader().toString();
+
+ client.setRequest(new String[]{
+ "GET /manager/text/deploy?war=" + URLEncoder.QUERY.encode(appDir.getAbsolutePath(), StandardCharsets.UTF_8) + " HTTP/1.1" + CRLF +
+ "Host: localhost" + CRLF +
+ "Authorization: " + basicHeader + CRLF +
+ "Connection: Close" + CRLF + CRLF
+ });
+ client.connect();
+ client.processRequest(true);
+ Assert.assertEquals(HttpServletResponse.SC_OK, client.getStatusCode());
+
+ client.setRequest(new String[]{
+ "GET /examples/servlets/servlet/RequestInfoExample HTTP/1.1" + CRLF +
+ "Host: localhost" + CRLF +
+ "Connection: Close" + CRLF + CRLF
+ });
+ client.connect();
+ client.processRequest(true);
+ Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, client.getStatusCode());
+
+ client.setRequest(new String[]{
+ "GET /manager/text/start?path=/examples HTTP/1.1" + CRLF +
+ "Host: localhost" + CRLF +
+ "Authorization: " + basicHeader + CRLF +
+ "Connection: Close" + CRLF + CRLF
+ });
+ client.connect();
+ client.processRequest(true);
+ Assert.assertEquals(HttpServletResponse.SC_OK, client.getStatusCode());
+
+ client.setRequest(new String[]{
+ "GET /examples/servlets/servlet/RequestInfoExample HTTP/1.1" + CRLF +
+ "Host: localhost" + CRLF +
+ "Connection: Close" + CRLF + CRLF
+ });
+ client.connect();
+ client.processRequest(true);
+ Assert.assertEquals(HttpServletResponse.SC_OK, client.getStatusCode());
+
+ tomcat.stop();
+ }
}
private static class FailOnceListener implements LifecycleListener {
diff --git a/test/org/apache/catalina/startup/TomcatBaseTest.java b/test/org/apache/catalina/startup/TomcatBaseTest.java
index 37773d78b6..b04bfd8084 100644
--- a/test/org/apache/catalina/startup/TomcatBaseTest.java
+++ b/test/org/apache/catalina/startup/TomcatBaseTest.java
@@ -55,6 +55,8 @@ import org.junit.Assert;
import org.junit.Before;
import org.apache.catalina.Container;
+import org.apache.catalina.ContainerEvent;
+import org.apache.catalina.ContainerListener;
import org.apache.catalina.Context;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleEvent;
@@ -1093,4 +1095,50 @@ public abstract class TomcatBaseTest extends LoggingBaseTest {
int insertIndex = formatted.indexOf("XXX");
return (insertIndex == -1) ? formatted : formatted.substring(0, insertIndex);
}
+
+ /**
+ * Injects a {@link LifecycleListener} to a {@link Context} of a {@link Container} that sends {@code ADD_CHILD_EVENT}.
+ * Useful when deploying with the Manager / HostConfig.
+ */
+ //TODO: make it generic to set other fields in the context as well like Valve, xmlValidation etc
+ public static class ContainerInjector implements ContainerListener, AutoCloseable {
+
+ private final Container container;
+ private final String contextPathToTest;
+ private final LifecycleListener listener;
+ private volatile boolean installed = false;
+
+ private ContainerInjector(Container container,
+ String contextPathToTest,
+ LifecycleListener listener) {
+ this.container = container;
+ this.contextPathToTest = contextPathToTest;
+ this.listener = listener;
+ container.addContainerListener(this);
+ }
+
+ public static ContainerInjector inject(Container container,
+ String contextPathToTest,
+ LifecycleListener listener) {
+ return new ContainerInjector(container, contextPathToTest, listener);
+ }
+
+ @Override
+ public void containerEvent(ContainerEvent event) {
+ if (Container.ADD_CHILD_EVENT.equals(event.getType()) && !installed) {
+ Object data = event.getData();
+ if (data instanceof Context ctx) {
+ if (ctx.getPath().equals(contextPathToTest)) {
+ ctx.addLifecycleListener(listener);
+ installed = true;
+ }
+ }
+ }
+ }
+
+ @Override
+ public void close() {
+ container.removeContainerListener(this);
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org