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