KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > authoring > struts > actions > WeblogEntryActionTest


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.authoring.struts.actions;
19
20
21 import com.mockrunner.mock.web.MockActionMapping;
22 import com.mockrunner.mock.web.MockHttpServletRequest;
23
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25
26 import junit.framework.Test;
27 import junit.framework.TestSuite;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 import org.apache.roller.RollerException;
33 import org.apache.roller.TestUtils;
34 import org.apache.roller.model.RollerFactory;
35 import org.apache.roller.model.UserManager;
36 import org.apache.roller.pojos.UserData;
37 import org.apache.roller.pojos.WebsiteData;
38 import org.apache.roller.ui.StrutsActionTestBase;
39 import org.apache.roller.ui.authoring.struts.formbeans.WeblogEntryFormEx;
40 import org.apache.roller.ui.core.RollerRequest;
41
42 /**
43  * @author dave
44  */

45 public class WeblogEntryActionTest extends StrutsActionTestBase {
46     private WebsiteData testWeblog = null;
47     private UserData testUser = null;
48     public static Log log = LogFactory.getLog(WeblogEntryActionTest.class);
49     
50     /**
51      * All tests in this suite require a user and a weblog.
52      */

53     public void setUp() throws Exception JavaDoc {
54         super.setUp();
55         try {
56             testUser = TestUtils.setupUser("bkmrkTestUser");
57             testWeblog = TestUtils.setupWeblog("bkmrkTestWeblog", testUser);
58             TestUtils.endSession(true);
59         } catch (Exception JavaDoc ex) {
60             log.error(ex);
61             throw new Exception JavaDoc("Test setup failed", ex);
62         }
63     }
64     
65     public void tearDown() throws Exception JavaDoc {
66         super.tearDown();
67         try {
68             TestUtils.teardownWeblog(testWeblog.getId());
69             TestUtils.teardownUser(testUser.getId());
70             TestUtils.endSession(true);
71         } catch (Exception JavaDoc ex) {
72             log.error(ex);
73             throw new Exception JavaDoc("Test teardown failed", ex);
74         }
75     }
76     public void testCreateWeblogEntry() {
77         MockHttpServletRequest mockRequest = getMockFactory().getMockRequest();
78         mockRequest.setContextPath("/dummy");
79         doFilters();
80         
81         UserManager umgr = null;
82         UserData user = null;
83         try {
84             umgr = RollerFactory.getRoller().getUserManager();
85             user = (UserData)umgr.getUsers(testWeblog, null, 0, -1).get(0);
86             authenticateUser(user.getUserName(), "editor");
87         } catch (RollerException e) {
88             e.printStackTrace();
89             fail();
90         }
91         
92         // Setup mapping and request parameters
93
MockActionMapping mapping = strutsModule.getMockActionMapping();
94         mapping.setupForwards(new String JavaDoc[] {
95             "access-denied","weblogEdit.page","weblogEntryRemove.page"});
96         mapping.setParameter("method");
97         strutsModule.addRequestParameter("weblog",testWeblog.getHandle());
98         strutsModule.addRequestParameter("method","create");
99         
100         // Setup form bean
101
WeblogEntryFormEx form = (WeblogEntryFormEx)
102         strutsModule.createActionForm(WeblogEntryFormEx.class);
103         form.setTitle("test_title");
104         form.setText("Test blog text");
105         
106         try {
107             RollerRequest rreq = new RollerRequest(strutsModule.getMockPageContext());
108             rreq.setWebsite(testWeblog);
109             strutsModule.setRequestAttribute(RollerRequest.ROLLER_REQUEST, rreq);
110             strutsModule.actionPerform(WeblogEntryFormAction.class, form);
111         } catch (Throwable JavaDoc t) {
112             t.printStackTrace();
113             fail();
114         }
115         // Test for success
116
strutsModule.verifyNoActionMessages();
117         strutsModule.verifyForward("weblogEdit.page");
118         
119         // Verify objects we put in context for JSP page
120
verifyPageContext();
121     }
122     
123     protected void verifyPageContext() {
124         HttpServletRequest req = (HttpServletRequest)
125         servletModule.getFilteredRequest();
126         assertNotNull(req.getAttribute("model"));
127     }
128     
129     public static Test suite() {
130         return new TestSuite(WeblogEntryActionTest.class);
131     }
132     
133 }
134
Popular Tags