1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.interceptor;
19
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import org.apache.struts2.StrutsTestCase;
24
25 import com.mockobjects.dynamic.C;
26 import com.mockobjects.dynamic.Mock;
27 import com.opensymphony.xwork2.Action;
28 import com.opensymphony.xwork2.ActionContext;
29 import com.opensymphony.xwork2.ActionInvocation;
30 import com.opensymphony.xwork2.ActionSupport;
31 import com.opensymphony.xwork2.util.ValueStack;
32 import com.opensymphony.xwork2.util.ValueStackFactory;
33
34
35 /***
36 * StrutsConversionErrorInterceptorTest
37 *
38 */
39 public class StrutsConversionErrorInterceptorTest extends StrutsTestCase {
40
41 protected ActionContext context;
42 protected ActionInvocation invocation;
43 protected Map conversionErrors;
44 protected Mock mockInvocation;
45 protected ValueStack stack;
46 protected StrutsConversionErrorInterceptor interceptor;
47
48
49 public void testEmptyValuesDoNotSetFieldErrors() throws Exception {
50 conversionErrors.put("foo", new Long(123));
51 conversionErrors.put("bar", "");
52 conversionErrors.put("baz", new String[]{""});
53
54 ActionSupport action = new ActionSupport();
55 mockInvocation.expectAndReturn("getAction", action);
56 stack.push(action);
57 mockInvocation.matchAndReturn("getAction",action);
58 assertNull(action.getFieldErrors().get("foo"));
59 assertNull(action.getFieldErrors().get("bar"));
60 assertNull(action.getFieldErrors().get("baz"));
61 interceptor.intercept(invocation);
62 assertTrue(action.hasFieldErrors());
63 assertNotNull(action.getFieldErrors().get("foo"));
64 assertNull(action.getFieldErrors().get("bar"));
65 assertNull(action.getFieldErrors().get("baz"));
66 }
67
68 public void testFieldErrorAdded() throws Exception {
69 conversionErrors.put("foo", new Long(123));
70
71 ActionSupport action = new ActionSupport();
72 mockInvocation.expectAndReturn("getAction", action);
73 stack.push(action);
74 mockInvocation.matchAndReturn("getAction",action);
75 assertNull(action.getFieldErrors().get("foo"));
76 interceptor.intercept(invocation);
77 assertTrue(action.hasFieldErrors());
78 assertNotNull(action.getFieldErrors().get("foo"));
79 }
80
81 protected void setUp() throws Exception {
82 super.setUp();
83 interceptor = new StrutsConversionErrorInterceptor();
84 mockInvocation = new Mock(ActionInvocation.class);
85 invocation = (ActionInvocation) mockInvocation.proxy();
86 stack = ValueStackFactory.getFactory().createValueStack();
87 context = new ActionContext(stack.getContext());
88 conversionErrors = new HashMap();
89 context.setConversionErrors(conversionErrors);
90 mockInvocation.matchAndReturn("getInvocationContext", context);
91 mockInvocation.expectAndReturn("invoke", Action.SUCCESS);
92 mockInvocation.expectAndReturn("getStack", stack);
93 mockInvocation.expect("addPreResultListener", C.ANY_ARGS);
94 }
95 }