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: * CommentTest.java
020: *
021: * Created on April 12, 2006, 3:12 PM
022: */
023:
024: package org.apache.roller.business;
025:
026: import java.util.List;
027: import junit.framework.Test;
028: import junit.framework.TestCase;
029: import junit.framework.TestSuite;
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.apache.roller.TestUtils;
033: import org.apache.roller.business.RollerFactory;
034: import org.apache.roller.business.WeblogManager;
035: import org.apache.roller.pojos.CommentData;
036: import org.apache.roller.pojos.UserData;
037: import org.apache.roller.pojos.WeblogEntryData;
038: import org.apache.roller.pojos.WebsiteData;
039:
040: /**
041: * Test Comment related business operations.
042: *
043: * That includes:
044: */
045: public class CommentTest extends TestCase {
046:
047: public static Log log = LogFactory.getLog(CommentTest.class);
048:
049: UserData testUser = null;
050: WebsiteData testWeblog = null;
051: WeblogEntryData testEntry = null;
052:
053: public CommentTest(String name) {
054: super (name);
055: }
056:
057: public static Test suite() {
058: return new TestSuite(CommentTest.class);
059: }
060:
061: /**
062: * All tests in this suite require a user, weblog, and an entry.
063: */
064: public void setUp() throws Exception {
065:
066: try {
067: testUser = TestUtils.setupUser("commentTestUser");
068: testWeblog = TestUtils.setupWeblog("commentTestWeblog",
069: testUser);
070: testEntry = TestUtils.setupWeblogEntry("commentTestEntry",
071: testWeblog.getDefaultCategory(), testWeblog,
072: testUser);
073: TestUtils.endSession(true);
074: } catch (Exception ex) {
075: log.error(ex);
076: throw new Exception("Test setup failed", ex);
077: }
078: }
079:
080: public void tearDown() throws Exception {
081:
082: try {
083: TestUtils.teardownWeblogEntry(testEntry.getId());
084: TestUtils.teardownWeblog(testWeblog.getId());
085: TestUtils.teardownUser(testUser.getId());
086: TestUtils.endSession(true);
087: } catch (Exception ex) {
088: log.error(ex);
089: throw new Exception("Test teardown failed", ex);
090: }
091: }
092:
093: /**
094: * Test basic persistence operations ... Create, Update, Delete
095: */
096: public void testCommentCRUD() throws Exception {
097:
098: WeblogManager mgr = RollerFactory.getRoller()
099: .getWeblogManager();
100:
101: CommentData comment = new CommentData();
102: comment.setName("test");
103: comment.setEmail("test");
104: comment.setUrl("test");
105: comment.setRemoteHost("foofoo");
106: comment.setContent("this is a test comment");
107: comment.setPostTime(new java.sql.Timestamp(new java.util.Date()
108: .getTime()));
109: comment.setWeblogEntry(testEntry);
110: comment.setPending(Boolean.FALSE);
111: comment.setApproved(Boolean.TRUE);
112:
113: // create a comment
114: mgr.saveComment(comment);
115: String id = comment.getId();
116: TestUtils.endSession(true);
117:
118: // make sure comment was created
119: comment = null;
120: comment = mgr.getComment(id);
121: assertNotNull(comment);
122: assertEquals("this is a test comment", comment.getContent());
123:
124: // update a comment
125: comment.setContent("testtest");
126: mgr.saveComment(comment);
127: TestUtils.endSession(true);
128:
129: // make sure comment was updated
130: comment = null;
131: comment = mgr.getComment(id);
132: assertNotNull(comment);
133: assertEquals("testtest", comment.getContent());
134:
135: // delete a comment
136: mgr.removeComment(comment);
137: TestUtils.endSession(true);
138:
139: // make sure comment was deleted
140: comment = null;
141: comment = mgr.getComment(id);
142: assertNull(comment);
143: }
144:
145: /**
146: * Test lookup mechanisms ...
147: */
148: public void testCommentLookups() throws Exception {
149:
150: WeblogManager mgr = RollerFactory.getRoller()
151: .getWeblogManager();
152: List comments = null;
153:
154: // we need some comments to play with
155: CommentData comment1 = TestUtils.setupComment("comment1",
156: testEntry);
157: CommentData comment2 = TestUtils.setupComment("comment2",
158: testEntry);
159: CommentData comment3 = TestUtils.setupComment("comment3",
160: testEntry);
161: TestUtils.endSession(true);
162:
163: // get all comments
164: comments = null;
165: comments = mgr.getComments(null, null, null, null, null, null,
166: null, null, false, 0, -1);
167: assertNotNull(comments);
168: assertEquals(3, comments.size());
169:
170: // get all comments for entry
171: comments = null;
172: comments = mgr.getComments(null, testEntry, null, null, null,
173: null, null, null, false, 0, -1);
174: assertNotNull(comments);
175: assertEquals(3, comments.size());
176:
177: // make some changes
178: comment3.setPending(Boolean.TRUE);
179: comment3.setApproved(Boolean.FALSE);
180: mgr.saveComment(comment3);
181:
182: // get pending comments
183: comments = null;
184: comments = mgr.getComments(null, null, null, null, null,
185: Boolean.TRUE, null, null, false, 0, -1);
186: assertNotNull(comments);
187: assertEquals(1, comments.size());
188:
189: // get approved comments
190: comments = null;
191: comments = mgr.getComments(null, null, null, null, null, null,
192: Boolean.TRUE, null, false, 0, -1);
193: assertNotNull(comments);
194: assertEquals(2, comments.size());
195:
196: // get comments with offset
197: comments = null;
198: comments = mgr.getComments(null, null, null, null, null, null,
199: null, null, false, 1, -1);
200: assertNotNull(comments);
201: assertEquals(2, comments.size());
202:
203: // remove test comments
204: TestUtils.teardownComment(comment1.getId());
205: TestUtils.teardownComment(comment2.getId());
206: TestUtils.teardownComment(comment3.getId());
207: TestUtils.endSession(true);
208: }
209:
210: /**
211: * Apparently, HSQL has "issues" with LIKE expressions,
212: * so I'm commenting this out for now.
213:
214: public void _testBulkCommentDelete() throws Exception {
215:
216: WeblogManager mgr = RollerFactory.getRoller().getWeblogManager();
217: List comments = null;
218:
219: // we need some comments to play with
220: CommentData comment1 = TestUtils.setupComment("deletemeXXX", testEntry);
221: CommentData comment2 = TestUtils.setupComment("XXXdeleteme", testEntry);
222: CommentData comment3 = TestUtils.setupComment("deleteme", testEntry);
223: CommentData comment4 = TestUtils.setupComment("saveme", testEntry);
224: CommentData comment5 = TestUtils.setupComment("saveme", testEntry);
225: CommentData comment6 = TestUtils.setupComment("saveme", testEntry);
226: TestUtils.endSession(true);
227:
228: // get all comments
229: comments = null;
230: comments = mgr.getComments(
231: null, // website
232: null, // entry
233: null, // searchString
234: null, // startDate
235: null, // endDate
236: null, // pending
237: null, // approved
238: null, // spam
239: true, // reverseChrono
240: 0, // offset
241: -1); // length
242: assertNotNull(comments);
243: assertEquals(6, comments.size());
244:
245: comments = mgr.getComments(
246: null, // website
247: null, // entry
248: "deleteme", // searchString
249: null, // startDate
250: null, // endDate
251: null, // pending
252: null, // approved
253: null, // spam
254: true, // reverseChrono
255: 0, // offset
256: -1); // length
257: assertNotNull(comments);
258: assertEquals(3, comments.size());
259:
260: int countDeleted = mgr.removeMatchingComments(
261: null, // website
262: null, // entry
263: "deleteme", // searchString
264: null, // startDate
265: null, // endDate
266: null, // pending
267: null, // approved
268: null); // spam
269: assertEquals(3, countDeleted);
270:
271: comments = mgr.getComments(
272: null, // website
273: null, // entry
274: null, // searchString
275: null, // startDate
276: null, // endDate
277: null, // pending
278: null, // approved
279: null, // spam
280: true, // reverseChrono
281: 0, // offset
282: -1); // length
283: assertNotNull(comments);
284: assertEquals(3, comments.size());
285:
286: // remove test comments
287: countDeleted = mgr.removeMatchingComments(
288: null, // website
289: null, // entry
290: "saveme", // searchString
291: null, // startDate
292: null, // endDate
293: null, // pending
294: null, // approved
295: null); // spam
296: assertEquals(3, countDeleted);
297: TestUtils.endSession(true);
298: }
299: */
300:
301: /**
302: * Test extra CRUD methods ... removeComments(ids), removeCommentsForEntry
303: */
304: // public void testAdvancedCommentCRUD() throws Exception {
305: //
306: // WeblogManager mgr = RollerFactory.getRoller().getWeblogManager();
307: // List comments = null;
308: //
309: // // we need some comments to play with
310: // CommentData comment1 = TestUtils.setupComment("comment1", testEntry);
311: // CommentData comment2 = TestUtils.setupComment("comment2", testEntry);
312: // CommentData comment3 = TestUtils.setupComment("comment3", testEntry);
313: // CommentData comment4 = TestUtils.setupComment("comment4", testEntry);
314: // TestUtils.endSession(true);
315: //
316: // // remove a collection of comments
317: // String[] delComments = new String[2];
318: // delComments[0] = comment1.getId();
319: // delComments[1] = comment2.getId();
320: // mgr.removeComments(delComments);
321: // TestUtils.endSession(true);
322: //
323: // // make sure comments were deleted
324: // comments = null;
325: // comments = mgr.getComments(null, null, null, null, null, null, null, null, false, 0, -1);
326: // assertNotNull(comments);
327: // assertEquals(2, comments.size());
328: //
329: // // remove all comments for entry
330: // mgr.removeCommentsForEntry(testEntry.getId());
331: // TestUtils.endSession(true);
332: //
333: // // make sure comments were deleted
334: // comments = null;
335: // comments = mgr.getComments(null, null, null, null, null, null, null, null, false, 0, -1);
336: // assertNotNull(comments);
337: // assertEquals(0, comments.size());
338: // }
339: }
|