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.dispatcher;
23
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.apache.struts2.ServletActionContext;
30 import org.apache.struts2.StrutsTestCase;
31
32 import com.mockobjects.dynamic.C;
33 import com.mockobjects.dynamic.Mock;
34 import com.opensymphony.xwork2.ActionContext;
35 import com.opensymphony.xwork2.ActionInvocation;
36 import com.opensymphony.xwork2.ognl.OgnlUtil;
37 import com.opensymphony.xwork2.util.reflection.ReflectionProvider;
38
39
40 /***
41 * HttpHeaderResultTest
42 *
43 */
44 public class HttpHeaderResultTest extends StrutsTestCase {
45
46 ActionInvocation invocation;
47 HttpHeaderResult result;
48 HttpServletResponse response;
49 Mock responseMock;
50 ReflectionProvider reflectionProvider;
51
52
53 public void testHeaderValuesAreNotParsedWhenParseIsFalse() throws Exception {
54 Map params = new HashMap();
55 params.put("headers.foo", "${bar}");
56 params.put("headers.baz", "baz");
57
58 Map values = new HashMap();
59 values.put("bar", "abc");
60 ActionContext.getContext().getValueStack().push(values);
61
62 reflectionProvider.setProperties(params, result);
63
64 responseMock.expect("addHeader", C.args(C.eq("foo"), C.eq("${bar}")));
65 responseMock.expect("addHeader", C.args(C.eq("baz"), C.eq("baz")));
66 result.setParse(false);
67 result.execute(invocation);
68 responseMock.verify();
69 }
70
71 public void testHeaderValuesAreParsedAndSet() throws Exception {
72 Map params = new HashMap();
73 params.put("headers.foo", "${bar}");
74 params.put("headers.baz", "baz");
75
76 Map values = new HashMap();
77 values.put("bar", "abc");
78 ActionContext.getContext().getValueStack().push(values);
79
80 reflectionProvider.setProperties(params, result);
81
82 responseMock.expect("addHeader", C.args(C.eq("foo"), C.eq("abc")));
83 responseMock.expect("addHeader", C.args(C.eq("baz"), C.eq("baz")));
84 result.execute(invocation);
85 responseMock.verify();
86 }
87
88 public void testErrorMessageIsParsedAndSet() throws Exception {
89 ActionContext.getContext().getValueStack().set("errMsg", "abc");
90 result.setError(404);
91 result.setErrorMessage("${errMsg}");
92
93 responseMock.expect("sendError", C.args(C.eq(404), C.eq("abc")));
94 result.execute(invocation);
95 responseMock.verify();
96 }
97
98 public void testErrorMessageIsNotParsedAndSet() throws Exception {
99 ActionContext.getContext().getValueStack().set("errMsg", "abc");
100 result.setError(404);
101 result.setParse(false);
102 result.setErrorMessage("${errMsg}");
103
104 responseMock.expect("sendError", C.args(C.eq(404), C.eq("${errMsg}")));
105 result.execute(invocation);
106 responseMock.verify();
107 }
108
109 public void testStatusIsSet() throws Exception {
110 responseMock.expect("setStatus", C.eq(123));
111 result.setStatus(123);
112 result.execute(invocation);
113 responseMock.verify();
114 }
115
116 public void testErrorIsSet() throws Exception {
117 responseMock.expect("sendError", C.eq(404));
118 result.setError(404);
119 result.execute(invocation);
120 responseMock.verify();
121 }
122
123 protected void setUp() throws Exception {
124 super.setUp();
125 result = new HttpHeaderResult();
126 responseMock = new Mock(HttpServletResponse.class);
127 response = (HttpServletResponse) responseMock.proxy();
128 invocation = (ActionInvocation) new Mock(ActionInvocation.class).proxy();
129 reflectionProvider = container.getInstance(ReflectionProvider.class);
130 ServletActionContext.setResponse(response);
131 }
132
133 protected void tearDown() throws Exception {
134 super.tearDown();
135 ActionContext.setContext(null);
136 }
137 }