KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > StrutsActionTestBase


1 package org.roller.presentation;
2
3 import javax.servlet.http.HttpServletRequest JavaDoc;
4 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
5
6 import org.roller.RollerTestBase;
7 import org.roller.presentation.filters.PersistenceSessionFilter;
8 import org.roller.presentation.filters.RequestFilter;
9
10 import com.mockrunner.mock.web.ActionMockObjectFactory;
11 import com.mockrunner.mock.web.MockHttpServletRequest;
12 import com.mockrunner.mock.web.MockServletContext;
13 import com.mockrunner.mock.web.WebMockObjectFactory;
14 import com.mockrunner.servlet.ServletTestModule;
15 import com.mockrunner.struts.ActionTestModule;
16 import com.mockrunner.struts.MapMessageResources;
17
18 /**
19  * Base for Struts Action testing.
20  * @author Dave Johnson
21  */

22 public class StrutsActionTestBase extends RollerTestBase
23 {
24     private ActionMockObjectFactory mockFactory;
25     protected MockRollerContext rollerContext;
26     protected ActionTestModule strutsModule;
27     protected ServletTestModule servletModule;
28
29     public void setUp() throws Exception JavaDoc
30     {
31         super.setUp();
32         getMockFactory().refresh();
33         strutsModule = new ActionTestModule(getStrutsMockFactory());
34         servletModule = new ServletTestModule(getStrutsMockFactory());
35         
36         MapMessageResources resources = new MapMessageResources();
37         resources.putMessages("WEB-INF/classes/ApplicationResources.properties");
38         strutsModule.setResources(resources);
39         
40         MockServletContext ctx = getMockFactory().getMockServletContext();
41         ctx.setRealPath("/", "");
42         rollerContext = new MockRollerContext();
43         rollerContext.init(ctx);
44     }
45     protected void authenticateUser(String JavaDoc username, String JavaDoc role)
46     {
47         MockHttpServletRequest mockRequest = getMockFactory().getMockRequest();
48         mockRequest.setRemoteUser(username);
49         mockRequest.setUserPrincipal(new MockPrincipal(username));
50         mockRequest.setUserInRole(role, true);
51     }
52     
53     protected void doFilters()
54     {
55         servletModule.createFilter(PersistenceSessionFilter.class);
56         servletModule.createFilter(RequestFilter.class);
57         servletModule.setDoChain(true);
58         servletModule.doFilter();
59         getMockFactory().addRequestWrapper(new HttpServletRequestWrapper JavaDoc(
60             (HttpServletRequest JavaDoc)servletModule.getFilteredRequest()));
61     }
62     protected ActionMockObjectFactory getStrutsMockFactory()
63     {
64         return (ActionMockObjectFactory)getMockFactory();
65     }
66     protected WebMockObjectFactory getMockFactory()
67     {
68         if (mockFactory == null)
69         {
70             mockFactory = new ActionMockObjectFactory();
71         }
72         return mockFactory;
73     }
74 }
75
Popular Tags