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: /*
019: * Created on Oct 27, 2003
020: */
021: package org.apache.roller.ui.authoring.struts.actions;
022:
023: import com.mockrunner.mock.web.MockActionMapping;
024: import com.mockrunner.mock.web.MockHttpServletRequest;
025: import com.mockrunner.mock.web.MockServletContext;
026:
027: import javax.servlet.http.HttpServletRequest;
028:
029: import junit.framework.Test;
030: import junit.framework.TestSuite;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034:
035: import org.apache.roller.RollerException;
036: import org.apache.roller.TestUtils;
037: import org.apache.roller.business.RollerFactory;
038: import org.apache.roller.business.UserManager;
039: import org.apache.roller.pojos.FolderData;
040: import org.apache.roller.pojos.UserData;
041: import org.apache.roller.pojos.WebsiteData;
042: import org.apache.roller.ui.StrutsActionTestBase;
043: import org.apache.roller.ui.authoring.struts.formbeans.BookmarksForm;
044: import org.apache.roller.ui.core.BasePageModel;
045: import org.apache.roller.ui.core.RollerRequest;
046:
047: /**
048: * Test BookmarkAction (proof-of-concept for Mockrunner Struts testing)
049: * @author Dave Johnson
050: */
051: public class BookmarksActionTest extends StrutsActionTestBase {
052: private WebsiteData testWeblog = null;
053: private UserData testUser = null;
054: public static Log log = LogFactory
055: .getLog(BookmarksActionTest.class);
056:
057: /**
058: * All tests in this suite require a user and a weblog.
059: */
060: public void setUp() throws Exception {
061: super .setUp();
062: try {
063: testUser = TestUtils.setupUser("bkmrkTestUser");
064: testWeblog = TestUtils.setupWeblog("bkmrkTestWeblog",
065: testUser);
066: TestUtils.endSession(true);
067: } catch (Exception ex) {
068: log.error(ex);
069: throw new Exception("Test setup failed", ex);
070: }
071: }
072:
073: public void tearDown() throws Exception {
074: super .tearDown();
075: try {
076: TestUtils.teardownWeblog(testWeblog.getId());
077: TestUtils.teardownUser(testUser.getId());
078: TestUtils.endSession(true);
079: } catch (Exception ex) {
080: log.error(ex);
081: throw new Exception("Test teardown failed", ex);
082: }
083: }
084:
085: public void testSelectFolder() {
086: MockServletContext ctx = getMockFactory()
087: .getMockServletContext();
088: ctx.setServletContextName("/roller");
089: MockHttpServletRequest request = getMockFactory()
090: .getMockRequest();
091: request.setContextPath("/roller");
092:
093: UserManager umgr = null;
094: UserData user = null;
095: try {
096: umgr = RollerFactory.getRoller().getUserManager();
097: user = (UserData) umgr.getUsers(testWeblog, null, 0,
098: Integer.MAX_VALUE).get(0);
099: doFilters();
100: authenticateUser(user.getUserName(), "editor");
101: } catch (RollerException e) {
102: e.printStackTrace();
103: fail();
104: }
105:
106: // Setup form bean
107: BookmarksForm form = (BookmarksForm) strutsModule
108: .createActionForm(BookmarksForm.class);
109:
110: // Setup mapping and request parameters
111: MockActionMapping mapping = strutsModule.getMockActionMapping();
112: mapping.setupForwards(new String[] { "access-denied",
113: "BookmarksForm" });
114: mapping.setParameter("method");
115: strutsModule.addRequestParameter("weblog", testWeblog
116: .getHandle());
117: strutsModule.addRequestParameter("method", "selectFolder");
118:
119: try {
120: RollerRequest rreq = new RollerRequest(strutsModule
121: .getMockPageContext());
122: rreq.setWebsite(testWeblog);
123: strutsModule.setRequestAttribute(
124: RollerRequest.ROLLER_REQUEST, rreq);
125: strutsModule.actionPerform(BookmarksAction.class, form);
126: } catch (Throwable e) {
127: e.printStackTrace();
128: fail();
129: }
130: // Test for success
131: strutsModule.verifyNoActionMessages();
132: strutsModule.verifyForward("BookmarksForm");
133:
134: // Verify objects we put in context for JSP page
135: verifyPageContext();
136: }
137:
138: protected void verifyPageContext() {
139: HttpServletRequest req = (HttpServletRequest) servletModule
140: .getFilteredRequest();
141: assertTrue(req.getAttribute("folder") instanceof FolderData);
142: assertTrue(req.getAttribute("model") instanceof BasePageModel);
143: }
144:
145: public static Test suite() {
146: return new TestSuite(BookmarksActionTest.class);
147: }
148:
149: }
|