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.MockServletContext;
023: import com.mockrunner.mock.web.WebMockObjectFactory;
024: import com.mockrunner.servlet.ServletTestModule;
025: import com.mockrunner.struts.ActionTestModule;
026: import com.mockrunner.struts.MapMessageResources;
027:
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletRequestWrapper;
030: import javax.servlet.http.HttpSession;
031:
032: import junit.framework.TestCase;
033:
034: import org.apache.roller.RollerException;
035: import org.apache.roller.business.RollerFactory;
036: import org.apache.roller.business.UserManager;
037: import org.apache.roller.pojos.UserData;
038: import org.apache.roller.ui.MockPrincipal;
039: import org.apache.roller.ui.MockRollerContext;
040: import org.apache.roller.ui.core.RollerSession;
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 action tests with setup method that creates required
046: * MockRunner mocks for Servlet context and request objects.
047: */
048: public class StrutsActionTestBase extends TestCase {
049: private ActionMockObjectFactory mockFactory;
050: protected MockRollerContext rollerContext;
051: protected ActionTestModule strutsModule;
052: protected ServletTestModule servletModule;
053:
054: public void setUp() throws Exception {
055: getMockFactory().refresh();
056:
057: strutsModule = new ActionTestModule(getStrutsMockFactory());
058: servletModule = new ServletTestModule(getStrutsMockFactory());
059:
060: // Setup mocks needed to run a Struts action
061: MapMessageResources resources = new MapMessageResources();
062: resources
063: .putMessages("WEB-INF/classes/ApplicationResources.properties");
064: strutsModule.setResources(resources);
065:
066: MockServletContext ctx = getMockFactory()
067: .getMockServletContext();
068: ctx.setRealPath("/", "");
069: rollerContext = new MockRollerContext();
070: rollerContext.init(ctx);
071: }
072:
073: protected void authenticateUser(String username, String role)
074: throws RollerException {
075:
076: MockHttpServletRequest mockRequest = getMockFactory()
077: .getMockRequest();
078: mockRequest.setRemoteUser(username);
079: mockRequest.setUserPrincipal(new MockPrincipal(username));
080: mockRequest.setUserInRole(role, true);
081:
082: HttpSession session = mockRequest.getSession(true);
083: UserManager umgr = RollerFactory.getRoller().getUserManager();
084: UserData user = umgr.getUserByUserName(username);
085:
086: RollerSession rollerSession = new RollerSession();
087: rollerSession.setAuthenticatedUser(user);
088: session.setAttribute(RollerSession.ROLLER_SESSION,
089: rollerSession);
090: }
091:
092: protected void doFilters() {
093: servletModule.createFilter(PersistenceSessionFilter.class);
094: servletModule.createFilter(RequestFilter.class);
095: servletModule.setDoChain(true);
096: servletModule.doFilter();
097: getMockFactory().addRequestWrapper(
098: new HttpServletRequestWrapper(
099: (HttpServletRequest) servletModule
100: .getFilteredRequest()));
101: }
102:
103: protected ActionMockObjectFactory getStrutsMockFactory() {
104: return (ActionMockObjectFactory) getMockFactory();
105: }
106:
107: protected WebMockObjectFactory getMockFactory() {
108: if (mockFactory == null) {
109: mockFactory = new ActionMockObjectFactory();
110: }
111: return mockFactory;
112: }
113: }
|