View Javadoc

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