Clover Coverage Report - Atlassian XWork(Aggregated)
Coverage timestamp: Wed Jul 27 2011 23:39:31 CDT
175   385   27   6.48
0   305   0.15   5.4
27     1  
5    
 
 
  TestRestrictHttpMethodInterceptor       Line # 26 142 12 100% 1.0
  TestRestrictHttpMethodInterceptor.UnAnnotatedAction       Line # 36 3 3 66.7% 0.6666667
  TestRestrictHttpMethodInterceptor.AnnotatedAction       Line # 57 5 5 100% 1.0
  TestRestrictHttpMethodInterceptor.SimpleInterceptor       Line # 93 1 1 100% 1.0
  TestRestrictHttpMethodInterceptor.CustomConfigurationProvider       Line # 102 24 6 90% 0.9
 
  (5)
 
1    package com.atlassian.xwork.interceptors;
2   
3    import com.opensymphony.xwork.config.ConfigurationProvider;
4    import com.opensymphony.xwork.config.Configuration;
5    import com.opensymphony.xwork.config.ConfigurationException;
6    import com.opensymphony.xwork.config.ConfigurationManager;
7    import com.opensymphony.xwork.config.entities.PackageConfig;
8    import com.opensymphony.xwork.config.entities.ActionConfig;
9    import com.opensymphony.xwork.interceptor.Interceptor;
10    import com.opensymphony.xwork.ActionSupport;
11    import com.opensymphony.xwork.ActionProxy;
12    import com.opensymphony.xwork.ActionProxyFactory;
13    import com.opensymphony.xwork.Action;
14    import com.opensymphony.webwork.ServletActionContext;
15    import com.opensymphony.webwork.WebWorkStatics;
16    import com.atlassian.xwork.PermittedMethods;
17    import com.atlassian.xwork.HttpMethod;
18   
19    import java.util.*;
20   
21    import javax.servlet.http.HttpServletRequest;
22   
23    import org.jmock.Mock;
24    import org.jmock.MockObjectTestCase;
25   
 
