1 18 package org.apache.roller.ui; 19 20 import com.mockrunner.mock.web.ActionMockObjectFactory; 21 import com.mockrunner.mock.web.MockHttpServletRequest; 22 import com.mockrunner.mock.web.MockServletConfig; 23 import com.mockrunner.mock.web.MockServletContext; 24 import com.mockrunner.mock.web.WebMockObjectFactory; 25 import com.mockrunner.servlet.ServletTestModule; 26 import com.mockrunner.struts.ActionTestModule; 27 import com.mockrunner.struts.MapMessageResources; 28 29 import java.io.FileInputStream ; 30 31 import javax.servlet.Servlet ; 32 import javax.servlet.ServletRequest ; 33 import javax.servlet.ServletResponse ; 34 import javax.servlet.http.HttpServletRequest ; 35 import javax.servlet.http.HttpServletRequestWrapper ; 36 import javax.servlet.jsp.JspEngineInfo ; 37 import javax.servlet.jsp.JspFactory ; 38 import javax.servlet.jsp.PageContext ; 39 40 import junit.framework.TestCase; 41 import org.apache.roller.ui.core.filters.PersistenceSessionFilter; 42 import org.apache.roller.ui.core.filters.RequestFilter; 43 44 45 49 public abstract class ServletTestBase extends TestCase { 50 protected ServletTestModule servletModule; 51 private WebMockObjectFactory mockFactory; 52 protected MockRollerContext rollerContext; 53 protected ActionTestModule strutsModule; 55 public void setUp() throws Exception { 56 getMockFactory().refresh(); 57 58 servletModule = new ServletTestModule(getMockFactory()); 59 strutsModule = new ActionTestModule(getStrutsMockFactory()); 60 61 MockServletContext ctx = getMockFactory().getMockServletContext(); 62 ctx.setServletContextName("/roller"); 63 ctx.setRealPath("/", ""); 64 rollerContext = new MockRollerContext(); 65 rollerContext.init(ctx); 66 67 MockHttpServletRequest request = getMockFactory().getMockRequest(); 68 request.setContextPath("/roller"); 69 70 JspFactory.setDefaultFactory(new MockJspFactory(getMockFactory())); 71 72 MockServletContext app = getMockFactory().getMockServletContext(); 74 app.addResourcePath("/WEB-INF/toolbox.xml","/WEB-INF/toolbox.xml"); 75 app.setResourceAsStream( 76 "/WEB-INF/toolbox.xml", 77 new FileInputStream ("./WEB-INF/toolbox.xml")); 78 MockServletConfig config = getMockFactory().getMockServletConfig(); 79 config.setInitParameter( 80 "org.apache.velocity.properties","WEB-INF/velocity.properties"); 81 MapMessageResources resources = new MapMessageResources(); 82 resources.putMessages( 83 "WEB-INF/classes/ApplicationResources.properties"); 84 strutsModule.setResources(resources); 85 } 86 87 88 protected void authenticateUser(String username, String role) { 89 MockHttpServletRequest mockRequest = getMockFactory().getMockRequest(); 90 mockRequest.setRemoteUser(username); 91 mockRequest.setUserPrincipal(new MockPrincipal(username)); 92 mockRequest.setUserInRole(role, true); 93 } 94 95 96 protected void doFilters() { 97 servletModule.createFilter(PersistenceSessionFilter.class); 98 servletModule.createFilter(RequestFilter.class); 99 servletModule.setDoChain(false); 100 servletModule.doFilter(); 101 getMockFactory().addRequestWrapper(new HttpServletRequestWrapper ( 102 (HttpServletRequest)servletModule.getFilteredRequest())); 103 } 104 105 protected ActionMockObjectFactory getStrutsMockFactory() { 106 return (ActionMockObjectFactory)getMockFactory(); 107 } 108 109 protected WebMockObjectFactory getMockFactory() { 110 if (mockFactory == null) { 111 mockFactory = new ActionMockObjectFactory(); 112 } 113 return mockFactory; 114 } 115 116 117 public class MockJspFactory extends JspFactory { 118 119 public WebMockObjectFactory factory; 120 121 public MockJspFactory(WebMockObjectFactory factory) { 122 this.factory = factory; 123 } 124 125 public PageContext getPageContext( 126 Servlet arg0, 127 ServletRequest arg1, 128 ServletResponse arg2, 129 String arg3, 130 boolean arg4, 131 int arg5, 132 boolean arg6) { 133 return factory.getMockPageContext(); 134 } 135 136 public void releasePageContext(PageContext arg0) {} 137 138 public JspEngineInfo getEngineInfo() {return null;} 139 } 140 } 141 | Popular Tags |