KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
19  * Created on Oct 27, 2003
20  */

21 package org.apache.roller.ui.authoring.struts.actions;
22
23
24
25 import com.mockrunner.mock.web.MockActionMapping;
26 import com.mockrunner.mock.web.MockHttpServletRequest;
27 import com.mockrunner.mock.web.MockServletContext;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30
31 import junit.framework.Test;
32 import junit.framework.TestSuite;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37 import org.apache.roller.RollerException;
38 import org.apache.roller.TestUtils;
39 import org.apache.roller.model.RollerFactory;
40 import org.apache.roller.model.UserManager;
41 import org.apache.roller.pojos.FolderData;
42 import org.apache.roller.pojos.UserData;
43 import org.apache.roller.pojos.WebsiteData;
44 import org.apache.roller.ui.StrutsActionTestBase;
45 import org.apache.roller.ui.authoring.struts.formbeans.BookmarksForm;
46 import org.apache.roller.ui.core.BasePageModel;
47 import org.apache.roller.ui.core.RollerRequest;
48
49 /**
50  * Test BookmarkAction (proof-of-concept for Mockrunner Struts testing)
51  * @author Dave Johnson
52  */

53 public class BookmarksActionTest extends StrutsActionTestBase {
54     private WebsiteData testWeblog = null;
55     private UserData testUser = null;
56     public static Log log = LogFactory.getLog(BookmarksActionTest.class);
57     
58     /**
59      * All tests in this suite require a user and a weblog.
60      */

61     public void setUp() throws Exception JavaDoc {
62         super.setUp();
63         try {
64             testUser = TestUtils.setupUser("bkmrkTestUser");
65             testWeblog = TestUtils.setupWeblog("bkmrkTestWeblog", testUser);
66             TestUtils.endSession(true);
67         } catch (Exception JavaDoc ex) {
68             log.error(ex);
69             throw new Exception JavaDoc("Test setup failed", ex);
70         }
71     }
72     
73     public void tearDown() throws Exception JavaDoc {
74         super.tearDown();
75         try {
76             TestUtils.teardownWeblog(testWeblog.getId());
77             TestUtils.teardownUser(testUser.getId());
78             TestUtils.endSession(true);
79         } catch (Exception JavaDoc ex) {
80             log.error(ex);
81             throw new Exception JavaDoc("Test teardown failed", ex);
82         }
83     }
84     
85     public void testSelectFolder() {
86         MockServletContext ctx = getMockFactory().getMockServletContext();
87         ctx.setServletContextName("/roller");
88         MockHttpServletRequest request = getMockFactory().getMockRequest();
89         request.setContextPath("/roller");
90         
91         UserManager umgr = null;
92         UserData user = null;
93         try {
94             umgr = RollerFactory.getRoller().getUserManager();
95             user = (UserData)umgr.getUsers(testWeblog, null, 0, Integer.MAX_VALUE).get(0);
96             doFilters();
97             authenticateUser(user.getUserName(), "editor");
98         } catch (RollerException e) {
99             e.printStackTrace();
100             fail();
101         }
102         
103         // Setup form bean
104
BookmarksForm form = (BookmarksForm)
105         strutsModule.createActionForm(BookmarksForm.class);
106         
107         // Setup mapping and request parameters
108
MockActionMapping mapping = strutsModule.getMockActionMapping();
109         mapping.setupForwards(new String JavaDoc[] {"access-denied","BookmarksForm"});
110         mapping.setParameter("method");
111         strutsModule.addRequestParameter("weblog",testWeblog.getHandle());
112         strutsModule.addRequestParameter("method","selectFolder");
113         
114         try {
115             RollerRequest rreq = new RollerRequest(strutsModule.getMockPageContext());
116             rreq.setWebsite(testWeblog);
117             strutsModule.setRequestAttribute(RollerRequest.ROLLER_REQUEST, rreq);
118             strutsModule.actionPerform(BookmarksAction.class, form);
119         } catch (Throwable JavaDoc e) {
120             e.printStackTrace();
121             fail();
122         }
123         // Test for success
124
strutsModule.verifyNoActionMessages();
125         strutsModule.verifyForward("BookmarksForm");
126         
127         // Verify objects we put in context for JSP page
128
verifyPageContext();
129     }
130     
131     protected void verifyPageContext() {
132         HttpServletRequest req = (HttpServletRequest)
133         servletModule.getFilteredRequest();
134         assertTrue(req.getAttribute("folder") instanceof FolderData);
135         assertTrue(req.getAttribute("model") instanceof BasePageModel);
136     }
137     
138     public static Test suite() {
139         return new TestSuite(BookmarksActionTest.class);
140     }
141     
142 }
143
Popular Tags