1 18 package org.apache.roller.ui; 19 20 21 import com.mockrunner.mock.web.ActionMockObjectFactory; 22 import com.mockrunner.mock.web.MockHttpServletRequest; 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 javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletRequestWrapper ; 31 import javax.servlet.http.HttpSession ; 32 33 import junit.framework.TestCase; 34 35 import org.apache.roller.RollerException; 36 import org.apache.roller.model.RollerFactory; 37 import org.apache.roller.model.UserManager; 38 import org.apache.roller.pojos.UserData; 39 import org.apache.roller.ui.MockPrincipal; 40 import org.apache.roller.ui.MockRollerContext; 41 import org.apache.roller.ui.core.RollerSession; 42 import org.apache.roller.ui.core.filters.PersistenceSessionFilter; 43 import org.apache.roller.ui.core.filters.RequestFilter; 44 45 49 public class StrutsActionTestBase extends TestCase { 50 private ActionMockObjectFactory mockFactory; 51 protected MockRollerContext rollerContext; 52 protected ActionTestModule strutsModule; 53 protected ServletTestModule servletModule; 54 55 public void setUp() throws Exception { 56 getMockFactory().refresh(); 57 58 strutsModule = new ActionTestModule(getStrutsMockFactory()); 59 servletModule = new ServletTestModule(getStrutsMockFactory()); 60 61 MapMessageResources resources = new MapMessageResources(); 63 resources.putMessages("WEB-INF/classes/ApplicationResources.properties"); 64 strutsModule.setResources(resources); 65 66 MockServletContext ctx = getMockFactory().getMockServletContext(); 67 ctx.setRealPath("/", ""); 68 rollerContext = new MockRollerContext(); 69 rollerContext.init(ctx); 70 } 71 72 protected void authenticateUser(String username, String role) 73 throws RollerException { 74 75 MockHttpServletRequest mockRequest = getMockFactory().getMockRequest(); 76 mockRequest.setRemoteUser(username); 77 mockRequest.setUserPrincipal(new MockPrincipal(username)); 78 mockRequest.setUserInRole(role, true); 79 80 HttpSession session = mockRequest.getSession(true); 81 UserManager umgr = RollerFactory.getRoller().getUserManager(); 82 UserData user = umgr.getUserByUserName(username); 83 84 RollerSession rollerSession = new RollerSession(); 85 rollerSession.setAuthenticatedUser(user); 86 session.setAttribute(RollerSession.ROLLER_SESSION, rollerSession); 87 } 88 89 protected void doFilters() { 90 servletModule.createFilter(PersistenceSessionFilter.class); 91 servletModule.createFilter(RequestFilter.class); 92 servletModule.setDoChain(true); 93 servletModule.doFilter(); 94 getMockFactory().addRequestWrapper(new HttpServletRequestWrapper ( 95 (HttpServletRequest)servletModule.getFilteredRequest())); 96 } 97 98 protected ActionMockObjectFactory getStrutsMockFactory() { 99 return (ActionMockObjectFactory)getMockFactory(); 100 } 101 102 protected WebMockObjectFactory getMockFactory() { 103 if (mockFactory == null) { 104 mockFactory = new ActionMockObjectFactory(); 105 } 106 return mockFactory; 107 } 108 } 109 | Popular Tags |