KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > xmlrpc > RollerXmlRpcServerTest


1 /*
2  * Created on Jun 15, 2004
3  */

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 JavaDoc;
18 import java.util.Date JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.regex.Pattern JavaDoc;
22
23 import junit.framework.Test;
24 import junit.framework.TestSuite;
25
26 /**
27  * Makes calls to the RollerXmlRpcServer, which should handle a
28  * post just as it would with a real XML-RPC call.
29  *
30  * @author lance.lavandowska
31  */

32 public class RollerXmlRpcServerTest extends RollerTestBase
33 {
34     private static HashMap JavaDoc typeMap = new HashMap JavaDoc();
35     static {
36         typeMap.put(Boolean JavaDoc.class, "boolean");
37         typeMap.put(Double JavaDoc.class, "double");
38         typeMap.put(Date JavaDoc.class, "dateTime.iso8601");
39         typeMap.put(Integer JavaDoc.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 JavaDoc params = new ArrayList JavaDoc();
50         params.add("roller"); // appkey
51
params.add("roller"); // blogid
52
params.add("testuser0"); // userid
53
params.add("password"); // password
54
params.add(new Integer JavaDoc(5)); // numposts
55
String JavaDoc message = buildXmlRpcString("blogger.getRecentPosts", params);
56
57         mockRequest.setBodyContent(message);
58         servletTestModule.doPost();
59         MockHttpServletResponse response = mockFactory.getMockResponse();
60         String JavaDoc responseBody = response.getOutputStreamContent();
61
62         // assert no fault code
63
assertTrue(responseBody,
64                 responseBody.indexOf("<name>faultCode</name>") == -1);
65         
66         // make sure all/any userids returned belong to our test user
67
Pattern JavaDoc userPattern =
68                 Pattern.compile("<name>userid</name><value>(.*?)</value>");
69         ArrayList JavaDoc users = RegexUtil.getMatches(userPattern, responseBody, 1);
70         Iterator JavaDoc it = users.iterator();
71         while (it.hasNext())
72         {
73             String JavaDoc user = (String JavaDoc)it.next();
74             //System.out.println(user);
75
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     /**
88      * Build an XML-RPC message from methodName and params.
89      *
90      * @param methodName
91      * @param params
92      * @return
93      */

94     private String JavaDoc buildXmlRpcString(String JavaDoc methodName, ArrayList JavaDoc params)
95     {
96         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("<?xml version=\"1.0\"?>");
97         buf.append("<methodCall>");
98         buf.append("<methodName>").append(methodName).append("</methodName>");
99         buf.append("<params>");
100         Iterator JavaDoc it = params.iterator();
101         while (it.hasNext()) {
102             buf.append("<param><value>");
103             Object JavaDoc param = it.next();
104             String JavaDoc paramType = (String JavaDoc)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     //-----------------------------------------------------------------------
123
public void setUp() throws Exception JavaDoc
124     {
125         // must do super.setup() before creating MockRollerContext
126
super.setUp();
127         setUpTestWeblogs();
128
129         mockFactory = new WebMockObjectFactory();
130
131         // create mock RollerContext
132
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     //-----------------------------------------------------------------------
147
public void tearDown() throws Exception JavaDoc
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