001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.views.jsp;
006:
007: import com.mockobjects.servlet.MockRequestDispatcher;
008: import com.opensymphony.webwork.components.Include;
009: import org.easymock.MockControl;
010:
011: import javax.servlet.RequestDispatcher;
012: import javax.servlet.http.HttpServletRequest;
013:
014: /**
015: * Unit test of {@link IncludeTag}.
016: *
017: * @author Claus Ibsen
018: */
019: public class IncludeTagTest extends AbstractTagTest {
020:
021: private MockControl controlRequestDispatcher;
022: private RequestDispatcher mockRequestDispatcher;
023:
024: private IncludeTag tag;
025:
026: public void testNoURL() throws Exception {
027: try {
028: tag.doStartTag();
029: tag.doEndTag();
030: fail("Should have thrown exception as no URL is specified in setValue");
031: } catch (RuntimeException e) {
032: assertEquals(
033: "tag 'include', field 'value': You must specify the URL to include. Example: /foo.jsp",
034: e.getMessage());
035: }
036: }
037:
038: public void testIncludeNoParam() throws Exception {
039: mockRequestDispatcher.include(null, null);
040: controlRequestDispatcher.setVoidCallable();
041: controlRequestDispatcher.replay();
042:
043: tag.setValue("person/list.jsp");
044: tag.doStartTag();
045: tag.doEndTag();
046:
047: controlRequestDispatcher.verify();
048: assertEquals("/person/list.jsp", request
049: .getRequestDispatherString());
050: assertEquals("", writer.toString());
051: }
052:
053: public void testIncludeWithParameters() throws Exception {
054: mockRequestDispatcher.include(null, null);
055: controlRequestDispatcher.setVoidCallable();
056: controlRequestDispatcher.replay();
057:
058: tag.setValue("person/create.jsp");
059: tag.doStartTag();
060: // adding param must be done after doStartTag()
061: Include include = (Include) tag.getComponent();
062: include.addParameter("user", "Santa Claus");
063: tag.doEndTag();
064:
065: controlRequestDispatcher.verify();
066: assertEquals("/person/create.jsp?user=Santa+Claus", request
067: .getRequestDispatherString());
068: assertEquals("", writer.toString());
069: }
070:
071: public void testIncludeRelative2Dots() throws Exception {
072: // TODO: we should test for .. in unit test - is this test correct?
073: mockRequestDispatcher.include(null, null);
074: controlRequestDispatcher.setVoidCallable();
075: controlRequestDispatcher.replay();
076:
077: request.setupGetServletPath("app/manager");
078: tag.setValue("../car/view.jsp");
079: tag.doStartTag();
080: tag.doEndTag();
081:
082: controlRequestDispatcher.verify();
083: assertEquals("/car/view.jsp", request
084: .getRequestDispatherString());
085: assertEquals("", writer.toString());
086: }
087:
088: protected void setUp() throws Exception {
089: super .setUp();
090: request.setupGetRequestDispatcher(new MockRequestDispatcher());
091: tag = new IncludeTag();
092:
093: controlRequestDispatcher = MockControl
094: .createNiceControl(RequestDispatcher.class);
095: // use always matcher as we can not determine the excact objects used in mock.include(req, res) call
096: controlRequestDispatcher
097: .setDefaultMatcher(MockControl.ALWAYS_MATCHER);
098: mockRequestDispatcher = (RequestDispatcher) controlRequestDispatcher
099: .getMock();
100:
101: request.setupGetRequestDispatcher(mockRequestDispatcher);
102: tag.setPageContext(pageContext);
103: tag.setPageContext(pageContext);
104: }
105:
106: protected void tearDown() throws Exception {
107: super.tearDown();
108: tag = null;
109: controlRequestDispatcher = null;
110: mockRequestDispatcher = null;
111: }
112:
113: }
|