Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
184   359   44   13,14
0   274   0,24   14
14     3,14  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  MultiBlogMetaWeblogAPIHandlerTest       Line # 50 184 0% 44 30 84,8% 0.8484849
 
  (26)
 
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 MetaWeblogAPIHandler class, when using a composite blog.
47    *
48    * @author Simon Brown
49    */
 
50    public class MultiBlogMetaWeblogAPIHandlerTest extends MultiBlogTestCase {
51   
52    private MetaWeblogAPIHandler handler = new MetaWeblogAPIHandler();
53   
 
54  26 toggle protected void setUp() throws Exception {
55  26 super.setUp();
56   
57  26 handler.setAuthenticationManager(new net.sourceforge.pebble.mock.MockAuthenticationManager(true, new GrantedAuthority[] {new GrantedAuthorityImpl(Constants.BLOG_CONTRIBUTOR_ROLE)}));
58  26 blog1.setProperty(Blog.BLOG_CONTRIBUTORS_KEY, "username");
59    }
60   
61    /**
62    * Tests that authentication fails properly.
63    */
 
64  2 toggle public void testAuthenticationFailure() {
65  2 handler.setAuthenticationManager(new MockAuthenticationManager(false));
66  2 try {
67  2 handler.getCategories("blog1/123", "username", "password");
68  0 fail();
69    } catch (XmlRpcAuthenticationException xmlrpcae) {
70    } catch (XmlRpcException xmlrpce) {
71    }
72  2 try {
73  2 handler.editPost("blog1/123", "username", "password", new Hashtable(), true);
74  0 fail();
75    } catch (XmlRpcAuthenticationException xmlrpcae) {
76    } catch (XmlRpcException xmlrpce) {
77  0 fail();
78    }
79  2 try {
80  2 handler.getPost("blog1/123", "username", "password");
81  0 fail();
82    } catch (XmlRpcAuthenticationException xmlrpcae) {
83    } catch (XmlRpcException xmlrpce) {
84  0 fail();
85    }
86  2 try {
87  2 handler.getRecentPosts("blog1", "username", "password", 10);
88  0 fail();
89    } catch (XmlRpcAuthenticationException xmlrpcae) {
90    } catch (XmlRpcException xmlrpce) {
91  0 fail();
92    }
93  2 try {
94  2 handler.newPost("blog1", "username", "password", new Hashtable(), true);
95  0 fail();
96    } catch (XmlRpcAuthenticationException xmlrpcae) {
97    } catch (XmlRpcException xmlrpce) {
98  0 fail();
99    }
100    }
101   
102    /**
103    * Tests that authentication works properly.
104    */
 
105  2 toggle public void testAuthenticationSuccess() {
106  2 try {
107  2 handler.getCategories("blog1/123", "username", "password");
108    } catch (XmlRpcAuthenticationException xmlrpcae) {
109  0 fail();
110    } catch (XmlRpcException xmlrpce) {
111    }
112  2 try {
113  2 handler.editPost("blog1/123", "username", "password", new Hashtable(), true);
114    } catch (XmlRpcAuthenticationException xmlrpcae) {
115  0 fail();
116    } catch (XmlRpcException xmlrpce) {
117    }
118  2 try {
119  2 handler.getPost("blog1/123", "username", "password");
120    } catch (XmlRpcAuthenticationException xmlrpcae) {
121  0 fail();
122    } catch (XmlRpcException xmlrpce) {
123    }
124  2 try {
125  2 handler.getRecentPosts("blog1", "username", "password", 10);
126    } catch (XmlRpcAuthenticationException xmlrpcae) {
127  0 fail();
128    } catch (XmlRpcException xmlrpce) {
129    }
130  2 try {
131  2 handler.newPost("blog1", "username", "password", new Hashtable(), true);
132    } catch (XmlRpcAuthenticationException xmlrpcae) {
133  0 fail();
134    } catch (XmlRpcException xmlrpce) {
135    }
136    }
137   
 
138  2 toggle public void testGetRecentPostsFromEmptyBlog() {
139  2 try {
140  2 Vector posts = handler.getRecentPosts("blog1", "username", "password", 3);
141  2 assertTrue(posts.isEmpty());
142    } catch (Exception e) {
143  0 e.printStackTrace();
144  0 fail();
145    }
146    }
147   
 
148  2 toggle public void testGetRecentPosts() {
149  2 try {
150  2 BlogService service = new BlogService();
151   
152  2 BlogEntry entry1 = new BlogEntry(blog1);
153  2 entry1.setTitle("title1");
154  2 entry1.setBody("body1");
155  2 service.putBlogEntry(entry1);
156   
157  2 BlogEntry entry2 = new BlogEntry(blog1);
158  2 entry2.setTitle("title2");
159  2 entry2.setBody("body2");
160  2 service.putBlogEntry(entry2);
161   
162  2 BlogEntry entry3 = new BlogEntry(blog1);
163  2 entry3.setTitle("title3");
164  2 entry3.setBody("body3");
165  2 service.putBlogEntry(entry3);
166   
167  2 BlogEntry entry4 = new BlogEntry(blog1);
168  2 entry4.setTitle("title4");
169  2 entry4.setBody("body4");
170  2 service.putBlogEntry(entry4);
171   
172  2 Vector posts = handler.getRecentPosts("blog1", "username", "password", 3);
173   
174  2 assertFalse(posts.isEmpty());
175  2 assertEquals(3, posts.size());
176  2 Hashtable ht = (Hashtable)posts.get(0);
177  2 assertEquals("blog1/" + entry4.getId(), ht.get(MetaWeblogAPIHandler.POST_ID));
178  2 assertEquals("body4", ht.get(MetaWeblogAPIHandler.DESCRIPTION));
179  2 assertEquals("title4", ht.get(MetaWeblogAPIHandler.TITLE));
180  2 ht = (Hashtable)posts.get(1);
181  2 assertEquals("blog1/" + entry3.getId(), ht.get(MetaWeblogAPIHandler.POST_ID));
182  2 assertEquals("body3", ht.get(MetaWeblogAPIHandler.DESCRIPTION));
183  2 assertEquals("title3", ht.get(MetaWeblogAPIHandler.TITLE));
184  2 ht = (Hashtable)posts.get(2);
185  2 assertEquals("blog1/" + entry2.getId(), ht.get(MetaWeblogAPIHandler.POST_ID));
186  2 assertEquals("body2", ht.get(MetaWeblogAPIHandler.DESCRIPTION));
187  2 assertEquals("title2", ht.get(MetaWeblogAPIHandler.TITLE));
188    } catch (Exception e) {
189  0 e.printStackTrace();
190  0 fail();
191    }
192    }
193   
 
194  2 toggle public void testGetPost() {
195  2 try {
196  2 Category category = new Category("/aCategory", "A Category");
197  2 blog1.addCategory(category);
198   
199  2 BlogService service = new BlogService();
200  2 BlogEntry entry = new BlogEntry(blog1);
201  2 entry.setTitle("title");
202  2 entry.setBody("body");
203  2 entry.setAuthor("simon");
204  2 entry.addCategory(category);
205  2 service.putBlogEntry(entry);
206   
207  2 Hashtable post = handler.getPost("blog1/" + entry.getId(), "username", "password");
208  2 assertEquals("title", post.get(MetaWeblogAPIHandler.TITLE));
209  2 assertEquals("body", post.get(MetaWeblogAPIHandler.DESCRIPTION));
210  2 Vector categories = (Vector)post.get(MetaWeblogAPIHandler.CATEGORIES);
211  2 assertEquals(1, categories.size());
212  2 assertEquals("/aCategory", categories.get(0));
213  2 assertEquals(entry.getAuthor(), post.get(MetaWeblogAPIHandler.USER_ID));
214  2 assertEquals(entry.getDate(), post.get(MetaWeblogAPIHandler.DATE_CREATED));
215  2 assertEquals("blog1/" + entry.getId(), post.get(MetaWeblogAPIHandler.POST_ID));
216    } catch (Exception e) {
217  0 e.printStackTrace();
218  0 fail();
219    }
220    }
221   
 
222  2 toggle public void testGetPostWithIdThatDoesntExist() {
223  2 String postid = "1234567890123";
224  2 try {
225  2 handler.getPost("blog1/" + postid, "username", "password");
226  0 fail();
227    } catch (XmlRpcException xmlrpce) {
228  2 assertEquals("Blog entry with ID of " + postid + " was not found.", xmlrpce.getMessage());
229    }
230    }
231   
 
232  2 toggle public void testGetPostWithNullId() {
233  2 try {
234  2 handler.getPost("blog1/", "username", "password");
235  0 fail();
236    } catch (XmlRpcException xmlrpce) {
237  2 assertEquals("Blog entry with ID of was not found.", xmlrpce.getMessage());
238    }
239    }
240   
 
241  2 toggle public void testNewPost() {
242  2 try {
243  2 Category category = new Category("/aCategory", "A Category");
244  2 blog1.addCategory(category);
245  2 Hashtable struct = new Hashtable();
246  2 struct.put(MetaWeblogAPIHandler.TITLE, "Title");
247  2 struct.put(MetaWeblogAPIHandler.DESCRIPTION, "<p>Content</p>");
248  2 Vector categories = new Vector();
249  2 categories.add(category.getId());
250  2 struct.put(MetaWeblogAPIHandler.CATEGORIES, categories);
251   
252  2 String postid = handler.newPost("blog1", "username", "password", struct, true);
253   
254  2 BlogService service = new BlogService();
255  2 BlogEntry entry = service.getBlogEntry(blog1, postid.substring("blog1".length()+1));
256   
257  2 assertEquals("blog1/" + entry.getId(), postid);
258  2 assertEquals("Title", entry.getTitle());
259  2 assertTrue(entry.inCategory(category));
260  2 assertEquals("<p>Content</p>", entry.getBody());
261  2 assertEquals("username", entry.getAuthor());
262    } catch (Exception e) {
263  0 e.printStackTrace();
264  0 fail();
265    }
266    }
267   
268    /**
269    * Tests that non-existent categories are just ignored and no error
270    * is produced.
271    */
 
272  2 toggle public void testNewPostWithCategoryThatDoesntExist() {
273  2 try {
274  2 Hashtable struct = new Hashtable();
275  2 struct.put(MetaWeblogAPIHandler.TITLE, "Title");
276  2 struct.put(MetaWeblogAPIHandler.DESCRIPTION, "<p>Content</p>");
277  2 Vector categories = new Vector();
278  2 categories.add("someUnknownCategory");
279  2 struct.put(MetaWeblogAPIHandler.CATEGORIES, categories);
280   
281  2 String postid = handler.newPost("blog1", "username", "password", struct, true);
282   
283  2 BlogService service = new BlogService();
284  2 BlogEntry entry = service.getBlogEntry(blog1, postid.substring("blog1".length()+1));
285   
286  2 assertEquals("blog1/" + entry.getId(), postid);
287  2 assertEquals("Title", entry.getTitle());
288  2 assertEquals(0, entry.getCategories().size());
289  2 assertEquals("<p>Content</p>", entry.getBody());
290  2 assertEquals("username", entry.getAuthor());
291    } catch (Exception e) {
292  0 e.printStackTrace();
293  0 fail();
294    }
295    }
296   
 
297  2 toggle public void testEditPost() {
298  2 try {
299  2 BlogService service = new BlogService();
300  2 BlogEntry entry = new BlogEntry(blog1);
301  2 entry.setTitle("title");
302  2 entry.setBody("body");
303  2 service.putBlogEntry(entry);
304   
305  2 Hashtable struct = new Hashtable();
306  2 struct.put(MetaWeblogAPIHandler.TITLE, "Title");
307  2 struct.put(MetaWeblogAPIHandler.DESCRIPTION, "<p>Content</p>");
308  2 boolean result = handler.editPost("blog1/" + entry.getId(), "username", "password", struct, true);
309   
310  2 assertTrue(result);
311  2 entry = service.getBlogEntry(blog1, entry.getId());
312  2 assertEquals("Title", entry.getTitle());
313  2 assertEquals("<p>Content</p>", entry.getBody());
314  2 assertEquals("username", entry.getAuthor());
315   
316    } catch (Exception e) {
317  0 e.printStackTrace();
318  0 fail();
319    }
320    }
321   
 
322  2 toggle public void testEditPostWithNullId() {
323  2 try {
324  2 handler.editPost("blog1/", "username", "password", new Hashtable(), true);
325  0 fail();
326    } catch (XmlRpcException xmlrpce) {
327  2 assertEquals("Blog entry with ID of was not found.", xmlrpce.getMessage());
328    }
329    }
330   
 
331  2 toggle public void testEditPostWithIdThatDoesntExist() {
332  2 String postid = "1234567890123";
333  2 try {
334  2 handler.editPost("blog1/" + postid, "username", "password", new Hashtable(), true);
335  0 fail();
336    } catch (XmlRpcException xmlrpce) {
337  2 assertEquals("Blog entry with ID of " + postid + " was not found.", xmlrpce.getMessage());
338    }
339    }
340   
 
341  2 toggle public void testGetCategories() throws Exception {
342  2 Hashtable categories = handler.getCategories("blog1", "username", "password");
343  2 assertEquals(0, categories.size());
344   
345  2 blog1.addCategory(new Category("/category1", "Category 1"));
346  2 blog1.addCategory(new Category("/category2", "Category 2"));
347  2 categories = handler.getCategories("blog1", "username", "password");
348  2 assertTrue(categories.size() == 2);
349  2 Hashtable struct = (Hashtable)categories.get("/category1");
350  2 assertEquals("/category1", struct.get(MetaWeblogAPIHandler.DESCRIPTION));
351  2 assertEquals(blog1.getUrl() + "categories/category1/", struct.get(MetaWeblogAPIHandler.HTML_URL));
352  2 assertEquals(blog1.getUrl() + "rss.xml?category=/category1", struct.get(MetaWeblogAPIHandler.RSS_URL));
353  2 struct = (Hashtable)categories.get("/category2");
354  2 assertEquals("/category2", struct.get(MetaWeblogAPIHandler.DESCRIPTION));
355  2 assertEquals(blog1.getUrl() + "categories/category2/", struct.get(MetaWeblogAPIHandler.HTML_URL));
356  2 assertEquals(blog1.getUrl() + "rss.xml?category=/category2", struct.get(MetaWeblogAPIHandler.RSS_URL));
357    }
358   
359    }