Skip to content

Commit

Permalink
Fixes piranhacloud#3604 - Merge core/tests into core/impl (piranhaclo…
Browse files Browse the repository at this point in the history
  • Loading branch information
mnriem authored Jan 4, 2024
1 parent 1a7c5d1 commit 7386486
Show file tree
Hide file tree
Showing 25 changed files with 1,303 additions and 2,042 deletions.
6 changes: 0 additions & 6 deletions core/impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@
<scope>compile</scope>
</dependency>
<!-- test -->
<dependency>
<groupId>cloud.piranha.core</groupId>
<artifactId>piranha-core-tests</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cloud.piranha.http</groupId>
<artifactId>piranha-http-impl</artifactId>
Expand Down
287 changes: 280 additions & 7 deletions core/impl/src/test/java/cloud/piranha/core/impl/AsyncContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 ([email protected])
*/
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 {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -45,34 +45,34 @@
*
* @author Manfred Riem ([email protected])
*/
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.
Expand Down
Loading

0 comments on commit 7386486

Please sign in to comment.