1 5 9 package com.opensymphony.webwork.dispatcher; 10 11 import com.mockobjects.dynamic.C; 12 import com.mockobjects.dynamic.Mock; 13 import com.mockobjects.servlet.MockHttpServletResponse; 14 import com.mockobjects.servlet.MockHttpSession; 15 import com.mockobjects.servlet.MockServletConfig; 16 import com.mockobjects.servlet.MockServletOutputStream; 17 import com.opensymphony.webwork.WebWorkTestCase; 18 import com.opensymphony.webwork.views.jsp.WebWorkMockHttpServletRequest; 19 import com.opensymphony.webwork.views.jsp.WebWorkMockHttpServletResponse; 20 import com.opensymphony.xwork.config.ConfigurationManager; 21 import com.opensymphony.xwork.config.providers.XmlConfigurationProvider; 22 23 import javax.servlet.RequestDispatcher ; 24 import javax.servlet.ServletContext ; 25 import javax.servlet.ServletException ; 26 import javax.servlet.http.HttpServlet ; 27 import java.io.IOException ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 31 32 35 public abstract class AbstractServletDispatcherTestCase extends WebWorkTestCase { 36 38 public String getConfigFilename() { 39 return "xwork.xml"; 40 } 41 42 public abstract ServletDispatcher getServletDispatcher(); 43 44 public abstract String getServletPath(); 45 46 public void testServletDispatcher() throws ServletException , IOException { 47 service(getServletDispatcher()); 48 } 49 50 protected Map getParameterMap() { 51 return new HashMap (); 52 } 53 54 protected void setUp() throws Exception { 55 super.setUp(); 56 57 loadConfig(); 58 } 59 60 protected void loadConfig() { 61 XmlConfigurationProvider c = new XmlConfigurationProvider(getConfigFilename()); 62 ConfigurationManager.clearConfigurationProviders(); 63 ConfigurationManager.destroyConfiguration(); 64 ConfigurationManager.addConfigurationProvider(c); 65 ConfigurationManager.getConfiguration(); 66 } 67 68 protected void tearDown() throws Exception { 69 ConfigurationManager.destroyConfiguration(); 70 } 71 72 private void service(HttpServlet servlet) throws ServletException , IOException { 73 Mock dispatcherMock = new Mock(RequestDispatcher .class); 74 dispatcherMock.expect("include", C.ANY_ARGS); 75 76 MockHttpSession session = new MockHttpSession(); 77 78 WebWorkMockHttpServletRequest request = new WebWorkMockHttpServletRequest(); 79 request.setSession(session); 80 request.setupAddHeader("Content-Type", "dunno what this should be... just not multipart !"); 81 request.setParameterMap(getParameterMap()); 82 request.setupGetServletPath(getServletPath()); 83 request.setupGetPathInfo(getServletPath()); 84 request.setupGetContentType("text/plain"); 85 86 MockHttpServletResponse response = new WebWorkMockHttpServletResponse(); 87 response.setupOutputStream(new MockServletOutputStream()); 88 89 Mock servletContextDMock = new Mock(ServletContext .class); 90 servletContextDMock.matchAndReturn("getRealPath", C.args(C.eq("")), ""); 91 servletContextDMock.matchAndReturn("getRealPath", C.args(C.eq("velocity.properties")), null); 92 servletContextDMock.matchAndReturn("getRealPath", C.args(C.eq("/WEB-INF/velocity.properties")), null); 93 servletContextDMock.matchAndReturn("log", C.ANY_ARGS, null); 94 95 ServletContext servletContextMock = (ServletContext ) servletContextDMock.proxy(); 96 servletContextDMock.expectAndReturn("getServerInfo", "Resin"); 97 98 MockServletConfig servletConfigMock = new MockServletConfig(); 99 servletConfigMock.setServletContext(servletContextMock); 100 101 servlet.init(servletConfigMock); 102 servlet.service(request, response); 103 } 104 } 105 | Popular Tags |