View Javadoc

1   /*
2    * $Id: HttpHeaderResultTest.java 526222 2007-04-06 16:37:54Z hermanns $
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  import com.opensymphony.xwork2.util.XWorkConverter;
37  import com.opensymphony.xwork2.util.ObjectTypeDeterminerFactory;
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  
51  
52      public void testHeaderValuesAreNotParsedWhenParseIsFalse() throws Exception {
53          Map params = new HashMap();
54          params.put("headers.foo", "${bar}");
55          params.put("headers.baz", "baz");
56  
57          Map values = new HashMap();
58          values.put("bar", "abc");
59  
60          ActionContext.getContext().getValueStack().push(values);
61          OgnlUtil.setProperties(params, result);
62  
63          responseMock.expect("addHeader", C.args(C.eq("foo"), C.eq("${bar}")));
64          responseMock.expect("addHeader", C.args(C.eq("baz"), C.eq("baz")));
65          result.setParse(false);
66          result.execute(invocation);
67          responseMock.verify();
68      }
69  
70      public void testHeaderValuesAreParsedAndSet() throws Exception {
71          Map params = new HashMap();
72          params.put("headers.foo", "${bar}");
73          params.put("headers.baz", "baz");
74  
75          Map values = new HashMap();
76          values.put("bar", "abc");
77  
78          ActionContext.getContext().getValueStack().push(values);
79          OgnlUtil.setProperties(params, result);
80  
81          responseMock.expect("addHeader", C.args(C.eq("foo"), C.eq("abc")));
82          responseMock.expect("addHeader", C.args(C.eq("baz"), C.eq("baz")));
83          result.execute(invocation);
84          responseMock.verify();
85      }
86  
87      public void testStatusIsSet() throws Exception {
88          responseMock.expect("setStatus", C.eq(123));
89          result.setStatus(123);
90          result.execute(invocation);
91          responseMock.verify();
92      }
93  
94      protected void setUp() throws Exception {
95          super.setUp();
96          result = new HttpHeaderResult();
97          responseMock = new Mock(HttpServletResponse.class);
98          response = (HttpServletResponse) responseMock.proxy();
99          invocation = (ActionInvocation) new Mock(ActionInvocation.class).proxy();
100         ServletActionContext.setResponse(response);
101 
102         XWorkConverter.getInstance().setObjectTypeDeterminer(ObjectTypeDeterminerFactory.getInstance());
103     }
104 
105     protected void tearDown() throws Exception {
106         super.tearDown();
107         ServletActionContext.setResponse(null);
108         ActionContext.setContext(null);
109         XWorkConverter.resetInstance();
110     }
111 }