View Javadoc

1   /*
2    * $Id: HttpHeaderResultTest.java 439747 2006-09-03 09:22:46Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.dispatcher;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import javax.servlet.http.HttpServletResponse;
24  
25  import org.apache.struts2.ServletActionContext;
26  import org.apache.struts2.StrutsTestCase;
27  
28  import com.mockobjects.dynamic.C;
29  import com.mockobjects.dynamic.Mock;
30  import com.opensymphony.xwork2.ActionContext;
31  import com.opensymphony.xwork2.ActionInvocation;
32  import com.opensymphony.xwork2.util.OgnlUtil;
33  
34  
35  /***
36   * HttpHeaderResultTest
37   *
38   */
39  public class HttpHeaderResultTest extends StrutsTestCase {
40  
41      ActionInvocation invocation;
42      HttpHeaderResult result;
43      HttpServletResponse response;
44      Mock responseMock;
45  
46  
47      public void testHeaderValuesAreNotParsedWhenParseIsFalse() throws Exception {
48          Map params = new HashMap();
49          params.put("headers.foo", "${bar}");
50          params.put("headers.baz", "baz");
51  
52          Map values = new HashMap();
53          values.put("bar", "abc");
54          ActionContext.getContext().getValueStack().push(values);
55  
56          OgnlUtil.setProperties(params, result);
57  
58          responseMock.expect("addHeader", C.args(C.eq("foo"), C.eq("${bar}")));
59          responseMock.expect("addHeader", C.args(C.eq("baz"), C.eq("baz")));
60          result.setParse(false);
61          result.execute(invocation);
62          responseMock.verify();
63      }
64  
65      public void testHeaderValuesAreParsedAndSet() throws Exception {
66          Map params = new HashMap();
67          params.put("headers.foo", "${bar}");
68          params.put("headers.baz", "baz");
69  
70          Map values = new HashMap();
71          values.put("bar", "abc");
72          ActionContext.getContext().getValueStack().push(values);
73  
74          OgnlUtil.setProperties(params, result);
75  
76          responseMock.expect("addHeader", C.args(C.eq("foo"), C.eq("abc")));
77          responseMock.expect("addHeader", C.args(C.eq("baz"), C.eq("baz")));
78          result.execute(invocation);
79          responseMock.verify();
80      }
81  
82      public void testStatusIsSet() throws Exception {
83          responseMock.expect("setStatus", C.eq(123));
84          result.setStatus(123);
85          result.execute(invocation);
86          responseMock.verify();
87      }
88  
89      protected void setUp() throws Exception {
90          super.setUp();
91          result = new HttpHeaderResult();
92          responseMock = new Mock(HttpServletResponse.class);
93          response = (HttpServletResponse) responseMock.proxy();
94          invocation = (ActionInvocation) new Mock(ActionInvocation.class).proxy();
95          ServletActionContext.setResponse(response);
96      }
97  
98      protected void tearDown() throws Exception {
99          super.tearDown();
100         ServletActionContext.setResponse(null);
101         ActionContext.setContext(null);
102     }
103 }