001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: /*
006: * Created on 2/10/2003
007: *
008: */
009: package com.opensymphony.webwork.dispatcher;
010:
011: import com.mockobjects.dynamic.C;
012: import com.mockobjects.dynamic.Mock;
013: import com.mockobjects.servlet.MockHttpServletResponse;
014: import com.mockobjects.servlet.MockHttpSession;
015: import com.mockobjects.servlet.MockServletConfig;
016: import com.mockobjects.servlet.MockServletOutputStream;
017: import com.opensymphony.webwork.WebWorkTestCase;
018: import com.opensymphony.webwork.views.jsp.WebWorkMockHttpServletRequest;
019: import com.opensymphony.webwork.views.jsp.WebWorkMockHttpServletResponse;
020: import com.opensymphony.xwork.config.ConfigurationManager;
021: import com.opensymphony.xwork.config.providers.XmlConfigurationProvider;
022:
023: import javax.servlet.RequestDispatcher;
024: import javax.servlet.ServletContext;
025: import javax.servlet.ServletException;
026: import javax.servlet.http.HttpServlet;
027: import java.io.IOException;
028: import java.util.HashMap;
029: import java.util.Map;
030:
031: /**
032: * @author CameronBraid
033: */
034: public abstract class AbstractServletDispatcherTestCase extends
035: WebWorkTestCase {
036:
037: public String getConfigFilename() {
038: return "xwork.xml";
039: }
040:
041: public abstract ServletDispatcher getServletDispatcher();
042:
043: public abstract String getServletPath();
044:
045: public void testServletDispatcher() throws ServletException,
046: IOException {
047: service(getServletDispatcher());
048: }
049:
050: protected Map getParameterMap() {
051: return new HashMap();
052: }
053:
054: protected void setUp() throws Exception {
055: super .setUp();
056:
057: loadConfig();
058: }
059:
060: protected void loadConfig() {
061: XmlConfigurationProvider c = new XmlConfigurationProvider(
062: getConfigFilename());
063: ConfigurationManager.clearConfigurationProviders();
064: ConfigurationManager.destroyConfiguration();
065: ConfigurationManager.addConfigurationProvider(c);
066: ConfigurationManager.getConfiguration();
067: }
068:
069: protected void tearDown() throws Exception {
070: ConfigurationManager.destroyConfiguration();
071: }
072:
073: private void service(HttpServlet servlet) throws ServletException,
074: IOException {
075: Mock dispatcherMock = new Mock(RequestDispatcher.class);
076: dispatcherMock.expect("include", C.ANY_ARGS);
077:
078: MockHttpSession session = new MockHttpSession();
079:
080: WebWorkMockHttpServletRequest request = new WebWorkMockHttpServletRequest();
081: request.setSession(session);
082: request.setupAddHeader("Content-Type",
083: "dunno what this should be... just not multipart !");
084: request.setParameterMap(getParameterMap());
085: request.setupGetServletPath(getServletPath());
086: request.setupGetPathInfo(getServletPath());
087: request.setupGetContentType("text/plain");
088:
089: MockHttpServletResponse response = new WebWorkMockHttpServletResponse();
090: response.setupOutputStream(new MockServletOutputStream());
091:
092: Mock servletContextDMock = new Mock(ServletContext.class);
093: servletContextDMock.matchAndReturn("getRealPath", C.args(C
094: .eq("")), "");
095: servletContextDMock.matchAndReturn("getRealPath", C.args(C
096: .eq("velocity.properties")), null);
097: servletContextDMock.matchAndReturn("getRealPath", C.args(C
098: .eq("/WEB-INF/velocity.properties")), null);
099: servletContextDMock.matchAndReturn("log", C.ANY_ARGS, null);
100:
101: ServletContext servletContextMock = (ServletContext) servletContextDMock
102: .proxy();
103: servletContextDMock.expectAndReturn("getServerInfo", "Resin");
104: servletContextDMock.expectAndReturn("getServerInfo", "Resin");
105:
106: MockServletConfig servletConfigMock = new MockServletConfig();
107: servletConfigMock.setServletContext(servletContextMock);
108:
109: servlet.init(servletConfigMock);
110: servlet.service(request, response);
111: }
112: }
|