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