View Javadoc

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