View Javadoc

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