View Javadoc

    1   /*
    2    * $Id: RequestUtilsTest.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;
    22
    23  /***
    24   * <code>RequestUtilsTest</code>
    25   *
    26   */
    27  import javax.servlet.http.HttpServletRequest;
    28
    29  import junit.framework.TestCase;
    30
    31  import org.easymock.MockControl;
    32
    33  public class RequestUtilsTest extends TestCase {
    34
    35      private MockControl control;
    36      private HttpServletRequest requestMock;
    37
    38      public void testGetServletPathWithServletPathSet() throws Exception {
    39          control.expectAndReturn(requestMock.getServletPath(), "/mycontext/");
    40          control.replay();
    41          assertEquals("/mycontext/", RequestUtils.getServletPath(requestMock));
    42          control.verify();
    43      }
    44
    45      public void testGetServletPathWithRequestURIAndEmptyContextPath() throws Exception {
    46          control.expectAndReturn(requestMock.getServletPath(), null);
    47          control.expectAndReturn(requestMock.getRequestURI(), "/mycontext/test.jsp");
    48          control.expectAndReturn(requestMock.getContextPath(), "");
    49          control.expectAndReturn(requestMock.getPathInfo(), "test.jsp");
    50          control.expectAndReturn(requestMock.getPathInfo(), "test.jsp");
    51          control.replay();
    52          assertEquals("/mycontext/", RequestUtils.getServletPath(requestMock));
    53          control.verify();
    54      }
    55
    56      public void testGetServletPathWithRequestURIAndContextPathSet() throws Exception {
    57          control.expectAndReturn(requestMock.getServletPath(), null);
    58          control.expectAndReturn(requestMock.getRequestURI(), "/servlet/mycontext/test.jsp");
    59          control.expectAndReturn(requestMock.getContextPath(), "/servlet");
    60          control.expectAndReturn(requestMock.getContextPath(), "/servlet");
    61          control.expectAndReturn(requestMock.getPathInfo(), "test.jsp");
    62          control.expectAndReturn(requestMock.getPathInfo(), "test.jsp");
    63          control.replay();
    64          assertEquals("/mycontext/", RequestUtils.getServletPath(requestMock));
    65          control.verify();
    66      }
    67
    68      public void testGetServletPathWithRequestURIAndContextPathSetButNoPatchInfo() throws Exception {
    69          control.expectAndReturn(requestMock.getServletPath(), null);
    70          control.expectAndReturn(requestMock.getRequestURI(), "/servlet/mycontext/");
    71          control.expectAndReturn(requestMock.getContextPath(), "/servlet");
    72          control.expectAndReturn(requestMock.getContextPath(), "/servlet");
    73          control.expectAndReturn(requestMock.getPathInfo(), null);
    74          control.replay();
    75          assertEquals("/mycontext/", RequestUtils.getServletPath(requestMock));
    76          control.verify();
    77      }
    78
    79      protected void setUp() {
    80          control = MockControl.createControl(HttpServletRequest.class);
    81          requestMock = (HttpServletRequest) control.getMock();
    82      }
    83
    84  }