KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > business > CommentTest


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  * CommentTest.java
20  *
21  * Created on April 12, 2006, 3:12 PM
22  */

23
24 package org.apache.roller.business;
25
26 import java.util.List JavaDoc;
27 import junit.framework.Test;
28 import junit.framework.TestCase;
29 import junit.framework.TestSuite;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.roller.TestUtils;
33 import org.apache.roller.model.RollerFactory;
34 import org.apache.roller.model.WeblogManager;
35 import org.apache.roller.pojos.CommentData;
36 import org.apache.roller.pojos.UserData;
37 import org.apache.roller.pojos.WeblogEntryData;
38 import org.apache.roller.pojos.WebsiteData;
39
40
41 /**
42  * Test Comment related business operations.
43  *
44  * That includes:
45  */

46 public class CommentTest extends TestCase {
47     
48     public static Log log = LogFactory.getLog(CommentTest.class);
49     
50     UserData testUser = null;
51     WebsiteData testWeblog = null;
52     WeblogEntryData testEntry = null;
53     
54     
55     public CommentTest(String JavaDoc name) {
56         super(name);
57     }
58     
59     
60     public static Test suite() {
61         return new TestSuite(CommentTest.class);
62     }
63     
64     
65     /**
66      * All tests in this suite require a user, weblog, and an entry.
67      */

68     public void setUp() throws Exception JavaDoc {
69         
70         try {
71             testUser = TestUtils.setupUser("commentTestUser");
72             testWeblog = TestUtils.setupWeblog("commentTestWeblog", testUser);
73             testEntry = TestUtils.setupWeblogEntry("commentTestEntry", testWeblog.getDefaultCategory(), testWeblog, testUser);
74             TestUtils.endSession(true);
75         } catch (Exception JavaDoc ex) {
76             log.error(ex);
77             throw new Exception JavaDoc("Test setup failed", ex);
78         }
79     }
80     
81     public void tearDown() throws Exception JavaDoc {
82         
83         try {
84             TestUtils.teardownWeblogEntry(testEntry.getId());
85             TestUtils.teardownWeblog(testWeblog.getId());
86             TestUtils.teardownUser(testUser.getId());
87             TestUtils.endSession(true);
88         } catch (Exception JavaDoc ex) {
89             log.error(ex);
90             throw new Exception JavaDoc("Test teardown failed", ex);
91         }
92     }
93     
94     
95     /**
96      * Test basic persistence operations ... Create, Update, Delete
97      */

98     public void testCommentCRUD() throws Exception JavaDoc {
99         
100         WeblogManager mgr = RollerFactory.getRoller().getWeblogManager();
101         
102         CommentData comment = new CommentData();
103         comment.setName("test");
104         comment.setEmail("test");
105         comment.setUrl("test");
106         comment.setRemoteHost("foofoo");
107         comment.setContent("this is a test comment");
108         comment.setPostTime(new java.sql.Timestamp JavaDoc(new java.util.Date JavaDoc().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 JavaDoc 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     /**
147      * Test lookup mechanisms ...
148      */

149     public void testCommentLookups() throws Exception JavaDoc {
150         
151         WeblogManager mgr = RollerFactory.getRoller().getWeblogManager();
152         List JavaDoc comments = null;
153         
154         // we need some comments to play with
155
CommentData comment1 = TestUtils.setupComment("comment1", testEntry);
156         CommentData comment2 = TestUtils.setupComment("comment2", testEntry);
157         CommentData comment3 = TestUtils.setupComment("comment3", testEntry);
158         TestUtils.endSession(true);
159         
160         // get all comments
161
comments = null;
162         comments = mgr.getComments(null, null, null, null, null, null, null, null, false, 0, -1);
163         assertNotNull(comments);
164         assertEquals(3, comments.size());
165         
166         // get all comments for entry
167
comments = null;
168         comments = mgr.getComments(null, testEntry, null, null, null, null, null, null, false, 0, -1);
169         assertNotNull(comments);
170         assertEquals(3, comments.size());
171         
172         // make some changes
173
comment3.setPending(Boolean.TRUE);
174         comment3.setApproved(Boolean.FALSE);
175         mgr.saveComment(comment3);
176         
177         // get pending comments
178
comments = null;
179         comments = mgr.getComments(null, null, null, null, null, Boolean.TRUE, null, null, false, 0, -1);
180         assertNotNull(comments);
181         assertEquals(1, comments.size());
182         
183         // get approved comments
184
comments = null;
185         comments = mgr.getComments(null, null, null, null, null, null, Boolean.TRUE, null, false, 0, -1);
186         assertNotNull(comments);
187         assertEquals(2, comments.size());
188         
189         // get comments with offset
190
comments = null;
191         comments = mgr.getComments(null, null, null, null, null, null, null, null, false, 1, -1);
192         assertNotNull(comments);
193         assertEquals(2, comments.size());
194         
195         // remove test comments
196
TestUtils.teardownComment(comment1.getId());
197         TestUtils.teardownComment(comment2.getId());
198         TestUtils.teardownComment(comment3.getId());
199         TestUtils.endSession(true);
200     }
201     
202     
203     /**
204      * Test extra CRUD methods ... removeComments(ids), removeCommentsForEntry
205      */

206 // public void testAdvancedCommentCRUD() throws Exception {
207
//
208
// WeblogManager mgr = RollerFactory.getRoller().getWeblogManager();
209
// List comments = null;
210
//
211
// // we need some comments to play with
212
// CommentData comment1 = TestUtils.setupComment("comment1", testEntry);
213
// CommentData comment2 = TestUtils.setupComment("comment2", testEntry);
214
// CommentData comment3 = TestUtils.setupComment("comment3", testEntry);
215
// CommentData comment4 = TestUtils.setupComment("comment4", testEntry);
216
// TestUtils.endSession(true);
217
//
218
// // remove a collection of comments
219
// String[] delComments = new String[2];
220
// delComments[0] = comment1.getId();
221
// delComments[1] = comment2.getId();
222
// mgr.removeComments(delComments);
223
// TestUtils.endSession(true);
224
//
225
// // make sure comments were deleted
226
// comments = null;
227
// comments = mgr.getComments(null, null, null, null, null, null, null, null, false, 0, -1);
228
// assertNotNull(comments);
229
// assertEquals(2, comments.size());
230
//
231
// // remove all comments for entry
232
// mgr.removeCommentsForEntry(testEntry.getId());
233
// TestUtils.endSession(true);
234
//
235
// // make sure comments were deleted
236
// comments = null;
237
// comments = mgr.getComments(null, null, null, null, null, null, null, null, false, 0, -1);
238
// assertNotNull(comments);
239
// assertEquals(0, comments.size());
240
// }
241

242 }
243
Popular Tags