KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > TestUtils


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  * TestUtils.java
20  *
21  * Created on April 6, 2006, 8:38 PM
22  */

23
24 package org.apache.roller;
25
26 import org.apache.roller.model.AutoPingManager;
27 import org.apache.roller.model.PingTargetManager;
28 import org.apache.roller.model.RollerFactory;
29 import org.apache.roller.model.UserManager;
30 import org.apache.roller.model.WeblogManager;
31 import org.apache.roller.pojos.AutoPingData;
32 import org.apache.roller.pojos.CommentData;
33 import org.apache.roller.pojos.PingTargetData;
34 import org.apache.roller.pojos.UserData;
35 import org.apache.roller.pojos.WeblogCategoryData;
36 import org.apache.roller.pojos.WeblogEntryData;
37 import org.apache.roller.pojos.WebsiteData;
38
39
40 /**
41  * Utility class for unit test classes.
42  */

43 public final class TestUtils {
44     
45     
46     /**
47      * Convenience method that simulates the end of a typical session.
48      *
49      * Normally this would be triggered by the end of the response in the webapp
50      * but for unit tests we need to do this explicitly.
51      *
52      * @param flush true if you want to flush changes to db before releasing
53      */

54     public static void endSession(boolean flush) throws Exception JavaDoc {
55         
56         if(flush) {
57             RollerFactory.getRoller().flush();
58         }
59         
60         RollerFactory.getRoller().release();
61     }
62     
63     
64     /**
65      * Convenience method that creates a user and stores it.
66      */

67     public static UserData setupUser(String JavaDoc username) throws Exception JavaDoc {
68         
69         UserData testUser = new UserData();
70         testUser.setUserName(username);
71         testUser.setPassword("password");
72         testUser.setFullName("Test User");
73         testUser.setEmailAddress("TestUser@dev.null");
74         testUser.setLocale("en_US");
75         testUser.setTimeZone("America/Los_Angeles");
76         testUser.setDateCreated(new java.util.Date JavaDoc());
77         testUser.setEnabled(Boolean.TRUE);
78         
79         // store the user
80
UserManager mgr = RollerFactory.getRoller().getUserManager();
81         mgr.addUser(testUser);
82         
83         // query for the user to make sure we return the persisted object
84
UserData user = mgr.getUserByUserName(username);
85         
86         if(user == null)
87             throw new RollerException("error inserting new user");
88         
89         return user;
90     }
91     
92     
93     /**
94      * Convenience method for removing a user.
95      */

96     public static void teardownUser(String JavaDoc id) throws Exception JavaDoc {
97         
98         // lookup the user
99
UserManager mgr = RollerFactory.getRoller().getUserManager();
100         UserData user = mgr.getUser(id);
101         
102         // remove the user
103
mgr.removeUser(user);
104     }
105     
106     
107     /**
108      * Convenience method that creates a weblog and stores it.
109      */

110     public static WebsiteData setupWeblog(String JavaDoc handle, UserData creator) throws Exception JavaDoc {
111         
112         WebsiteData testWeblog = new WebsiteData();
113         testWeblog.setName("Test Weblog");
114         testWeblog.setDescription("Test Weblog");
115         testWeblog.setHandle(handle);
116         testWeblog.setEmailAddress("testweblog@dev.null");
117         testWeblog.setEditorPage("editor-text.jsp");
118         testWeblog.setBlacklist("");
119         testWeblog.setEmailFromAddress("");
120         testWeblog.setEditorTheme("basic");
121         testWeblog.setLocale("en_US");
122         testWeblog.setTimeZone("America/Los_Angeles");
123         testWeblog.setDateCreated(new java.util.Date JavaDoc());
124         testWeblog.setCreator(creator);
125         
126         // add weblog
127
UserManager mgr = RollerFactory.getRoller().getUserManager();
128         mgr.addWebsite(testWeblog);
129         
130         // query for the new weblog and return it
131
WebsiteData weblog = mgr.getWebsiteByHandle(handle);
132         
133         if(weblog == null)
134             throw new RollerException("error setting up weblog");
135         
136         return weblog;
137     }
138     
139     
140     /**
141      * Convenience method for removing a weblog.
142      */

143     public static void teardownWeblog(String JavaDoc id) throws Exception JavaDoc {
144         
145         // lookup the weblog
146
UserManager mgr = RollerFactory.getRoller().getUserManager();
147         WebsiteData weblog = mgr.getWebsite(id);
148         
149         // remove the weblog
150
mgr.removeWebsite(weblog);
151     }
152  
153     
154     /**
155      * Convenience method for creating a weblog entry.
156      */

157     public static WeblogEntryData setupWeblogEntry(String JavaDoc anchor,
158                                                    WeblogCategoryData cat,
159                                                    WebsiteData weblog,
160                                                    UserData user)
161             throws Exception JavaDoc {
162         
163         WeblogEntryData testEntry = new WeblogEntryData();
164         testEntry.setTitle(anchor);
165         testEntry.setLink("testEntryLink");
166         testEntry.setText("blah blah entry");
167         testEntry.setAnchor(anchor);
168         testEntry.setPubTime(new java.sql.Timestamp JavaDoc(new java.util.Date JavaDoc().getTime()));
169         testEntry.setUpdateTime(new java.sql.Timestamp JavaDoc(new java.util.Date JavaDoc().getTime()));
170         testEntry.setStatus(WeblogEntryData.PUBLISHED);
171         testEntry.setWebsite(weblog);
172         testEntry.setCreator(user);
173         testEntry.setCategory(cat);
174         
175         // store entry
176
WeblogManager mgr = RollerFactory.getRoller().getWeblogManager();
177         mgr.saveWeblogEntry(testEntry);
178         
179         // query for object
180
WeblogEntryData entry = mgr.getWeblogEntry(testEntry.getId());
181         
182         if(entry == null)
183             throw new RollerException("error setting up weblog entry");
184         
185         return entry;
186     }
187     
188     
189     /**
190      * Convenience method for removing a weblog entry.
191      */

192     public static void teardownWeblogEntry(String JavaDoc id) throws Exception JavaDoc {
193         
194         // lookup the entry
195
WeblogManager mgr = RollerFactory.getRoller().getWeblogManager();
196         WeblogEntryData entry = mgr.getWeblogEntry(id);
197         
198         // remove the entry
199
mgr.removeWeblogEntry(entry);
200     }
201     
202     
203     /**
204      * Convenience method for creating a comment.
205      */

206     public static CommentData setupComment(String JavaDoc content, WeblogEntryData entry)
207             throws Exception JavaDoc {
208         
209         CommentData testComment = new CommentData();
210         testComment.setName("test");
211         testComment.setEmail("test");
212         testComment.setUrl("test");
213         testComment.setRemoteHost("foofoo");
214         testComment.setContent("this is a test comment");
215         testComment.setPostTime(new java.sql.Timestamp JavaDoc(new java.util.Date JavaDoc().getTime()));
216         testComment.setWeblogEntry(entry);
217         testComment.setPending(Boolean.FALSE);
218         testComment.setApproved(Boolean.TRUE);
219         
220         // store testComment
221
WeblogManager mgr = RollerFactory.getRoller().getWeblogManager();
222         mgr.saveComment(testComment);
223         
224         // query for object
225
CommentData comment = mgr.getComment(testComment.getId());
226         
227         if(comment == null)
228             throw new RollerException("error setting up comment");
229         
230         return comment;
231     }
232     
233     
234     /**
235      * Convenience method for removing a comment.
236      */

237     public static void teardownComment(String JavaDoc id) throws Exception JavaDoc {
238         
239         // lookup the comment
240
WeblogManager mgr = RollerFactory.getRoller().getWeblogManager();
241         CommentData comment = mgr.getComment(id);
242         
243         // remove the comment
244
mgr.removeComment(comment);
245     }
246     
247     
248     /**
249      * Convenience method for creating a ping target.
250      */

251     public static PingTargetData setupPingTarget(String JavaDoc name, String JavaDoc url)
252             throws Exception JavaDoc {
253         
254         PingTargetData testPing = new PingTargetData();
255         testPing.setName("testCommonPing");
256         testPing.setPingUrl("http://localhost/testCommonPing");
257         
258         // store ping
259
PingTargetManager pingMgr = RollerFactory.getRoller().getPingTargetManager();
260         pingMgr.savePingTarget(testPing);
261         
262         // query for it
263
PingTargetData ping = pingMgr.getPingTarget(testPing.getId());
264         
265         if(ping == null)
266             throw new RollerException("error setting up ping target");
267         
268         return ping;
269     }
270     
271     
272     /**
273      * Convenience method for removing a ping target.
274      */

275     public static void teardownPingTarget(String JavaDoc id) throws Exception JavaDoc {
276         
277         // query for it
278
PingTargetManager pingMgr = RollerFactory.getRoller().getPingTargetManager();
279         PingTargetData ping = pingMgr.getPingTarget(id);
280         
281         // remove the ping
282
pingMgr.removePingTarget(ping);
283     }
284     
285     
286     /**
287      * Convenience method for creating an auto ping.
288      */

289     public static AutoPingData setupAutoPing(PingTargetData ping, WebsiteData weblog)
290             throws Exception JavaDoc {
291         
292         AutoPingManager mgr = RollerFactory.getRoller().getAutopingManager();
293         
294         // store auto ping
295
AutoPingData autoPing = new AutoPingData(null, ping, weblog);
296         mgr.saveAutoPing(autoPing);
297         
298         // query for it
299
autoPing = mgr.getAutoPing(autoPing.getId());
300         
301         if(autoPing == null)
302             throw new RollerException("error setting up auto ping");
303         
304         return autoPing;
305     }
306     
307     
308     /**
309      * Convenience method for removing an auto ping.
310      */

311     public static void teardownAutoPing(String JavaDoc id) throws Exception JavaDoc {
312         
313         // query for it
314
AutoPingManager mgr = RollerFactory.getRoller().getAutopingManager();
315         AutoPingData autoPing = mgr.getAutoPing(id);
316         
317         // remove the auto ping
318
mgr.removeAutoPing(autoPing);
319     }
320     
321 }
322
Popular Tags