1 package org.roller.presentation; 2 3 import java.io.FileInputStream ; 4 import java.io.FileNotFoundException ; 5 6 import javax.servlet.Servlet ; 7 import javax.servlet.ServletRequest ; 8 import javax.servlet.ServletResponse ; 9 import javax.servlet.http.HttpServletRequest ; 10 import javax.servlet.http.HttpServletRequestWrapper ; 11 import javax.servlet.jsp.JspEngineInfo ; 12 import javax.servlet.jsp.JspFactory ; 13 import javax.servlet.jsp.PageContext ; 14 15 import org.roller.RollerTestBase; 16 import org.roller.presentation.filters.PersistenceSessionFilter; 17 import org.roller.presentation.filters.RequestFilter; 18 19 import com.mockrunner.mock.web.ActionMockObjectFactory; 20 import com.mockrunner.mock.web.MockHttpServletRequest; 21 import com.mockrunner.mock.web.MockServletConfig; 22 import com.mockrunner.mock.web.MockServletContext; 23 import com.mockrunner.mock.web.WebMockObjectFactory; 24 import com.mockrunner.servlet.ServletTestModule; 25 import com.mockrunner.struts.ActionTestModule; 26 import com.mockrunner.struts.MapMessageResources; 27 28 29 33 public abstract class VelocityServletTestBase extends RollerTestBase 34 { 35 protected ServletTestModule servletModule; 36 private WebMockObjectFactory mockFactory; 37 protected MockRollerContext rollerContext; 38 protected ActionTestModule strutsModule; 40 public void setUp() throws Exception 41 { 42 super.setUp(); 43 getMockFactory().refresh(); 44 servletModule = new ServletTestModule(getMockFactory()); 45 strutsModule = new ActionTestModule(getStrutsMockFactory()); 46 47 MockServletContext app = getMockFactory().getMockServletContext(); 48 app.addResourcePath("/WEB-INF/toolbox.xml","/WEB-INF/toolbox.xml"); 49 app.setResourceAsStream("/WEB-INF/toolbox.xml", 50 new FileInputStream ("./WEB-INF/toolbox.xml")); 51 52 MockServletConfig config = getMockFactory().getMockServletConfig(); 53 config.setInitParameter( 54 "org.apache.velocity.properties","WEB-INF/velocity.properties"); 55 56 MapMessageResources resources = new MapMessageResources(); 57 resources.putMessages("WEB-INF/classes/ApplicationResources.properties"); 58 strutsModule.setResources(resources); 59 60 MockServletContext ctx = getMockFactory().getMockServletContext(); 61 ctx.setRealPath("/", ""); 62 rollerContext = new MockRollerContext(); 63 rollerContext.init(ctx); 64 65 JspFactory.setDefaultFactory(new MockJspFactory(getMockFactory())); 66 } 67 protected void authenticateUser(String username, String role) 68 { 69 MockHttpServletRequest mockRequest = getMockFactory().getMockRequest(); 70 mockRequest.setRemoteUser(username); 71 mockRequest.setUserPrincipal(new MockPrincipal(username)); 72 mockRequest.setUserInRole(role, true); 73 } 74 protected void doFilters() 75 { 76 servletModule.createFilter(PersistenceSessionFilter.class); 77 servletModule.createFilter(RequestFilter.class); 78 servletModule.setDoChain(false); 79 servletModule.doFilter(); 80 getMockFactory().addRequestWrapper(new HttpServletRequestWrapper ( 81 (HttpServletRequest )servletModule.getFilteredRequest())); 82 } 83 84 public class MockJspFactory extends JspFactory 85 { 86 public WebMockObjectFactory factory; 87 public MockJspFactory(WebMockObjectFactory factory) 88 { 89 this.factory = factory; 90 } 91 public PageContext getPageContext( 92 Servlet arg0, ServletRequest arg1, ServletResponse arg2, 93 String arg3, boolean arg4, int arg5, boolean arg6) 94 { 95 return factory.getMockPageContext(); 96 } 97 public void releasePageContext(PageContext arg0) {} 98 public JspEngineInfo getEngineInfo() {return null;} 99 } 100 protected ActionMockObjectFactory getStrutsMockFactory() 101 { 102 return (ActionMockObjectFactory)getMockFactory(); 103 } 104 protected WebMockObjectFactory getMockFactory() 105 { 106 if (mockFactory == null) 107 { 108 mockFactory = new ActionMockObjectFactory(); 109 } 110 return mockFactory; 111 } 112 } 113 | Popular Tags |