1 4 package org.roller.presentation.xmlrpc; 5 6 import com.mockrunner.mock.web.MockHttpServletRequest; 7 import com.mockrunner.mock.web.MockHttpServletResponse; 8 import com.mockrunner.mock.web.MockServletContext; 9 import com.mockrunner.mock.web.WebMockObjectFactory; 10 import com.mockrunner.servlet.ServletTestModule; 11 12 import org.roller.RollerTestBase; 13 import org.roller.presentation.MockRollerContext; 14 import org.roller.presentation.RollerRequest; 15 import org.roller.util.RegexUtil; 16 17 import java.util.ArrayList ; 18 import java.util.Date ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.regex.Pattern ; 22 23 import junit.framework.Test; 24 import junit.framework.TestSuite; 25 26 32 public class RollerXmlRpcServerTest extends RollerTestBase 33 { 34 private static HashMap typeMap = new HashMap (); 35 static { 36 typeMap.put(Boolean .class, "boolean"); 37 typeMap.put(Double .class, "double"); 38 typeMap.put(Date .class, "dateTime.iso8601"); 39 typeMap.put(Integer .class, "int"); 40 } 41 42 protected WebMockObjectFactory mockFactory; 43 protected MockRollerContext rollerContext; 44 protected MockHttpServletRequest mockRequest; 45 protected ServletTestModule servletTestModule; 46 47 public void testBloggerGetRecentPosts() 48 { 49 ArrayList params = new ArrayList (); 50 params.add("roller"); params.add("roller"); params.add("testuser0"); params.add("password"); params.add(new Integer (5)); String message = buildXmlRpcString("blogger.getRecentPosts", params); 56 57 mockRequest.setBodyContent(message); 58 servletTestModule.doPost(); 59 MockHttpServletResponse response = mockFactory.getMockResponse(); 60 String responseBody = response.getOutputStreamContent(); 61 62 assertTrue(responseBody, 64 responseBody.indexOf("<name>faultCode</name>") == -1); 65 66 Pattern userPattern = 68 Pattern.compile("<name>userid</name><value>(.*?)</value>"); 69 ArrayList users = RegexUtil.getMatches(userPattern, responseBody, 1); 70 Iterator it = users.iterator(); 71 while (it.hasNext()) 72 { 73 String user = (String )it.next(); 74 if (user.equals("testuser0")) 76 { 77 continue; 78 } 79 else 80 { 81 fail("getRecentPosts() returned entry for a user [" 82 + user + "] other than " + testUsername); 83 } 84 } 85 } 86 87 94 private String buildXmlRpcString(String methodName, ArrayList params) 95 { 96 StringBuffer buf = new StringBuffer ("<?xml version=\"1.0\"?>"); 97 buf.append("<methodCall>"); 98 buf.append("<methodName>").append(methodName).append("</methodName>"); 99 buf.append("<params>"); 100 Iterator it = params.iterator(); 101 while (it.hasNext()) { 102 buf.append("<param><value>"); 103 Object param = it.next(); 104 String paramType = (String )typeMap.get(param.getClass()); 105 if (paramType != null) 106 { 107 buf.append("<").append(paramType).append(">") 108 .append(param) 109 .append("</").append(paramType).append(">"); 110 } 111 else 112 { 113 buf.append("<string>").append(param).append("</string>"); 114 } 115 buf.append("</value></param>"); 116 } 117 buf.append("</params> "); 118 buf.append("</methodCall>"); 119 return buf.toString(); 120 } 121 122 public void setUp() throws Exception 124 { 125 super.setUp(); 127 setUpTestWeblogs(); 128 129 mockFactory = new WebMockObjectFactory(); 130 131 MockServletContext ctx = mockFactory.getMockServletContext(); 133 ctx.setRealPath("/", "."); 134 rollerContext = new MockRollerContext(); 135 rollerContext.init(ctx); 136 137 mockRequest = mockFactory.getMockRequest(); 138 mockRequest.setContextPath("/roller"); 139 RollerRequest.getRollerRequest( 140 mockRequest, mockFactory.getMockServletContext()); 141 142 servletTestModule = new ServletTestModule(mockFactory); 143 servletTestModule.createServlet(RollerXMLRPCServlet.class); 144 } 145 146 public void tearDown() throws Exception 148 { 149 super.tearDown(); 150 mockRequest = null; 151 servletTestModule.clearOutput(); 152 servletTestModule.releaseFilters(); 153 servletTestModule = null; 154 rollerContext = null; 155 mockFactory = null; 156 } 157 158 public static Test suite() 159 { 160 return new TestSuite(RollerXmlRpcServerTest.class); 161 } 162 } | Popular Tags |