View Javadoc

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