View Javadoc

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