    1   /*
    2    * $Id: TestSetOriginalURI.java 481833 2006-12-03 17:32:52Z niallp $
    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.struts.chain.commands.servlet;
    22
    23  import junit.framework.TestCase;
    24
    25  import org.apache.struts.Globals;
    26  import org.apache.struts.chain.contexts.ServletActionContext;
    27  import org.apache.struts.mock.MockActionServlet;
    28  import org.apache.struts.mock.MockHttpServletRequest;
    29  import org.apache.struts.mock.MockHttpServletResponse;
    30  import org.apache.struts.mock.MockServletConfig;
    31  import org.apache.struts.mock.MockServletContext;
    32
    33  /* JUnitTest case for class: org.apache.struts.chain.commands.servlet.SetOriginalURI */
    34  public class TestSetOriginalURI extends TestCase {
    35      SetOriginalURI command = null;
    36
    37      public TestSetOriginalURI(String _name) {
    38          super(_name);
    39      }
    40
    41      /* setUp method for test case */
    42      protected void setUp() throws Exception {
    43          this.command = new SetOriginalURI();
    44      }
    45
    46      /* tearDown method for test case */
    47      protected void tearDown() {
    48      }
    49
    50      public void testSetOriginalURI()
    51          throws Exception {
    52          MockHttpServletRequest request =
    53              new MockHttpServletRequest("foo/", "bar.do", null, null);
    54          MockServletConfig servletConfig = new MockServletConfig();
    55          MockServletContext servletContext = new MockServletContext();
    56          MockActionServlet servlet =
    57              new MockActionServlet(servletContext, servletConfig);
    58
    59          servlet.initInternal();
    60
    61          ServletActionContext saContext =
    62              new ServletActionContext(servletContext, request,
    63                  new MockHttpServletResponse());
    64
    65          saContext.setActionServlet(servlet);
    66
    67          boolean result = command.execute(saContext);
    68
    69          assertTrue(!result);
    70
    71          String uri = (String) request.getAttribute(Globals.ORIGINAL_URI_KEY);
    72
    73          assertTrue("Original uri not correct: " + uri, "bar.do".equals(uri));
    74
    75          request.setPathElements("foo/", "bar2.do", null, null);
    76          uri = (String) request.getAttribute(Globals.ORIGINAL_URI_KEY);
    77          assertTrue("Original uri not correct: " + uri, "bar.do".equals(uri));
    78      }
    79
    80      /* Executes the test case */
    81      public static void main(String[] argv) {
    82          String[] testCaseList = { TestSetOriginalURI.class.getName() };
    83
    84          junit.textui.TestRunner.main(testCaseList);
    85      }
    86  }