26    public class TestRestrictHttpMethodInterceptor extends MockObjectTestCase
27    {
28    /*
29    This is a reasonably complicated test because I wanted to ensure the interceptor works within the xwork
30    framework (instead of mocking out how I expected the framework MIGHT work).
31    */
32   
33    private RestrictHttpMethodInterceptor.SecurityLevel securityLevel;
34    private Mock mockServletRequest;
35   
 
36    public static class UnAnnotatedAction extends ActionSupport
37    {
 
38  64 toggle @Override
39    public String execute() throws Exception
40    {
41  64 return SUCCESS;
42    }
43   
 
44  11 toggle @Override
45    public String doDefault() throws Exception
46    {
47  11 return SUCCESS;
48   
49    }
50   
 
51  0 toggle public String doEdit() throws Exception
52    {
53  0 return SUCCESS;
54    }
55    }
56   
 
57    public static class AnnotatedAction extends ActionSupport
58    {
 
59  26 toggle @Override
60    @PermittedMethods({HttpMethod.POST, HttpMethod.GET})
61    public String execute() throws Exception
62    {
63  26 return SUCCESS;
64    }
65   
 
66  10 toggle @Override
67    @PermittedMethods({HttpMethod.POST, HttpMethod.PUT})
68    public String doDefault() throws Exception
69    {
70  10 return SUCCESS;
71   
72    }
73   
 
74  15 toggle @PermittedMethods({HttpMethod.ANY_METHOD})
75    public String anyMethod() throws Exception
76    {
77  15 return SUCCESS;
78    }
79   
 
80  9 toggle @PermittedMethods({HttpMethod.ALL_RFC2616})
81    public String allRfc2616() throws Exception
82    {
83  9 return SUCCESS;
84    }
85   
 
86  6 toggle @PermittedMethods({HttpMethod.ALL_DEFINED})
87    public String allDefined() throws Exception
88    {
89  6 return SUCCESS;
90    }
91    }
92   
 
93    private class SimpleInterceptor extends RestrictHttpMethodInterceptor
94    {
 
95  222 toggle @Override
96    protected SecurityLevel getSecurityLevel()
97    {
98  222 return securityLevel;
99    }
100    }
101   
 
102    private class CustomConfigurationProvider implements ConfigurationProvider
103    {
104    private List<Interceptor> interceptors;
105   
 
106  0 toggle public void destroy()
107    {
108    }
109   
 
110  9 toggle public void init(Configuration configuration) throws ConfigurationException
111    {
112  9 PackageConfig packageConfig = new PackageConfig();
113  9 interceptors = new ArrayList<Interceptor>();
114  9 interceptors.add(new SimpleInterceptor());
115   
116    // Note: XWork mapps a null method name to execute(), but config#getMethodName() still returns null so we
117    // still test it separately just in case.
118   
119  9 packageConfig.addActionConfig("unannotated-execute", makeActionConfig(UnAnnotatedAction.class, "execute"));
120  9 packageConfig.addActionConfig("unannotated-null", makeActionConfig(UnAnnotatedAction.class, null));
121  9 packageConfig.addActionConfig("unannotated-dodefault", makeActionConfig(UnAnnotatedAction.class, "doDefault"));
122  9 packageConfig.addActionConfig("unannotated-doedit", makeActionConfig(UnAnnotatedAction.class, "doEdit"));
123   
124  9 packageConfig.addActionConfig("configured-null", makeActionConfig(UnAnnotatedAction.class, null, "GET, POST"));
125  9 packageConfig.addActionConfig("configured-execute", makeActionConfig(UnAnnotatedAction.class, "execute", "GET, POST"));
126  9 packageConfig.addActionConfig("configured-dodefault", makeActionConfig(UnAnnotatedAction.class, "execute", "POST"));
127  9 packageConfig.addActionConfig("configured-doedit", makeActionConfig(UnAnnotatedAction.class, "execute", " GET, PUT , POST,CHEESE"));
128  9 packageConfig.addActionConfig("configured-anymethod", makeActionConfig(UnAnnotatedAction.class, "execute", "ANY_METHOD"));
129  9 packageConfig.addActionConfig("configured-rfc2616", makeActionConfig(UnAnnotatedAction.class, "execute", "ALL_RFC2616"));
130   
131  9 packageConfig.addActionConfig("annotated-execute", makeActionConfig(AnnotatedAction.class, "execute"));
132  9 packageConfig.addActionConfig("annotated-null", makeActionConfig(AnnotatedAction.class, null));
133  9 packageConfig.addActionConfig("annotated-dodefault", makeActionConfig(AnnotatedAction.class, "doDefault"));
134  9 packageConfig.addActionConfig("annotated-anymethod", makeActionConfig(AnnotatedAction.class, "anyMethod"));
135  9 packageConfig.addActionConfig("annotated-allrfc", makeActionConfig(AnnotatedAction.class, "allRfc2616"));
136  9 packageConfig.addActionConfig("annotated-alldefined", makeActionConfig(AnnotatedAction.class, "allDefined"));
137   
138  9 configuration.addPackageConfig("defaultPackage", packageConfig);
139    }
140   
 
141  54 toggle private ActionConfig makeActionConfig(Class actionClass, String methodName, String permittedMethodsParameter)
142    {
143  54 return makeActionConfig(actionClass, methodName, Collections.singletonMap(RestrictHttpMethodInterceptor.PERMITTED_METHODS_PARAM_NAME, permittedMethodsParameter));
144    }
145   
 
146  144 toggle private ActionConfig makeActionConfig(Class actionClass, String methodName, Map parameterMap)
147    {
148  144 return new ActionConfig(methodName, actionClass, parameterMap, new HashMap(), interceptors);
149    }
150   
 
151  90 toggle private ActionConfig makeActionConfig(Class actionClass, String methodName)
152    {
153  90 return makeActionConfig(actionClass, methodName, new HashMap());
154    }
155   
 
156  0 toggle public boolean needsReload()
157    {
158  0 return false;
159    }
160    }
161   
 
162  5 toggle @Override
163    protected void setUp() throws Exception
164    {
165  5 super.setUp();
166   
167  5 ConfigurationManager.addConfigurationProvider(new CustomConfigurationProvider());
168  5 ConfigurationManager.getConfiguration().reload();
169   
170  5 mockServletRequest = new Mock(HttpServletRequest.class);
171  5 ServletActionContext.setRequest((HttpServletRequest) mockServletRequest.proxy());
172    }
173   
 
174  5 toggle @Override
175    protected void tearDown() throws Exception
176    {
177  5 ServletActionContext.setRequest(null);
178  5 ConfigurationManager.destroyConfiguration();
179  5 ConfigurationManager.clearConfigurationProviders();
180  5 super.tearDown();
181    }
182   
 
183  1 toggle public void testSecurityLevelNone() throws Exception
184    {
185  1 this.securityLevel = RestrictHttpMethodInterceptor.SecurityLevel.NONE;
186   
187  1 testInterceptor("unannotated-execute", "GET", Action.SUCCESS);
188  1 testInterceptor("unannotated-execute", "POST", Action.SUCCESS);
189  1 testInterceptor("unannotated-execute", "PROPFIND", Action.SUCCESS);
190  1 testInterceptor("unannotated-execute", "MONKEY", Action.SUCCESS);
191   
192  1 testInterceptor("unannotated-null", "GET", Action.SUCCESS);
193  1 testInterceptor("unannotated-null", "POST", Action.SUCCESS);
194  1 testInterceptor("unannotated-null", "PROPFIND", Action.SUCCESS);
195  1 testInterceptor("unannotated-null", "MONKEY", Action.SUCCESS);
196   
197  1 testInterceptor("unannotated-dodefault", "GET", Action.SUCCESS);
198  1 testInterceptor("unannotated-dodefault", "POST", Action.SUCCESS);
199  1 testInterceptor("unannotated-dodefault", "PROPFIND", Action.SUCCESS);
200  1 testInterceptor("unannotated-dodefault", "MONKEY", Action.SUCCESS);
201   
202  1 testInterceptor("annotated-execute", "GET", Action.SUCCESS);
203  1 testInterceptor("annotated-execute", "POST", Action.SUCCESS);
204  1 testInterceptor("annotated-execute", "PROPFIND", Action.SUCCESS);
205  1 testInterceptor("annotated-execute", "MONKEY", Action.SUCCESS);
206   
207  1 testInterceptor("annotated-null", "GET", Action.SUCCESS);
208  1 testInterceptor("annotated-null", "POST", Action.SUCCESS);
209  1 testInterceptor("annotated-null", "PROPFIND", Action.SUCCESS);
210  1 testInterceptor("annotated-null", "MONKEY", Action.SUCCESS);
211   
212  1 testInterceptor("annotated-dodefault", "GET", Action.SUCCESS);
213  1 testInterceptor("annotated-dodefault", "POST", Action.SUCCESS);
214  1 testInterceptor("annotated-dodefault", "PROPFIND", Action.SUCCESS);
215  1 testInterceptor("annotated-dodefault", "MONKEY", Action.SUCCESS); }
216   
 
217  1 toggle public void testSecurityLevelOptIn() throws Exception
218    {
219  1 this.securityLevel = RestrictHttpMethodInterceptor.SecurityLevel.OPT_IN;
220   
221  1 testInterceptor("unannotated-execute", "GET", Action.SUCCESS);
222  1 testInterceptor("unannotated-execute", "POST", Action.SUCCESS);
223  1 testInterceptor("unannotated-execute", "PROPFIND", Action.SUCCESS);
224  1 testInterceptor("unannotated-execute", "MONKEY", Action.SUCCESS);
225   
226  1 testInterceptor("unannotated-null", "GET", Action.SUCCESS);
227  1 testInterceptor("unannotated-null", "POST", Action.SUCCESS);
228  1 testInterceptor("unannotated-null", "PROPFIND", Action.SUCCESS);
229  1 testInterceptor("unannotated-null", "MONKEY", Action.SUCCESS);
230   
231  1 testInterceptor("unannotated-dodefault", "GET", Action.SUCCESS);
232  1 testInterceptor("unannotated-dodefault", "POST", Action.SUCCESS);
233  1 testInterceptor("unannotated-dodefault", "PROPFIND", Action.SUCCESS);
234  1 testInterceptor("unannotated-dodefault", "MONKEY", Action.SUCCESS);
235   
236  1 assertAnnotationsRespected();
237    }
238   
 
239  1 toggle public void testSecurityLevelDefault() throws Exception
240    {
241  1 this.securityLevel = RestrictHttpMethodInterceptor.SecurityLevel.DEFAULT;
242   
243  1 testInterceptor("unannotated-execute", "GET", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
244  1 testInterceptor("unannotated-execute", "POST", Action.SUCCESS);
245  1 testInterceptor("unannotated-execute", "PROPFIND", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
246  1 testInterceptor("unannotated-execute", "MONKEY", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
247   
248  1 testInterceptor("unannotated-null", "GET", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
249  1 testInterceptor("unannotated-null", "POST", Action.SUCCESS);
250  1 testInterceptor("unannotated-null", "PROPFIND", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
251  1 testInterceptor("unannotated-null", "MONKEY", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
252   
253  1 testInterceptor("unannotated-dodefault", "GET", Action.SUCCESS);
254  1 testInterceptor("unannotated-dodefault", "HEAD", Action.SUCCESS);
255  1 testInterceptor("unannotated-dodefault", "POST", Action.SUCCESS);
256  1 testInterceptor("unannotated-dodefault", "PROPFIND", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
257  1 testInterceptor("unannotated-dodefault", "MONKEY", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
258   
259  1 assertAnnotationsRespected();
260    }
261   
 
262  1 toggle public void testNoRequest() throws Exception
263    {
264  1 this.securityLevel = RestrictHttpMethodInterceptor.SecurityLevel.OPT_IN;
265   
266  1 testInterceptorWithNoRequest("annotated-execute", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
267  1 testInterceptorWithNoRequest("unannotated-execute", Action.SUCCESS);
268    }
269   
 
270  1 toggle public void testSecurityLevelStrict() throws Exception
271    {
272  1 this.securityLevel = RestrictHttpMethodInterceptor.SecurityLevel.STRICT;
273   
274  1 testInterceptor("unannotated-execute", "GET", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
275  1 testInterceptor("unannotated-execute", "POST", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
276  1 testInterceptor("unannotated-execute", "PROPFIND", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
277  1 testInterceptor("unannotated-execute", "MONKEY", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
278   
279  1 testInterceptor("unannotated-null", "GET", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
280  1 testInterceptor("unannotated-null", "POST", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
281  1 testInterceptor("unannotated-null", "PROPFIND", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
282  1 testInterceptor("unannotated-null", "MONKEY", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
283   
284  1 testInterceptor("unannotated-dodefault", "GET", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
285  1 testInterceptor("unannotated-dodefault", "POST", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
286  1 testInterceptor("unannotated-dodefault", "PROPFIND", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
287  1 testInterceptor("unannotated-dodefault", "MONKEY", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
288   
289  1 assertAnnotationsRespected();
290    }
291   
 
292  3 toggle private void assertAnnotationsRespected() throws Exception
293    {
294  3 testInterceptor("annotated-execute", "GET", Action.SUCCESS);
295  3 testInterceptor("annotated-execute", "HEAD", Action.SUCCESS);
296  3 testInterceptor("annotated-execute", "POST", Action.SUCCESS);
297  3 testInterceptor("annotated-execute", "PROPFIND", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
298  3 testInterceptor("annotated-execute", "MONKEY", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
299   
300  3 testInterceptor("annotated-null", "GET", Action.SUCCESS);
301  3 testInterceptor("annotated-null", "HEAD", Action.SUCCESS);
302  3 testInterceptor("annotated-null", "POST", Action.SUCCESS);
303  3 testInterceptor("annotated-null", "PROPFIND", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
304  3 testInterceptor("annotated-null", "MONKEY", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
305   
306  3 testInterceptor("annotated-dodefault", "GET", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
307  3 testInterceptor("annotated-dodefault", "POST", Action.SUCCESS);
308  3 testInterceptor("annotated-dodefault", "PROPFIND", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
309  3 testInterceptor("annotated-dodefault", "PUT", Action.SUCCESS);
310   
311  3 testInterceptor("annotated-allrfc", "GET", Action.SUCCESS);
312  3 testInterceptor("annotated-allrfc", "HEAD", Action.SUCCESS);
313  3 testInterceptor("annotated-allrfc", "POST", Action.SUCCESS);
314  3 testInterceptor("annotated-allrfc", "PROPFIND", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
315  3 testInterceptor("annotated-allrfc", "MONKEY", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
316   
317  3 testInterceptor("annotated-anymethod", "GET", Action.SUCCESS);
318  3 testInterceptor("annotated-anymethod", "HEAD", Action.SUCCESS);
319  3 testInterceptor("annotated-anymethod", "POST", Action.SUCCESS);
320  3 testInterceptor("annotated-anymethod", "PROPFIND", Action.SUCCESS);
321  3 testInterceptor("annotated-anymethod", "MONKEY", Action.SUCCESS);
322   
323  3 testInterceptor("annotated-alldefined", "GET", Action.SUCCESS);
324  3 testInterceptor("annotated-alldefined", "PROPFIND", Action.SUCCESS);
325  3 testInterceptor("annotated-alldefined", "CHEESE", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
326   
327  3 testInterceptor("configured-execute", "GET", Action.SUCCESS);
328  3 testInterceptor("configured-execute", "HEAD", Action.SUCCESS);
329  3 testInterceptor("configured-execute", "POST", Action.SUCCESS);
330  3 testInterceptor("configured-execute", "PROPFIND", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
331  3 testInterceptor("configured-execute", "MONKEY", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
332   
333  3 testInterceptor("configured-null", "GET", Action.SUCCESS);
334  3 testInterceptor("configured-null", "HEAD", Action.SUCCESS);
335  3 testInterceptor("configured-null", "POST", Action.SUCCESS);
336  3 testInterceptor("configured-null", "PROPFIND", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
337  3 testInterceptor("configured-null", "MONKEY", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
338   
339  3 testInterceptor("configured-dodefault", "GET", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
340  3 testInterceptor("configured-dodefault", "POST", Action.SUCCESS);
341  3 testInterceptor("configured-dodefault", "PROPFIND", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
342  3 testInterceptor("configured-dodefault", "MONKEY", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
343   
344  3 testInterceptor("configured-doedit", "GET", Action.SUCCESS);
345  3 testInterceptor("configured-doedit", "HEAD", Action.SUCCESS);
346  3 testInterceptor("configured-doedit", "PUT", Action.SUCCESS);
347  3 testInterceptor("configured-doedit", "POST", Action.SUCCESS);
348  3 testInterceptor("configured-doedit", "PROPFIND", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
349    // Not really a method, so it's not even allowed to be configured.
350  3 testInterceptor("configured-doedit", "CHEESE", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
351   
352  3 testInterceptor("configured-anymethod", "GET", Action.SUCCESS);
353  3 testInterceptor("configured-anymethod", "PROPFIND", Action.SUCCESS);
354  3 testInterceptor("configured-anymethod", "CHEESE", Action.SUCCESS);
355   
356  3 testInterceptor("configured-rfc2616", "GET", Action.SUCCESS);
357  3 testInterceptor("configured-rfc2616", "PROPFIND", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
358  3 testInterceptor("configured-rfc2616", "CHEESE", RestrictHttpMethodInterceptor.INVALID_METHOD_RESULT);
359    }
360   
 
361  220 toggle private void testInterceptor(String actionAlias, String httpMethod, String expectedResult) throws Exception
362    {
363  220 ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy("", actionAlias, makeContext(), false);
364  220 matchMethod(httpMethod);
365  220 assertEquals("Testing " + httpMethod + " against " + actionAlias, expectedResult, proxy.execute());
366    }
367   
 
368  2 toggle private void testInterceptorWithNoRequest(String actionAlias, String expectedResult) throws Exception
369    {
370  2 ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy("", actionAlias, new HashMap(), false);
371  2 assertEquals("Testing no request against " + actionAlias, expectedResult, proxy.execute());
372    }
373   
 
374  220 toggle private HashMap makeContext()
375    {
376  220 HashMap<String, Object> context = new HashMap<String, Object>();
377  220 context.put(WebWorkStatics.HTTP_REQUEST, mockServletRequest.proxy());
378  220 return context;
379    }
380   
 
381  220 toggle private void matchMethod(String httpMethodName)
382    {
383  220 mockServletRequest.stubs().method("getMethod").withNoArguments().will(returnValue(httpMethodName));
384    }
385    }