View Javadoc

    1   /*
    2    * $Id: IncludeTagTest.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.views.jsp;
    22
    23  import javax.servlet.RequestDispatcher;
    24
    25  import org.apache.struts2.StrutsException;
    26  import org.apache.struts2.components.Include;
    27  import org.easymock.MockControl;
    28
    29  import com.mockobjects.servlet.MockRequestDispatcher;
    30
    31  /***
    32   * Unit test of {@link IncludeTag}.
    33   *
    34   */
    35  public class IncludeTagTest extends AbstractTagTest {
    36
    37      private MockControl controlRequestDispatcher;
    38      private RequestDispatcher mockRequestDispatcher;
    39
    40      private IncludeTag tag;
    41
    42      public void testNoURL() throws Exception {
    43          try {
    44              tag.doStartTag();
    45              tag.doEndTag();
    46              fail("Should have thrown exception as no URL is specified in setValue");
    47          } catch (StrutsException e) {
    48              assertEquals("tag 'include', field 'value': You must specify the URL to include. Example: /foo.jsp", e.getMessage());
    49          }
    50      }
    51
    52      public void testIncludeNoParam() throws Exception {
    53          mockRequestDispatcher.include(null, null);
    54          controlRequestDispatcher.setVoidCallable();
    55          controlRequestDispatcher.replay();
    56
    57          tag.setValue("person/list.jsp");
    58          tag.doStartTag();
    59          tag.doEndTag();
    60
    61          controlRequestDispatcher.verify();
    62          assertEquals("/person/list.jsp", request.getRequestDispatherString());
    63          assertEquals("", writer.toString());
    64      }
    65
    66      public void testIncludeWithParameters() throws Exception {
    67          mockRequestDispatcher.include(null, null);
    68          controlRequestDispatcher.setVoidCallable();
    69          controlRequestDispatcher.replay();
    70
    71          tag.setValue("person/create.jsp");
    72          tag.doStartTag();
    73          // adding param must be done after doStartTag()
    74          Include include = (Include) tag.getComponent();
    75          include.addParameter("user", "Santa Claus");
    76          tag.doEndTag();
    77
    78          controlRequestDispatcher.verify();
    79          assertEquals("/person/create.jsp?user=Santa+Claus", request.getRequestDispatherString());
    80          assertEquals("", writer.toString());
    81      }
    82
    83      public void testIncludeRelative2Dots() throws Exception {
    84          // TODO: we should test for .. in unit test - is this test correct?
    85          mockRequestDispatcher.include(null, null);
    86          controlRequestDispatcher.setVoidCallable();
    87          controlRequestDispatcher.replay();
    88
    89          request.setupGetServletPath("app/manager");
    90          tag.setValue("../car/view.jsp");
    91          tag.doStartTag();
    92          tag.doEndTag();
    93
    94          controlRequestDispatcher.verify();
    95          assertEquals("/car/view.jsp", request.getRequestDispatherString());
    96          assertEquals("", writer.toString());
    97      }
    98
    99      protected void setUp() throws Exception {
    100         super.setUp();
    101         request.setupGetRequestDispatcher(new MockRequestDispatcher());
    102         tag = new IncludeTag();
    103
    104         controlRequestDispatcher = MockControl.createNiceControl(RequestDispatcher.class);
    105         // use always matcher as we can not determine the excact objects used in mock.include(request, response) call
    106         controlRequestDispatcher.setDefaultMatcher(MockControl.ALWAYS_MATCHER);
    107         mockRequestDispatcher = (RequestDispatcher) controlRequestDispatcher.getMock();
    108
    109         request.setupGetRequestDispatcher(mockRequestDispatcher);
    110         tag.setPageContext(pageContext);
    111         tag.setPageContext(pageContext);
    112     }
    113
    114     protected void tearDown() throws Exception {
    115         super.tearDown();
    116         tag = null;
    117         controlRequestDispatcher = null;
    118         mockRequestDispatcher = null;
    119     }
    120
    121 }