Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
251   488   71   10,04
0   394   0,28   25
25     2,84  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  SingleBlogBloggerAPIHandlerTest       Line # 50 251 0% 71 51 81,5% 0.8152174
 
  (48)
 
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.webservice;
33   
34    import net.sourceforge.pebble.Constants;
35    import net.sourceforge.pebble.domain.*;
36    import org.acegisecurity.GrantedAuthority;
37    import org.acegisecurity.GrantedAuthorityImpl;
38    import org.acegisecurity.MockAuthenticationManager;
39    import org.apache.xmlrpc.XmlRpcException;
40   
41    import java.util.Calendar;
42    import java.util.Hashtable;
43    import java.util.Vector;
44   
45    /**
46    * Tests for the BloggerAPIHandler class, when using a simple blog.
47    *
48    * @author Simon Brown
49    */
 
50    public class SingleBlogBloggerAPIHandlerTest extends SingleBlogTestCase {
51   
52    private BloggerAPIHandler handler = new BloggerAPIHandler();
53   
 
54  48 toggle protected void setUp() throws Exception {
55  48 super.setUp();
56   
57  48 handler.setAuthenticationManager(new net.sourceforge.pebble.mock.MockAuthenticationManager(true, new GrantedAuthority[] {new GrantedAuthorityImpl(Constants.BLOG_CONTRIBUTOR_ROLE)}));
58  48 blog.setProperty(Blog.BLOG_CONTRIBUTORS_KEY, "username");
59    }
60   
 
61  2 toggle public void testAuthenticationFailure() {
62  2 handler.setAuthenticationManager(new MockAuthenticationManager(false));
63  2 try {
64  2 handler.getUserInfo("", "username", "password");
65  0 fail();
66    } catch (XmlRpcAuthenticationException xmlrpcae) {
67    } catch (XmlRpcException xmlrpce) {
68  0 fail();
69    }
70  2 try {
71  2 handler.deletePost("", "default/123", "username", "password", true);
72  0 fail();
73    } catch (XmlRpcAuthenticationException xmlrpcae) {
74    } catch (XmlRpcException xmlrpce) {
75  0 fail();
76    }
77  2 try {
78  2 handler.editPost("", "default/123", "username", "password", "content", true);
79  0 fail();
80    } catch (XmlRpcAuthenticationException xmlrpcae) {
81    } catch (XmlRpcException xmlrpce) {
82  0 fail();
83    }
84  2 try {
85  2 handler.getPost("", "default/123", "username", "password");
86  0 fail();
87    } catch (XmlRpcAuthenticationException xmlrpcae) {
88    } catch (XmlRpcException xmlrpce) {
89  0 fail();
90    }
91  2 try {
92  2 handler.getRecentPosts("", "default", "username", "password", 10);
93  0 fail();
94    } catch (XmlRpcAuthenticationException xmlrpcae) {
95    } catch (XmlRpcException xmlrpce) {
96  0 fail();
97    }
98  2 try {
99  2 handler.newPost("", "default", "username", "password", "content", true);
100  0 fail();
101    } catch (XmlRpcAuthenticationException xmlrpcae) {
102    } catch (XmlRpcException xmlrpce) {
103  0 fail();
104    }
105    }
106   
 
107  2 toggle public void testAuthenticationSuccess() {
108  2 try {
109  2 handler.deletePost("", "123", "username", "password", true);
110    } catch (XmlRpcAuthenticationException xmlrpcae) {
111  0 fail();
112    } catch (XmlRpcException xmlrpce) {
113    }
114  2 try {
115  2 handler.editPost("", "123", "username", "password", "content", true);
116    } catch (XmlRpcAuthenticationException xmlrpcae) {
117  0 fail();
118    } catch (XmlRpcException xmlrpce) {
119    }
120  2 try {
121  2 handler.getPost("", "123", "username", "password");
122    } catch (XmlRpcAuthenticationException xmlrpcae) {
123  0 fail();
124    } catch (XmlRpcException xmlrpce) {
125    }
126  2 try {
127  2 handler.getRecentPosts("", "", "username", "password", 10);
128    } catch (XmlRpcAuthenticationException xmlrpcae) {
129  0 fail();
130    } catch (XmlRpcException xmlrpce) {
131    }
132  2 try {
133  2 handler.getUsersBlogs("", "username", "password");
134    } catch (XmlRpcAuthenticationException xmlrpcae) {
135  0 fail();
136    } catch (XmlRpcException xmlrpce) {
137    }
138  2 try {
139  2 handler.newPost("", "", "username", "password", "<title>title</title>content", true);
140    } catch (XmlRpcAuthenticationException xmlrpcae) {
141  0 fail();
142    } catch (XmlRpcException xmlrpce) {
143    }
144    }
145   
 
146  2 toggle public void testGetRecentPostsFromEmptyBlog() {
147  2 try {
148  2 Vector posts = handler.getRecentPosts("appkey", "default", "username", "password", 3);
149  2 assertTrue(posts.isEmpty());
150    } catch (Exception e) {
151  0 fail();
152    }
153    }
154   
 
155  2 toggle public void testGetRecentPosts() {
156  2 try {
157  2 BlogService service = new BlogService();
158   
159  2 BlogEntry entry1 = new BlogEntry(blog);
160  2 entry1.setTitle("title1");
161  2 entry1.setBody("body1");
162  2 service.putBlogEntry(entry1);
163   
164  2 BlogEntry entry2 = new BlogEntry(blog);
165  2 entry2.setTitle("title2");
166  2 entry2.setBody("body2");
167  2 service.putBlogEntry(entry2);
168   
169  2 BlogEntry entry3 = new BlogEntry(blog);
170  2 entry3.setTitle("title3");
171  2 entry3.setBody("body3");
172  2 service.putBlogEntry(entry3);
173   
174  2 BlogEntry entry4 = new BlogEntry(blog);
175  2 entry4.setTitle("title4");
176  2 entry4.setBody("body4");
177  2 service.putBlogEntry(entry4);
178   
179  2 Vector posts = handler.getRecentPosts("appkey", "default", "username", "password", 3);
180   
181  2 assertFalse(posts.isEmpty());
182  2 assertEquals(3, posts.size());
183  2 Hashtable ht = (Hashtable)posts.get(0);
184  2 assertEquals("default/" + entry4.getId(), ht.get(BloggerAPIHandler.POST_ID));
185  2 assertEquals("<title>title4</title><category></category>body4", ht.get(BloggerAPIHandler.CONTENT));
186  2 ht = (Hashtable)posts.get(1);
187  2 assertEquals("default/" + entry3.getId(), ht.get(BloggerAPIHandler.POST_ID));
188  2 assertEquals("<title>title3</title><category></category>body3", ht.get(BloggerAPIHandler.CONTENT));
189  2 ht = (Hashtable)posts.get(2);
190  2 assertEquals("default/" + entry2.getId(), ht.get(BloggerAPIHandler.POST_ID));
191  2 assertEquals("<title>title2</title><category></category>body2", ht.get(BloggerAPIHandler.CONTENT));
192    } catch (Exception e) {
193  0 e.printStackTrace();
194  0 fail();
195    }
196    }
197   
 
198  2 toggle public void testGetPost() {
199  2 try {
200  2 BlogService service = new BlogService();
201  2 BlogEntry entry = new BlogEntry(blog);
202  2 entry.setTitle("title");
203  2 entry.setBody("body");
204  2 entry.setAuthor("simon");
205  2 service.putBlogEntry(entry);
206   
207  2 Hashtable post = handler.getPost("appkey", "default/" + entry.getId(), "username", "password");
208  2 assertEquals("<title>title</title><category></category>body", post.get(BloggerAPIHandler.CONTENT));
209  2 assertEquals(entry.getAuthor(), post.get(BloggerAPIHandler.USER_ID));
210  2 assertEquals(entry.getDate(), post.get(BloggerAPIHandler.DATE_CREATED));
211  2 assertEquals("default/" + entry.getId(), post.get(BloggerAPIHandler.POST_ID));
212    } catch (Exception e) {
213  0 e.printStackTrace();
214  0 fail();
215    }
216    }
217   
 
218  2 toggle public void testGetPostWithCategory() {
219  2 try {
220  2 BlogService service = new BlogService();
221  2 BlogEntry entry = new BlogEntry(blog);
222  2 entry.setTitle("title");
223  2 entry.setBody("body");
224  2 entry.setAuthor("simon");
225  2 entry.addCategory(new Category("java", "Java"));
226  2 service.putBlogEntry(entry);
227   
228  2 Hashtable post = handler.getPost("appkey", "default/" + entry.getId(), "username", "password");
229  2 assertEquals("<title>title</title><category>/java</category>body", post.get(BloggerAPIHandler.CONTENT));
230  2 assertEquals(entry.getAuthor(), post.get(BloggerAPIHandler.USER_ID));
231  2 assertEquals(entry.getDate(), post.get(BloggerAPIHandler.DATE_CREATED));
232  2 assertEquals("default/" + entry.getId(), post.get(BloggerAPIHandler.POST_ID));
233    } catch (Exception e) {
234  0 e.printStackTrace();
235  0 fail();
236    }
237    }
238   
 
239  2 toggle public void testGetPostWithIdThatDoesntExist() {
240  2 String postid = "1234567890123";
241  2 try {
242  2 handler.getPost("appkey", "default/" + postid, "username", "password");
243  0 fail();
244    } catch (XmlRpcException xmlrpce) {
245  2 assertEquals("Blog entry with ID of " + postid + " was not found.", xmlrpce.getMessage());
246    }
247    }
248   
 
249  2 toggle public void testGetPostWithNullId() {
250  2 String postid = null;
251  2 try {
252  2 handler.getPost("appkey", postid, "username", "password");
253  0 fail();
254    } catch (XmlRpcException xmlrpce) {
255  2 assertEquals("Blog with ID of " + null + " not found.", xmlrpce.getMessage());
256    }
257    }
258   
 
259  2 toggle public void testNewPostWithTitleAndCategory() {
260  2 try {
261  2 Category category = new Category("/aCategory", "A Category");
262  2 blog.addCategory(category);
263   
264  2 String postid = handler.newPost("appkey", "default", "username", "password", "<title>Title</title><category>/aCategory</category><p>Content</p>", true);
265   
266  2 BlogService service = new BlogService();
267  2 BlogEntry entry = service.getBlogEntry(blog, postid.substring("default".length()+1));
268   
269  2 assertEquals("default/" + entry.getId(), postid);
270  2 assertEquals("Title", entry.getTitle());
271  2 assertTrue(entry.inCategory(category));
272  2 assertEquals("<p>Content</p>", entry.getBody());
273  2 assertEquals("username", entry.getAuthor());
274   
275    } catch (Exception e) {
276  0 e.printStackTrace();
277  0 fail();
278    }
279    }
280   
 
281  2 toggle public void testNewPostWithTitleAndCategories() {
282  2 try {
283  2 Category category1 = new Category("/category1", "Category 1");
284  2 blog.addCategory(category1);
285  2 Category category2 = new Category("/category2", "Category 2");
286  2 blog.addCategory(category2);
287   
288  2 String postid = handler.newPost("appkey", "default", "username", "password", "<title>Title</title><category>/category1, /category2</category><p>Content</p>", true);
289   
290  2 BlogService service = new BlogService();
291  2 BlogEntry entry = service.getBlogEntry(blog, postid.substring("default".length()+1));
292   
293  2 assertEquals("default/" + entry.getId(), postid);
294  2 assertEquals("Title", entry.getTitle());
295  2 assertTrue(entry.inCategory(category1));
296  2 assertTrue(entry.inCategory(category2));
297  2 assertEquals("<p>Content</p>", entry.getBody());
298  2 assertEquals("username", entry.getAuthor());
299   
300    } catch (Exception e) {
301  0 e.printStackTrace();
302  0 fail();
303    }
304    }
305   
 
306  2 toggle public void testNewPostWithoutTitle() {
307  2 try {
308  2 String postid = handler.newPost("appkey", "default", "username", "password", "<p>Content</p>", true);
309   
310  2 BlogService service = new BlogService();
311  2 BlogEntry entry = service.getBlogEntry(blog, postid.substring("default".length()+1));
312  2 assertEquals("default/" + entry.getId(), postid);
313  2 assertEquals("", entry.getTitle());
314  2 assertEquals("<p>Content</p>", entry.getBody());
315  2 assertEquals("username", entry.getAuthor());
316   
317    } catch (Exception e) {
318  0 e.printStackTrace();
319  0 fail();
320    }
321    }
322   
 
323  2 toggle public void testEditPost() {
324  2 try {
325  2 BlogService service = new BlogService();
326  2 BlogEntry entry = new BlogEntry(blog);
327  2 entry.setTitle("title");
328  2 entry.setBody("body");
329  2 service.putBlogEntry(entry);
330   
331  2 boolean result = handler.editPost("appkey", "default/" + entry.getId(), "username", "password", "<title>Title</title><p>Content</p>", true);
332   
333  2 entry = service.getBlogEntry(blog, entry.getId());
334  2 assertTrue(result);
335  2 assertEquals("Title", entry.getTitle());
336  2 assertEquals("<p>Content</p>", entry.getBody());
337  2 assertEquals("username", entry.getAuthor());
338   
339    } catch (Exception e) {
340  0 e.printStackTrace();
341  0 fail();
342    }
343    }
344   
 
345  2 toggle public void testEditPostWithNullId() {
346  2 String postid = null;
347  2 try {
348  2 handler.editPost("appkey", postid, "username", "password", "<title>Title</title><p>Content</p>", true);
349  0 fail();
350    } catch (XmlRpcException xmlrpce) {
351  2 assertEquals("Blog with ID of " + postid + " not found.", xmlrpce.getMessage());
352    }
353    }
354   
 
355  2 toggle public void testEditPostWithIdThatDoesntExist() {
356  2 String postid = "1234567890123";
357  2 try {
358  2 handler.editPost("appkey", "default/" + postid, "username", "password", "<title>Title</title><p>Content</p>", true);
359  0 fail();
360    } catch (XmlRpcException xmlrpce) {
361  2 assertEquals("Blog entry with ID of " + postid + " was not found.", xmlrpce.getMessage());
362    }
363    }
364   
 
365  2 toggle public void testDeletePost() {
366  2 try {
367  2 BlogService service = new BlogService();
368  2 BlogEntry entry = new BlogEntry(blog);
369  2 entry.setTitle("title");
370  2 entry.setBody("body");
371  2 entry.setAuthor("simon");
372  2 service.putBlogEntry(entry);
373   
374  2 boolean result = handler.deletePost("appkey", "default/" + entry.getId(), "username", "password", true);
375  2 assertTrue("deletePost() returned false instead of true", result);
376  2 assertNull(service.getBlogEntry(blog, entry.getId()));
377    } catch (Exception e) {
378  0 e.printStackTrace();
379  0 fail();
380    }
381    }
382   
 
383  2 toggle public void testDeletePostWithIdThatDoesntExist() {
384  2 String postid = "1234567890123";
385  2 try {
386  2 handler.deletePost("appkey", "default/" + postid, "username", "password", true);
387  0 fail();
388    } catch (XmlRpcException xmlrpce) {
389  2 assertEquals("Blog entry with ID of " + postid + " was not found.", xmlrpce.getMessage());
390    }
391    }
392   
 
393  2 toggle public void testDeletePostWithNullId() {
394  2 String postid = null;
395  2 try {
396  2 handler.deletePost("appkey", postid, "username", "password", true);
397  0 fail();
398    } catch (XmlRpcException xmlrpce) {
399  2 assertEquals("Blog with ID of " + null + " not found.", xmlrpce.getMessage());
400    }
401    }
402   
 
403  2 toggle public void testGetUserInfo() {
404  2 try {
405  2 Hashtable userInfo = handler.getUserInfo("appkey", "username", "password");
406  2 assertEquals("username", userInfo.get("userid"));
407    } catch (Exception e) {
408  0 e.printStackTrace();
409  0 fail();
410    }
411    }
412   
 
413  2 toggle public void testGetUsersBlogs() {
414  2 try {
415  2 Vector blogs = handler.getUsersBlogs("appkey", "username", "password");
416  2 assertEquals(1, blogs.size());
417   
418  2 Hashtable blog = (Hashtable)blogs.get(0);
419  2 assertEquals("http://www.yourdomain.com/blog/", blog.get("url"));
420  2 assertEquals("default", blog.get("blogid"));
421  2 assertEquals("My blog", blog.get("blogName"));
422    } catch (Exception e) {
423  0 e.printStackTrace();
424  0 fail();
425    }
426    }
427   
 
428  2 toggle public void testGetTemplate() {
429  2 try {
430  2 handler.getTemplate("appkey", "blogid", "username", "password", "templateType");
431  0 fail();
432    } catch (XmlRpcException xmlrpce) {
433  2 assertEquals("getTemplate is not supported by Pebble.", xmlrpce.getMessage());
434    }
435    }
436   
 
437  2 toggle public void testSetTemplate() {
438  2 try {
439  2 handler.setTemplate("appkey", "blogid", "username", "password", "template", "templateType");
440  0 fail();
441    } catch (XmlRpcException xmlrpce) {
442  2 assertEquals("setTemplate is not supported by Pebble.", xmlrpce.getMessage());
443    }
444    }
445   
 
446  2 toggle public void testAddCategory() {
447  2 try {
448  2 BlogService service = new BlogService();
449  2 BlogEntry entry = new BlogEntry(blog);
450  2 service.putBlogEntry(entry);
451  2 blog.addCategory(new Category("/aCategory", "A Category"));
452   
453  2 boolean result = handler.addCategory("appkey", "default/" + entry.getId(), "username", "password", "/aCategory");
454   
455  2 entry = service.getBlogEntry(blog, entry.getId());
456  2 assertTrue("Category wasn't added", result);
457  2 assertTrue(entry.inCategory(blog.getCategory("aCategory")));
458   
459  2 result = handler.addCategory("appkey", "default/" + entry.getId(), "username", "password", "/aNonExistentCategory");
460  2 assertFalse("Category was added", result);
461   
462    } catch (Exception e) {
463  0 e.printStackTrace();
464  0 fail();
465    }
466    }
467   
 
468  2 toggle public void testAddCategoryWithIdThatDoesntExist() {
469  2 String postid = "1234567890123";
470  2 try {
471  2 handler.addCategory("appkey", "default/" + postid, "username", "password", "aCategory");
472  0 fail();
473    } catch (XmlRpcException xmlrpce) {
474  2 assertEquals("Blog entry with ID of " + postid + " was not found.", xmlrpce.getMessage());
475    }
476    }
477   
 
478  2 toggle public void testAddCategoryWithNullId() {
479  2 String postid = null;
480  2 try {
481  2 handler.addCategory("appkey", postid, "username", "password", "aCategory");
482  0 fail();
483    } catch (XmlRpcException xmlrpce) {
484  2 assertEquals("Blog with ID of " + null + " not found.", xmlrpce.getMessage());
485    }
486    }
487   
488    }