1 package com.atlassian.plugin.servlet;
2
3 import com.atlassian.plugin.event.impl.DefaultPluginEventManager;
4 import com.atlassian.plugin.module.ModuleFactory;
5 import com.atlassian.plugin.servlet.descriptors.ServletModuleDescriptor;
6 import org.junit.Test;
7
8 import javax.servlet.RequestDispatcher;
9 import javax.servlet.ServletConfig;
10 import javax.servlet.ServletException;
11 import javax.servlet.UnavailableException;
12 import javax.servlet.http.HttpServlet;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15 import java.io.IOException;
16
17 import static org.hamcrest.MatcherAssert.assertThat;
18 import static org.hamcrest.Matchers.is;
19 import static org.mockito.ArgumentMatchers.anyString;
20 import static org.mockito.ArgumentMatchers.eq;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24
25 public class TestServletModuleContainerServlet {
26
27 @Test
28 public void testServletDoesntUnloadItself() throws IOException, ServletException {
29 ServletModuleManager mockServletModuleManager = mock(ServletModuleManager.class);
30 ModuleFactory mockModuleClassFactory = mock(ModuleFactory.class);
31 ServletModuleDescriptor servletModuleDescriptor = new ServletModuleDescriptor(mockModuleClassFactory, mockServletModuleManager);
32
33
34 final DelegatingPluginServlet delegatingPluginServlet = new DelegatingPluginServlet(servletModuleDescriptor) {
35 public void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException {
36 throw new UnavailableException("Error in plugin servlet");
37 }
38 };
39
40 final ServletModuleManager servletModuleManager = new DefaultServletModuleManager(new DefaultPluginEventManager()) {
41 public DelegatingPluginServlet getServlet(String path, ServletConfig servletConfig) {
42 return delegatingPluginServlet;
43 }
44 };
45
46 HttpServletRequest mockRequest = mock(HttpServletRequest.class);
47 when(mockRequest.getPathInfo()).thenReturn("some/path");
48 HttpServletResponse mockResponse = mock(HttpServletResponse.class);
49
50 ServletModuleContainerServlet servlet = new ServletModuleContainerServlet() {
51 protected ServletModuleManager getServletModuleManager() {
52 return servletModuleManager;
53 }
54 };
55
56 servlet.service(mockRequest, mockResponse);
57 verify(mockResponse).sendError(eq(500), anyString());
58 }
59
60 @Test
61 public void testIncludedServletDispatchesCorrectly() throws IOException, ServletException {
62 HttpServletRequest mockRequest = mock(HttpServletRequest.class);
63 when(mockRequest.getPathInfo()).thenReturn("/original");
64 when(mockRequest.getAttribute(RequestDispatcher.INCLUDE_PATH_INFO)).thenReturn("/included");
65 HttpServletResponse mockResponse = mock(HttpServletResponse.class);
66
67 final MockHttpServlet originalServlet = new MockHttpServlet();
68 final MockHttpServlet includedServlet = new MockHttpServlet();
69
70 final ServletModuleManager servletModuleManager =
71 new DefaultServletModuleManager(new DefaultPluginEventManager()) {
72 @Override
73 public HttpServlet getServlet(String path, ServletConfig servletConfig) throws ServletException {
74 if (path.equals("/original")) {
75 return originalServlet;
76 } else if (path.equals("/included")) {
77 return includedServlet;
78 }
79 return null;
80 }
81 };
82
83 final ServletModuleContainerServlet servlet = new ServletModuleContainerServlet() {
84 @Override
85 protected ServletModuleManager getServletModuleManager() {
86 return servletModuleManager;
87 }
88 };
89
90 servlet.service(mockRequest, mockResponse);
91 assertThat("includedServlet should have been invoked", includedServlet.wasCalled, is(true));
92 assertThat("originalServlet should not have been invoked", originalServlet.wasCalled, is(false));
93 }
94
95 private static class MockHttpServlet extends HttpServlet {
96 private boolean wasCalled = false;
97
98 @Override
99 protected void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
100 throws ServletException, IOException {
101 wasCalled = true;
102 }
103 }
104 }