Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
164   435   36   4,56
0   256   0,22   36
36     1  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  CommentTest       Line # 45 164 0% 36 39 80,5% 0.805
 
  (36)
 
1    /*
2    * Copyright (c) 2003-2006, Simon Brown
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * - Redistributions of source code must retain the above copyright
9    * notice, this list of conditions and the following disclaimer.
10    *
11    * - Redistributions in binary form must reproduce the above copyright
12    * notice, this list of conditions and the following disclaimer in
13    * the documentation and/or other materials provided with the
14    * distribution.
15    *
16    * - Neither the name of Pebble nor the names of its contributors may
17    * be used to endorse or promote products derived from this software
18    * without specific prior written permission.
19    *
20    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21    * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23    * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30    * POSSIBILITY OF SUCH DAMAGE.
31    */
32    package net.sourceforge.pebble.domain;
33   
34    import net.sourceforge.pebble.api.event.comment.CommentEvent;
35    import net.sourceforge.pebble.api.event.comment.CommentListener;
36   
37    import java.util.Calendar;
38    import java.util.Date;
39   
40    /**
41    * Tests for the Comment class.
42    *
43    * @author Simon Brown
44    */
 
45    public class CommentTest extends SingleBlogTestCase {
46   
47    private BlogEntry blogEntry;
48    private Comment comment;
49   
 
50  36 toggle protected void setUp() throws Exception {
51  36 super.setUp();
52   
53  36 blogEntry = new BlogEntry(blog);
54  36 comment = blogEntry.createComment("Title", "Body", "Author", "me@somedomain.com", "http://www.google.com", "127.0.0.1");
55  36 comment.setEventsEnabled(true);
56    }
57   
58    /**
59    * Test that a Comment instance can be created correctly.
60    */
 
61  2 toggle public void testConstructionOfSimpleInstance() {
62  2 assertNotNull(comment);
63  2 assertEquals("Title", comment.getTitle());
64  2 assertEquals("Body", comment.getBody());
65  2 assertEquals("Author", comment.getAuthor());
66  2 assertEquals("me@somedomain.com", comment.getEmail());
67  2 assertEquals("http://www.google.com", comment.getWebsite());
68  2 assertEquals("127.0.0.1", comment.getIpAddress());
69  2 assertNotNull(comment.getDate());
70  2 assertEquals(comment.getDate().getTime(), comment.getId());
71  2 assertNotNull(comment.getBlogEntry());
72  2 assertEquals(State.APPROVED, comment.getState());
73  2 assertEquals("c/" + comment.getBlogEntry().getId() + "/" + comment.getId(), comment.getGuid());
74    }
75   
76    /**
77    * Tests that the author name is properly escaped and set.
78    */
 
79  2 toggle public void testAuthor() {
80  2 assertEquals("Author", comment.getAuthor());
81   
82    // blank or null author name defaults to "Anonymous"
83  2 comment.setAuthor("");
84  2 assertEquals("Anonymous", comment.getAuthor());
85  2 comment.setAuthor(null);
86  2 assertEquals("Anonymous", comment.getAuthor());
87   
88    // for security, special HTML characters aren't removed
89    // (they are rendered out at runtime)
90  2 comment.setAuthor("<Author>");
91  2 assertEquals("<Author>", comment.getAuthor());
92    }
93   
94    /**
95    * Tests that the e-mail address is properly escaped and set.
96    */
 
97  2 toggle public void testEmailAddress() {
98  2 assertEquals("me@somedomain.com", comment.getEmail());
99   
100    // blank or null e-mail defaults to null
101  2 comment.setEmail("");
102  2 assertEquals(null, comment.getEmail());
103  2 comment.setEmail(null);
104  2 assertEquals(null, comment.getEmail());
105   
106    // for security, special HTML characters are removed
107  2 comment.setEmail("<me@somedomain.com>");
108  2 assertEquals("&lt;me@somedomain.com&gt;", comment.getEmail());
109    }
110   
111    /**
112    * Tests that the website is properly escaped and set.
113    */
 
114  2 toggle public void testWebsite() {
115  2 assertEquals("http://www.google.com", comment.getWebsite());
116   
117    // blank or null website name defaults to null
118  2 comment.setWebsite("");
119  2 assertEquals(null, comment.getWebsite());
120  2 comment.setWebsite(null);
121  2 assertEquals(null, comment.getWebsite());
122   
123    // for security, special HTML characters are removed
124  2 comment.setWebsite("<script>http://www.google.com");
125  2 assertEquals("http://www.google.com", comment.getWebsite());
126   
127    // anything websites are also checked for known prefixes and "http://"
128    // is prepended if missing
129  2 comment.setWebsite("http://www.google.com");
130  2 assertEquals("http://www.google.com", comment.getWebsite());
131  2 comment.setWebsite("https://www.google.com");
132  2 assertEquals("https://www.google.com", comment.getWebsite());
133  2 comment.setWebsite("ftp://www.google.com");
134  2 assertEquals("ftp://www.google.com", comment.getWebsite());
135  2 comment.setWebsite("mailto://www.google.com");
136  2 assertEquals("mailto://www.google.com", comment.getWebsite());
137  2 comment.setWebsite("www.google.com");
138  2 assertEquals("http://www.google.com", comment.getWebsite());
139    }
140   
141    /**
142    * Tests the body.
143    */
 
144  2 toggle public void testBody() {
145  2 comment.setBody("");
146  2 assertEquals(null, comment.getBody());
147  2 comment.setBody(null);
148  2 assertEquals(null, comment.getBody());
149   
150  2 comment.setBody("Here is some text");
151  2 assertEquals("Here is some text", comment.getBody());
152    }
153   
154    /**
155    * Tests that the date can never be null.
156    */
 
157  2 toggle public void testDate() {
158  2 assertNotNull(comment.getDate());
159   
160  2 comment.setDate(new Date());
161  2 assertNotNull(comment.getDate());
162   
163  2 comment.setDate(null);
164  2 assertNotNull(comment.getDate());
165    }
166   
167    /**
168    * Tests that the title is set when an owning blog entry is present.
169    */
 
170  2 toggle public void testTitleTakenFromOwningBlogEntryWhenNotSpecified() {
171  2 BlogEntry entry = new BlogEntry(blog);
172  2 entry.setTitle("My blog entry title");
173  2 comment = entry.createComment(null, "", "", "", "", "");
174  2 assertEquals("Re: My blog entry title", comment.getTitle());
175  2 comment = entry.createComment("", "", "", "", "", "");
176  2 assertEquals("Re: My blog entry title", comment.getTitle());
177    }
178   
179    /**
180    * Tests the number of parents is 0 by default.
181    */
 
182  2 toggle public void testNumberOfParentsIsZeroByDefault() {
183  2 assertEquals(0, comment.getNumberOfParents());
184    }
185   
186    /**
187    * Tests that the number of parents is correct when comments are nested.
188    */
 
189  2 toggle public void testNumberOfParentsIsCorrectWhenNested() {
190  2 comment.setParent(new BlogEntry(blog).createComment("", "", "", "", "", ""));
191  2 assertEquals(1, comment.getNumberOfParents());
192    }
193   
194    /**
195    * Tests that adding a null comment doesn't cause an NPE.
196    */
 
197  2 toggle public void testAddingNullCommentDoesntCauseException() {
198  2 comment.addComment(null);
199    }
200   
201    /**
202    * Tests that removing a null comment doesn't cause an NPE.
203    */
 
204  2 toggle public void testRemovingNullCommentDoesntCauseException() {
205  2 comment.removeComment(null);
206    }
207   
208    /**
209    * Tests for the truncated body.
210    */
 
211  2 toggle public void testTruncatedBody() {
212  2 comment.setBody(null);
213  2 assertEquals("", comment.getTruncatedBody());
214   
215  2 comment.setBody("1234567890");
216  2 assertEquals("1234567890", comment.getTruncatedBody());
217   
218  2 comment.setBody("Here is <b>some</b> <i>HTML</i>.");
219  2 assertEquals("Here is some HTML.", comment.getTruncatedBody());
220   
221  2 comment.setBody("Here is &lt;some&gt; text.");
222  2 assertEquals("Here is some text.", comment.getTruncatedBody());
223   
224  2 comment.setBody("1234567890123456789012345678901234567890123456789012345678901234567890");
225  2 assertEquals("12345678901234567890...", comment.getTruncatedBody());
226   
227  2 comment.setBody("1234567890 123456789012345678901234567890123456789012345678901234567890");
228  2 assertEquals("1234567890 12345678901234567890...", comment.getTruncatedBody());
229   
230  2 comment.setBody("<p>" +
231    "You can grab the source for Pebble 1.6 by doing the following:<pre> cvs -d:pserver:anonymous@cvs.sourceforge.net:/cvsroot/pebble login \n" +
232    " cvs -d:pserver:anonymous@cvs.sourceforge.net:/cvsroot/pebble checkout -r v1_6_0 pebble</pre>\n" +
233    "When prompted for a password, just press enter (there is no password).\n" +
234    "</p>");
235  2 assertEquals("You can grab the source for Pebble 1.6 by doing the following: cvs -d:pserver:anonymous...", comment.getTruncatedBody());
236   
237  2 comment.setBody("<p>" +
238    "123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 " +
239    "123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 " +
240    "123456789 123456789 123456789 123456789 12345678W&uuml;nsche");
241  2 assertEquals("123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 ...", comment.getTruncatedBody());
242    }
243   
244    /**
245    * Tests that a comment can be cloned.
246    */
 
247  2 toggle public void testClone() {
248  2 Comment clonedComment = (Comment)comment.clone();
249   
250  2 assertEquals(comment.getTitle(), clonedComment.getTitle());
251  2 assertEquals(comment.getBody(), clonedComment.getBody());
252  2 assertEquals(comment.getWebsite(), clonedComment.getWebsite());
253  2 assertEquals(comment.getAuthor(), clonedComment.getAuthor());
254  2 assertEquals(comment.getIpAddress(), clonedComment.getIpAddress());
255  2 assertEquals(comment.getDate(), clonedComment.getDate());
256  2 assertEquals(comment.getId(), clonedComment.getId());
257  2 assertEquals(comment.getState(), clonedComment.getState());
258  2 assertEquals(comment.getParent(), clonedComment.getParent());
259  2 assertEquals(comment.getBlogEntry(), clonedComment.getBlogEntry());
260    }
261   
262    /**
263    * Test the equals() method.
264    */
 
265  2 toggle public void testEquals() {
266  2 assertTrue(comment.equals(comment));
267   
268  2 Calendar cal = blog.getCalendar();
269  2 cal.set(Calendar.YEAR, cal.get(Calendar.YEAR)-1);
270  2 Comment comment2 = new BlogEntry(blog).createComment("Title", "Body", "Author", "me@somedomain.com", "http://www.google.com", "127.0.0.1", cal.getTime(), State.APPROVED);
271  2 assertFalse(comment.equals(comment2));
272   
273    }
274   
275    /**
276    * Tests the various states for a comment.
277    */
 
278  2 toggle public void testStates() {
279    // the default is approved
280  2 assertEquals(State.APPROVED, comment.getState());
281  2 assertTrue(comment.isApproved());
282  2 assertFalse(comment.isPending());
283  2 assertFalse(comment.isRejected());
284   
285  2 comment.setPending();
286  2 assertEquals(State.PENDING, comment.getState());
287  2 assertFalse(comment.isApproved());
288  2 assertTrue(comment.isPending());
289  2 assertFalse(comment.isRejected());
290   
291  2 comment.setRejected();
292  2 assertEquals(State.REJECTED, comment.getState());
293  2 assertFalse(comment.isApproved());
294  2 assertFalse(comment.isPending());
295  2 assertTrue(comment.isRejected());
296    }
297   
298    /**
299    * Tests that listeners are not fired when a comment is marked as pending.
300    */
 
301  2 toggle public void testListenersFiredWhenCommentMarkedAsPending() {
302   
303  2 CommentListener listener = new CommentListener() {
 
304  0 toggle public void commentAdded(CommentEvent event) {
305  0 fail();
306    }
307   
 
308  0 toggle public void commentRemoved(CommentEvent event) {
309  0 fail();
310    }
311   
 
312  0 toggle public void commentApproved(CommentEvent event) {
313  0 fail();
314    }
315   
 
316  0 toggle public void commentRejected(CommentEvent event) {
317  0 fail();
318    }
319    };
320   
321  2 blog.getEventListenerList().addCommentListener(listener);
322  2 comment.setPending();
323    }
324   
325    /**
326    * Tests that a CommentEvent can be vetoed.
327    */
 
328  0 toggle public void commentEventCanBeVetoed() {
329    // create 2 listeners, veto the event in the first and
330    // fail if the second receives the event
331   
332  0 comment.setPending();
333   
334  0 CommentListener listener1 = new CommentListener() {
 
335  0 toggle public void commentAdded(CommentEvent event) {
336  0 fail();
337    }
338   
 
339  0 toggle public void commentRemoved(CommentEvent event) {
340  0 fail();
341    }
342   
 
343  0 toggle public void commentApproved(CommentEvent event) {
344  0 event.veto();
345    }
346   
 
347  0 toggle public void commentRejected(CommentEvent event) {
348  0 fail();
349    }
350    };
351   
352  0 CommentListener listener2 = new CommentListener() {
 
353  0 toggle public void commentAdded(CommentEvent event) {
354  0 fail();
355    }
356   
 
357  0 toggle public void commentRemoved(CommentEvent event) {
358  0 fail();
359    }
360   
 
361  0 toggle public void commentApproved(CommentEvent event) {
362  0 fail();
363    }
364   
 
365  0 toggle public void commentRejected(CommentEvent event) {
366  0 fail();
367    }
368    };
369   
370  0 blog.getEventListenerList().addCommentListener(listener1);
371  0 blog.getEventListenerList().addCommentListener(listener2);
372   
373  0 comment.setApproved();
374    }
375   
376    /**
377    * Tests that listeners are not fired when a cloned comment is approved.
378    * Why? Because manipulating comments from a blog entry decorator will
379    * generate excess events if not disabled.
380    */
 
381  2 toggle public void testListenersNotFiredWhenCommentApprovedOnClone() {
382  2 comment.setPending();
383  2 comment = (Comment)comment.clone();
384   
385  2 CommentListener listener = new CommentListener() {
 
386  0 toggle public void commentAdded(CommentEvent event) {
387  0 fail();
388    }
389   
 
390  0 toggle public void commentRemoved(CommentEvent event) {
391  0 fail();
392    }
393   
 
394  0 toggle public void commentApproved(CommentEvent event) {
395  0 fail();
396    }
397   
 
398  0 toggle public void commentRejected(CommentEvent event) {
399  0 fail();
400    }
401    };
402   
403  2 blog.getEventListenerList().addCommentListener(listener);
404  2 comment.setApproved();
405    }
406   
 
407  2 toggle public void testNestedCommentsAreUnindexedWhenParentDeleted() throws Exception {
408  2 BlogService service = new BlogService();
409  2 Comment comment2 = blogEntry.createComment("Title", "Body", "Author", "me@somedomain.com", "http://www.google.com", "127.0.0.1");
410  2 Comment comment3 = blogEntry.createComment("Title", "Body", "Author", "me@somedomain.com", "http://www.google.com", "127.0.0.1");
411   
412  2 service.putBlogEntry(blogEntry);
413  2 blogEntry.addComment(comment);
414   
415  2 comment2.setParent(comment);
416  2 blogEntry.addComment(comment2);
417  2 service.putBlogEntry(blogEntry);
418   
419  2 comment3.setParent(comment);
420  2 blogEntry.addComment(comment3);
421  2 service.putBlogEntry(blogEntry);
422   
423  2 assertTrue(blog.getResponseIndex().getPendingResponses().contains(comment.getGuid()));
424  2 assertTrue(blog.getResponseIndex().getPendingResponses().contains(comment2.getGuid()));
425  2 assertTrue(blog.getResponseIndex().getPendingResponses().contains(comment3.getGuid()));
426   
427  2 blogEntry.removeComment(comment.getId());
428  2 service.putBlogEntry(blogEntry);
429   
430  2 assertFalse(blog.getResponseIndex().getPendingResponses().contains(comment.getGuid()));
431  2 assertFalse(blog.getResponseIndex().getPendingResponses().contains(comment2.getGuid()));
432  2 assertFalse(blog.getResponseIndex().getPendingResponses().contains(comment3.getGuid()));
433    }
434   
435    }