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 com.google.common.collect.ImmutableSet;
11 import com.mockobjects.dynamic.Mock;
12 import junit.framework.TestCase;
13 import org.dom4j.Element;
14 import org.dom4j.dom.DOMElement;
15
16 import javax.servlet.http.HttpServlet;
17
18 public class TestServletModuleDescriptor extends TestCase
19 {
20 private ServletModuleDescriptor descriptor;
21
22 @Override
23 public void setUp()
24 {
25 descriptor = new ServletModuleDescriptor(ModuleFactory.LEGACY_MODULE_FACTORY, (ServletModuleManager) new Mock(ServletModuleManager.class).proxy());
26 }
27
28 @Override
29 public void tearDown()
30 {
31 descriptor = null;
32 }
33
34 public void testInitWithExecuteJavaPermission()
35 {
36 Plugin plugin = new StaticPlugin();
37 plugin.setKey("somekey");
38 Permissions.addPermission(plugin, Permissions.EXECUTE_JAVA, null);
39
40 Element e = getValidConfig();
41 descriptor.init(plugin, e);
42 }
43
44 public void testInitWithAllPermission()
45 {
46 Plugin plugin = new StaticPlugin();
47 plugin.setKey("somekey");
48 Permissions.addPermission(plugin, Permissions.ALL_PERMISSIONS, null);
49
50 Element e = getValidConfig();
51 descriptor.init(plugin, e);
52 }
53
54 public void testInitWithoutExecuteJavaPermission()
55 {
56 Plugin plugin = new StaticPlugin();
57 plugin.setKey("somekey");
58 Element e = getValidConfig();
59 try
60 {
61 descriptor.init(plugin, e);
62 fail("Was expecting exception");
63 }
64 catch (ModulePermissionException ex)
65 {
66 assertEquals(ImmutableSet.of(Permissions.EXECUTE_JAVA), ex.getPermissions());
67 }
68 }
69
70 private Element getValidConfig()
71 {
72 Element e = new DOMElement("servlet");
73 e.addAttribute("key", "key2");
74 e.addAttribute("class", SomeServlet.class.getName());
75 Element url = new DOMElement("url-pattern");
76 url.setText("/foo");
77 e.add(url);
78 return e;
79 }
80
81 public void testInitWithNoUrlPattern()
82 {
83 Plugin plugin = new StaticPlugin();
84 plugin.setKey("somekey");
85 Element e = new DOMElement("servlet");
86 e.addAttribute("key", "key2");
87 e.addAttribute("class", SomeServlet.class.getName());
88 try
89 {
90 descriptor.init(plugin, e);
91 fail("Should have thrown exception");
92 }
93 catch (PluginParseException ex)
94 {
95
96 }
97 }
98
99 public void testInitWithMissingParamValue()
100 {
101 Plugin plugin = new StaticPlugin();
102 plugin.setKey("somekey");
103 Element e = new DOMElement("servlet");
104 e.addAttribute("key", "key2");
105 e.addAttribute("class", SomeServlet.class.getName());
106 Element url = new DOMElement("url-pattern");
107 url.setText("/foo");
108 e.add(url);
109 Element param = new DOMElement("init-param");
110 e.add(param);
111 try
112 {
113 descriptor.init(plugin, e);
114 fail("Should have thrown exception");
115 } catch (PluginParseException ex)
116 {
117
118 }
119 }
120
121 static class SomeServlet extends HttpServlet {}
122 }