1 package com.atlassian.plugin.servlet;
2
3 import java.io.IOException;
4
5 import javax.servlet.ServletConfig;
6 import javax.servlet.ServletException;
7 import javax.servlet.UnavailableException;
8 import javax.servlet.http.HttpServlet;
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11
12 import com.atlassian.plugin.module.ModuleFactory;
13 import com.mockobjects.dynamic.AnyConstraintMatcher;
14 import junit.framework.TestCase;
15
16 import com.atlassian.plugin.event.impl.DefaultPluginEventManager;
17 import com.atlassian.plugin.servlet.descriptors.ServletModuleDescriptor;
18 import com.mockobjects.dynamic.C;
19 import com.mockobjects.dynamic.Mock;
20
21 public class TestServletModuleContainerServlet extends TestCase
22 {
23
24 public void testServletDoesntUnloadItself() throws IOException, ServletException
25 {
26 Mock mockServletModuleManager = new Mock(ServletModuleManager.class);
27 Mock mockModuleClassFactory = new Mock(ModuleFactory.class);
28 mockModuleClassFactory.expectAndReturn("createModule", new AnyConstraintMatcher(), null);
29 ServletModuleDescriptor servletModuleDescriptor = new ServletModuleDescriptor((ModuleFactory) mockModuleClassFactory.proxy(), (ServletModuleManager) mockServletModuleManager.proxy());
30
31
32 final DelegatingPluginServlet delegatingPluginServlet = new DelegatingPluginServlet(servletModuleDescriptor)
33 {
34 public void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException
35 {
36 throw new UnavailableException("Error in plugin servlet");
37 }
38 };
39
40 final ServletModuleManager servletModuleManager = new DefaultServletModuleManager(new DefaultPluginEventManager())
41 {
42 public DelegatingPluginServlet getServlet(String path, ServletConfig servletConfig)
43 {
44 return delegatingPluginServlet;
45 }
46 };
47
48 Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
49 mockHttpServletRequest.matchAndReturn("getAttribute", C.anyArgs(1), null);
50 mockHttpServletRequest.expectAndReturn("getPathInfo", "confluence");
51 Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
52 mockHttpServletResponse.expect("sendError", C.args(C.eq(500), C.isA(String.class)));
53
54 ServletModuleContainerServlet servlet = new ServletModuleContainerServlet()
55 {
56 protected ServletModuleManager getServletModuleManager()
57 {
58 return servletModuleManager;
59 }
60 };
61
62 servlet.service((HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy());
63 }
64
65 public void testIncludedServletDispatchesCorrectly() throws IOException, ServletException
66 {
67 final Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
68 mockHttpServletRequest.matchAndReturn("getPathInfo", "/original");
69 mockHttpServletRequest.expectAndReturn("getAttribute", "javax.servlet.include.path_info", "/included");
70 final Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
71
72 final MockHttpServlet originalServlet = new MockHttpServlet();
73 final MockHttpServlet includedServlet = new MockHttpServlet();
74
75 final ServletModuleManager servletModuleManager =
76 new DefaultServletModuleManager(new DefaultPluginEventManager())
77 {
78 @Override
79 public HttpServlet getServlet(String path, ServletConfig servletConfig) throws ServletException
80 {
81 if (path.equals("/original"))
82 {
83 return originalServlet;
84 }
85 else if (path.equals("/included"))
86 {
87 return includedServlet;
88 }
89 return null;
90 }
91 };
92
93 final ServletModuleContainerServlet servlet = new ServletModuleContainerServlet()
94 {
95 @Override
96 protected ServletModuleManager getServletModuleManager()
97 {
98 return servletModuleManager;
99 }
100 };
101
102 servlet.service((HttpServletRequest) mockHttpServletRequest.proxy(),
103 (HttpServletResponse) mockHttpServletResponse.proxy());
104
105 assertTrue("includedServlet should have been invoked", includedServlet.wasCalled);
106 assertFalse("originalServlet should not have been invoked", originalServlet.wasCalled);
107 }
108
109 private static class MockHttpServlet extends HttpServlet
110 {
111 private boolean wasCalled = false;
112
113 @Override
114 protected void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
115 throws ServletException, IOException
116 {
117 wasCalled = true;
118 }
119 }
120 }