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