View Javadoc

1   package com.atlassian.plugin.servlet;
2   
3   import javax.servlet.ServletConfig;
4   import javax.servlet.http.HttpServlet;
5   import javax.servlet.http.HttpServletRequest;
6   import javax.servlet.http.HttpServletResponse;
7   
8   import junit.framework.TestCase;
9   
10  import com.atlassian.plugin.Plugin;
11  import com.atlassian.plugin.PluginArtifact;
12  import com.atlassian.plugin.classloader.PluginClassLoader;
13  import com.atlassian.plugin.impl.DefaultDynamicPlugin;
14  import com.atlassian.plugin.servlet.descriptors.ServletModuleDescriptor;
15  import com.atlassian.plugin.servlet.descriptors.ServletModuleDescriptorBuilder;
16  import com.atlassian.plugin.test.PluginTestUtils;
17  import com.mockobjects.dynamic.Mock;
18  
19  public class TestDelegatingPluginServlet extends TestCase
20  {
21      private PluginClassLoader classLoader;
22      private Plugin plugin;
23      private Mock mockRequest;
24      private Mock mockResponse;
25      
26      public void setUp() throws Exception
27      {
28          classLoader = new PluginClassLoader(PluginTestUtils.getFileForResource(PluginTestUtils.SIMPLE_TEST_JAR));
29          plugin = new DefaultDynamicPlugin((PluginArtifact) new Mock(PluginArtifact.class).proxy(), classLoader);
30          
31          mockRequest = new Mock(HttpServletRequest.class);
32          mockRequest.matchAndReturn("getPathInfo", "/servlet/test");
33          mockResponse = new Mock(HttpServletResponse.class);
34      }
35      
36      /**
37       * Test to make sure the plugin class loader is set for the thread context class loader when init is called.
38       * @throws Exception on test error
39       */
40      public void testInitCalledWithPluginClassLoaderAsThreadClassLoader() throws Exception
41      {
42          HttpServlet wrappedServlet = new HttpServlet()
43          {
44              public void init(ServletConfig config)
45              {
46                  assertSame(classLoader, Thread.currentThread().getContextClassLoader());
47              }
48          };
49  
50          getDelegatingServlet(wrappedServlet).init(null);
51      }
52      
53      /**
54       * Test to make sure the plugin class loader is set for the thread context class loader when service is called.
55       * @throws Exception on test error
56       */
57      public void testServiceCalledWithPluginClassLoaderAsThreadClassLoader() throws Exception
58      {
59          HttpServlet wrappedServlet = new HttpServlet()
60          {
61              public void service(HttpServletRequest request, HttpServletResponse response)
62              {
63                  assertSame(classLoader, Thread.currentThread().getContextClassLoader());
64              }
65          };
66  
67          getDelegatingServlet(wrappedServlet).service((HttpServletRequest) mockRequest.proxy(), (HttpServletResponse) mockResponse.proxy());
68      }
69      
70      /**
71       * Test to make sure the servlet is called with our request wrapper.
72       * @throws Exception on test error
73       */
74      public void testServiceCalledWithWrappedRequest() throws Exception
75      {
76          HttpServlet wrappedServlet = new HttpServlet()
77          {
78              public void service(HttpServletRequest request, HttpServletResponse response)
79              {
80                  assertTrue(request instanceof PluginHttpRequestWrapper);
81              }
82          };
83  
84          getDelegatingServlet(wrappedServlet).service((HttpServletRequest) mockRequest.proxy(), (HttpServletResponse) mockResponse.proxy());
85      }
86  
87      private DelegatingPluginServlet getDelegatingServlet(HttpServlet wrappedServlet)
88      {
89          ServletModuleDescriptor descriptor = new ServletModuleDescriptorBuilder()
90              .with(plugin)
91              .with(wrappedServlet)
92              .withPath("/servlet/*")
93              .build();
94          return new DelegatingPluginServlet(descriptor);
95      }
96  }