View Javadoc

1   /*
2    * $Id: HttpHeaderResultTest.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
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 }