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: * TestUtils.java
020: *
021: * Created on April 6, 2006, 8:38 PM
022: */
023:
024: package org.apache.roller;
025:
026: import org.apache.roller.business.pings.AutoPingManager;
027: import org.apache.roller.business.pings.PingTargetManager;
028: import org.apache.roller.business.RollerFactory;
029: import org.apache.roller.business.UserManager;
030: import org.apache.roller.business.WeblogManager;
031: import org.apache.roller.pojos.AutoPingData;
032: import org.apache.roller.pojos.CommentData;
033: import org.apache.roller.pojos.HitCountData;
034: import org.apache.roller.pojos.PingTargetData;
035: import org.apache.roller.pojos.UserData;
036: import org.apache.roller.pojos.WeblogCategoryData;
037: import org.apache.roller.pojos.WeblogEntryData;
038: import org.apache.roller.pojos.WebsiteData;
039:
040: /**
041: * Utility class for unit test classes.
042: */
043: public final class TestUtils {
044:
045: /**
046: * Convenience method that simulates the end of a typical session.
047: *
048: * Normally this would be triggered by the end of the response in the webapp
049: * but for unit tests we need to do this explicitly.
050: *
051: * @param flush true if you want to flush changes to db before releasing
052: */
053: public static void endSession(boolean flush) throws Exception {
054:
055: if (flush) {
056: RollerFactory.getRoller().flush();
057: }
058:
059: RollerFactory.getRoller().release();
060: }
061:
062: /**
063: * Convenience method that creates a user and stores it.
064: */
065: public static UserData setupUser(String username) throws Exception {
066:
067: UserData testUser = new UserData();
068: testUser.setUserName(username);
069: testUser.setPassword("password");
070: testUser.setFullName("Test User");
071: testUser.setEmailAddress("TestUser@dev.null");
072: testUser.setLocale("en_US");
073: testUser.setTimeZone("America/Los_Angeles");
074: testUser.setDateCreated(new java.util.Date());
075: testUser.setEnabled(Boolean.TRUE);
076:
077: // store the user
078: UserManager mgr = RollerFactory.getRoller().getUserManager();
079: mgr.addUser(testUser);
080:
081: // query for the user to make sure we return the persisted object
082: UserData user = mgr.getUserByUserName(username);
083:
084: if (user == null)
085: throw new RollerException("error inserting new user");
086:
087: return user;
088: }
089:
090: /**
091: * Convenience method for removing a user.
092: */
093: public static void teardownUser(String id) throws Exception {
094:
095: // lookup the user
096: UserManager mgr = RollerFactory.getRoller().getUserManager();
097: UserData user = mgr.getUser(id);
098:
099: // remove the user
100: mgr.removeUser(user);
101: }
102:
103: /**
104: * Convenience method that creates a weblog and stores it.
105: */
106: public static WebsiteData setupWeblog(String handle,
107: UserData creator) throws Exception {
108:
109: WebsiteData testWeblog = new WebsiteData();
110: testWeblog.setName("Test Weblog");
111: testWeblog.setDescription("Test Weblog");
112: testWeblog.setHandle(handle);
113: testWeblog.setEmailAddress("testweblog@dev.null");
114: testWeblog.setEditorPage("editor-text.jsp");
115: testWeblog.setBlacklist("");
116: testWeblog.setEmailFromAddress("");
117: testWeblog.setEditorTheme("basic");
118: testWeblog.setLocale("en_US");
119: testWeblog.setTimeZone("America/Los_Angeles");
120: testWeblog.setDateCreated(new java.util.Date());
121: testWeblog.setCreator(creator);
122:
123: // add weblog
124: UserManager mgr = RollerFactory.getRoller().getUserManager();
125: mgr.addWebsite(testWeblog);
126:
127: // query for the new weblog and return it
128: WebsiteData weblog = mgr.getWebsiteByHandle(handle);
129:
130: if (weblog == null)
131: throw new RollerException("error setting up weblog");
132:
133: return weblog;
134: }
135:
136: /**
137: * Convenience method for removing a weblog.
138: */
139: public static void teardownWeblog(String id) throws Exception {
140:
141: // lookup the weblog
142: UserManager mgr = RollerFactory.getRoller().getUserManager();
143: WebsiteData weblog = mgr.getWebsite(id);
144:
145: // remove the weblog
146: mgr.removeWebsite(weblog);
147: }
148:
149: /**
150: * Convenience method for creating a weblog entry.
151: */
152: public static WeblogEntryData setupWeblogEntry(String anchor,
153: WeblogCategoryData cat, WebsiteData weblog, UserData user)
154: throws Exception {
155:
156: WeblogEntryData testEntry = new WeblogEntryData();
157: testEntry.setTitle(anchor);
158: testEntry.setLink("testEntryLink");
159: testEntry.setText("blah blah entry");
160: testEntry.setAnchor(anchor);
161: testEntry.setPubTime(new java.sql.Timestamp(
162: new java.util.Date().getTime()));
163: testEntry.setUpdateTime(new java.sql.Timestamp(
164: new java.util.Date().getTime()));
165: testEntry.setStatus(WeblogEntryData.PUBLISHED);
166: testEntry.setWebsite(weblog);
167: testEntry.setCreator(user);
168: testEntry.setCategory(cat);
169:
170: // store entry
171: WeblogManager mgr = RollerFactory.getRoller()
172: .getWeblogManager();
173: mgr.saveWeblogEntry(testEntry);
174:
175: // query for object
176: WeblogEntryData entry = mgr.getWeblogEntry(testEntry.getId());
177:
178: if (entry == null)
179: throw new RollerException("error setting up weblog entry");
180:
181: return entry;
182: }
183:
184: /**
185: * Convenience method for removing a weblog entry.
186: */
187: public static void teardownWeblogEntry(String id) throws Exception {
188:
189: // lookup the entry
190: WeblogManager mgr = RollerFactory.getRoller()
191: .getWeblogManager();
192: WeblogEntryData entry = mgr.getWeblogEntry(id);
193:
194: // remove the entry
195: mgr.removeWeblogEntry(entry);
196: }
197:
198: /**
199: * Convenience method for creating a comment.
200: */
201: public static CommentData setupComment(String content,
202: WeblogEntryData entry) throws Exception {
203:
204: CommentData testComment = new CommentData();
205: testComment.setName("test");
206: testComment.setEmail("test");
207: testComment.setUrl("test");
208: testComment.setRemoteHost("foofoo");
209: testComment.setContent("this is a test comment");
210: testComment.setPostTime(new java.sql.Timestamp(
211: new java.util.Date().getTime()));
212: testComment.setWeblogEntry(entry);
213: testComment.setPending(Boolean.FALSE);
214: testComment.setApproved(Boolean.TRUE);
215:
216: // store testComment
217: WeblogManager mgr = RollerFactory.getRoller()
218: .getWeblogManager();
219: mgr.saveComment(testComment);
220:
221: // query for object
222: CommentData comment = mgr.getComment(testComment.getId());
223:
224: if (comment == null)
225: throw new RollerException("error setting up comment");
226:
227: return comment;
228: }
229:
230: /**
231: * Convenience method for removing a comment.
232: */
233: public static void teardownComment(String id) throws Exception {
234:
235: // lookup the comment
236: WeblogManager mgr = RollerFactory.getRoller()
237: .getWeblogManager();
238: CommentData comment = mgr.getComment(id);
239:
240: // remove the comment
241: mgr.removeComment(comment);
242: }
243:
244: /**
245: * Convenience method for creating a ping target.
246: */
247: public static PingTargetData setupPingTarget(String name, String url)
248: throws Exception {
249:
250: PingTargetData testPing = new PingTargetData();
251: testPing.setName("testCommonPing");
252: testPing.setPingUrl("http://localhost/testCommonPing");
253:
254: // store ping
255: PingTargetManager pingMgr = RollerFactory.getRoller()
256: .getPingTargetManager();
257: pingMgr.savePingTarget(testPing);
258:
259: // query for it
260: PingTargetData ping = pingMgr.getPingTarget(testPing.getId());
261:
262: if (ping == null)
263: throw new RollerException("error setting up ping target");
264:
265: return ping;
266: }
267:
268: /**
269: * Convenience method for removing a ping target.
270: */
271: public static void teardownPingTarget(String id) throws Exception {
272:
273: // query for it
274: PingTargetManager pingMgr = RollerFactory.getRoller()
275: .getPingTargetManager();
276: PingTargetData ping = pingMgr.getPingTarget(id);
277:
278: // remove the ping
279: pingMgr.removePingTarget(ping);
280: }
281:
282: /**
283: * Convenience method for creating an auto ping.
284: */
285: public static AutoPingData setupAutoPing(PingTargetData ping,
286: WebsiteData weblog) throws Exception {
287:
288: AutoPingManager mgr = RollerFactory.getRoller()
289: .getAutopingManager();
290:
291: // store auto ping
292: AutoPingData autoPing = new AutoPingData(null, ping, weblog);
293: mgr.saveAutoPing(autoPing);
294:
295: // query for it
296: autoPing = mgr.getAutoPing(autoPing.getId());
297:
298: if (autoPing == null)
299: throw new RollerException("error setting up auto ping");
300:
301: return autoPing;
302: }
303:
304: /**
305: * Convenience method for removing an auto ping.
306: */
307: public static void teardownAutoPing(String id) throws Exception {
308:
309: // query for it
310: AutoPingManager mgr = RollerFactory.getRoller()
311: .getAutopingManager();
312: AutoPingData autoPing = mgr.getAutoPing(id);
313:
314: // remove the auto ping
315: mgr.removeAutoPing(autoPing);
316: }
317:
318: /**
319: * Convenience method for creating a hit count.
320: */
321: public static HitCountData setupHitCount(WebsiteData weblog,
322: int amount) throws Exception {
323:
324: WeblogManager mgr = RollerFactory.getRoller()
325: .getWeblogManager();
326:
327: // store
328: HitCountData testCount = new HitCountData();
329: testCount.setWeblog(weblog);
330: testCount.setDailyHits(amount);
331: mgr.saveHitCount(testCount);
332:
333: // query for it
334: testCount = mgr.getHitCount(testCount.getId());
335:
336: if (testCount == null)
337: throw new RollerException("error setting up hit count");
338:
339: return testCount;
340: }
341:
342: /**
343: * Convenience method for removing a hit count.
344: */
345: public static void teardownHitCount(String id) throws Exception {
346:
347: // query for it
348: WeblogManager mgr = RollerFactory.getRoller()
349: .getWeblogManager();
350: HitCountData testCount = mgr.getHitCount(id);
351:
352: // remove
353: mgr.removeHitCount(testCount);
354: }
355:
356: }
|