KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > StrutsActionTestBase


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

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 JavaDoc;
30 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
31 import javax.servlet.http.HttpSession JavaDoc;
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 /**
46  * Base class for Roller action tests with setup method that creates required
47  * MockRunner mocks for Servlet context and request objects.
48  */

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 JavaDoc {
56         getMockFactory().refresh();
57         
58         strutsModule = new ActionTestModule(getStrutsMockFactory());
59         servletModule = new ServletTestModule(getStrutsMockFactory());
60         
61         // Setup mocks needed to run a Struts action
62
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 JavaDoc username, String JavaDoc 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 JavaDoc 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 JavaDoc(
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