1 18 21 package org.apache.roller.webservices.xmlrpc; 22 23 import com.mockrunner.mock.web.MockHttpServletRequest; 24 import com.mockrunner.mock.web.MockHttpServletResponse; 25 import com.mockrunner.mock.web.MockServletContext; 26 import com.mockrunner.mock.web.WebMockObjectFactory; 27 import com.mockrunner.servlet.ServletTestModule; 28 29 import java.sql.Timestamp ; 30 31 import java.util.ArrayList ; 32 import java.util.Date ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.regex.Pattern ; 36 37 import junit.framework.Test; 38 import junit.framework.TestCase; 39 import junit.framework.TestSuite; 40 41 import org.apache.commons.logging.Log; 42 import org.apache.commons.logging.LogFactory; 43 import org.apache.roller.TestUtils; 44 import org.apache.roller.model.RollerFactory; 45 import org.apache.roller.pojos.UserData; 46 import org.apache.roller.pojos.WeblogEntryData; 47 import org.apache.roller.pojos.WebsiteData; 48 import org.apache.roller.ui.MockRollerContext; 49 import org.apache.roller.ui.core.RollerRequest; 50 import org.apache.roller.util.RegexUtil; 51 52 58 public class RollerXmlRpcServerTest extends TestCase { 59 public static Log log = LogFactory.getLog(RollerXmlRpcServerTest.class); 60 UserData testUser = null; 61 WebsiteData testWeblog = null; 62 private static HashMap typeMap = new HashMap (); 63 static { 64 typeMap.put(Boolean .class, "boolean"); 65 typeMap.put(Double .class, "double"); 66 typeMap.put(Date .class, "dateTime.iso8601"); 67 typeMap.put(Integer .class, "int"); 68 } 69 70 protected WebMockObjectFactory mockFactory; 71 protected MockRollerContext rollerContext; 72 protected MockHttpServletRequest mockRequest; 73 protected ServletTestModule servletTestModule; 74 75 public void testBloggerGetRecentPosts() { 76 ArrayList params = new ArrayList (); 77 params.add("roller"); params.add(testWeblog.getHandle()); params.add(testUser.getUserName()); params.add(testUser.getPassword()); params.add(new Integer (5)); String message = buildXmlRpcString("blogger.getRecentPosts", params); 83 84 mockRequest.setBodyContent(message); 85 servletTestModule.doPost(); 86 MockHttpServletResponse response = mockFactory.getMockResponse(); 87 String responseBody = response.getOutputStreamContent(); 88 89 assertTrue(responseBody, 91 responseBody.indexOf("<name>faultCode</name>") == -1); 92 93 Pattern userPattern = 95 Pattern.compile("<name>userid</name><value>(.*?)</value>"); 96 ArrayList users = RegexUtil.getMatches(userPattern, responseBody, 1); 97 Iterator it = users.iterator(); 98 while (it.hasNext()) { 99 String user = (String )it.next(); 100 if (user.equals(testUser.getUserName())) { 102 continue; 103 } else { 104 fail("getRecentPosts() returned entry for a user [" 105 + user + "] other than " + testUser.getUserName()); 106 } 107 } 108 } 109 110 117 private String buildXmlRpcString(String methodName, ArrayList params) { 118 StringBuffer buf = new StringBuffer ("<?xml version=\"1.0\"?>"); 119 buf.append("<methodCall>"); 120 buf.append("<methodName>").append(methodName).append("</methodName>"); 121 buf.append("<params>"); 122 Iterator it = params.iterator(); 123 while (it.hasNext()) { 124 buf.append("<param><value>"); 125 Object param = it.next(); 126 String paramType = (String )typeMap.get(param.getClass()); 127 if (paramType != null) { 128 buf.append("<").append(paramType).append(">") 129 .append(param) 130 .append("</").append(paramType).append(">"); 131 } else { 132 buf.append("<string>").append(param).append("</string>"); 133 } 134 buf.append("</value></param>"); 135 } 136 buf.append("</params> "); 137 buf.append("</methodCall>"); 138 return buf.toString(); 139 } 140 141 143 146 public void setUp() throws Exception { 147 148 try { 149 mockFactory = new WebMockObjectFactory(); 150 151 MockServletContext ctx = mockFactory.getMockServletContext(); 153 ctx.setRealPath("/", "."); 154 rollerContext = new MockRollerContext(); 155 rollerContext.init(ctx); 156 157 mockRequest = mockFactory.getMockRequest(); 158 mockRequest.setContextPath("/roller"); 159 RollerRequest.getRollerRequest( 160 mockRequest, mockFactory.getMockServletContext()); 161 162 servletTestModule = new ServletTestModule(mockFactory); 163 servletTestModule.createServlet(RollerXMLRPCServlet.class); 164 165 testUser = TestUtils.setupUser("entryTestUser"); 166 testWeblog = TestUtils.setupWeblog("entryTestWeblog", testUser); 167 168 WeblogEntryData testEntry1 = new WeblogEntryData(); 169 testEntry1.setTitle("entryTestEntry1"); 170 testEntry1.setLink("testEntryLink1"); 171 testEntry1.setText("blah blah entry1"); 172 testEntry1.setAnchor("testEntryAnchor1"); 173 testEntry1.setPubTime(new Timestamp (new Date ().getTime())); 174 testEntry1.setUpdateTime(new Timestamp (new Date ().getTime())); 175 testEntry1.setWebsite(testWeblog); 176 testEntry1.setCreator(testUser); 177 testEntry1.setCategory(testWeblog.getDefaultCategory()); 178 RollerFactory.getRoller().getWeblogManager().saveWeblogEntry(testEntry1); 179 180 WeblogEntryData testEntry2 = new WeblogEntryData(); 181 testEntry2.setTitle("entryTestEntry2"); 182 testEntry2.setLink("testEntryLink2"); 183 testEntry2.setText("blah blah entry2"); 184 testEntry2.setAnchor("testEntryAnchor2"); 185 testEntry2.setPubTime(new Timestamp (new Date ().getTime())); 186 testEntry2.setUpdateTime(new Timestamp (new Date ().getTime())); 187 testEntry2.setWebsite(testWeblog); 188 testEntry2.setCreator(testUser); 189 testEntry2.setCategory(testWeblog.getDefaultCategory()); 190 RollerFactory.getRoller().getWeblogManager().saveWeblogEntry(testEntry1); 191 192 WeblogEntryData testEntry3 = new WeblogEntryData(); 193 testEntry3.setTitle("entryTestEntry3"); 194 testEntry3.setLink("testEntryLink3"); 195 testEntry3.setText("blah blah entry3"); 196 testEntry3.setAnchor("testEntryAnchor3"); 197 testEntry3.setPubTime(new Timestamp (new Date ().getTime())); 198 testEntry3.setUpdateTime(new Timestamp (new Date ().getTime())); 199 testEntry3.setWebsite(testWeblog); 200 testEntry3.setCreator(testUser); 201 testEntry3.setCategory(testWeblog.getDefaultCategory()); 202 RollerFactory.getRoller().getWeblogManager().saveWeblogEntry(testEntry1); 203 204 TestUtils.endSession(true); 205 206 } catch (Exception ex) { 207 log.error(ex); 208 throw new Exception ("Test setup failed", ex); 209 } 210 } 211 212 public void tearDown() throws Exception { 213 214 try { 215 TestUtils.teardownWeblog(testWeblog.getId()); 216 TestUtils.teardownUser(testUser.getId()); 217 TestUtils.endSession(true); 218 219 mockRequest = null; 220 servletTestModule.clearOutput(); 221 servletTestModule.releaseFilters(); 222 servletTestModule = null; 223 rollerContext = null; 224 mockFactory = null; 225 226 } catch (Exception ex) { 227 log.error(ex); 228 throw new Exception ("Test teardown failed", ex); 229 } 230 } 231 232 public static Test suite() { 233 return new TestSuite(RollerXmlRpcServerTest.class); 234 } 235 } | Popular Tags |