📄 singleblogbloggerapihandlertest.java
字号:
/* * Copyright (c) 2003-2006, Simon Brown * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * - Neither the name of Pebble nor the names of its contributors may * be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */package net.sourceforge.pebble.webservice;import net.sourceforge.pebble.Constants;import net.sourceforge.pebble.domain.*;import org.acegisecurity.GrantedAuthority;import org.acegisecurity.GrantedAuthorityImpl;import org.acegisecurity.MockAuthenticationManager;import org.apache.xmlrpc.XmlRpcException;import java.util.Calendar;import java.util.Hashtable;import java.util.Vector;/** * Tests for the BloggerAPIHandler class, when using a simple blog. * * @author Simon Brown */public class SingleBlogBloggerAPIHandlerTest extends SingleBlogTestCase { private BloggerAPIHandler handler = new BloggerAPIHandler(); protected void setUp() throws Exception { super.setUp(); handler.setAuthenticationManager(new net.sourceforge.pebble.mock.MockAuthenticationManager(true, new GrantedAuthority[] {new GrantedAuthorityImpl(Constants.BLOG_CONTRIBUTOR_ROLE)})); blog.setProperty(Blog.BLOG_CONTRIBUTORS_KEY, "username"); } public void testAuthenticationFailure() { handler.setAuthenticationManager(new MockAuthenticationManager(false)); try { handler.getUserInfo("", "username", "password"); fail(); } catch (XmlRpcAuthenticationException xmlrpcae) { } catch (XmlRpcException xmlrpce) { fail(); } try { handler.deletePost("", "default/123", "username", "password", true); fail(); } catch (XmlRpcAuthenticationException xmlrpcae) { } catch (XmlRpcException xmlrpce) { fail(); } try { handler.editPost("", "default/123", "username", "password", "content", true); fail(); } catch (XmlRpcAuthenticationException xmlrpcae) { } catch (XmlRpcException xmlrpce) { fail(); } try { handler.getPost("", "default/123", "username", "password"); fail(); } catch (XmlRpcAuthenticationException xmlrpcae) { } catch (XmlRpcException xmlrpce) { fail(); } try { handler.getRecentPosts("", "default", "username", "password", 10); fail(); } catch (XmlRpcAuthenticationException xmlrpcae) { } catch (XmlRpcException xmlrpce) { fail(); } try { handler.newPost("", "default", "username", "password", "content", true); fail(); } catch (XmlRpcAuthenticationException xmlrpcae) { } catch (XmlRpcException xmlrpce) { fail(); } } public void testAuthenticationSuccess() { try { handler.deletePost("", "123", "username", "password", true); } catch (XmlRpcAuthenticationException xmlrpcae) { fail(); } catch (XmlRpcException xmlrpce) { } try { handler.editPost("", "123", "username", "password", "content", true); } catch (XmlRpcAuthenticationException xmlrpcae) { fail(); } catch (XmlRpcException xmlrpce) { } try { handler.getPost("", "123", "username", "password"); } catch (XmlRpcAuthenticationException xmlrpcae) { fail(); } catch (XmlRpcException xmlrpce) { } try { handler.getRecentPosts("", "", "username", "password", 10); } catch (XmlRpcAuthenticationException xmlrpcae) { fail(); } catch (XmlRpcException xmlrpce) { } try { handler.getUsersBlogs("", "username", "password"); } catch (XmlRpcAuthenticationException xmlrpcae) { fail(); } catch (XmlRpcException xmlrpce) { } try { handler.newPost("", "", "username", "password", "<title>title</title>content", true); } catch (XmlRpcAuthenticationException xmlrpcae) { fail(); } catch (XmlRpcException xmlrpce) { } } public void testGetRecentPostsFromEmptyBlog() { try { Vector posts = handler.getRecentPosts("appkey", "default", "username", "password", 3); assertTrue(posts.isEmpty()); } catch (Exception e) { fail(); } } public void testGetRecentPosts() { try { BlogService service = new BlogService(); BlogEntry entry1 = new BlogEntry(blog); entry1.setTitle("title1"); entry1.setBody("body1"); service.putBlogEntry(entry1); BlogEntry entry2 = new BlogEntry(blog); entry2.setTitle("title2"); entry2.setBody("body2"); service.putBlogEntry(entry2); BlogEntry entry3 = new BlogEntry(blog); entry3.setTitle("title3"); entry3.setBody("body3"); service.putBlogEntry(entry3); BlogEntry entry4 = new BlogEntry(blog); entry4.setTitle("title4"); entry4.setBody("body4"); service.putBlogEntry(entry4); Vector posts = handler.getRecentPosts("appkey", "default", "username", "password", 3); assertFalse(posts.isEmpty()); assertEquals(3, posts.size()); Hashtable ht = (Hashtable)posts.get(0); assertEquals("default/" + entry4.getId(), ht.get(BloggerAPIHandler.POST_ID)); assertEquals("<title>title4</title><category></category>body4", ht.get(BloggerAPIHandler.CONTENT)); ht = (Hashtable)posts.get(1); assertEquals("default/" + entry3.getId(), ht.get(BloggerAPIHandler.POST_ID)); assertEquals("<title>title3</title><category></category>body3", ht.get(BloggerAPIHandler.CONTENT)); ht = (Hashtable)posts.get(2); assertEquals("default/" + entry2.getId(), ht.get(BloggerAPIHandler.POST_ID)); assertEquals("<title>title2</title><category></category>body2", ht.get(BloggerAPIHandler.CONTENT)); } catch (Exception e) { e.printStackTrace(); fail(); } } public void testGetPost() { try { BlogService service = new BlogService(); BlogEntry entry = new BlogEntry(blog); entry.setTitle("title"); entry.setBody("body"); entry.setAuthor("simon"); service.putBlogEntry(entry); Hashtable post = handler.getPost("appkey", "default/" + entry.getId(), "username", "password"); assertEquals("<title>title</title><category></category>body", post.get(BloggerAPIHandler.CONTENT)); assertEquals(entry.getAuthor(), post.get(BloggerAPIHandler.USER_ID)); assertEquals(entry.getDate(), post.get(BloggerAPIHandler.DATE_CREATED)); assertEquals("default/" + entry.getId(), post.get(BloggerAPIHandler.POST_ID)); } catch (Exception e) { e.printStackTrace(); fail(); } } public void testGetPostWithCategory() { try { BlogService service = new BlogService(); BlogEntry entry = new BlogEntry(blog); entry.setTitle("title"); entry.setBody("body"); entry.setAuthor("simon"); entry.addCategory(new Category("java", "Java")); service.putBlogEntry(entry); Hashtable post = handler.getPost("appkey", "default/" + entry.getId(), "username", "password"); assertEquals("<title>title</title><category>/java</category>body", post.get(BloggerAPIHandler.CONTENT)); assertEquals(entry.getAuthor(), post.get(BloggerAPIHandler.USER_ID)); assertEquals(entry.getDate(), post.get(BloggerAPIHandler.DATE_CREATED)); assertEquals("default/" + entry.getId(), post.get(BloggerAPIHandler.POST_ID)); } catch (Exception e) { e.printStackTrace(); fail(); } } public void testGetPostWithIdThatDoesntExist() { String postid = "1234567890123"; try { handler.getPost("appkey", "default/" + postid, "username", "password"); fail(); } catch (XmlRpcException xmlrpce) { assertEquals("Blog entry with ID of " + postid + " was not found.", xmlrpce.getMessage());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -