001: /*
002: * $Id: IncludeTagTest.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.views.jsp;
022:
023: import javax.servlet.RequestDispatcher;
024:
025: import org.apache.struts2.StrutsException;
026: import org.apache.struts2.components.Include;
027: import org.easymock.MockControl;
028:
029: import com.mockobjects.servlet.MockRequestDispatcher;
030:
031: /**
032: * Unit test of {@link IncludeTag}.
033: *
034: */
035: public class IncludeTagTest extends AbstractTagTest {
036:
037: private MockControl controlRequestDispatcher;
038: private RequestDispatcher mockRequestDispatcher;
039:
040: private IncludeTag tag;
041:
042: public void testNoURL() throws Exception {
043: try {
044: tag.doStartTag();
045: tag.doEndTag();
046: fail("Should have thrown exception as no URL is specified in setValue");
047: } catch (StrutsException e) {
048: assertEquals(
049: "tag 'include', field 'value': You must specify the URL to include. Example: /foo.jsp",
050: e.getMessage());
051: }
052: }
053:
054: public void testIncludeNoParam() throws Exception {
055: mockRequestDispatcher.include(null, null);
056: controlRequestDispatcher.setVoidCallable();
057: controlRequestDispatcher.replay();
058:
059: tag.setValue("person/list.jsp");
060: tag.doStartTag();
061: tag.doEndTag();
062:
063: controlRequestDispatcher.verify();
064: assertEquals("/person/list.jsp", request
065: .getRequestDispatherString());
066: assertEquals("", writer.toString());
067: }
068:
069: public void testIncludeWithParameters() throws Exception {
070: mockRequestDispatcher.include(null, null);
071: controlRequestDispatcher.setVoidCallable();
072: controlRequestDispatcher.replay();
073:
074: tag.setValue("person/create.jsp");
075: tag.doStartTag();
076: // adding param must be done after doStartTag()
077: Include include = (Include) tag.getComponent();
078: include.addParameter("user", "Santa Claus");
079: tag.doEndTag();
080:
081: controlRequestDispatcher.verify();
082: assertEquals("/person/create.jsp?user=Santa+Claus", request
083: .getRequestDispatherString());
084: assertEquals("", writer.toString());
085: }
086:
087: public void testIncludeRelative2Dots() throws Exception {
088: // TODO: we should test for .. in unit test - is this test correct?
089: mockRequestDispatcher.include(null, null);
090: controlRequestDispatcher.setVoidCallable();
091: controlRequestDispatcher.replay();
092:
093: request.setupGetServletPath("app/manager");
094: tag.setValue("../car/view.jsp");
095: tag.doStartTag();
096: tag.doEndTag();
097:
098: controlRequestDispatcher.verify();
099: assertEquals("/car/view.jsp", request
100: .getRequestDispatherString());
101: assertEquals("", writer.toString());
102: }
103:
104: protected void setUp() throws Exception {
105: super .setUp();
106: request.setupGetRequestDispatcher(new MockRequestDispatcher());
107: tag = new IncludeTag();
108:
109: controlRequestDispatcher = MockControl
110: .createNiceControl(RequestDispatcher.class);
111: // use always matcher as we can not determine the excact objects used in mock.include(request, response) call
112: controlRequestDispatcher
113: .setDefaultMatcher(MockControl.ALWAYS_MATCHER);
114: mockRequestDispatcher = (RequestDispatcher) controlRequestDispatcher
115: .getMock();
116:
117: request.setupGetRequestDispatcher(mockRequestDispatcher);
118: tag.setPageContext(pageContext);
119: tag.setPageContext(pageContext);
120: }
121:
122: protected void tearDown() throws Exception {
123: super.tearDown();
124: tag = null;
125: controlRequestDispatcher = null;
126: mockRequestDispatcher = null;
127: }
128:
129: }
|