1 package com.atlassian.plugin.servlet.descriptors;
2
3 import com.atlassian.plugin.Plugin;
4 import com.atlassian.plugin.PluginParseException;
5 import com.atlassian.plugin.impl.StaticPlugin;
6 import com.atlassian.plugin.module.ModuleFactory;
7 import com.atlassian.plugin.servlet.ServletModuleManager;
8 import com.mockobjects.dynamic.Mock;
9 import junit.framework.TestCase;
10 import org.dom4j.Element;
11 import org.dom4j.dom.DOMElement;
12
13 import javax.servlet.http.HttpServlet;
14
15 public class TestServletModuleDescriptor extends TestCase
16 {
17 private ServletModuleDescriptor descriptor;
18
19 @Override
20 public void setUp()
21 {
22 descriptor = new ServletModuleDescriptor(ModuleFactory.LEGACY_MODULE_FACTORY, (ServletModuleManager) new Mock(ServletModuleManager.class).proxy());
23 }
24
25 @Override
26 public void tearDown()
27 {
28 descriptor = null;
29 }
30
31 public void testInit()
32 {
33 Plugin plugin = new StaticPlugin();
34 plugin.setKey("somekey");
35 Element e = getValidConfig();
36 descriptor.init(plugin, e);
37 }
38
39 private Element getValidConfig()
40 {
41 Element e = new DOMElement("servlet");
42 e.addAttribute("key", "key2");
43 e.addAttribute("class", SomeServlet.class.getName());
44 Element url = new DOMElement("url-pattern");
45 url.setText("/foo");
46 e.add(url);
47 return e;
48 }
49
50 public void testInitWithNoUrlPattern()
51 {
52 Plugin plugin = new StaticPlugin();
53 plugin.setKey("somekey");
54 Element e = new DOMElement("servlet");
55 e.addAttribute("key", "key2");
56 e.addAttribute("class", SomeServlet.class.getName());
57 try
58 {
59 descriptor.init(plugin, e);
60 fail("Should have thrown exception");
61 }
62 catch (PluginParseException ex)
63 {
64
65 }
66 }
67
68 public void testInitWithMissingParamValue()
69 {
70 Plugin plugin = new StaticPlugin();
71 plugin.setKey("somekey");
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 Element param = new DOMElement("init-param");
79 e.add(param);
80 try
81 {
82 descriptor.init(plugin, e);
83 fail("Should have thrown exception");
84 } catch (PluginParseException ex)
85 {
86
87 }
88 }
89
90 static class SomeServlet extends HttpServlet {}
91 }