KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > webservices > xmlrpc > RollerXmlRpcServerTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18 /*
19  * Created on Jun 15, 2004
20  */

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 JavaDoc;
30
31 import java.util.ArrayList JavaDoc;
32 import java.util.Date JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.regex.Pattern JavaDoc;
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 /**
53  * Makes calls to the RollerXmlRpcServer, which should handle a
54  * post just as it would with a real XML-RPC call.
55  *
56  * @author lance.lavandowska
57  */

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 JavaDoc typeMap = new HashMap JavaDoc();
63     static {
64         typeMap.put(Boolean JavaDoc.class, "boolean");
65         typeMap.put(Double JavaDoc.class, "double");
66         typeMap.put(Date JavaDoc.class, "dateTime.iso8601");
67         typeMap.put(Integer JavaDoc.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 JavaDoc params = new ArrayList JavaDoc();
77         params.add("roller"); // appkey
78
params.add(testWeblog.getHandle()); // blogid
79
params.add(testUser.getUserName()); // userid
80
params.add(testUser.getPassword()); // password
81
params.add(new Integer JavaDoc(5)); // numposts
82
String JavaDoc message = buildXmlRpcString("blogger.getRecentPosts", params);
83         
84         mockRequest.setBodyContent(message);
85         servletTestModule.doPost();
86         MockHttpServletResponse response = mockFactory.getMockResponse();
87         String JavaDoc responseBody = response.getOutputStreamContent();
88         
89         // assert no fault code
90
assertTrue(responseBody,
91             responseBody.indexOf("<name>faultCode</name>") == -1);
92         
93         // make sure all/any userids returned belong to our test user
94
Pattern JavaDoc userPattern =
95                 Pattern.compile("<name>userid</name><value>(.*?)</value>");
96         ArrayList JavaDoc users = RegexUtil.getMatches(userPattern, responseBody, 1);
97         Iterator JavaDoc it = users.iterator();
98         while (it.hasNext()) {
99             String JavaDoc user = (String JavaDoc)it.next();
100             //System.out.println(user);
101
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     /**
111      * Build an XML-RPC message from methodName and params.
112      *
113      * @param methodName
114      * @param params
115      * @return
116      */

117     private String JavaDoc buildXmlRpcString(String JavaDoc methodName, ArrayList JavaDoc params) {
118         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("<?xml version=\"1.0\"?>");
119         buf.append("<methodCall>");
120         buf.append("<methodName>").append(methodName).append("</methodName>");
121         buf.append("<params>");
122         Iterator JavaDoc it = params.iterator();
123         while (it.hasNext()) {
124             buf.append("<param><value>");
125             Object JavaDoc param = it.next();
126             String JavaDoc paramType = (String JavaDoc)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     //-----------------------------------------------------------------------
142

143     /**
144      * All tests in this suite require a user and a weblog.
145      */

146     public void setUp() throws Exception JavaDoc {
147         
148         try {
149             mockFactory = new WebMockObjectFactory();
150             
151             // create mock RollerContext
152
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 JavaDoc(new Date JavaDoc().getTime()));
174             testEntry1.setUpdateTime(new Timestamp JavaDoc(new Date JavaDoc().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 JavaDoc(new Date JavaDoc().getTime()));
186             testEntry2.setUpdateTime(new Timestamp JavaDoc(new Date JavaDoc().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 JavaDoc(new Date JavaDoc().getTime()));
198             testEntry3.setUpdateTime(new Timestamp JavaDoc(new Date JavaDoc().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 JavaDoc ex) {
207             log.error(ex);
208             throw new Exception JavaDoc("Test setup failed", ex);
209         }
210     }
211     
212     public void tearDown() throws Exception JavaDoc {
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 JavaDoc ex) {
227             log.error(ex);
228             throw new Exception JavaDoc("Test teardown failed", ex);
229         }
230     }
231     
232     public static Test suite() {
233         return new TestSuite(RollerXmlRpcServerTest.class);
234     }
235 }
Popular Tags