View Javadoc

1   /*
2    * $Id: ServletDispatcherResultTest.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 javax.servlet.RequestDispatcher;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  import ognl.Ognl;
29  
30  import org.apache.struts2.ServletActionContext;
31  import org.apache.struts2.StrutsStatics;
32  import org.apache.struts2.StrutsTestCase;
33  
34  import com.mockobjects.dynamic.C;
35  import com.mockobjects.dynamic.Mock;
36  import com.opensymphony.xwork2.ActionContext;
37  
38  
39  /***
40   *
41   */
42  public class ServletDispatcherResultTest extends StrutsTestCase implements StrutsStatics {
43  
44      public void testInclude() {
45          ServletDispatcherResult view = new ServletDispatcherResult();
46          view.setLocation("foo.jsp");
47  
48          Mock dispatcherMock = new Mock(RequestDispatcher.class);
49          dispatcherMock.expect("include", C.ANY_ARGS);
50  
51          Mock requestMock = new Mock(HttpServletRequest.class);
52          requestMock.expectAndReturn("getRequestDispatcher", C.args(C.eq("foo.jsp")), dispatcherMock.proxy());
53  
54          Mock responseMock = new Mock(HttpServletResponse.class);
55          responseMock.expectAndReturn("isCommitted", Boolean.TRUE);
56  
57          ActionContext ac = new ActionContext(Ognl.createDefaultContext(null));
58          ActionContext.setContext(ac);
59          ServletActionContext.setRequest((HttpServletRequest) requestMock.proxy());
60          ServletActionContext.setResponse((HttpServletResponse) responseMock.proxy());
61  
62          try {
63              view.execute(null);
64          } catch (Exception e) {
65              e.printStackTrace();
66              fail();
67          }
68  
69          dispatcherMock.verify();
70          requestMock.verify();
71          dispatcherMock.verify();
72      }
73  
74      public void testSimple() {
75          ServletDispatcherResult view = new ServletDispatcherResult();
76          view.setLocation("foo.jsp");
77  
78          Mock dispatcherMock = new Mock(RequestDispatcher.class);
79          dispatcherMock.expect("forward", C.ANY_ARGS);
80  
81          Mock requestMock = new Mock(HttpServletRequest.class);
82          requestMock.expectAndReturn("getAttribute", "javax.servlet.include.servlet_path", null);
83          requestMock.expectAndReturn("getRequestDispatcher", C.args(C.eq("foo.jsp")), dispatcherMock.proxy());
84          requestMock.expect("setAttribute", C.ANY_ARGS); // this is a bad mock, but it works
85          requestMock.expect("setAttribute", C.ANY_ARGS); // this is a bad mock, but it works
86          requestMock.matchAndReturn("getRequestURI", "foo.jsp");
87  
88          Mock responseMock = new Mock(HttpServletResponse.class);
89          responseMock.expectAndReturn("isCommitted", Boolean.FALSE);
90  
91          ActionContext ac = new ActionContext(Ognl.createDefaultContext(null));
92          ActionContext.setContext(ac);
93          ServletActionContext.setRequest((HttpServletRequest) requestMock.proxy());
94          ServletActionContext.setResponse((HttpServletResponse) responseMock.proxy());
95  
96          try {
97              view.execute(null);
98          } catch (Exception e) {
99              e.printStackTrace();
100             fail();
101         }
102 
103         dispatcherMock.verify();
104         requestMock.verify();
105         dispatcherMock.verify();
106     }
107 }