View Javadoc

1   /*
2    * $Id: IncludeTagTest.java 442939 2006-09-13 11:06:55Z hermanns $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.views.jsp;
19  
20  import javax.servlet.RequestDispatcher;
21  
22  import org.apache.struts2.StrutsException;
23  import org.apache.struts2.components.Include;
24  import org.easymock.MockControl;
25  
26  import com.mockobjects.servlet.MockRequestDispatcher;
27  
28  /***
29   * Unit test of {@link IncludeTag}.
30   *
31   */
32  public class IncludeTagTest extends AbstractTagTest {
33  
34      private MockControl controlRequestDispatcher;
35      private RequestDispatcher mockRequestDispatcher;
36  
37      private IncludeTag tag;
38  
39      public void testNoURL() throws Exception {
40          try {
41              tag.doStartTag();
42              tag.doEndTag();
43              fail("Should have thrown exception as no URL is specified in setValue");
44          } catch (StrutsException e) {
45              assertEquals("tag 'include', field 'value': You must specify the URL to include. Example: /foo.jsp", e.getMessage());
46          }
47      }
48  
49      public void testIncludeNoParam() throws Exception {
50          mockRequestDispatcher.include(null, null);
51          controlRequestDispatcher.setVoidCallable();
52          controlRequestDispatcher.replay();
53  
54          tag.setValue("person/list.jsp");
55          tag.doStartTag();
56          tag.doEndTag();
57  
58          controlRequestDispatcher.verify();
59          assertEquals("/person/list.jsp", request.getRequestDispatherString());
60          assertEquals("", writer.toString());
61      }
62  
63      public void testIncludeWithParameters() throws Exception {
64          mockRequestDispatcher.include(null, null);
65          controlRequestDispatcher.setVoidCallable();
66          controlRequestDispatcher.replay();
67  
68          tag.setValue("person/create.jsp");
69          tag.doStartTag();
70          // adding param must be done after doStartTag()
71          Include include = (Include) tag.getComponent();
72          include.addParameter("user", "Santa Claus");
73          tag.doEndTag();
74  
75          controlRequestDispatcher.verify();
76          assertEquals("/person/create.jsp?user=Santa+Claus", request.getRequestDispatherString());
77          assertEquals("", writer.toString());
78      }
79  
80      public void testIncludeRelative2Dots() throws Exception {
81          // TODO: we should test for .. in unit test - is this test correct?
82          mockRequestDispatcher.include(null, null);
83          controlRequestDispatcher.setVoidCallable();
84          controlRequestDispatcher.replay();
85  
86          request.setupGetServletPath("app/manager");
87          tag.setValue("../car/view.jsp");
88          tag.doStartTag();
89          tag.doEndTag();
90  
91          controlRequestDispatcher.verify();
92          assertEquals("/car/view.jsp", request.getRequestDispatherString());
93          assertEquals("", writer.toString());
94      }
95  
96      protected void setUp() throws Exception {
97          super.setUp();
98          request.setupGetRequestDispatcher(new MockRequestDispatcher());
99          tag = new IncludeTag();
100 
101         controlRequestDispatcher = MockControl.createNiceControl(RequestDispatcher.class);
102         // use always matcher as we can not determine the excact objects used in mock.include(request, response) call
103         controlRequestDispatcher.setDefaultMatcher(MockControl.ALWAYS_MATCHER);
104         mockRequestDispatcher = (RequestDispatcher) controlRequestDispatcher.getMock();
105 
106         request.setupGetRequestDispatcher(mockRequestDispatcher);
107         tag.setPageContext(pageContext);
108         tag.setPageContext(pageContext);
109     }
110 
111     protected void tearDown() throws Exception {
112         super.tearDown();
113         tag = null;
114         controlRequestDispatcher = null;
115         mockRequestDispatcher = null;
116     }
117 
118 }