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