001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018: package org.apache.roller.ui;
019:
020: import com.mockrunner.mock.web.ActionMockObjectFactory;
021: import com.mockrunner.mock.web.MockHttpServletRequest;
022: import com.mockrunner.mock.web.MockServletConfig;
023: import com.mockrunner.mock.web.MockServletContext;
024: import com.mockrunner.mock.web.WebMockObjectFactory;
025: import com.mockrunner.servlet.ServletTestModule;
026: import com.mockrunner.struts.ActionTestModule;
027: import com.mockrunner.struts.MapMessageResources;
028:
029: import java.io.FileInputStream;
030:
031: import javax.servlet.Servlet;
032: import javax.servlet.ServletRequest;
033: import javax.servlet.ServletResponse;
034: import javax.servlet.http.HttpServletRequest;
035: import javax.servlet.http.HttpServletRequestWrapper;
036: import javax.servlet.jsp.JspEngineInfo;
037: import javax.servlet.jsp.JspFactory;
038: import javax.servlet.jsp.PageContext;
039:
040: import junit.framework.TestCase;
041: import org.apache.roller.ui.core.filters.PersistenceSessionFilter;
042: import org.apache.roller.ui.core.filters.RequestFilter;
043:
044: /**
045: * Base class for Roller Servlet tests with setup method that creates required
046: * MockRunner mocks for Servlet context, request and JSP factory objects.
047: */
048: public abstract class ServletTestBase extends TestCase {
049: protected ServletTestModule servletModule;
050: private WebMockObjectFactory mockFactory;
051: protected MockRollerContext rollerContext;
052: protected ActionTestModule strutsModule; // need Struts for message resources
053:
054: public void setUp() throws Exception {
055: getMockFactory().refresh();
056:
057: servletModule = new ServletTestModule(getMockFactory());
058: strutsModule = new ActionTestModule(getStrutsMockFactory());
059:
060: MockServletContext ctx = getMockFactory()
061: .getMockServletContext();
062: ctx.setServletContextName("/roller");
063: ctx.setRealPath("/", "");
064: rollerContext = new MockRollerContext();
065: rollerContext.init(ctx);
066:
067: MockHttpServletRequest request = getMockFactory()
068: .getMockRequest();
069: request.setContextPath("/roller");
070:
071: JspFactory.setDefaultFactory(new MockJspFactory(
072: getMockFactory()));
073:
074: // setup resources needed for running Velocity
075: MockServletContext app = getMockFactory()
076: .getMockServletContext();
077: app.addResourcePath("/WEB-INF/toolbox.xml",
078: "/WEB-INF/toolbox.xml");
079: app.setResourceAsStream("/WEB-INF/toolbox.xml",
080: new FileInputStream("./WEB-INF/toolbox.xml"));
081: MockServletConfig config = getMockFactory()
082: .getMockServletConfig();
083: config.setInitParameter("org.apache.velocity.properties",
084: "WEB-INF/velocity.properties");
085: MapMessageResources resources = new MapMessageResources();
086: resources
087: .putMessages("WEB-INF/classes/ApplicationResources.properties");
088: strutsModule.setResources(resources);
089: }
090:
091: /** Convenience method for placing username in role via mockRequest. */
092: protected void authenticateUser(String username, String role) {
093: MockHttpServletRequest mockRequest = getMockFactory()
094: .getMockRequest();
095: mockRequest.setRemoteUser(username);
096: mockRequest.setUserPrincipal(new MockPrincipal(username));
097: mockRequest.setUserInRole(role, true);
098: }
099:
100: /** Run request through Roller filters (PersistenceSessionFilter and RequestFilter) */
101: protected void doFilters() {
102: servletModule.createFilter(PersistenceSessionFilter.class);
103: servletModule.createFilter(RequestFilter.class);
104: servletModule.setDoChain(false);
105: servletModule.doFilter();
106: getMockFactory().addRequestWrapper(
107: new HttpServletRequestWrapper(
108: (HttpServletRequest) servletModule
109: .getFilteredRequest()));
110: }
111:
112: protected ActionMockObjectFactory getStrutsMockFactory() {
113: return (ActionMockObjectFactory) getMockFactory();
114: }
115:
116: protected WebMockObjectFactory getMockFactory() {
117: if (mockFactory == null) {
118: mockFactory = new ActionMockObjectFactory();
119: }
120: return mockFactory;
121: }
122:
123: /** MockRunner doesn't have one of these, but Roller rendering process needs one. */
124: public class MockJspFactory extends JspFactory {
125:
126: public WebMockObjectFactory factory;
127:
128: public MockJspFactory(WebMockObjectFactory factory) {
129: this .factory = factory;
130: }
131:
132: public PageContext getPageContext(Servlet arg0,
133: ServletRequest arg1, ServletResponse arg2, String arg3,
134: boolean arg4, int arg5, boolean arg6) {
135: return factory.getMockPageContext();
136: }
137:
138: public void releasePageContext(PageContext arg0) {
139: }
140:
141: public JspEngineInfo getEngineInfo() {
142: return null;
143: }
144: }
145: }
|