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