KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.roller.presentation;
2
3 import java.io.FileInputStream JavaDoc;
4 import java.io.FileNotFoundException JavaDoc;
5
6 import javax.servlet.Servlet JavaDoc;
7 import javax.servlet.ServletRequest JavaDoc;
8 import javax.servlet.ServletResponse JavaDoc;
9 import javax.servlet.http.HttpServletRequest JavaDoc;
10 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
11 import javax.servlet.jsp.JspEngineInfo JavaDoc;
12 import javax.servlet.jsp.JspFactory JavaDoc;
13 import javax.servlet.jsp.PageContext JavaDoc;
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 /**
30  * Base for VelocityServlet testing.
31  * @author Dave Johnson
32  */

33 public abstract class VelocityServletTestBase extends RollerTestBase
34 {
35     protected ServletTestModule servletModule;
36     private WebMockObjectFactory mockFactory;
37     protected MockRollerContext rollerContext;
38     protected ActionTestModule strutsModule; // need Struts for message resources
39

40     public void setUp() throws Exception JavaDoc
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 JavaDoc("./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 JavaDoc username, String JavaDoc 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 JavaDoc(
81             (HttpServletRequest JavaDoc)servletModule.getFilteredRequest()));
82     }
83     /** MockRunner doesn't have one of these */
84     public class MockJspFactory extends JspFactory JavaDoc
85     {
86         public WebMockObjectFactory factory;
87         public MockJspFactory(WebMockObjectFactory factory)
88         {
89             this.factory = factory;
90         }
91         public PageContext JavaDoc getPageContext(
92             Servlet JavaDoc arg0, ServletRequest JavaDoc arg1, ServletResponse JavaDoc arg2,
93             String JavaDoc arg3, boolean arg4, int arg5, boolean arg6)
94         {
95             return factory.getMockPageContext();
96         }
97         public void releasePageContext(PageContext JavaDoc arg0) {}
98         public JspEngineInfo JavaDoc 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