From 73864862c13d3466e02c9d041bab0b40d9e8c8cf Mon Sep 17 00:00:00 2001 From: Manfred Riem Date: Thu, 4 Jan 2024 17:01:35 -0600 Subject: [PATCH] Fixes #3604 - Merge core/tests into core/impl (#3605) --- core/impl/pom.xml | 6 - .../piranha/core/impl/AsyncContextTest.java | 287 +++++++++++++- .../HttpSessionAttributeListenerTest.java | 22 +- .../core/impl/HttpSessionIdListenerTest.java | 55 ++- .../core/impl}/HttpSessionListenerTest.java | 42 +-- .../core/impl/HttpSesssionListenerTest.java | 58 --- .../piranha/core/impl/ReadListenerTest.java | 134 ++++++- .../core/impl/RequestDispatcherTest.java | 296 ++++++++++++++- .../ServletContextAttributeListenerTest.java | 132 ++++++- .../core/impl/ServletInputStreamTest.java | 93 ++++- .../core/impl/ServletRegistrationTest.java | 154 +++++++- .../ServletRequestAttributeListenerTest.java | 123 +++++- .../piranha/core/impl/WriteListenerTest.java | 70 +++- core/pom.xml | 1 - core/tests/pom.xml | 36 -- .../piranha/core/tests/AsyncContextTest.java | 343 ----------------- .../core/tests/HttpSessionIdListenerTest.java | 109 ------ .../piranha/core/tests/ReadListenerTest.java | 188 ---------- .../core/tests/RequestDispatcherTest.java | 351 ------------------ .../ServletContextAttributeListenerTest.java | 180 --------- .../core/tests/ServletInputStreamTest.java | 141 ------- .../core/tests/ServletRegistrationTest.java | 192 ---------- .../ServletRequestAttributeListenerTest.java | 172 --------- .../piranha/core/tests/WriteListenerTest.java | 118 ------ core/tests/src/main/java/module-info.java | 42 --- 25 files changed, 1303 insertions(+), 2042 deletions(-) rename core/{tests/src/main/java/cloud/piranha/core/tests => impl/src/test/java/cloud/piranha/core/impl}/HttpSessionAttributeListenerTest.java (94%) rename core/{tests/src/main/java/cloud/piranha/core/tests => impl/src/test/java/cloud/piranha/core/impl}/HttpSessionListenerTest.java (86%) delete mode 100644 core/impl/src/test/java/cloud/piranha/core/impl/HttpSesssionListenerTest.java delete mode 100644 core/tests/pom.xml delete mode 100644 core/tests/src/main/java/cloud/piranha/core/tests/AsyncContextTest.java delete mode 100644 core/tests/src/main/java/cloud/piranha/core/tests/HttpSessionIdListenerTest.java delete mode 100644 core/tests/src/main/java/cloud/piranha/core/tests/ReadListenerTest.java delete mode 100644 core/tests/src/main/java/cloud/piranha/core/tests/RequestDispatcherTest.java delete mode 100644 core/tests/src/main/java/cloud/piranha/core/tests/ServletContextAttributeListenerTest.java delete mode 100644 core/tests/src/main/java/cloud/piranha/core/tests/ServletInputStreamTest.java delete mode 100644 core/tests/src/main/java/cloud/piranha/core/tests/ServletRegistrationTest.java delete mode 100644 core/tests/src/main/java/cloud/piranha/core/tests/ServletRequestAttributeListenerTest.java delete mode 100644 core/tests/src/main/java/cloud/piranha/core/tests/WriteListenerTest.java delete mode 100644 core/tests/src/main/java/module-info.java diff --git a/core/impl/pom.xml b/core/impl/pom.xml index 242c8fc7e9..bba55e7296 100644 --- a/core/impl/pom.xml +++ b/core/impl/pom.xml @@ -40,12 +40,6 @@ compile - - cloud.piranha.core - piranha-core-tests - ${project.version} - test - cloud.piranha.http piranha-http-impl diff --git a/core/impl/src/test/java/cloud/piranha/core/impl/AsyncContextTest.java b/core/impl/src/test/java/cloud/piranha/core/impl/AsyncContextTest.java index 57744e1c15..217b2e3487 100644 --- a/core/impl/src/test/java/cloud/piranha/core/impl/AsyncContextTest.java +++ b/core/impl/src/test/java/cloud/piranha/core/impl/AsyncContextTest.java @@ -31,26 +31,299 @@ import cloud.piranha.core.api.WebApplication; import cloud.piranha.core.api.WebApplicationRequest; import cloud.piranha.core.api.WebApplicationResponse; +import jakarta.servlet.AsyncContext; +import jakarta.servlet.AsyncEvent; +import jakarta.servlet.AsyncListener; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRegistration; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Test; /** * The JUnit tests for testing everything related to the AsyncContext API. * * @author Manfred Riem (mriem@manorrock.com) */ -class AsyncContextTest extends cloud.piranha.core.tests.AsyncContextTest { +class AsyncContextTest { - @Override - public WebApplication createWebApplication() { + private WebApplication createWebApplication() { return new DefaultWebApplication(); } - @Override - public WebApplicationRequest createWebApplicationRequest() { + private WebApplicationRequest createWebApplicationRequest() { return new DefaultWebApplicationRequest(); } - @Override - public WebApplicationResponse createWebApplicationResponse() { + private WebApplicationResponse createWebApplicationResponse() { return new DefaultWebApplicationResponse(); } + + /** + * Test addListener method. + */ + @Test + void testAddListener() { + WebApplication webApplication = createWebApplication(); + WebApplicationRequest request = createWebApplicationRequest(); + request.setWebApplication(webApplication); + request.setAsyncSupported(true); + WebApplicationResponse response = createWebApplicationResponse(); + response.setWebApplication(webApplication); + webApplication.linkRequestAndResponse(request, response); + AsyncContext context = request.startAsync(); + context.addListener(new AsyncListener() { + @Override + public void onComplete(AsyncEvent event) throws IOException { + event.getSuppliedRequest().getServletContext().setAttribute("onComplete", "true"); + } + + @Override + public void onTimeout(AsyncEvent event) throws IOException { + } + + @Override + public void onError(AsyncEvent event) throws IOException { + } + + @Override + public void onStartAsync(AsyncEvent event) throws IOException { + } + }); + context.complete(); + assertNotNull(webApplication.getAttribute("onComplete")); + } + + /** + * Test complete method. + */ + @Test + void testComplete() { + WebApplication webApplication = createWebApplication(); + WebApplicationRequest request = createWebApplicationRequest(); + request.setWebApplication(webApplication); + request.setAsyncSupported(true); + WebApplicationResponse response = createWebApplicationResponse(); + response.setWebApplication(webApplication); + webApplication.linkRequestAndResponse(request, response); + AsyncContext context = request.startAsync(); + context.complete(); + assertTrue(response.isCommitted()); + } + + /** + * Test createListener method. + * + * @throws ServletException when a serious error occurs. + */ + @Test + void testCreateListener() throws ServletException { + WebApplication webApplication = createWebApplication(); + WebApplicationRequest request = createWebApplicationRequest(); + request.setWebApplication(webApplication); + request.setAsyncSupported(true); + WebApplicationResponse response = createWebApplicationResponse(); + response.setWebApplication(webApplication); + webApplication.linkRequestAndResponse(request, response); + AsyncContext context = request.startAsync(); + assertNotNull(context.createListener(TestCreateListenerListener.class)); + } + + /** + * Test createListener method. + * + * @throws ServletException when a serious error occurs. + */ + @Test + void testCreateListener2() throws ServletException { + WebApplication webApplication = createWebApplication(); + WebApplicationRequest request = createWebApplicationRequest(); + request.setWebApplication(webApplication); + request.setAsyncSupported(true); + WebApplicationResponse response = createWebApplicationResponse(); + response.setWebApplication(webApplication); + webApplication.linkRequestAndResponse(request, response); + AsyncContext context = request.startAsync(); + assertThrows(ServletException.class, + () -> { + context.createListener(TestCreateListener2Listener.class); + }); + } + + /** + * Test dispatch method. + */ + @Test + void testDispatch() { + WebApplication webApplication = createWebApplication(); + ServletRegistration.Dynamic registration = webApplication.addServlet("TestDispatchServlet", new HttpServlet() { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + AsyncContext context = request.startAsync(); + context.dispatch("/testDispatchB"); + context.dispatch("/testDispatchC"); + } + }); + registration.setAsyncSupported(true); + webApplication.addServletMapping("TestDispatchServlet", "/testDispatch"); + webApplication.initialize(); + webApplication.start(); + WebApplicationRequest request = createWebApplicationRequest(); + request.setServletPath("/testDispatch"); + request.setWebApplication(webApplication); + request.setAsyncSupported(true); + WebApplicationResponse response = createWebApplicationResponse(); + response.setWebApplication(webApplication); + webApplication.linkRequestAndResponse(request, response); + assertThrows(IllegalStateException.class, + () -> webApplication.service(request, response)); + } + + /** + * Test getRequest method. + */ + @Test + void testGetRequest() { + WebApplication webApplication = createWebApplication(); + WebApplicationRequest request = createWebApplicationRequest(); + request.setWebApplication(webApplication); + request.setAsyncSupported(true); + WebApplicationResponse response = createWebApplicationResponse(); + response.setWebApplication(webApplication); + webApplication.linkRequestAndResponse(request, response); + AsyncContext context = request.startAsync(); + assertNotNull(context.getRequest()); + } + + /** + * Test getResponse method. + */ + @Test + void testGetResponse() { + WebApplication webApplication = createWebApplication(); + WebApplicationRequest request = createWebApplicationRequest(); + request.setWebApplication(webApplication); + request.setAsyncSupported(true); + WebApplicationResponse response = createWebApplicationResponse(); + response.setWebApplication(webApplication); + webApplication.linkRequestAndResponse(request, response); + AsyncContext context = request.startAsync(); + assertNotNull(context.getResponse()); + } + + /** + * Test getTimeout method. + */ + @Test + void testGetTimeout() { + WebApplication webApplication = createWebApplication(); + WebApplicationRequest request = createWebApplicationRequest(); + request.setWebApplication(webApplication); + request.setAsyncSupported(true); + WebApplicationResponse response = createWebApplicationResponse(); + response.setWebApplication(webApplication); + webApplication.linkRequestAndResponse(request, response); + AsyncContext context = request.startAsync(); + assertEquals(30000, context.getTimeout()); + } + + /** + * Test hasOriginalRequestAndResponse method. + */ + @Test + void testHasOriginalRequestAndResponse() { + WebApplication webApplication = createWebApplication(); + WebApplicationRequest request = createWebApplicationRequest(); + request.setWebApplication(webApplication); + request.setAsyncSupported(true); + WebApplicationResponse response = createWebApplicationResponse(); + response.setWebApplication(webApplication); + webApplication.linkRequestAndResponse(request, response); + AsyncContext context = request.startAsync(); + assertTrue(context.hasOriginalRequestAndResponse()); + assertEquals(request, context.getRequest()); + assertEquals(response, context.getResponse()); + } + + /** + * Test setTimeout method. + */ + @Test + void testSetTimeout() { + WebApplication webApplication = createWebApplication(); + WebApplicationRequest request = createWebApplicationRequest(); + request.setWebApplication(webApplication); + request.setAsyncSupported(true); + WebApplicationResponse response = createWebApplicationResponse(); + response.setWebApplication(webApplication); + webApplication.linkRequestAndResponse(request, response); + AsyncContext context = request.startAsync(); + context.setTimeout(60000); + assertEquals(60000, context.getTimeout()); + } + + /** + * Listener that is used by testCreateListener. + */ + public static class TestCreateListenerListener implements AsyncListener { + + /** + * Default constructor. + */ + public TestCreateListenerListener() { + } + + @Override + public void onComplete(AsyncEvent event) throws IOException { + } + + @Override + public void onTimeout(AsyncEvent event) throws IOException { + } + + @Override + public void onError(AsyncEvent event) throws IOException { + } + + @Override + public void onStartAsync(AsyncEvent event) throws IOException { + } + } + + /** + * Listener that is used by testCreateListener2. + */ + public static class TestCreateListener2Listener implements AsyncListener { + + /** + * Constructor. + * + * @throws ServletException when constructor is called. + */ + public TestCreateListener2Listener() throws ServletException { + throw new ServletException(); + } + + @Override + public void onComplete(AsyncEvent event) throws IOException { + } + + @Override + public void onTimeout(AsyncEvent event) throws IOException { + } + + @Override + public void onError(AsyncEvent event) throws IOException { + } + + @Override + public void onStartAsync(AsyncEvent event) throws IOException { + } + } } diff --git a/core/tests/src/main/java/cloud/piranha/core/tests/HttpSessionAttributeListenerTest.java b/core/impl/src/test/java/cloud/piranha/core/impl/HttpSessionAttributeListenerTest.java similarity index 94% rename from core/tests/src/main/java/cloud/piranha/core/tests/HttpSessionAttributeListenerTest.java rename to core/impl/src/test/java/cloud/piranha/core/impl/HttpSessionAttributeListenerTest.java index 2cc57d4530..242427d982 100644 --- a/core/tests/src/main/java/cloud/piranha/core/tests/HttpSessionAttributeListenerTest.java +++ b/core/impl/src/test/java/cloud/piranha/core/impl/HttpSessionAttributeListenerTest.java @@ -25,7 +25,7 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ -package cloud.piranha.core.tests; +package cloud.piranha.core.impl; import cloud.piranha.core.api.WebApplication; import cloud.piranha.core.api.WebApplicationRequest; @@ -45,34 +45,34 @@ * * @author Manfred Riem (mriem@manorrock.com) */ -public abstract class HttpSessionAttributeListenerTest { +class HttpSessionAttributeListenerTest { - /** - * Default constructor. - */ - public HttpSessionAttributeListenerTest() { - } - /** * Create the web application. * * @return the web application. */ - protected abstract WebApplication createWebApplication(); + private WebApplication createWebApplication() { + return new DefaultWebApplication(); + } /** * Create the web application request. * * @return the web application request. */ - protected abstract WebApplicationRequest createWebApplicationRequest(); + private WebApplicationRequest createWebApplicationRequest() { + return new DefaultWebApplicationRequest(); + } /** * Create the web application response. * * @return the web application response. */ - protected abstract WebApplicationResponse createWebApplicationResponse(); + private WebApplicationResponse createWebApplicationResponse() { + return new DefaultWebApplicationResponse(); + } /** * Test attributeAdded method. diff --git a/core/impl/src/test/java/cloud/piranha/core/impl/HttpSessionIdListenerTest.java b/core/impl/src/test/java/cloud/piranha/core/impl/HttpSessionIdListenerTest.java index 0cc3bfcef8..1c1528ca50 100644 --- a/core/impl/src/test/java/cloud/piranha/core/impl/HttpSessionIdListenerTest.java +++ b/core/impl/src/test/java/cloud/piranha/core/impl/HttpSessionIdListenerTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002-2023 Manorrock.com. All Rights Reserved. + * Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -30,29 +30,68 @@ import cloud.piranha.core.api.WebApplication; import cloud.piranha.core.api.WebApplicationRequest; import cloud.piranha.core.api.WebApplicationResponse; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSessionEvent; +import jakarta.servlet.http.HttpSessionIdListener; import java.io.ByteArrayOutputStream; +import java.io.IOException; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import org.junit.jupiter.api.Test; /** * The JUnit tests for the HttpSessionIdListener API. * * @author Manfred Riem (mriem@manorrock.com) */ -class HttpSessionSessionIdListenerTest extends cloud.piranha.core.tests.HttpSessionIdListenerTest { +public class HttpSessionIdListenerTest { - @Override - protected WebApplication createWebApplication() { + private WebApplication createWebApplication() { return new DefaultWebApplication(); } - @Override - protected WebApplicationRequest createWebApplicationRequest() { + private WebApplicationRequest createWebApplicationRequest() { return new DefaultWebApplicationRequest(); } - @Override - protected WebApplicationResponse createWebApplicationResponse() { + private WebApplicationResponse createWebApplicationResponse() { DefaultWebApplicationResponse response = new DefaultWebApplicationResponse(); response.getWebApplicationOutputStream().setOutputStream(new ByteArrayOutputStream()); return response; } + + /** + * Test sessionIdChanged method. + * + * @throws Exception when a serious error occurs. + */ + @Test + void testSessionIdChanged() throws Exception { + WebApplication webApplication = createWebApplication(); + webApplication.addListener(new HttpSessionIdListener() { + @Override + public void sessionIdChanged(HttpSessionEvent event, String oldSessionId) { + event.getSession().getServletContext().setAttribute("testSessionIdChanged", true); + } + }); + webApplication.addServlet("TestSessionIdChangedServlet", new HttpServlet() { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.getSession(); + request.changeSessionId(); + } + }); + webApplication.addServletMapping("TestSessionIdChangedServlet", "/testSessionIdChangedServlet"); + webApplication.initialize(); + webApplication.start(); + WebApplicationRequest request = createWebApplicationRequest(); + request.setServletPath("/testSessionIdChangedServlet"); + request.setWebApplication(webApplication); + WebApplicationResponse response = createWebApplicationResponse(); + response.setWebApplication(webApplication); + webApplication.service(request, response); + assertNotNull(webApplication.getAttribute("testSessionIdChanged")); + } } diff --git a/core/tests/src/main/java/cloud/piranha/core/tests/HttpSessionListenerTest.java b/core/impl/src/test/java/cloud/piranha/core/impl/HttpSessionListenerTest.java similarity index 86% rename from core/tests/src/main/java/cloud/piranha/core/tests/HttpSessionListenerTest.java rename to core/impl/src/test/java/cloud/piranha/core/impl/HttpSessionListenerTest.java index 74142f9062..9e535b1936 100644 --- a/core/tests/src/main/java/cloud/piranha/core/tests/HttpSessionListenerTest.java +++ b/core/impl/src/test/java/cloud/piranha/core/impl/HttpSessionListenerTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002-2023 Manorrock.com. All Rights Reserved. + * Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -25,7 +25,7 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ -package cloud.piranha.core.tests; +package cloud.piranha.core.impl; import cloud.piranha.core.api.WebApplication; import cloud.piranha.core.api.WebApplicationRequest; @@ -36,43 +36,31 @@ import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpSessionEvent; import jakarta.servlet.http.HttpSessionListener; +import java.io.ByteArrayOutputStream; import java.io.IOException; import static org.junit.jupiter.api.Assertions.assertNotNull; import org.junit.jupiter.api.Test; /** - * The JUnit tests for the HttpSessionListenerTest API. + * The JUnit tests for the HttpSessionListener API. * * @author Manfred Riem (mriem@manorrock.com) */ -public abstract class HttpSessionListenerTest { +public class HttpSessionListenerTest { - /** - * Default constructor. - */ - public HttpSessionListenerTest() { + private WebApplication createWebApplication() { + return new DefaultWebApplication(); } - /** - * Create the web application. - * - * @return the web application. - */ - protected abstract WebApplication createWebApplication(); - - /** - * Create the web application request. - * - * @return the web application request. - */ - protected abstract WebApplicationRequest createWebApplicationRequest(); + private WebApplicationRequest createWebApplicationRequest() { + return new DefaultWebApplicationRequest(); + } - /** - * Create the web application response. - * - * @return the web application response. - */ - protected abstract WebApplicationResponse createWebApplicationResponse(); + private WebApplicationResponse createWebApplicationResponse() { + DefaultWebApplicationResponse response = new DefaultWebApplicationResponse(); + response.getWebApplicationOutputStream().setOutputStream(new ByteArrayOutputStream()); + return response; + } /** * Test sessionCreated method. diff --git a/core/impl/src/test/java/cloud/piranha/core/impl/HttpSesssionListenerTest.java b/core/impl/src/test/java/cloud/piranha/core/impl/HttpSesssionListenerTest.java deleted file mode 100644 index 5c679584d9..0000000000 --- a/core/impl/src/test/java/cloud/piranha/core/impl/HttpSesssionListenerTest.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (c) 2002-2023 Manorrock.com. All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package cloud.piranha.core.impl; - -import cloud.piranha.core.api.WebApplication; -import cloud.piranha.core.api.WebApplicationRequest; -import cloud.piranha.core.api.WebApplicationResponse; -import java.io.ByteArrayOutputStream; - -/** - * The JUnit tests for the HttpSessionListener API. - * - * @author Manfred Riem (mriem@manorrock.com) - */ -class HttpSessionListenerTest extends cloud.piranha.core.tests.HttpSessionListenerTest { - - @Override - protected WebApplication createWebApplication() { - return new DefaultWebApplication(); - } - - @Override - protected WebApplicationRequest createWebApplicationRequest() { - return new DefaultWebApplicationRequest(); - } - - @Override - protected WebApplicationResponse createWebApplicationResponse() { - DefaultWebApplicationResponse response = new DefaultWebApplicationResponse(); - response.getWebApplicationOutputStream().setOutputStream(new ByteArrayOutputStream()); - return response; - } -} diff --git a/core/impl/src/test/java/cloud/piranha/core/impl/ReadListenerTest.java b/core/impl/src/test/java/cloud/piranha/core/impl/ReadListenerTest.java index 6c9d4eb6a7..be854813cd 100644 --- a/core/impl/src/test/java/cloud/piranha/core/impl/ReadListenerTest.java +++ b/core/impl/src/test/java/cloud/piranha/core/impl/ReadListenerTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002-2023 Manorrock.com. All Rights Reserved. + * Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -30,26 +30,144 @@ import cloud.piranha.core.api.WebApplication; import cloud.piranha.core.api.WebApplicationRequest; import cloud.piranha.core.api.WebApplicationResponse; +import jakarta.servlet.ReadListener; +import jakarta.servlet.ServletInputStream; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import org.junit.jupiter.api.Test; /** * The JUnit tests for the ReadListener API. * * @author Manfred Riem (mriem@manorrock.com) */ -public class ReadListenerTest extends cloud.piranha.core.tests.ReadListenerTest { +public class ReadListenerTest { - @Override - public WebApplication createWebApplication() { + private WebApplication createWebApplication() { return new DefaultWebApplication(); } - @Override - public WebApplicationRequest createWebApplicationRequest() { + private WebApplicationRequest createWebApplicationRequest() { return new DefaultWebApplicationRequest(); } - @Override - public WebApplicationResponse createWebApplicationResponse() { + private WebApplicationResponse createWebApplicationResponse() { return new DefaultWebApplicationResponse(); } + + /** + * Test onAllDataRead method. + * + * @throws Exception when a serious error occurs. + */ + @Test + void testOnAllDataRead() throws Exception { + WebApplication webApplication = createWebApplication(); + WebApplicationRequest request = createWebApplicationRequest(); + request.setAsyncSupported(true); + request.setWebApplication(webApplication); + WebApplicationResponse response = createWebApplicationResponse(); + response.setWebApplication(webApplication); + webApplication.linkRequestAndResponse(request, response); + request.startAsync(); + try (ServletInputStream inputStream = request.getInputStream()) { + request.getWebApplicationInputStream().setInputStream(new ByteArrayInputStream("read this".getBytes())); + inputStream.setReadListener(new TestOnAllDataReadReadListener(webApplication)); + int character = inputStream.read(); + while(character != -1) { + character = inputStream.read(); + } + } + Thread.sleep(2000); + assertNotNull(webApplication.getAttribute("onAllDataRead")); + } + + /** + * Test onDataAvailable method. + * + * @throws Exception when a serious error occurs. + */ + @Test + void testOnDataAvailable() throws Exception { + WebApplication webApplication = createWebApplication(); + WebApplicationRequest request = createWebApplicationRequest(); + request.setAsyncSupported(true); + request.setWebApplication(webApplication); + WebApplicationResponse response = createWebApplicationResponse(); + response.setWebApplication(webApplication); + webApplication.linkRequestAndResponse(request, response); + request.startAsync(); + ServletInputStream inputStream = request.getInputStream(); + request.getWebApplicationInputStream().setInputStream(new ByteArrayInputStream("a".getBytes())); + inputStream.setReadListener(new TestOnDataAvailableReadListener(webApplication)); + Thread.sleep(5000); + assertNotNull(webApplication.getAttribute("onDataAvailable")); + } + + /** + * A test ReadListener for onAllDataRead. + */ + public static class TestOnAllDataReadReadListener implements ReadListener { + + /** + * Stores the web application. + */ + private final WebApplication webApplication; + + /** + * Constructor. + * + * @param webApplication the web application. + */ + public TestOnAllDataReadReadListener(WebApplication webApplication) { + this.webApplication = webApplication; + } + + @Override + public void onAllDataRead() throws IOException { + webApplication.setAttribute("onAllDataRead", true); + } + + @Override + public void onDataAvailable() throws IOException { + } + + @Override + public void onError(Throwable t) { + } + } + + /** + * A test ReadListener for onDataAvailable. + */ + public static class TestOnDataAvailableReadListener implements ReadListener { + + /** + * Stores the web application. + */ + private final WebApplication webApplication; + + /** + * Constructor. + * + * @param webApplication the web application. + */ + public TestOnDataAvailableReadListener(WebApplication webApplication) { + this.webApplication = webApplication; + } + + @Override + public void onAllDataRead() throws IOException { + } + + @Override + public void onDataAvailable() throws IOException { + webApplication.setAttribute("onDataAvailable", true); + } + + @Override + public void onError(Throwable t) { + } + } } diff --git a/core/impl/src/test/java/cloud/piranha/core/impl/RequestDispatcherTest.java b/core/impl/src/test/java/cloud/piranha/core/impl/RequestDispatcherTest.java index 447ec0220c..d2f321310f 100644 --- a/core/impl/src/test/java/cloud/piranha/core/impl/RequestDispatcherTest.java +++ b/core/impl/src/test/java/cloud/piranha/core/impl/RequestDispatcherTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002-2023 Manorrock.com. All Rights Reserved. + * Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -30,29 +30,309 @@ import cloud.piranha.core.api.WebApplication; import cloud.piranha.core.api.WebApplicationRequest; import cloud.piranha.core.api.WebApplicationResponse; +import jakarta.servlet.RequestDispatcher; +import static jakarta.servlet.RequestDispatcher.FORWARD_CONTEXT_PATH; +import static jakarta.servlet.RequestDispatcher.FORWARD_MAPPING; +import static jakarta.servlet.RequestDispatcher.FORWARD_PATH_INFO; +import static jakarta.servlet.RequestDispatcher.FORWARD_QUERY_STRING; +import static jakarta.servlet.RequestDispatcher.FORWARD_REQUEST_URI; +import static jakarta.servlet.RequestDispatcher.FORWARD_SERVLET_PATH; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintWriter; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; +import org.junit.jupiter.api.Test; /** * The JUnit tests for the RequestDispatcher API. * * @author Manfred Riem (mriem@manorrock.com) */ -class RequestDispatcherTest extends cloud.piranha.core.tests.RequestDispatcherTest { +class RequestDispatcherTest { - @Override - protected WebApplication createWebApplication() { + private WebApplication createWebApplication() { return new DefaultWebApplication(); } - @Override - protected WebApplicationRequest createWebApplicationRequest() { + private WebApplicationRequest createWebApplicationRequest() { return new DefaultWebApplicationRequest(); } - @Override - protected WebApplicationResponse createWebApplicationResponse() { + private WebApplicationResponse createWebApplicationResponse() { DefaultWebApplicationResponse response = new DefaultWebApplicationResponse(); response.getWebApplicationOutputStream().setOutputStream(new ByteArrayOutputStream()); return response; } + + /** + * Test forward method on a named RequestDispatcher. + * + * @jakarta.assertion Servlet:SPEC:181 + * @jakarta.assertion Servlet:SPEC:181.1 + * @jakarta.assertion Servlet:SPEC:181.2 + * @jakarta.assertion Servlet:SPEC:181.3 + * @jakarta.assertion Servlet:SPEC:181.4 + * @jakarta.assertion Servlet:SPEC:181.5 + * @throws Exception when a serious error occurs. + */ + @Test + void testNamedForward() throws Exception { + WebApplication webApplication = createWebApplication(); + webApplication.addServlet("TestNamedForwardServlet", new HttpServlet() { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + PrintWriter writer = response.getWriter(); + if (request.getAttribute(FORWARD_CONTEXT_PATH) != null) { + writer.println(request.getAttribute(FORWARD_CONTEXT_PATH)); + } + if (request.getAttribute(FORWARD_MAPPING) != null) { + writer.println(request.getAttribute(FORWARD_CONTEXT_PATH)); + } + if (request.getAttribute(FORWARD_PATH_INFO) != null) { + writer.println(request.getAttribute(FORWARD_PATH_INFO)); + } + if (request.getAttribute(FORWARD_QUERY_STRING) != null) { + writer.println(request.getAttribute(FORWARD_QUERY_STRING)); + } + if (request.getAttribute(FORWARD_REQUEST_URI) != null) { + writer.println(request.getAttribute(FORWARD_REQUEST_URI)); + } + if (request.getAttribute(FORWARD_SERVLET_PATH) != null) { + writer.println(request.getAttribute(FORWARD_SERVLET_PATH)); + } + writer.flush(); + } + }); + webApplication.initialize(); + webApplication.start(); + WebApplicationRequest request = createWebApplicationRequest(); + request.setWebApplication(webApplication); + WebApplicationResponse response = createWebApplicationResponse(); + response.setWebApplication(webApplication); + response.setBodyOnly(true); + ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); + response.getWebApplicationOutputStream().setOutputStream(byteOutput); + webApplication.linkRequestAndResponse(request, response); + RequestDispatcher dispatcher = webApplication.getNamedDispatcher("TestNamedForwardServlet"); + dispatcher.forward(request, response); + assertEquals("", new String(byteOutput.toByteArray())); + } + + /** + * Test forward method on a named RequestDispatcher. + * + * @jakarta.assertion Servlet:SPEC:79 + * @throws Exception when a serious error occurs. + */ + @Test + void testNamedForward2() throws Exception { + WebApplication webApplication = createWebApplication(); + webApplication.addServlet("TestNamedForward2Servlet", new HttpServlet() { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + PrintWriter writer = response.getWriter(); + writer.print(request.getRequestURI()); + writer.flush(); + } + }); + webApplication.initialize(); + webApplication.start(); + WebApplicationRequest request = createWebApplicationRequest(); + request.setWebApplication(webApplication); + WebApplicationResponse response = createWebApplicationResponse(); + response.setWebApplication(webApplication); + response.setBodyOnly(true); + ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); + response.getWebApplicationOutputStream().setOutputStream(byteOutput); + webApplication.linkRequestAndResponse(request, response); + RequestDispatcher dispatcher = webApplication.getNamedDispatcher("TestNamedForward2Servlet"); + dispatcher.forward(request, response); + assertEquals("/", new String(byteOutput.toByteArray())); + } + + /** + * Test forward method on a named RequestDispatcher. + * + * @jakarta.assertion Servlet:SPEC:80 + * @throws Exception when a serious error occurs. + */ + @Test + void testNamedForward3() throws Exception { + WebApplication webApplication = createWebApplication(); + webApplication.addServlet("TestNamedForward3Servlet", new HttpServlet() { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + PrintWriter writer = response.getWriter(); + writer.print("SUCCESS"); + } + }); + webApplication.initialize(); + webApplication.start(); + WebApplicationRequest request = createWebApplicationRequest(); + request.setWebApplication(webApplication); + WebApplicationResponse response = createWebApplicationResponse(); + response.setWebApplication(webApplication); + response.setBodyOnly(true); + ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); + response.getWebApplicationOutputStream().setOutputStream(byteOutput); + webApplication.linkRequestAndResponse(request, response); + RequestDispatcher dispatcher = webApplication.getNamedDispatcher("TestNamedForward3Servlet"); + dispatcher.forward(request, response); + assertEquals("SUCCESS", new String(byteOutput.toByteArray())); + } + + /** + * Test forward method on a named RequestDispatcher. + * + * @throws Exception when a serious error occurs. + */ + @Test + void testNamedForward4() throws Exception { + WebApplication webApplication = createWebApplication(); + webApplication.addServlet("TestNamedForward4Servlet", new HttpServlet() { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + PrintWriter writer = response.getWriter(); + writer.println("FAILED"); + } + }); + webApplication.initialize(); + webApplication.start(); + WebApplicationRequest request = createWebApplicationRequest(); + request.setWebApplication(webApplication); + WebApplicationResponse response = createWebApplicationResponse(); + response.setBodyOnly(true); + ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); + response.getWebApplicationOutputStream().setOutputStream(byteOutput); + response.flushBuffer(); + RequestDispatcher dispatcher = webApplication.getNamedDispatcher("TestNamedForward4Servlet"); + try { + dispatcher.forward(request, response); + fail(); + } catch (IllegalStateException ise) { + } + } + + /** + * Test include method on a named RequestDispatcher. + * + * @jakarta.assertion Servlet:SPEC:192.1 + * @throws Exception when a serious error occurs. + */ + @Test + void testNamedInclude() throws Exception { + WebApplication webApplication = createWebApplication(); + webApplication.addServlet("TestNamedIncludeServlet", new HttpServlet() { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + PrintWriter writer = response.getWriter(); + response.setStatus(202); + response.setHeader("header", "value"); + writer.println("INCLUDED"); + } + }); + webApplication.initialize(); + webApplication.start(); + WebApplicationRequest request = createWebApplicationRequest(); + request.setWebApplication(webApplication); + WebApplicationResponse response = createWebApplicationResponse(); + response.setWebApplication(webApplication); + response.setBodyOnly(false); + ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); + response.getWebApplicationOutputStream().setOutputStream(byteOutput); + webApplication.linkRequestAndResponse(request, response); + RequestDispatcher dispatcher = webApplication.getNamedDispatcher("TestNamedIncludeServlet"); + dispatcher.include(request, response); + response.flushBuffer(); + assertTrue(new String(byteOutput.toByteArray()).contains("HTTP/1.1")); + assertFalse(new String(byteOutput.toByteArray()).contains("202")); + assertFalse(new String(byteOutput.toByteArray()).contains("header")); + assertFalse(new String(byteOutput.toByteArray()).contains("value")); + assertTrue(new String(byteOutput.toByteArray()).contains("INCLUDED")); + } + + /** + * Test include method on a named RequestDispatcher. + * + * @jakarta.assertion Servlet:SPEC:192.2 + * @throws Exception when a serious error occurs. + */ + @Test + void testNamedInclude2() throws Exception { + WebApplication webApplication = createWebApplication(); + webApplication.addServlet("TestNamedInclude2Servlet", new HttpServlet() { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + OutputStream output = response.getOutputStream(); + output.write('1'); + output.write('2'); + output.write('3'); + output.write('4'); + output.write('5'); + if (response.isCommitted()) { + output.write('6'); + } + } + }); + webApplication.initialize(); + webApplication.start(); + WebApplicationRequest request = createWebApplicationRequest(); + request.setWebApplication(webApplication); + WebApplicationResponse response = createWebApplicationResponse(); + response.setBufferSize(5); + response.setWebApplication(webApplication); + response.setBodyOnly(true); + ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); + response.getWebApplicationOutputStream().setOutputStream(byteOutput); + webApplication.linkRequestAndResponse(request, response); + RequestDispatcher dispatcher = webApplication.getNamedDispatcher("TestNamedInclude2Servlet"); + dispatcher.include(request, response); + response.flushBuffer(); + assertEquals("123456", new String(byteOutput.toByteArray())); + } + + /** + * Test include method on a named RequestDispatcher. + * + * @jakarta.assertion Servlet:SPEC:192.3 + * @jakarta.assertion Servlet:SPEC:192.4 + * @throws Exception when a serious error occurs. + */ + @Test + void testNamedInclude3() throws Exception { + WebApplication webApplication = createWebApplication(); + webApplication.addServlet("TestNamedInclude3Servlet", new HttpServlet() { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + PrintWriter writer = response.getWriter(); + response.flushBuffer(); + writer.println(request.getSession().getId()); + } + }); + webApplication.initialize(); + webApplication.start(); + WebApplicationRequest request = createWebApplicationRequest(); + request.setWebApplication(webApplication); + WebApplicationResponse response = createWebApplicationResponse(); + ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); + response.getWebApplicationOutputStream().setOutputStream(byteOutput); + response.setWebApplication(webApplication); + response.setBodyOnly(true); + webApplication.linkRequestAndResponse(request, response); + RequestDispatcher dispatcher = webApplication.getNamedDispatcher("TestNamedInclude3Servlet"); + try { + dispatcher.include(request, response); + fail(); + } catch(IllegalStateException ise) { + assertTrue(ise.getMessage().contains("addCookie")); + } + } } diff --git a/core/impl/src/test/java/cloud/piranha/core/impl/ServletContextAttributeListenerTest.java b/core/impl/src/test/java/cloud/piranha/core/impl/ServletContextAttributeListenerTest.java index 1ca5e1d663..7e30bfa0d3 100644 --- a/core/impl/src/test/java/cloud/piranha/core/impl/ServletContextAttributeListenerTest.java +++ b/core/impl/src/test/java/cloud/piranha/core/impl/ServletContextAttributeListenerTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002-2023 Manorrock.com. All Rights Reserved. + * Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -30,29 +30,145 @@ import cloud.piranha.core.api.WebApplication; import cloud.piranha.core.api.WebApplicationRequest; import cloud.piranha.core.api.WebApplicationResponse; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletContextAttributeEvent; +import jakarta.servlet.ServletContextAttributeListener; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.io.ByteArrayOutputStream; +import java.io.IOException; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import org.junit.jupiter.api.Test; /** * The JUnit tests for the ServletContextAttributeListener API. * * @author Manfred Riem (mriem@manorrock.com) */ -class ServletContextAttributeListenerTest extends cloud.piranha.core.tests.ServletContextAttributeListenerTest { +class ServletContextAttributeListenerTest { - @Override - protected WebApplication createWebApplication() { + private WebApplication createWebApplication() { return new DefaultWebApplication(); } - @Override - protected WebApplicationRequest createWebApplicationRequest() { + private WebApplicationRequest createWebApplicationRequest() { return new DefaultWebApplicationRequest(); } - @Override - protected WebApplicationResponse createWebApplicationResponse() { + private WebApplicationResponse createWebApplicationResponse() { DefaultWebApplicationResponse response = new DefaultWebApplicationResponse(); response.getWebApplicationOutputStream().setOutputStream(new ByteArrayOutputStream()); return response; } + + /** + * Test attributeAdded method. + * + * @throws Exception when a serious error occurs. + */ + @Test + void testAttributeAdded() throws Exception { + WebApplication webApplication = createWebApplication(); + webApplication.addListener(new ServletContextAttributeListener() { + @Override + public void attributeAdded(ServletContextAttributeEvent event) { + event.getServletContext().setAttribute("testAttributeAdded", true); + } + }); + webApplication.addServlet("TestAttributeAddedServlet", new HttpServlet() { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.getServletContext().setAttribute("name", "value"); + } + }); + webApplication.addServletMapping( + "TestAttributeAddedServlet", + "/testAttributeAdded"); + WebApplicationRequest request = createWebApplicationRequest(); + request.setServletPath("/testAttributeAdded"); + request.setWebApplication(webApplication); + WebApplicationResponse response = createWebApplicationResponse(); + response.setWebApplication(webApplication); + webApplication.initialize(); + webApplication.start(); + webApplication.service(request, response); + assertNotNull(webApplication.getAttribute("testAttributeAdded")); + webApplication.stop(); + } + + /** + * Test attributeRemoved method. + * + * @throws Exception when a serious error occurs. + */ + @Test + void testAttributeRemoved() throws Exception { + WebApplication webApplication = createWebApplication(); + webApplication.addListener(new ServletContextAttributeListener() { + @Override + public void attributeRemoved(ServletContextAttributeEvent event) { + event.getServletContext().setAttribute("testAttributeRemoved", true); + } + }); + webApplication.addServlet("TestAttributeRemovedServlet", new HttpServlet() { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + ServletContext context = request.getServletContext(); + context.setAttribute("name", "value"); + context.removeAttribute("name"); + } + }); + webApplication.addServletMapping( + "TestAttributeRemovedServlet", + "/testAttributeRemoved"); + WebApplicationRequest request = createWebApplicationRequest(); + request.setServletPath("/testAttributeRemoved"); + request.setWebApplication(webApplication); + WebApplicationResponse response = createWebApplicationResponse(); + response.setWebApplication(webApplication); + webApplication.initialize(); + webApplication.start(); + webApplication.service(request, response); + assertNotNull(webApplication.getAttribute("testAttributeRemoved")); + webApplication.stop(); + } + + /** + * Test attributeReplaced method. + * + * @throws Exception when a serious error occurs. + */ + @Test + void testAttributeReplaced() throws Exception { + WebApplication webApplication = createWebApplication(); + webApplication.addListener(new ServletContextAttributeListener() { + @Override + public void attributeReplaced(ServletContextAttributeEvent event) { + event.getServletContext().setAttribute("testAttributeReplaced", true); + } + }); + webApplication.addServlet("TestAttributeReplacedServlet", new HttpServlet() { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + ServletContext context = request.getServletContext(); + context.setAttribute("name", "value"); + context.setAttribute("name", "value2"); + } + }); + webApplication.addServletMapping( + "TestAttributeReplacedServlet", + "/testAttributeReplaced"); + WebApplicationRequest request = createWebApplicationRequest(); + request.setServletPath("/testAttributeReplaced"); + request.setWebApplication(webApplication); + WebApplicationResponse response = createWebApplicationResponse(); + response.setWebApplication(webApplication); + webApplication.initialize(); + webApplication.start(); + webApplication.service(request, response); + assertNotNull(webApplication.getAttribute("testAttributeReplaced")); + webApplication.stop(); + } } diff --git a/core/impl/src/test/java/cloud/piranha/core/impl/ServletInputStreamTest.java b/core/impl/src/test/java/cloud/piranha/core/impl/ServletInputStreamTest.java index b91994b640..c8c1c5477b 100644 --- a/core/impl/src/test/java/cloud/piranha/core/impl/ServletInputStreamTest.java +++ b/core/impl/src/test/java/cloud/piranha/core/impl/ServletInputStreamTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002-2023 Manorrock.com. All Rights Reserved. + * Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -30,26 +30,103 @@ import cloud.piranha.core.api.WebApplication; import cloud.piranha.core.api.WebApplicationRequest; import cloud.piranha.core.api.WebApplicationResponse; +import jakarta.servlet.ReadListener; +import jakarta.servlet.ServletInputStream; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import org.junit.jupiter.api.Test; /** * The JUnit tests for the ServletInputStream API. * * @author Manfred Riem (mriem@manorrock.com) */ -public class ServletInputStreamTest extends cloud.piranha.core.tests.ServletInputStreamTest { +public class ServletInputStreamTest { - @Override - public WebApplication createWebApplication() { + private WebApplication createWebApplication() { return new DefaultWebApplication(); } - @Override - public WebApplicationRequest createWebApplicationRequest() { + private WebApplicationRequest createWebApplicationRequest() { return new DefaultWebApplicationRequest(); } - @Override - public WebApplicationResponse createWebApplicationResponse() { + private WebApplicationResponse createWebApplicationResponse() { return new DefaultWebApplicationResponse(); } + + /** + * Test isFinished method. + * + * @throws Exception when a serious error occurs. + */ + void testIsFinished() throws Exception { + WebApplicationRequest request = createWebApplicationRequest(); + ServletInputStream inputStream = request.getInputStream(); + assertFalse(inputStream.isFinished()); + } + + /** + * Test isReady method. + * + * @throws Exception when a serious error occurs. + */ + void testIsReady() throws Exception { + WebApplicationRequest request = createWebApplicationRequest(); + ServletInputStream inputStream = request.getInputStream(); + assertFalse(inputStream.isReady()); + } + + /** + * Test read method. + * + * @throws Exception when a serious error occurs. + */ + @Test + void testRead() throws Exception { + WebApplicationRequest request = createWebApplicationRequest(); + ServletInputStream inputStream = request.getInputStream(); + request.getWebApplicationInputStream() + .setInputStream(new ByteArrayInputStream("a".getBytes())); + int character = inputStream.read(); + assertEquals('a', character); + } + + /** + * Test setReadListener method. + * + * @throws Exception when a serious error occurs. + */ + @Test + void testSetReadListener() throws Exception { + WebApplication webApplication = createWebApplication(); + WebApplicationRequest request = createWebApplicationRequest(); + request.setAsyncSupported(true); + request.setWebApplication(webApplication); + WebApplicationResponse response = createWebApplicationResponse(); + response.setWebApplication(webApplication); + webApplication.linkRequestAndResponse(request, response); + request.startAsync(); + ServletInputStream inputStream = request.getInputStream(); + inputStream.setReadListener(new ReadListener() { + @Override + public void onDataAvailable() throws IOException { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void onAllDataRead() throws IOException { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void onError(Throwable t) { + throw new UnsupportedOperationException("Not supported yet."); + } + }); + assertNotNull(request.getWebApplicationInputStream().getReadListener()); + } } diff --git a/core/impl/src/test/java/cloud/piranha/core/impl/ServletRegistrationTest.java b/core/impl/src/test/java/cloud/piranha/core/impl/ServletRegistrationTest.java index 64bdc3fb36..e3e7459e98 100644 --- a/core/impl/src/test/java/cloud/piranha/core/impl/ServletRegistrationTest.java +++ b/core/impl/src/test/java/cloud/piranha/core/impl/ServletRegistrationTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002-2023 Manorrock.com. All Rights Reserved. + * Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -28,6 +28,15 @@ package cloud.piranha.core.impl; import cloud.piranha.core.api.WebApplication; +import jakarta.servlet.ServletRegistration; +import jakarta.servlet.http.HttpServlet; +import java.util.HashMap; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Test; /** * The JUnit tests for testing everything related to the ServletRegistration @@ -35,10 +44,147 @@ * * @author Manfred Riem (mriem@manorrock.com) */ -class ServletRegistrationTest extends cloud.piranha.core.tests.ServletRegistrationTest { +class ServletRegistrationTest { - @Override - public WebApplication createWebApplication() { + private WebApplication createWebApplication() { return new DefaultWebApplication(); } + + /** + * Test getClassName method. + */ + @Test + void testGetClassName() { + WebApplication webApplication = createWebApplication(); + webApplication.addServlet("testGetClassNameServlet", new TestGetClassNameServlet()); + ServletRegistration registration = webApplication.getServletRegistration("testGetClassNameServlet"); + assertEquals(TestGetClassNameServlet.class.getName(), registration.getClassName()); + } + + /** + * Test getInitParameter method. + */ + @Test + void testGetInitParameter() { + WebApplication webApplication = createWebApplication(); + webApplication.addServlet("testGetInitParameterServlet", new HttpServlet() {}); + ServletRegistration registration = webApplication.getServletRegistration("testGetInitParameterServlet"); + registration.setInitParameter("name", "value"); + assertEquals("value", registration.getInitParameter("name")); + } + /** + * Test getInitParameters method. + */ + @Test + void testGetInitParameters() { + WebApplication webApplication = createWebApplication(); + webApplication.addServlet("testGetInitParametersServlet", new HttpServlet() {}); + ServletRegistration registration = webApplication.getServletRegistration("testGetInitParametersServlet"); + assertNotNull(registration.getInitParameters()); + } + + /** + * Test getName method. + */ + @Test + void testGetName() { + WebApplication webApplication = createWebApplication(); + webApplication.addServlet("testGetNameServlet", new HttpServlet() {}); + assertNotNull("servlet", webApplication.getServletRegistration("testGetNameServlet").getName()); + } + + /** + * Test getRunAsRole method. + */ + @Test + void testGetRunAsRole() { + WebApplication webApplication = createWebApplication(); + webApplication.addServlet("testGetRunAsRoleServlet", new HttpServlet() {}); + ServletRegistration.Dynamic registration = (ServletRegistration.Dynamic) + webApplication.getServletRegistration("testGetRunAsRoleServlet"); + registration.setRunAsRole("role"); + assertNotNull(registration.getRunAsRole()); + assertEquals("role", registration.getRunAsRole()); + } + + /** + * Test getFilterRegistration method. + */ + @Test + void testGetServletRegistration2() { + WebApplication webApplication = createWebApplication(); + webApplication.addServlet("testGetServletRegistration2Servlet", "TestGetServletRegistration2"); + assertNotNull(webApplication.getServletRegistration("testGetServletRegistration2Servlet")); + } + + /** + * Test setInitParameters method. + */ + @Test + void testSetInitParameters() { + WebApplication webApplication = createWebApplication(); + webApplication.addServlet("testSetInitParametersServlet", new HttpServlet() {}); + ServletRegistration registration = webApplication.getServletRegistration("testSetInitParametersServlet"); + registration.setInitParameter("name", "value"); + assertTrue(registration.setInitParameters(new HashMap<>()).isEmpty()); + } + + /** + * Test setInitParameters method. + */ + @Test + void testSetInitParameters2() { + WebApplication webApplication = createWebApplication(); + webApplication.addServlet("testSetInitParameters2Servlet", new HttpServlet() {}); + ServletRegistration registration = webApplication.getServletRegistration("testSetInitParameters2Servlet"); + HashMap parameters = new HashMap<>(); + parameters.put(null, null); + assertThrows(IllegalArgumentException.class, () -> registration.setInitParameters(parameters)); + } + + /** + * Test setInitParameters method. + */ + @Test + void testSetInitParameters3() { + WebApplication webApplication = createWebApplication(); + webApplication.addServlet("testSetInitParameters3Servlet", new HttpServlet() {}); + ServletRegistration registration = webApplication.getServletRegistration("testSetInitParameters3Servlet"); + HashMap parameters = new HashMap<>(); + parameters.put("name", null); + assertThrows(IllegalArgumentException.class, () -> registration.setInitParameters(parameters)); + } + + /** + * Test setInitParameters method. + */ + @Test + void testSetInitParameters4() { + WebApplication webApplication = createWebApplication(); + webApplication.addServlet("testSetInitParameters4Servlet", new HttpServlet() {}); + ServletRegistration registration = webApplication.getServletRegistration("testSetInitParameters4Servlet"); + HashMap parameters = new HashMap<>(); + parameters.put("name", "value"); + assertTrue(registration.setInitParameters(parameters).isEmpty()); + } + + /** + * Test setInitParameters method. + */ + @Test + void testSetInitParameters5() { + WebApplication webApplication = createWebApplication(); + webApplication.addServlet("testSetInitParameters5Servlet", new HttpServlet() {}); + ServletRegistration registration = webApplication.getServletRegistration("testSetInitParameters5Servlet"); + HashMap parameters = new HashMap<>(); + parameters.put("name", "value"); + assertTrue(registration.setInitParameters(parameters).isEmpty()); + assertFalse(registration.setInitParameters(parameters).isEmpty()); + } + + /** + * Test servlet that is used by testGetClassName. + */ + public static class TestGetClassNameServlet extends HttpServlet { + } } diff --git a/core/impl/src/test/java/cloud/piranha/core/impl/ServletRequestAttributeListenerTest.java b/core/impl/src/test/java/cloud/piranha/core/impl/ServletRequestAttributeListenerTest.java index 0d6b609d3b..9ec518897d 100644 --- a/core/impl/src/test/java/cloud/piranha/core/impl/ServletRequestAttributeListenerTest.java +++ b/core/impl/src/test/java/cloud/piranha/core/impl/ServletRequestAttributeListenerTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002-2023 Manorrock.com. All Rights Reserved. + * Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -30,29 +30,136 @@ import cloud.piranha.core.api.WebApplication; import cloud.piranha.core.api.WebApplicationRequest; import cloud.piranha.core.api.WebApplicationResponse; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequestAttributeEvent; +import jakarta.servlet.ServletRequestAttributeListener; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.io.ByteArrayOutputStream; +import java.io.IOException; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import org.junit.jupiter.api.Test; /** * The JUnit tests for the ServletRequestAttributeListener API. * * @author Manfred Riem (mriem@manorrock.com) */ -class ServletRequestAttributeListenerTest extends cloud.piranha.core.tests.ServletRequestAttributeListenerTest { +class ServletRequestAttributeListenerTest { - @Override - public WebApplication createWebApplication() { + private WebApplication createWebApplication() { return new DefaultWebApplication(); } - @Override - public WebApplicationRequest createWebApplicationRequest() { + private WebApplicationRequest createWebApplicationRequest() { return new DefaultWebApplicationRequest(); } - @Override - public WebApplicationResponse createWebApplicationResponse() { + private WebApplicationResponse createWebApplicationResponse() { DefaultWebApplicationResponse response = new DefaultWebApplicationResponse(); response.getWebApplicationOutputStream().setOutputStream(new ByteArrayOutputStream()); return response; } + + /** + * Test attributeAdded method. + * + * @throws Exception when a serious error occurs. + */ + @Test + void testAttributeAdded() throws Exception { + WebApplication webApplication = createWebApplication(); + webApplication.addListener(new ServletRequestAttributeListener() { + @Override + public void attributeAdded(ServletRequestAttributeEvent event) { + event.getServletContext().setAttribute("testAttributeAdded", true); + } + }); + webApplication.addServlet("TestAttributeAddedServlet", new HttpServlet() { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setAttribute("name", "value"); + } + }); + webApplication.addServletMapping("TestAttributeAddedServlet", + "/testAttributeAdded"); + webApplication.initialize(); + webApplication.start(); + WebApplicationRequest request = createWebApplicationRequest(); + request.setServletPath("/testAttributeAdded"); + request.setWebApplication(webApplication); + WebApplicationResponse response = createWebApplicationResponse(); + response.setWebApplication(webApplication); + webApplication.service(request, response); + assertNotNull(webApplication.getAttribute("testAttributeAdded")); + } + + /** + * Test attributeRemoved method. + * + * @throws Exception when a serious error occurs. + */ + @Test + void testAttributeRemoved() throws Exception { + WebApplication webApplication = createWebApplication(); + webApplication.addListener(new ServletRequestAttributeListener() { + @Override + public void attributeRemoved(ServletRequestAttributeEvent event) { + event.getServletContext().setAttribute("testAttributeRemoved", true); + } + }); + webApplication.addServlet("TestAttributeRemovedServlet", new HttpServlet() { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setAttribute("name", "value"); + request.removeAttribute("name"); + } + }); + webApplication.addServletMapping("TestAttributeRemovedServlet", + "/testAttributeRemoved"); + webApplication.initialize(); + webApplication.start(); + WebApplicationRequest request = createWebApplicationRequest(); + request.setServletPath("/testAttributeRemoved"); + request.setWebApplication(webApplication); + WebApplicationResponse response = createWebApplicationResponse(); + response.setWebApplication(webApplication); + webApplication.service(request, response); + assertNotNull(webApplication.getAttribute("testAttributeRemoved")); + } + + /** + * Test attributeReplaced method. + * + * @throws Exception when a serious error occurs. + */ + @Test + void testAttributeReplaced() throws Exception { + WebApplication webApplication = createWebApplication(); + webApplication.addListener(new ServletRequestAttributeListener() { + @Override + public void attributeRemoved(ServletRequestAttributeEvent event) { + event.getServletContext().setAttribute("testAttributeReplaced", true); + } + }); + webApplication.addServlet("TestAttributeReplacedServlet", new HttpServlet() { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setAttribute("name", "value"); + request.setAttribute("name", "value2"); + } + }); + webApplication.addServletMapping("TestAttributeReplacedServlet", + "/testAttributeReplaced"); + webApplication.initialize(); + webApplication.start(); + WebApplicationRequest request = createWebApplicationRequest(); + request.setServletPath("/testAttributeReplaced"); + request.setWebApplication(webApplication); + WebApplicationResponse response = createWebApplicationResponse(); + response.setWebApplication(webApplication); + webApplication.service(request, response); + assertNotNull(webApplication.getAttribute("testAttributeReplaced")); + } } diff --git a/core/impl/src/test/java/cloud/piranha/core/impl/WriteListenerTest.java b/core/impl/src/test/java/cloud/piranha/core/impl/WriteListenerTest.java index 2d2e5064da..8c7e2803d6 100644 --- a/core/impl/src/test/java/cloud/piranha/core/impl/WriteListenerTest.java +++ b/core/impl/src/test/java/cloud/piranha/core/impl/WriteListenerTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002-2023 Manorrock.com. All Rights Reserved. + * Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -30,26 +30,80 @@ import cloud.piranha.core.api.WebApplication; import cloud.piranha.core.api.WebApplicationRequest; import cloud.piranha.core.api.WebApplicationResponse; +import jakarta.servlet.ServletOutputStream; +import jakarta.servlet.WriteListener; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import org.junit.jupiter.api.Test; /** * The JUnit tests for the WriteListener API. * * @author Manfred Riem (mriem@manorrock.com) */ -public class WriteListenerTest extends cloud.piranha.core.tests.WriteListenerTest { +public class WriteListenerTest { - @Override - public WebApplication createWebApplication() { + private WebApplication createWebApplication() { return new DefaultWebApplication(); } - @Override - public WebApplicationRequest createWebApplicationRequest() { + private WebApplicationRequest createWebApplicationRequest() { return new DefaultWebApplicationRequest(); } - @Override - public WebApplicationResponse createWebApplicationResponse() { + private WebApplicationResponse createWebApplicationResponse() { return new DefaultWebApplicationResponse(); } + + /** + * Test onWritePossible method. + * + * @throws Exception when a serious error occurs. + */ + @Test + void testOnWritePossible() throws Exception { + WebApplication webApplication = createWebApplication(); + WebApplicationRequest request = createWebApplicationRequest(); + request.setAsyncSupported(true); + request.setWebApplication(webApplication); + WebApplicationResponse response = createWebApplicationResponse(); + response.setWebApplication(webApplication); + webApplication.linkRequestAndResponse(request, response); + request.startAsync(); + ServletOutputStream outputStream = response.getOutputStream(); + response.getWebApplicationOutputStream().setOutputStream(new ByteArrayOutputStream()); + outputStream.setWriteListener(new TestOnWritePossibleWriteListener(webApplication)); + Thread.sleep(2000); + assertNotNull(webApplication.getAttribute("onWritePossible")); + } + + /** + * A WriteListener for onWritePossible. + */ + public static class TestOnWritePossibleWriteListener implements WriteListener { + + /** + * Stores the web application. + */ + private final WebApplication webApplication; + + /** + * Constructor. + * + * @param webApplication the web application. + */ + public TestOnWritePossibleWriteListener(WebApplication webApplication) { + this.webApplication = webApplication; + } + + @Override + public void onWritePossible() throws IOException { + webApplication.setAttribute("onWritePossible", true); + } + + @Override + public void onError(Throwable t) { + } + } } diff --git a/core/pom.xml b/core/pom.xml index b9ec01e819..72d1af3fb6 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -18,7 +18,6 @@ api impl - tests diff --git a/core/tests/pom.xml b/core/tests/pom.xml deleted file mode 100644 index 552ab81fd7..0000000000 --- a/core/tests/pom.xml +++ /dev/null @@ -1,36 +0,0 @@ - - - - 4.0.0 - - - cloud.piranha.core - project - 24.1.0-SNAPSHOT - - - piranha-core-tests - jar - - Piranha - Core - Tests - - - - cloud.piranha.core - piranha-core-api - ${project.version} - compile - - - cloud.piranha.resource - piranha-resource-impl - ${project.version} - compile - - - org.junit.jupiter - junit-jupiter-api - compile - - - diff --git a/core/tests/src/main/java/cloud/piranha/core/tests/AsyncContextTest.java b/core/tests/src/main/java/cloud/piranha/core/tests/AsyncContextTest.java deleted file mode 100644 index 078296c190..0000000000 --- a/core/tests/src/main/java/cloud/piranha/core/tests/AsyncContextTest.java +++ /dev/null @@ -1,343 +0,0 @@ -/* - * Copyright (c) 2002-2023 Manorrock.com. All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package cloud.piranha.core.tests; - -import cloud.piranha.core.api.WebApplication; -import cloud.piranha.core.api.WebApplicationRequest; -import cloud.piranha.core.api.WebApplicationResponse; -import jakarta.servlet.AsyncContext; -import jakarta.servlet.AsyncEvent; -import jakarta.servlet.AsyncListener; -import jakarta.servlet.ServletException; -import jakarta.servlet.ServletRegistration.Dynamic; -import jakarta.servlet.http.HttpServlet; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import java.io.IOException; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; -import org.junit.jupiter.api.Test; - -/** - * The JUnit tests for AsyncContext API. - * - * @author Manfred Riem (mriem@manorrock.com) - */ -public abstract class AsyncContextTest { - - /** - * Default constructor. - */ - public AsyncContextTest() { - } - - /** - * Create the web application, - * - * @return the web application. - */ - public abstract WebApplication createWebApplication(); - - /** - * Create the web application request. - * - * @return the web application request. - */ - public abstract WebApplicationRequest createWebApplicationRequest(); - - /** - * Create the web application response. - * - * @return the web application response. - */ - public abstract WebApplicationResponse createWebApplicationResponse(); - - /** - * Test addListener method. - */ - @Test - void testAddListener() { - WebApplication webApplication = createWebApplication(); - WebApplicationRequest request = createWebApplicationRequest(); - request.setWebApplication(webApplication); - request.setAsyncSupported(true); - WebApplicationResponse response = createWebApplicationResponse(); - response.setWebApplication(webApplication); - webApplication.linkRequestAndResponse(request, response); - AsyncContext context = request.startAsync(); - context.addListener(new AsyncListener() { - @Override - public void onComplete(AsyncEvent event) throws IOException { - event.getSuppliedRequest().getServletContext().setAttribute("onComplete", "true"); - } - - @Override - public void onTimeout(AsyncEvent event) throws IOException { - } - - @Override - public void onError(AsyncEvent event) throws IOException { - } - - @Override - public void onStartAsync(AsyncEvent event) throws IOException { - } - }); - context.complete(); - assertNotNull(webApplication.getAttribute("onComplete")); - } - - /** - * Test complete method. - */ - @Test - void testComplete() { - WebApplication webApplication = createWebApplication(); - WebApplicationRequest request = createWebApplicationRequest(); - request.setWebApplication(webApplication); - request.setAsyncSupported(true); - WebApplicationResponse response = createWebApplicationResponse(); - response.setWebApplication(webApplication); - webApplication.linkRequestAndResponse(request, response); - AsyncContext context = request.startAsync(); - context.complete(); - assertTrue(response.isCommitted()); - } - - /** - * Test createListener method. - * - * @throws ServletException when a serious error occurs. - */ - @Test - void testCreateListener() throws ServletException { - WebApplication webApplication = createWebApplication(); - WebApplicationRequest request = createWebApplicationRequest(); - request.setWebApplication(webApplication); - request.setAsyncSupported(true); - WebApplicationResponse response = createWebApplicationResponse(); - response.setWebApplication(webApplication); - webApplication.linkRequestAndResponse(request, response); - AsyncContext context = request.startAsync(); - assertNotNull(context.createListener(TestCreateListenerListener.class)); - } - - /** - * Test createListener method. - * - * @throws ServletException when a serious error occurs. - */ - @Test - void testCreateListener2() throws ServletException { - WebApplication webApplication = createWebApplication(); - WebApplicationRequest request = createWebApplicationRequest(); - request.setWebApplication(webApplication); - request.setAsyncSupported(true); - WebApplicationResponse response = createWebApplicationResponse(); - response.setWebApplication(webApplication); - webApplication.linkRequestAndResponse(request, response); - AsyncContext context = request.startAsync(); - assertThrows(ServletException.class, - () -> { - context.createListener(TestCreateListener2Listener.class); - }); - } - - /** - * Test dispatch method. - */ - @Test - void testDispatch() { - WebApplication webApplication = createWebApplication(); - Dynamic registration = webApplication.addServlet("TestDispatchServlet", new HttpServlet() { - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - AsyncContext context = request.startAsync(); - context.dispatch("/testDispatchB"); - context.dispatch("/testDispatchC"); - } - }); - registration.setAsyncSupported(true); - webApplication.addServletMapping("TestDispatchServlet", "/testDispatch"); - webApplication.initialize(); - webApplication.start(); - WebApplicationRequest request = createWebApplicationRequest(); - request.setServletPath("/testDispatch"); - request.setWebApplication(webApplication); - request.setAsyncSupported(true); - WebApplicationResponse response = createWebApplicationResponse(); - response.setWebApplication(webApplication); - webApplication.linkRequestAndResponse(request, response); - assertThrows(IllegalStateException.class, - () -> webApplication.service(request, response)); - } - - /** - * Test getRequest method. - */ - @Test - void testGetRequest() { - WebApplication webApplication = createWebApplication(); - WebApplicationRequest request = createWebApplicationRequest(); - request.setWebApplication(webApplication); - request.setAsyncSupported(true); - WebApplicationResponse response = createWebApplicationResponse(); - response.setWebApplication(webApplication); - webApplication.linkRequestAndResponse(request, response); - AsyncContext context = request.startAsync(); - assertNotNull(context.getRequest()); - } - - /** - * Test getResponse method. - */ - @Test - void testGetResponse() { - WebApplication webApplication = createWebApplication(); - WebApplicationRequest request = createWebApplicationRequest(); - request.setWebApplication(webApplication); - request.setAsyncSupported(true); - WebApplicationResponse response = createWebApplicationResponse(); - response.setWebApplication(webApplication); - webApplication.linkRequestAndResponse(request, response); - AsyncContext context = request.startAsync(); - assertNotNull(context.getResponse()); - } - - /** - * Test getTimeout method. - */ - @Test - void testGetTimeout() { - WebApplication webApplication = createWebApplication(); - WebApplicationRequest request = createWebApplicationRequest(); - request.setWebApplication(webApplication); - request.setAsyncSupported(true); - WebApplicationResponse response = createWebApplicationResponse(); - response.setWebApplication(webApplication); - webApplication.linkRequestAndResponse(request, response); - AsyncContext context = request.startAsync(); - assertEquals(30000, context.getTimeout()); - } - - /** - * Test hasOriginalRequestAndResponse method. - */ - @Test - void testHasOriginalRequestAndResponse() { - WebApplication webApplication = createWebApplication(); - WebApplicationRequest request = createWebApplicationRequest(); - request.setWebApplication(webApplication); - request.setAsyncSupported(true); - WebApplicationResponse response = createWebApplicationResponse(); - response.setWebApplication(webApplication); - webApplication.linkRequestAndResponse(request, response); - AsyncContext context = request.startAsync(); - assertTrue(context.hasOriginalRequestAndResponse()); - assertEquals(request, context.getRequest()); - assertEquals(response, context.getResponse()); - } - - /** - * Test setTimeout method. - */ - @Test - void testSetTimeout() { - WebApplication webApplication = createWebApplication(); - WebApplicationRequest request = createWebApplicationRequest(); - request.setWebApplication(webApplication); - request.setAsyncSupported(true); - WebApplicationResponse response = createWebApplicationResponse(); - response.setWebApplication(webApplication); - webApplication.linkRequestAndResponse(request, response); - AsyncContext context = request.startAsync(); - context.setTimeout(60000); - assertEquals(60000, context.getTimeout()); - } - - /** - * Listener that is used by testCreateListener. - */ - public static class TestCreateListenerListener implements AsyncListener { - - /** - * Default constructor. - */ - public TestCreateListenerListener() { - } - - @Override - public void onComplete(AsyncEvent event) throws IOException { - } - - @Override - public void onTimeout(AsyncEvent event) throws IOException { - } - - @Override - public void onError(AsyncEvent event) throws IOException { - } - - @Override - public void onStartAsync(AsyncEvent event) throws IOException { - } - } - - /** - * Listener that is used by testCreateListener2. - */ - public static class TestCreateListener2Listener implements AsyncListener { - - /** - * Constructor. - * - * @throws ServletException when constructor is called. - */ - public TestCreateListener2Listener() throws ServletException { - throw new ServletException(); - } - - @Override - public void onComplete(AsyncEvent event) throws IOException { - } - - @Override - public void onTimeout(AsyncEvent event) throws IOException { - } - - @Override - public void onError(AsyncEvent event) throws IOException { - } - - @Override - public void onStartAsync(AsyncEvent event) throws IOException { - } - } -} diff --git a/core/tests/src/main/java/cloud/piranha/core/tests/HttpSessionIdListenerTest.java b/core/tests/src/main/java/cloud/piranha/core/tests/HttpSessionIdListenerTest.java deleted file mode 100644 index f96c0b3cd4..0000000000 --- a/core/tests/src/main/java/cloud/piranha/core/tests/HttpSessionIdListenerTest.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright (c) 2002-2023 Manorrock.com. All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package cloud.piranha.core.tests; - -import cloud.piranha.core.api.WebApplication; -import cloud.piranha.core.api.WebApplicationRequest; -import cloud.piranha.core.api.WebApplicationResponse; -import jakarta.servlet.ServletException; -import jakarta.servlet.http.HttpServlet; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import jakarta.servlet.http.HttpSessionEvent; -import jakarta.servlet.http.HttpSessionIdListener; -import java.io.IOException; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import org.junit.jupiter.api.Test; - -/** - * The JUnit tests for the HttpSessionIdListener API. - * - * @author Manfred Riem (mriem@manorrock.com) - */ -public abstract class HttpSessionIdListenerTest { - - /** - * Default constructor. - */ - public HttpSessionIdListenerTest() { - } - - /** - * Create the web application. - * - * @return the web application. - */ - protected abstract WebApplication createWebApplication(); - - /** - * Create the web application request. - * - * @return the web application request. - */ - protected abstract WebApplicationRequest createWebApplicationRequest(); - - /** - * Create the web application response. - * - * @return the web application response. - */ - protected abstract WebApplicationResponse createWebApplicationResponse(); - - /** - * Test sessionIdChanged method. - * - * @throws Exception when a serious error occurs. - */ - @Test - void testSessionIdChanged() throws Exception { - WebApplication webApplication = createWebApplication(); - webApplication.addListener(new HttpSessionIdListener() { - @Override - public void sessionIdChanged(HttpSessionEvent event, String oldSessionId) { - event.getSession().getServletContext().setAttribute("testSessionIdChanged", true); - } - }); - webApplication.addServlet("TestSessionIdChangedServlet", new HttpServlet() { - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - request.getSession(); - request.changeSessionId(); - } - }); - webApplication.addServletMapping("TestSessionIdChangedServlet", "/testSessionIdChangedServlet"); - webApplication.initialize(); - webApplication.start(); - WebApplicationRequest request = createWebApplicationRequest(); - request.setServletPath("/testSessionIdChangedServlet"); - request.setWebApplication(webApplication); - WebApplicationResponse response = createWebApplicationResponse(); - response.setWebApplication(webApplication); - webApplication.service(request, response); - assertNotNull(webApplication.getAttribute("testSessionIdChanged")); - } -} diff --git a/core/tests/src/main/java/cloud/piranha/core/tests/ReadListenerTest.java b/core/tests/src/main/java/cloud/piranha/core/tests/ReadListenerTest.java deleted file mode 100644 index 64ae8d1b85..0000000000 --- a/core/tests/src/main/java/cloud/piranha/core/tests/ReadListenerTest.java +++ /dev/null @@ -1,188 +0,0 @@ -/* - * Copyright (c) 2002-2023 Manorrock.com. All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package cloud.piranha.core.tests; - -import cloud.piranha.core.api.WebApplication; -import cloud.piranha.core.api.WebApplicationRequest; -import cloud.piranha.core.api.WebApplicationResponse; -import jakarta.servlet.ReadListener; -import jakarta.servlet.ServletInputStream; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import org.junit.jupiter.api.Test; - -/** - * The JUnit tests for the ReadListener API. - * - * @author Manfred Riem (mriem@manorrock.com) - */ -public abstract class ReadListenerTest { - - /** - * Default constructor. - */ - public ReadListenerTest() { - } - - /** - * Create a web application. - * - * @return the web application. - */ - public abstract WebApplication createWebApplication(); - - /** - * Create a web application request. - * - * @return the web application request. - */ - public abstract WebApplicationRequest createWebApplicationRequest(); - - /** - * Create a web application response. - * - * @return the web application response. - */ - public abstract WebApplicationResponse createWebApplicationResponse(); - - /** - * Test onAllDataRead method. - * - * @throws Exception when a serious error occurs. - */ - @Test - void testOnAllDataRead() throws Exception { - WebApplication webApplication = createWebApplication(); - WebApplicationRequest request = createWebApplicationRequest(); - request.setAsyncSupported(true); - request.setWebApplication(webApplication); - WebApplicationResponse response = createWebApplicationResponse(); - response.setWebApplication(webApplication); - webApplication.linkRequestAndResponse(request, response); - request.startAsync(); - try (ServletInputStream inputStream = request.getInputStream()) { - request.getWebApplicationInputStream().setInputStream(new ByteArrayInputStream("read this".getBytes())); - inputStream.setReadListener(new TestOnAllDataReadReadListener(webApplication)); - int character = inputStream.read(); - while(character != -1) { - character = inputStream.read(); - } - } - Thread.sleep(2000); - assertNotNull(webApplication.getAttribute("onAllDataRead")); - } - - /** - * Test onDataAvailable method. - * - * @throws Exception when a serious error occurs. - */ - @Test - void testOnDataAvailable() throws Exception { - WebApplication webApplication = createWebApplication(); - WebApplicationRequest request = createWebApplicationRequest(); - request.setAsyncSupported(true); - request.setWebApplication(webApplication); - WebApplicationResponse response = createWebApplicationResponse(); - response.setWebApplication(webApplication); - webApplication.linkRequestAndResponse(request, response); - request.startAsync(); - ServletInputStream inputStream = request.getInputStream(); - request.getWebApplicationInputStream().setInputStream(new ByteArrayInputStream("a".getBytes())); - inputStream.setReadListener(new TestOnDataAvailableReadListener(webApplication)); - Thread.sleep(5000); - assertNotNull(webApplication.getAttribute("onDataAvailable")); - } - - /** - * A test ReadListener for onAllDataRead. - */ - public static class TestOnAllDataReadReadListener implements ReadListener { - - /** - * Stores the web application. - */ - private final WebApplication webApplication; - - /** - * Constructor. - * - * @param webApplication the web application. - */ - public TestOnAllDataReadReadListener(WebApplication webApplication) { - this.webApplication = webApplication; - } - - @Override - public void onAllDataRead() throws IOException { - webApplication.setAttribute("onAllDataRead", true); - } - - @Override - public void onDataAvailable() throws IOException { - } - - @Override - public void onError(Throwable t) { - } - } - - /** - * A test ReadListener for onDataAvailable. - */ - public static class TestOnDataAvailableReadListener implements ReadListener { - - /** - * Stores the web application. - */ - private final WebApplication webApplication; - - /** - * Constructor. - * - * @param webApplication the web application. - */ - public TestOnDataAvailableReadListener(WebApplication webApplication) { - this.webApplication = webApplication; - } - - @Override - public void onAllDataRead() throws IOException { - } - - @Override - public void onDataAvailable() throws IOException { - webApplication.setAttribute("onDataAvailable", true); - } - - @Override - public void onError(Throwable t) { - } - } -} diff --git a/core/tests/src/main/java/cloud/piranha/core/tests/RequestDispatcherTest.java b/core/tests/src/main/java/cloud/piranha/core/tests/RequestDispatcherTest.java deleted file mode 100644 index 334eac3b5e..0000000000 --- a/core/tests/src/main/java/cloud/piranha/core/tests/RequestDispatcherTest.java +++ /dev/null @@ -1,351 +0,0 @@ -/* - * Copyright (c) 2002-2023 Manorrock.com. All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package cloud.piranha.core.tests; - -import cloud.piranha.core.api.WebApplication; -import cloud.piranha.core.api.WebApplicationRequest; -import cloud.piranha.core.api.WebApplicationResponse; -import jakarta.servlet.RequestDispatcher; -import static jakarta.servlet.RequestDispatcher.FORWARD_CONTEXT_PATH; -import static jakarta.servlet.RequestDispatcher.FORWARD_MAPPING; -import static jakarta.servlet.RequestDispatcher.FORWARD_PATH_INFO; -import static jakarta.servlet.RequestDispatcher.FORWARD_QUERY_STRING; -import static jakarta.servlet.RequestDispatcher.FORWARD_REQUEST_URI; -import static jakarta.servlet.RequestDispatcher.FORWARD_SERVLET_PATH; -import jakarta.servlet.ServletException; -import jakarta.servlet.http.HttpServlet; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintWriter; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; -import org.junit.jupiter.api.Test; - -/** - * The JUnit tests for any RequestDispatcher implementation. - * - * @author Manfred Riem (mriem@manorrock.com) - */ -public abstract class RequestDispatcherTest { - - /** - * Default constructor. - */ - public RequestDispatcherTest() { - } - - /** - * Create the web application. - * - * @return the web application. - */ - protected abstract WebApplication createWebApplication(); - - /** - * Create the web application request. - * - * @return the web application request. - */ - protected abstract WebApplicationRequest createWebApplicationRequest(); - - /** - * Create the web application response. - * - * @return the web application response. - */ - protected abstract WebApplicationResponse createWebApplicationResponse(); - - /** - * Test forward method on a named RequestDispatcher. - * - * @jakarta.assertion Servlet:SPEC:181 - * @jakarta.assertion Servlet:SPEC:181.1 - * @jakarta.assertion Servlet:SPEC:181.2 - * @jakarta.assertion Servlet:SPEC:181.3 - * @jakarta.assertion Servlet:SPEC:181.4 - * @jakarta.assertion Servlet:SPEC:181.5 - * @throws Exception when a serious error occurs. - */ - @Test - void testNamedForward() throws Exception { - WebApplication webApplication = createWebApplication(); - webApplication.addServlet("TestNamedForwardServlet", new HttpServlet() { - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - PrintWriter writer = response.getWriter(); - if (request.getAttribute(FORWARD_CONTEXT_PATH) != null) { - writer.println(request.getAttribute(FORWARD_CONTEXT_PATH)); - } - if (request.getAttribute(FORWARD_MAPPING) != null) { - writer.println(request.getAttribute(FORWARD_CONTEXT_PATH)); - } - if (request.getAttribute(FORWARD_PATH_INFO) != null) { - writer.println(request.getAttribute(FORWARD_PATH_INFO)); - } - if (request.getAttribute(FORWARD_QUERY_STRING) != null) { - writer.println(request.getAttribute(FORWARD_QUERY_STRING)); - } - if (request.getAttribute(FORWARD_REQUEST_URI) != null) { - writer.println(request.getAttribute(FORWARD_REQUEST_URI)); - } - if (request.getAttribute(FORWARD_SERVLET_PATH) != null) { - writer.println(request.getAttribute(FORWARD_SERVLET_PATH)); - } - writer.flush(); - } - }); - webApplication.initialize(); - webApplication.start(); - WebApplicationRequest request = createWebApplicationRequest(); - request.setWebApplication(webApplication); - WebApplicationResponse response = createWebApplicationResponse(); - response.setWebApplication(webApplication); - response.setBodyOnly(true); - ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); - response.getWebApplicationOutputStream().setOutputStream(byteOutput); - webApplication.linkRequestAndResponse(request, response); - RequestDispatcher dispatcher = webApplication.getNamedDispatcher("TestNamedForwardServlet"); - dispatcher.forward(request, response); - assertEquals("", new String(byteOutput.toByteArray())); - } - - /** - * Test forward method on a named RequestDispatcher. - * - * @jakarta.assertion Servlet:SPEC:79 - * @throws Exception when a serious error occurs. - */ - @Test - void testNamedForward2() throws Exception { - WebApplication webApplication = createWebApplication(); - webApplication.addServlet("TestNamedForward2Servlet", new HttpServlet() { - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - PrintWriter writer = response.getWriter(); - writer.print(request.getRequestURI()); - writer.flush(); - } - }); - webApplication.initialize(); - webApplication.start(); - WebApplicationRequest request = createWebApplicationRequest(); - request.setWebApplication(webApplication); - WebApplicationResponse response = createWebApplicationResponse(); - response.setWebApplication(webApplication); - response.setBodyOnly(true); - ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); - response.getWebApplicationOutputStream().setOutputStream(byteOutput); - webApplication.linkRequestAndResponse(request, response); - RequestDispatcher dispatcher = webApplication.getNamedDispatcher("TestNamedForward2Servlet"); - dispatcher.forward(request, response); - assertEquals("/", new String(byteOutput.toByteArray())); - } - - /** - * Test forward method on a named RequestDispatcher. - * - * @jakarta.assertion Servlet:SPEC:80 - * @throws Exception when a serious error occurs. - */ - @Test - void testNamedForward3() throws Exception { - WebApplication webApplication = createWebApplication(); - webApplication.addServlet("TestNamedForward3Servlet", new HttpServlet() { - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - PrintWriter writer = response.getWriter(); - writer.print("SUCCESS"); - } - }); - webApplication.initialize(); - webApplication.start(); - WebApplicationRequest request = createWebApplicationRequest(); - request.setWebApplication(webApplication); - WebApplicationResponse response = createWebApplicationResponse(); - response.setWebApplication(webApplication); - response.setBodyOnly(true); - ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); - response.getWebApplicationOutputStream().setOutputStream(byteOutput); - webApplication.linkRequestAndResponse(request, response); - RequestDispatcher dispatcher = webApplication.getNamedDispatcher("TestNamedForward3Servlet"); - dispatcher.forward(request, response); - assertEquals("SUCCESS", new String(byteOutput.toByteArray())); - } - - /** - * Test forward method on a named RequestDispatcher. - * - * @throws Exception when a serious error occurs. - */ - @Test - void testNamedForward4() throws Exception { - WebApplication webApplication = createWebApplication(); - webApplication.addServlet("TestNamedForward4Servlet", new HttpServlet() { - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - PrintWriter writer = response.getWriter(); - writer.println("FAILED"); - } - }); - webApplication.initialize(); - webApplication.start(); - WebApplicationRequest request = createWebApplicationRequest(); - request.setWebApplication(webApplication); - WebApplicationResponse response = createWebApplicationResponse(); - response.setBodyOnly(true); - ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); - response.getWebApplicationOutputStream().setOutputStream(byteOutput); - response.flushBuffer(); - RequestDispatcher dispatcher = webApplication.getNamedDispatcher("TestNamedForward4Servlet"); - try { - dispatcher.forward(request, response); - fail(); - } catch (IllegalStateException ise) { - } - } - - /** - * Test include method on a named RequestDispatcher. - * - * @jakarta.assertion Servlet:SPEC:192.1 - * @throws Exception when a serious error occurs. - */ - @Test - void testNamedInclude() throws Exception { - WebApplication webApplication = createWebApplication(); - webApplication.addServlet("TestNamedIncludeServlet", new HttpServlet() { - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - PrintWriter writer = response.getWriter(); - response.setStatus(202); - response.setHeader("header", "value"); - writer.println("INCLUDED"); - } - }); - webApplication.initialize(); - webApplication.start(); - WebApplicationRequest request = createWebApplicationRequest(); - request.setWebApplication(webApplication); - WebApplicationResponse response = createWebApplicationResponse(); - response.setWebApplication(webApplication); - response.setBodyOnly(false); - ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); - response.getWebApplicationOutputStream().setOutputStream(byteOutput); - webApplication.linkRequestAndResponse(request, response); - RequestDispatcher dispatcher = webApplication.getNamedDispatcher("TestNamedIncludeServlet"); - dispatcher.include(request, response); - response.flushBuffer(); - assertTrue(new String(byteOutput.toByteArray()).contains("HTTP/1.1")); - assertFalse(new String(byteOutput.toByteArray()).contains("202")); - assertFalse(new String(byteOutput.toByteArray()).contains("header")); - assertFalse(new String(byteOutput.toByteArray()).contains("value")); - assertTrue(new String(byteOutput.toByteArray()).contains("INCLUDED")); - } - - /** - * Test include method on a named RequestDispatcher. - * - * @jakarta.assertion Servlet:SPEC:192.2 - * @throws Exception when a serious error occurs. - */ - @Test - void testNamedInclude2() throws Exception { - WebApplication webApplication = createWebApplication(); - webApplication.addServlet("TestNamedInclude2Servlet", new HttpServlet() { - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - OutputStream output = response.getOutputStream(); - output.write('1'); - output.write('2'); - output.write('3'); - output.write('4'); - output.write('5'); - if (response.isCommitted()) { - output.write('6'); - } - } - }); - webApplication.initialize(); - webApplication.start(); - WebApplicationRequest request = createWebApplicationRequest(); - request.setWebApplication(webApplication); - WebApplicationResponse response = createWebApplicationResponse(); - response.setBufferSize(5); - response.setWebApplication(webApplication); - response.setBodyOnly(true); - ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); - response.getWebApplicationOutputStream().setOutputStream(byteOutput); - webApplication.linkRequestAndResponse(request, response); - RequestDispatcher dispatcher = webApplication.getNamedDispatcher("TestNamedInclude2Servlet"); - dispatcher.include(request, response); - response.flushBuffer(); - assertEquals("123456", new String(byteOutput.toByteArray())); - } - - /** - * Test include method on a named RequestDispatcher. - * - * @jakarta.assertion Servlet:SPEC:192.3 - * @jakarta.assertion Servlet:SPEC:192.4 - * @throws Exception when a serious error occurs. - */ - @Test - void testNamedInclude3() throws Exception { - WebApplication webApplication = createWebApplication(); - webApplication.addServlet("TestNamedInclude3Servlet", new HttpServlet() { - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - PrintWriter writer = response.getWriter(); - response.flushBuffer(); - writer.println(request.getSession().getId()); - } - }); - webApplication.initialize(); - webApplication.start(); - WebApplicationRequest request = createWebApplicationRequest(); - request.setWebApplication(webApplication); - WebApplicationResponse response = createWebApplicationResponse(); - ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); - response.getWebApplicationOutputStream().setOutputStream(byteOutput); - response.setWebApplication(webApplication); - response.setBodyOnly(true); - webApplication.linkRequestAndResponse(request, response); - RequestDispatcher dispatcher = webApplication.getNamedDispatcher("TestNamedInclude3Servlet"); - try { - dispatcher.include(request, response); - fail(); - } catch(IllegalStateException ise) { - assertTrue(ise.getMessage().contains("addCookie")); - } - } -} diff --git a/core/tests/src/main/java/cloud/piranha/core/tests/ServletContextAttributeListenerTest.java b/core/tests/src/main/java/cloud/piranha/core/tests/ServletContextAttributeListenerTest.java deleted file mode 100644 index a59ccb8f74..0000000000 --- a/core/tests/src/main/java/cloud/piranha/core/tests/ServletContextAttributeListenerTest.java +++ /dev/null @@ -1,180 +0,0 @@ -/* - * Copyright (c) 2002-2023 Manorrock.com. All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package cloud.piranha.core.tests; - -import cloud.piranha.core.api.WebApplication; -import cloud.piranha.core.api.WebApplicationRequest; -import cloud.piranha.core.api.WebApplicationResponse; -import jakarta.servlet.ServletContext; -import jakarta.servlet.ServletContextAttributeEvent; -import jakarta.servlet.ServletContextAttributeListener; -import jakarta.servlet.ServletException; -import jakarta.servlet.http.HttpServlet; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import java.io.IOException; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import org.junit.jupiter.api.Test; - -/** - * The JUnit tests for the ServletContextAttributeListener API. - * - * @author Manfred Riem (mriem@manorrock.com) - */ -public abstract class ServletContextAttributeListenerTest { - - /** - * Create the web application. - * - * @return the web application. - */ - protected abstract WebApplication createWebApplication(); - - /** - * Create the web application request. - * - * @return the web application request. - */ - protected abstract WebApplicationRequest createWebApplicationRequest(); - - /** - * Create the web application response. - * - * @return the web application response. - */ - protected abstract WebApplicationResponse createWebApplicationResponse(); - - /** - * Test attributeAdded method. - * - * @throws Exception when a serious error occurs. - */ - @Test - void testAttributeAdded() throws Exception { - WebApplication webApplication = createWebApplication(); - webApplication.addListener(new ServletContextAttributeListener() { - @Override - public void attributeAdded(ServletContextAttributeEvent event) { - event.getServletContext().setAttribute("testAttributeAdded", true); - } - }); - webApplication.addServlet("TestAttributeAddedServlet", new HttpServlet() { - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - request.getServletContext().setAttribute("name", "value"); - } - }); - webApplication.addServletMapping( - "TestAttributeAddedServlet", - "/testAttributeAdded"); - WebApplicationRequest request = createWebApplicationRequest(); - request.setServletPath("/testAttributeAdded"); - request.setWebApplication(webApplication); - WebApplicationResponse response = createWebApplicationResponse(); - response.setWebApplication(webApplication); - webApplication.initialize(); - webApplication.start(); - webApplication.service(request, response); - assertNotNull(webApplication.getAttribute("testAttributeAdded")); - webApplication.stop(); - } - - /** - * Test attributeRemoved method. - * - * @throws Exception when a serious error occurs. - */ - @Test - void testAttributeRemoved() throws Exception { - WebApplication webApplication = createWebApplication(); - webApplication.addListener(new ServletContextAttributeListener() { - @Override - public void attributeRemoved(ServletContextAttributeEvent event) { - event.getServletContext().setAttribute("testAttributeRemoved", true); - } - }); - webApplication.addServlet("TestAttributeRemovedServlet", new HttpServlet() { - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - ServletContext context = request.getServletContext(); - context.setAttribute("name", "value"); - context.removeAttribute("name"); - } - }); - webApplication.addServletMapping( - "TestAttributeRemovedServlet", - "/testAttributeRemoved"); - WebApplicationRequest request = createWebApplicationRequest(); - request.setServletPath("/testAttributeRemoved"); - request.setWebApplication(webApplication); - WebApplicationResponse response = createWebApplicationResponse(); - response.setWebApplication(webApplication); - webApplication.initialize(); - webApplication.start(); - webApplication.service(request, response); - assertNotNull(webApplication.getAttribute("testAttributeRemoved")); - webApplication.stop(); - } - - /** - * Test attributeReplaced method. - * - * @throws Exception when a serious error occurs. - */ - @Test - void testAttributeReplaced() throws Exception { - WebApplication webApplication = createWebApplication(); - webApplication.addListener(new ServletContextAttributeListener() { - @Override - public void attributeReplaced(ServletContextAttributeEvent event) { - event.getServletContext().setAttribute("testAttributeReplaced", true); - } - }); - webApplication.addServlet("TestAttributeReplacedServlet", new HttpServlet() { - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - ServletContext context = request.getServletContext(); - context.setAttribute("name", "value"); - context.setAttribute("name", "value2"); - } - }); - webApplication.addServletMapping( - "TestAttributeReplacedServlet", - "/testAttributeReplaced"); - WebApplicationRequest request = createWebApplicationRequest(); - request.setServletPath("/testAttributeReplaced"); - request.setWebApplication(webApplication); - WebApplicationResponse response = createWebApplicationResponse(); - response.setWebApplication(webApplication); - webApplication.initialize(); - webApplication.start(); - webApplication.service(request, response); - assertNotNull(webApplication.getAttribute("testAttributeReplaced")); - webApplication.stop(); - } -} diff --git a/core/tests/src/main/java/cloud/piranha/core/tests/ServletInputStreamTest.java b/core/tests/src/main/java/cloud/piranha/core/tests/ServletInputStreamTest.java deleted file mode 100644 index c66b7582c3..0000000000 --- a/core/tests/src/main/java/cloud/piranha/core/tests/ServletInputStreamTest.java +++ /dev/null @@ -1,141 +0,0 @@ -/* - * Copyright (c) 2002-2023 Manorrock.com. All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package cloud.piranha.core.tests; - -import cloud.piranha.core.api.WebApplication; -import cloud.piranha.core.api.WebApplicationRequest; -import cloud.piranha.core.api.WebApplicationResponse; -import jakarta.servlet.ReadListener; -import jakarta.servlet.ServletInputStream; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import org.junit.jupiter.api.Test; - -/** - * The JUnit tests for the ServletInputStream API. - * - * @author Manfred Riem (mriem@manorrock.com) - */ -public abstract class ServletInputStreamTest { - - /** - * Create a web application. - * - * @return the web application. - */ - public abstract WebApplication createWebApplication(); - - /** - * Create a web application request. - * - * @return the web application request. - */ - public abstract WebApplicationRequest createWebApplicationRequest(); - - /** - * Create a web application response. - * - * @return the web application response. - */ - public abstract WebApplicationResponse createWebApplicationResponse(); - - /** - * Test isFinished method. - * - * @throws Exception when a serious error occurs. - */ - void testIsFinished() throws Exception { - WebApplicationRequest request = createWebApplicationRequest(); - ServletInputStream inputStream = request.getInputStream(); - assertFalse(inputStream.isFinished()); - } - - /** - * Test isReady method. - * - * @throws Exception when a serious error occurs. - */ - void testIsReady() throws Exception { - WebApplicationRequest request = createWebApplicationRequest(); - ServletInputStream inputStream = request.getInputStream(); - assertFalse(inputStream.isReady()); - } - - /** - * Test read method. - * - * @throws Exception when a serious error occurs. - */ - @Test - void testRead() throws Exception { - WebApplicationRequest request = createWebApplicationRequest(); - ServletInputStream inputStream = request.getInputStream(); - request.getWebApplicationInputStream() - .setInputStream(new ByteArrayInputStream("a".getBytes())); - int character = inputStream.read(); - assertEquals('a', character); - } - - /** - * Test setReadListener method. - * - * @throws Exception when a serious error occurs. - */ - @Test - void testSetReadListener() throws Exception { - WebApplication webApplication = createWebApplication(); - WebApplicationRequest request = createWebApplicationRequest(); - request.setAsyncSupported(true); - request.setWebApplication(webApplication); - WebApplicationResponse response = createWebApplicationResponse(); - response.setWebApplication(webApplication); - webApplication.linkRequestAndResponse(request, response); - request.startAsync(); - ServletInputStream inputStream = request.getInputStream(); - inputStream.setReadListener(new ReadListener() { - @Override - public void onDataAvailable() throws IOException { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void onAllDataRead() throws IOException { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void onError(Throwable t) { - throw new UnsupportedOperationException("Not supported yet."); - } - }); - assertNotNull(request.getWebApplicationInputStream().getReadListener()); - } -} diff --git a/core/tests/src/main/java/cloud/piranha/core/tests/ServletRegistrationTest.java b/core/tests/src/main/java/cloud/piranha/core/tests/ServletRegistrationTest.java deleted file mode 100644 index 57ca6f6810..0000000000 --- a/core/tests/src/main/java/cloud/piranha/core/tests/ServletRegistrationTest.java +++ /dev/null @@ -1,192 +0,0 @@ -/* - * Copyright (c) 2002-2023 Manorrock.com. All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package cloud.piranha.core.tests; - -import cloud.piranha.core.api.WebApplication; -import jakarta.servlet.ServletRegistration; -import jakarta.servlet.http.HttpServlet; -import java.util.HashMap; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; -import org.junit.jupiter.api.Test; - -/** - * The JUnit tests for the ServletRegistration API. - * - * @author Manfred Riem (mriem@manorrock.com) - */ -public abstract class ServletRegistrationTest { - - /** - * Create the web application. - * - * @return the web application. - */ - public abstract WebApplication createWebApplication(); - - /** - * Test getClassName method. - */ - @Test - void testGetClassName() { - WebApplication webApplication = createWebApplication(); - webApplication.addServlet("testGetClassNameServlet", new TestGetClassNameServlet()); - ServletRegistration registration = webApplication.getServletRegistration("testGetClassNameServlet"); - assertEquals(TestGetClassNameServlet.class.getName(), registration.getClassName()); - } - - /** - * Test getInitParameter method. - */ - @Test - void testGetInitParameter() { - WebApplication webApplication = createWebApplication(); - webApplication.addServlet("testGetInitParameterServlet", new HttpServlet() {}); - ServletRegistration registration = webApplication.getServletRegistration("testGetInitParameterServlet"); - registration.setInitParameter("name", "value"); - assertEquals("value", registration.getInitParameter("name")); - } - /** - * Test getInitParameters method. - */ - @Test - void testGetInitParameters() { - WebApplication webApplication = createWebApplication(); - webApplication.addServlet("testGetInitParametersServlet", new HttpServlet() {}); - ServletRegistration registration = webApplication.getServletRegistration("testGetInitParametersServlet"); - assertNotNull(registration.getInitParameters()); - } - - /** - * Test getName method. - */ - @Test - void testGetName() { - WebApplication webApplication = createWebApplication(); - webApplication.addServlet("testGetNameServlet", new HttpServlet() {}); - assertNotNull("servlet", webApplication.getServletRegistration("testGetNameServlet").getName()); - } - - /** - * Test getRunAsRole method. - */ - @Test - void testGetRunAsRole() { - WebApplication webApplication = createWebApplication(); - webApplication.addServlet("testGetRunAsRoleServlet", new HttpServlet() {}); - ServletRegistration.Dynamic registration = (ServletRegistration.Dynamic) - webApplication.getServletRegistration("testGetRunAsRoleServlet"); - registration.setRunAsRole("role"); - assertNotNull(registration.getRunAsRole()); - assertEquals("role", registration.getRunAsRole()); - } - - /** - * Test getFilterRegistration method. - */ - @Test - void testGetServletRegistration2() { - WebApplication webApplication = createWebApplication(); - webApplication.addServlet("testGetServletRegistration2Servlet", "TestGetServletRegistration2"); - assertNotNull(webApplication.getServletRegistration("testGetServletRegistration2Servlet")); - } - - /** - * Test setInitParameters method. - */ - @Test - void testSetInitParameters() { - WebApplication webApplication = createWebApplication(); - webApplication.addServlet("testSetInitParametersServlet", new HttpServlet() {}); - ServletRegistration registration = webApplication.getServletRegistration("testSetInitParametersServlet"); - registration.setInitParameter("name", "value"); - assertTrue(registration.setInitParameters(new HashMap<>()).isEmpty()); - } - - /** - * Test setInitParameters method. - */ - @Test - void testSetInitParameters2() { - WebApplication webApplication = createWebApplication(); - webApplication.addServlet("testSetInitParameters2Servlet", new HttpServlet() {}); - ServletRegistration registration = webApplication.getServletRegistration("testSetInitParameters2Servlet"); - HashMap parameters = new HashMap<>(); - parameters.put(null, null); - assertThrows(IllegalArgumentException.class, () -> registration.setInitParameters(parameters)); - } - - /** - * Test setInitParameters method. - */ - @Test - void testSetInitParameters3() { - WebApplication webApplication = createWebApplication(); - webApplication.addServlet("testSetInitParameters3Servlet", new HttpServlet() {}); - ServletRegistration registration = webApplication.getServletRegistration("testSetInitParameters3Servlet"); - HashMap parameters = new HashMap<>(); - parameters.put("name", null); - assertThrows(IllegalArgumentException.class, () -> registration.setInitParameters(parameters)); - } - - /** - * Test setInitParameters method. - */ - @Test - void testSetInitParameters4() { - WebApplication webApplication = createWebApplication(); - webApplication.addServlet("testSetInitParameters4Servlet", new HttpServlet() {}); - ServletRegistration registration = webApplication.getServletRegistration("testSetInitParameters4Servlet"); - HashMap parameters = new HashMap<>(); - parameters.put("name", "value"); - assertTrue(registration.setInitParameters(parameters).isEmpty()); - } - - /** - * Test setInitParameters method. - */ - @Test - void testSetInitParameters5() { - WebApplication webApplication = createWebApplication(); - webApplication.addServlet("testSetInitParameters5Servlet", new HttpServlet() {}); - ServletRegistration registration = webApplication.getServletRegistration("testSetInitParameters5Servlet"); - HashMap parameters = new HashMap<>(); - parameters.put("name", "value"); - assertTrue(registration.setInitParameters(parameters).isEmpty()); - assertFalse(registration.setInitParameters(parameters).isEmpty()); - } - - /** - * Test servlet that is used by testGetClassName. - */ - public static class TestGetClassNameServlet extends HttpServlet { - } -} diff --git a/core/tests/src/main/java/cloud/piranha/core/tests/ServletRequestAttributeListenerTest.java b/core/tests/src/main/java/cloud/piranha/core/tests/ServletRequestAttributeListenerTest.java deleted file mode 100644 index 2f3e236dbb..0000000000 --- a/core/tests/src/main/java/cloud/piranha/core/tests/ServletRequestAttributeListenerTest.java +++ /dev/null @@ -1,172 +0,0 @@ -/* - * Copyright (c) 2002-2023 Manorrock.com. All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package cloud.piranha.core.tests; - -import cloud.piranha.core.api.WebApplication; -import cloud.piranha.core.api.WebApplicationRequest; -import cloud.piranha.core.api.WebApplicationResponse; -import jakarta.servlet.ServletException; -import jakarta.servlet.ServletRequestAttributeEvent; -import jakarta.servlet.ServletRequestAttributeListener; -import jakarta.servlet.http.HttpServlet; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import java.io.IOException; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import org.junit.jupiter.api.Test; - -/** - * The JUnit tests for the ServletRequestAttributeListener API. - * - * @author Manfred Riem (mriem@manorrock.com) - */ -public abstract class ServletRequestAttributeListenerTest { - - /** - * Create the web application. - * - * @return the web application. - */ - public abstract WebApplication createWebApplication(); - - /** - * Create the web application request. - * - * @return the web application request. - */ - public abstract WebApplicationRequest createWebApplicationRequest(); - - /** - * Create the web application response. - * - * @return the web application response. - */ - public abstract WebApplicationResponse createWebApplicationResponse(); - - - /** - * Test attributeAdded method. - * - * @throws Exception when a serious error occurs. - */ - @Test - void testAttributeAdded() throws Exception { - WebApplication webApplication = createWebApplication(); - webApplication.addListener(new ServletRequestAttributeListener() { - @Override - public void attributeAdded(ServletRequestAttributeEvent event) { - event.getServletContext().setAttribute("testAttributeAdded", true); - } - }); - webApplication.addServlet("TestAttributeAddedServlet", new HttpServlet() { - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - request.setAttribute("name", "value"); - } - }); - webApplication.addServletMapping("TestAttributeAddedServlet", - "/testAttributeAdded"); - webApplication.initialize(); - webApplication.start(); - WebApplicationRequest request = createWebApplicationRequest(); - request.setServletPath("/testAttributeAdded"); - request.setWebApplication(webApplication); - WebApplicationResponse response = createWebApplicationResponse(); - response.setWebApplication(webApplication); - webApplication.service(request, response); - assertNotNull(webApplication.getAttribute("testAttributeAdded")); - } - - /** - * Test attributeRemoved method. - * - * @throws Exception when a serious error occurs. - */ - @Test - void testAttributeRemoved() throws Exception { - WebApplication webApplication = createWebApplication(); - webApplication.addListener(new ServletRequestAttributeListener() { - @Override - public void attributeRemoved(ServletRequestAttributeEvent event) { - event.getServletContext().setAttribute("testAttributeRemoved", true); - } - }); - webApplication.addServlet("TestAttributeRemovedServlet", new HttpServlet() { - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - request.setAttribute("name", "value"); - request.removeAttribute("name"); - } - }); - webApplication.addServletMapping("TestAttributeRemovedServlet", - "/testAttributeRemoved"); - webApplication.initialize(); - webApplication.start(); - WebApplicationRequest request = createWebApplicationRequest(); - request.setServletPath("/testAttributeRemoved"); - request.setWebApplication(webApplication); - WebApplicationResponse response = createWebApplicationResponse(); - response.setWebApplication(webApplication); - webApplication.service(request, response); - assertNotNull(webApplication.getAttribute("testAttributeRemoved")); - } - - /** - * Test attributeReplaced method. - * - * @throws Exception when a serious error occurs. - */ - @Test - void testAttributeReplaced() throws Exception { - WebApplication webApplication = createWebApplication(); - webApplication.addListener(new ServletRequestAttributeListener() { - @Override - public void attributeRemoved(ServletRequestAttributeEvent event) { - event.getServletContext().setAttribute("testAttributeReplaced", true); - } - }); - webApplication.addServlet("TestAttributeReplacedServlet", new HttpServlet() { - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - request.setAttribute("name", "value"); - request.setAttribute("name", "value2"); - } - }); - webApplication.addServletMapping("TestAttributeReplacedServlet", - "/testAttributeReplaced"); - webApplication.initialize(); - webApplication.start(); - WebApplicationRequest request = createWebApplicationRequest(); - request.setServletPath("/testAttributeReplaced"); - request.setWebApplication(webApplication); - WebApplicationResponse response = createWebApplicationResponse(); - response.setWebApplication(webApplication); - webApplication.service(request, response); - assertNotNull(webApplication.getAttribute("testAttributeReplaced")); - } -} diff --git a/core/tests/src/main/java/cloud/piranha/core/tests/WriteListenerTest.java b/core/tests/src/main/java/cloud/piranha/core/tests/WriteListenerTest.java deleted file mode 100644 index 977e744e4c..0000000000 --- a/core/tests/src/main/java/cloud/piranha/core/tests/WriteListenerTest.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright (c) 2002-2023 Manorrock.com. All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package cloud.piranha.core.tests; - -import cloud.piranha.core.api.WebApplication; -import cloud.piranha.core.api.WebApplicationRequest; -import cloud.piranha.core.api.WebApplicationResponse; -import jakarta.servlet.ServletOutputStream; -import jakarta.servlet.WriteListener; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import org.junit.jupiter.api.Test; - -/** - * The JUnit tests for the WriteListener API. - * - * @author Manfred Riem (mriem@manorrock.com) - */ -public abstract class WriteListenerTest { - - /** - * Create a web application. - * - * @return the web application. - */ - public abstract WebApplication createWebApplication(); - - /** - * Create a web application request. - * - * @return the web application request. - */ - public abstract WebApplicationRequest createWebApplicationRequest(); - - /** - * Create a web application response. - * - * @return the web application response. - */ - public abstract WebApplicationResponse createWebApplicationResponse(); - - /** - * Test onWritePossible method. - * - * @throws Exception when a serious error occurs. - */ - @Test - void testOnWritePossible() throws Exception { - WebApplication webApplication = createWebApplication(); - WebApplicationRequest request = createWebApplicationRequest(); - request.setAsyncSupported(true); - request.setWebApplication(webApplication); - WebApplicationResponse response = createWebApplicationResponse(); - response.setWebApplication(webApplication); - webApplication.linkRequestAndResponse(request, response); - request.startAsync(); - ServletOutputStream outputStream = response.getOutputStream(); - response.getWebApplicationOutputStream().setOutputStream(new ByteArrayOutputStream()); - outputStream.setWriteListener(new TestOnWritePossibleWriteListener(webApplication)); - Thread.sleep(2000); - assertNotNull(webApplication.getAttribute("onWritePossible")); - } - - /** - * A WriteListener for onWritePossible. - */ - public static class TestOnWritePossibleWriteListener implements WriteListener { - - /** - * Stores the web application. - */ - private final WebApplication webApplication; - - /** - * Constructor. - * - * @param webApplication the web application. - */ - public TestOnWritePossibleWriteListener(WebApplication webApplication) { - this.webApplication = webApplication; - } - - @Override - public void onWritePossible() throws IOException { - webApplication.setAttribute("onWritePossible", true); - } - - @Override - public void onError(Throwable t) { - } - } -} diff --git a/core/tests/src/main/java/module-info.java b/core/tests/src/main/java/module-info.java deleted file mode 100644 index 2f30ec72cf..0000000000 --- a/core/tests/src/main/java/module-info.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2002-2023 Manorrock.com. All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -/** - * This module delivers the abstract unit tests that can be used for testing a - * Piranha implementation. - * - * @author Manfred Riem (mriem@manorrock.com) - */ -module cloud.piranha.core.tests { - - exports cloud.piranha.core.tests; - opens cloud.piranha.core.tests; - requires cloud.piranha.core.api; - requires cloud.piranha.resource.impl; - requires org.junit.jupiter.api; -}