View Javadoc

1   package com.atlassian.plugin.servlet.descriptors;
2   
3   import com.atlassian.plugin.Permissions;
4   import com.atlassian.plugin.module.PrefixDelegatingModuleFactory;
5   import com.atlassian.plugin.module.PrefixModuleFactory;
6   import com.atlassian.plugin.servlet.filter.FilterDispatcherCondition;
7   import junit.framework.TestCase;
8   
9   import org.dom4j.Element;
10  import org.dom4j.dom.DOMElement;
11  
12  import com.atlassian.plugin.Plugin;
13  import com.atlassian.plugin.PluginParseException;
14  import com.atlassian.plugin.impl.StaticPlugin;
15  import com.atlassian.plugin.servlet.ServletModuleManager;
16  import com.atlassian.plugin.servlet.filter.FilterLocation;
17  import com.atlassian.plugin.servlet.filter.FilterTestUtils.FilterAdapter;
18  import com.mockobjects.dynamic.Mock;
19  
20  import java.util.Collections;
21  
22  import static com.atlassian.plugin.Permissions.addPermission;
23  
24  public class TestServletFilterModuleDescriptor extends TestCase
25  {
26      public static final String PLUGIN_KEY = "somekey";
27  
28      ServletFilterModuleDescriptor descriptor;
29  
30      @Override
31      public void setUp()
32      {
33          descriptor = new ServletFilterModuleDescriptor
34                  ( new PrefixDelegatingModuleFactory(Collections.<PrefixModuleFactory>emptySet()), (ServletModuleManager) new Mock(ServletModuleManager.class).proxy());
35      }
36  
37      @Override
38      public void tearDown()
39      {
40          descriptor = null;
41      }
42      
43      public void testInit() 
44      {
45          final Plugin plugin = newPlugin(PLUGIN_KEY);
46          Element e = getValidConfig();
47          descriptor.init(plugin, e);
48          assertEquals(FilterLocation.BEFORE_DISPATCH, descriptor.getLocation());
49          assertEquals(100, descriptor.getWeight());
50      }
51  
52      private Plugin newPlugin(String key)
53      {
54          Plugin plugin = new StaticPlugin();
55          plugin.setKey(key);
56          return addPermission(plugin, Permissions.EXECUTE_JAVA, null);
57      }
58  
59      private Element getValidConfig()
60      {
61          Element e = new DOMElement("servlet-filter");
62          e.addAttribute("key", "key2");
63          e.addAttribute("class", FilterAdapter.class.getName());
64          Element url = new DOMElement("url-pattern");
65          url.setText("/foo");
66          e.add(url);
67          Element dispatcher1 = new DOMElement("dispatcher");
68          dispatcher1.setText("REQUEST");
69          e.add(dispatcher1);
70          Element dispatcher2 = new DOMElement("dispatcher");
71          dispatcher2.setText("FORWARD");
72          e.add(dispatcher2);
73          return e;
74      }
75  
76      public void testInitWithNoUrlPattern() 
77      {
78          Plugin plugin = newPlugin(PLUGIN_KEY);
79          Element e = new DOMElement("servlet-filter");
80          e.addAttribute("key", "key2");
81          e.addAttribute("class", FilterAdapter.class.getName());
82          try
83          {
84              descriptor.init(plugin, e);
85              fail("Should have thrown exception");
86          }
87          catch (PluginParseException ex)
88          {
89              // very good
90          }
91      }
92  
93      public void testInitWithDetails()
94      {
95          Plugin plugin = newPlugin(PLUGIN_KEY);
96          Element e = getValidConfig();
97          e.addAttribute("location", "after-encoding");
98          e.addAttribute("weight", "122");
99          descriptor.init(plugin, e);
100         assertEquals(FilterLocation.AFTER_ENCODING, descriptor.getLocation());
101         assertEquals(122, descriptor.getWeight());
102         assertTrue(descriptor.getDispatcherConditions().contains(FilterDispatcherCondition.REQUEST));
103         assertTrue(descriptor.getDispatcherConditions().contains(FilterDispatcherCondition.FORWARD));
104     }
105 
106     public void testInitWithBadLocation()
107     {
108         Plugin plugin = newPlugin(PLUGIN_KEY);
109         Element e = getValidConfig();
110         e.addAttribute("location", "t23op");
111         try
112         {
113             descriptor.init(plugin, e);
114             fail("Should have thrown exception");
115         }
116         catch (PluginParseException ex)
117         {
118             // very good
119         }
120     }
121 
122     public void testInitWithBadWeight()
123     {
124         Plugin plugin = newPlugin(PLUGIN_KEY);
125         Element e = getValidConfig();
126         e.addAttribute("weight", "t23op");
127         try
128         {
129             descriptor.init(plugin, e);
130             fail("Should have thrown exception");
131         }
132         catch (PluginParseException ex)
133         {
134             // very good
135         }
136     }
137 
138     public void testInitWithBadDispatcher()
139     {
140         Plugin plugin = newPlugin(PLUGIN_KEY);
141         Element e = getValidConfig();
142         Element badDispatcher = new DOMElement("dispatcher");
143         badDispatcher.setText("badValue");
144         e.add(badDispatcher);
145         try
146         {
147             descriptor.init(plugin, e);
148             fail("Should have thrown exception");
149         }
150         catch (PluginParseException ex)
151         {
152             // very good
153         }
154     }
155 
156     public void testWithNoDispatcher()
157     {
158         Plugin plugin = newPlugin(PLUGIN_KEY);
159 
160         Element e = new DOMElement("servlet-filter");
161         e.addAttribute("key", "key2");
162         e.addAttribute("class", FilterAdapter.class.getName());
163         Element url = new DOMElement("url-pattern");
164         url.setText("/foo");
165         e.add(url);
166 
167         descriptor.init(plugin, e);
168     }
169 }