View Javadoc
1   package com.atlassian.plugin.servlet.descriptors;
2   
3   import com.atlassian.plugin.ModulePermissionException;
4   import com.atlassian.plugin.Permissions;
5   import com.atlassian.plugin.Plugin;
6   import com.atlassian.plugin.PluginParseException;
7   import com.atlassian.plugin.impl.StaticPlugin;
8   import com.atlassian.plugin.module.ModuleFactory;
9   import com.atlassian.plugin.servlet.ServletModuleManager;
10  import org.dom4j.Element;
11  import org.dom4j.dom.DOMElement;
12  import org.junit.After;
13  import org.junit.Before;
14  import org.junit.Rule;
15  import org.junit.Test;
16  import org.junit.rules.ExpectedException;
17  import org.junit.runner.RunWith;
18  import org.mockito.Mock;
19  import org.mockito.junit.MockitoJUnitRunner;
20  
21  import javax.servlet.http.HttpServlet;
22  
23  @RunWith(MockitoJUnitRunner.class)
24  public class TestServletModuleDescriptor {
25      @Rule
26      public final ExpectedException expectedException = ExpectedException.none();
27  
28      private ServletModuleDescriptor descriptor;
29  
30      @Mock
31      private ServletModuleManager servletModuleManager;
32  
33      @Before
34      public void setUp() {
35          descriptor = new ServletModuleDescriptor(ModuleFactory.LEGACY_MODULE_FACTORY, servletModuleManager);
36      }
37  
38      @After
39      public void tearDown() {
40          descriptor = null;
41      }
42  
43      @Test
44      public void initWithExecuteJavaPermission() {
45          Plugin plugin = new StaticPlugin();
46          plugin.setKey("somekey");
47          Permissions.addPermission(plugin, Permissions.EXECUTE_JAVA, null);
48  
49          Element e = getValidConfig();
50          e.addAttribute("class", SomeServlet.class.getName());
51  
52          // test that this does not throw
53          descriptor.init(plugin, e);
54      }
55  
56      @Test
57      public void initWithAllPermission() {
58          Plugin plugin = new StaticPlugin();
59          plugin.setKey("somekey");
60          Permissions.addPermission(plugin, Permissions.ALL_PERMISSIONS, null);
61  
62          Element e = getValidConfig();
63          e.addAttribute("class", SomeServlet.class.getName());
64  
65          // test that this does not throw
66          descriptor.init(plugin, e);
67      }
68  
69      @Test
70      public void initWithoutExecuteJavaPermission() {
71          Plugin plugin = new StaticPlugin();
72          plugin.setKey("somekey");
73          Element e = getValidConfig();
74          e.addAttribute("class", SomeServlet.class.getName());
75  
76          expectedException.expect(ModulePermissionException.class);
77  
78          descriptor.init(plugin, e);
79      }
80  
81      private Element getValidConfig() {
82          Element e = new DOMElement("servlet");
83          e.addAttribute("key", "key2");
84          Element url = new DOMElement("url-pattern");
85          url.setText("/foo");
86          e.add(url);
87          return e;
88      }
89  
90      @Test
91      public void initWithNoUrlPattern() {
92          Plugin plugin = new StaticPlugin();
93          plugin.setKey("somekey");
94          Element e = new DOMElement("servlet");
95          e.addAttribute("key", "key2");
96          e.addAttribute("class", SomeServlet.class.getName());
97  
98          expectedException.expect(PluginParseException.class);
99  
100         descriptor.init(plugin, e);
101     }
102 
103     @Test
104     public void initWithMissingParamValue() {
105         Plugin plugin = new StaticPlugin();
106         plugin.setKey("somekey");
107         Element e = new DOMElement("servlet");
108         e.addAttribute("key", "key2");
109         e.addAttribute("class", SomeServlet.class.getName());
110         Element url = new DOMElement("url-pattern");
111         url.setText("/foo");
112         e.add(url);
113         Element param = new DOMElement("init-param");
114         e.add(param);
115 
116         expectedException.expect(PluginParseException.class);
117 
118         descriptor.init(plugin, e);
119     }
120 
121     @Test
122     public void initWithNoClass() {
123         Plugin plugin = new StaticPlugin();
124         plugin.setKey("somekey");
125         Permissions.addPermission(plugin, Permissions.ALL_PERMISSIONS, null);
126 
127         Element e = getValidConfig();
128 
129         // test that this does not throw
130         descriptor.init(plugin, e);
131     }
132 
133     static class SomeServlet extends HttpServlet {
134     }
135 }