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