diff --git a/runtime/test/src/test/java/com/google/apphosting/runtime/jetty9/ServletContextListenerTest.java b/runtime/test/src/test/java/com/google/apphosting/runtime/jetty9/ServletContextListenerTest.java new file mode 100644 index 00000000..5cf4d06e --- /dev/null +++ b/runtime/test/src/test/java/com/google/apphosting/runtime/jetty9/ServletContextListenerTest.java @@ -0,0 +1,123 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed 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 + * + * https://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 com.google.apphosting.runtime.jetty9; + +import org.eclipse.jetty.client.HttpClient; +import org.eclipse.jetty.client.api.ContentProvider; +import org.eclipse.jetty.client.api.ContentResponse; +import org.eclipse.jetty.client.api.Response; +import org.eclipse.jetty.client.api.Result; +import org.eclipse.jetty.client.util.ByteBufferContentProvider; +import org.eclipse.jetty.client.util.DeferredContentProvider; +import org.eclipse.jetty.client.util.InputStreamContentProvider; +import org.eclipse.jetty.http.HttpHeader; +import org.eclipse.jetty.http.HttpStatus; +import org.eclipse.jetty.util.BufferUtil; +import org.eclipse.jetty.util.Utf8StringBuilder; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.Collection; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; +import java.util.zip.GZIPOutputStream; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.lessThan; +import static org.hamcrest.Matchers.lessThanOrEqualTo; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +@RunWith(Parameterized.class) +public class ServletContextListenerTest extends JavaRuntimeViaHttpBase { + + @Parameterized.Parameters + public static Collection data() { + return Arrays.asList( + new Object[][] { + {"jetty94", false}, + {"ee8", false}, + {"ee10", false}, + {"ee8", true}, + {"ee10", true}, + }); + } + + @Rule public TemporaryFolder temp = new TemporaryFolder(); + private final HttpClient httpClient = new HttpClient(); + private final boolean httpMode; + private final String environment; + private RuntimeContext runtime; + + public ServletContextListenerTest(String environment, boolean httpMode) { + this.environment = environment; + this.httpMode = httpMode; + System.setProperty("appengine.use.HttpConnector", Boolean.toString(httpMode)); + } + + private RuntimeContext runtimeContext() throws Exception { + RuntimeContext.Config config = + RuntimeContext.Config.builder().setApplicationPath(temp.getRoot().toString()).build(); + return RuntimeContext.create(config); + } + + @Before + public void before() throws Exception { + String app = "com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/" + environment; + copyAppToDir(app, temp.getRoot().toPath()); + httpClient.start(); + runtime = runtimeContext(); + System.err.println("==== Using Environment: " + environment + " " + httpMode + " ===="); + } + + @After + public void after() throws Exception + { + httpClient.stop(); + runtime.close(); + } + + @Test + public void testServletContextListener() throws Exception { + String url = runtime.jettyUrl("/"); + CompletableFuture completionListener = new CompletableFuture<>(); + Utf8StringBuilder contentReceived = new Utf8StringBuilder(); + httpClient.newRequest(url).onResponseContentAsync((response, content, callback) -> { + contentReceived.append(content); + callback.succeeded(); + }).send(completionListener::complete); + + Result result = completionListener.get(5, TimeUnit.SECONDS); + assertThat(result.getResponse().getStatus(), equalTo(HttpStatus.OK_200)); + assertThat(contentReceived.toString(), equalTo("ServletContextListenerAttribute: true")); + } +} diff --git a/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/EE10MainServlet.java b/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/EE10MainServlet.java new file mode 100644 index 00000000..5ad318d1 --- /dev/null +++ b/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/EE10MainServlet.java @@ -0,0 +1,31 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed 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 + * + * https://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 com.google.apphosting.runtime.jetty9.servletcontextlistenerapp; + +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; + +public class EE10MainServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + Object listenerAttribute = req.getServletContext().getAttribute("ServletContextListenerAttribute"); + resp.getWriter().write("ServletContextListenerAttribute: " + listenerAttribute); + } +} diff --git a/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/EE10ServletContextListener.java b/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/EE10ServletContextListener.java new file mode 100644 index 00000000..e461686b --- /dev/null +++ b/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/EE10ServletContextListener.java @@ -0,0 +1,27 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed 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 + * + * https://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 com.google.apphosting.runtime.jetty9.servletcontextlistenerapp; + +import jakarta.servlet.ServletContextEvent; +import jakarta.servlet.ServletContextListener; + +public class EE10ServletContextListener implements ServletContextListener { + @Override + public void contextInitialized(ServletContextEvent sce) { + sce.getServletContext().setAttribute("ServletContextListenerAttribute", "true"); + } +} diff --git a/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/EE8MainServlet.java b/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/EE8MainServlet.java new file mode 100644 index 00000000..a5b3d67e --- /dev/null +++ b/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/EE8MainServlet.java @@ -0,0 +1,31 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed 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 + * + * https://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 com.google.apphosting.runtime.jetty9.servletcontextlistenerapp; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +public class EE8MainServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + Object listenerAttribute = req.getServletContext().getAttribute("ServletContextListenerAttribute"); + resp.getWriter().write("ServletContextListenerAttribute: " + listenerAttribute); + } +} diff --git a/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/EE8ServletContextListener.java b/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/EE8ServletContextListener.java new file mode 100644 index 00000000..18119809 --- /dev/null +++ b/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/EE8ServletContextListener.java @@ -0,0 +1,31 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed 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 + * + * https://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 com.google.apphosting.runtime.jetty9.servletcontextlistenerapp; + +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +public class EE8ServletContextListener implements ServletContextListener { + @Override + public void contextInitialized(ServletContextEvent sce) { + sce.getServletContext().setAttribute("ServletContextListenerAttribute", "true"); + } + + @Override + public void contextDestroyed(ServletContextEvent sce) { + } +} diff --git a/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/ee10/WEB-INF/appengine-web.xml b/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/ee10/WEB-INF/appengine-web.xml new file mode 100644 index 00000000..c9c48291 --- /dev/null +++ b/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/ee10/WEB-INF/appengine-web.xml @@ -0,0 +1,27 @@ + + + + + java21 + sizelimithandler + 1 + true + + + + + diff --git a/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/ee10/WEB-INF/web.xml b/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/ee10/WEB-INF/web.xml new file mode 100644 index 00000000..ba735b3f --- /dev/null +++ b/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/ee10/WEB-INF/web.xml @@ -0,0 +1,33 @@ + + + + + com.google.apphosting.runtime.jetty9.servletcontextlistenerapp.EE10ServletContextListener + + + + MainServlet + com.google.apphosting.runtime.jetty9.servletcontextlistenerapp.EE10MainServlet + + + MainServlet + + + diff --git a/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/ee8/WEB-INF/appengine-web.xml b/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/ee8/WEB-INF/appengine-web.xml new file mode 100644 index 00000000..56f5a3ff --- /dev/null +++ b/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/ee8/WEB-INF/appengine-web.xml @@ -0,0 +1,27 @@ + + + + + java21 + sizelimithandler + 1 + true + + + + + diff --git a/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/ee8/WEB-INF/web.xml b/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/ee8/WEB-INF/web.xml new file mode 100644 index 00000000..7a9402ac --- /dev/null +++ b/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/ee8/WEB-INF/web.xml @@ -0,0 +1,33 @@ + + + + + com.google.apphosting.runtime.jetty9.servletcontextlistenerapp.EE8ServletContextListener + + + + MainServlet + com.google.apphosting.runtime.jetty9.servletcontextlistenerapp.EE8MainServlet + + + MainServlet + + + diff --git a/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/jetty94/WEB-INF/appengine-web.xml b/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/jetty94/WEB-INF/appengine-web.xml new file mode 100644 index 00000000..4503849a --- /dev/null +++ b/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/jetty94/WEB-INF/appengine-web.xml @@ -0,0 +1,27 @@ + + + + + java21 + sizelimithandler + 1 + true + + + + + diff --git a/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/jetty94/WEB-INF/web.xml b/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/jetty94/WEB-INF/web.xml new file mode 100644 index 00000000..7a9402ac --- /dev/null +++ b/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/servletcontextlistenerapp/jetty94/WEB-INF/web.xml @@ -0,0 +1,33 @@ + + + + + com.google.apphosting.runtime.jetty9.servletcontextlistenerapp.EE8ServletContextListener + + + + MainServlet + com.google.apphosting.runtime.jetty9.servletcontextlistenerapp.EE8MainServlet + + + MainServlet + + +