View Javadoc

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