1 16 17 18 package org.apache.xmlrpc; 19 20 import java.io.ByteArrayInputStream ; 21 import java.io.InputStream ; 22 import java.net.InetAddress ; 23 import java.net.UnknownHostException ; 24 import java.util.Hashtable ; 25 import java.util.Vector ; 26 27 import junit.framework.Test; 28 import junit.framework.TestSuite; 29 30 36 public class ClientServerRpcTest 37 extends LocalServerRpcTest 38 { 39 46 private static final String SAX_DRIVER = "uk.co.wilson.xml.MinML"; 47 48 51 private static final int NBR_REQUESTS = 1000; 52 53 56 private static final int NUM_MULTICALLS = 10; 57 58 private XmlRpcServer server; 59 60 private XmlRpcClient client; 61 62 private XmlRpcClientLite liteClient; 63 64 67 public ClientServerRpcTest(String testName) 68 { 69 super(testName); 70 71 XmlRpc.setDebug(true); 72 try 73 { 74 XmlRpc.setDriver(SAX_DRIVER); 75 } 76 catch (ClassNotFoundException e) 77 { 78 fail(e.toString()); 79 } 80 81 server = new XmlRpcServer(); 83 server.addHandler(HANDLER_NAME, new TestHandler()); 84 85 SystemHandler webServerSysHandler = new SystemHandler(); 87 webServerSysHandler.addSystemHandler("multicall", new MultiCall()); 88 89 setUpWebServer(); 91 webServer.addHandler("system", webServerSysHandler); 92 } 93 94 97 public static Test suite() 98 { 99 return new TestSuite(ClientServerRpcTest.class); 100 } 101 102 105 public void setUp() 106 { 107 try 108 { 109 startWebServer(); 110 } 111 catch (RuntimeException e) 112 { 113 e.printStackTrace(); 114 fail(e.toString()); 115 } 116 117 InetAddress localhost = null; 118 try 119 { 120 localhost = InetAddress.getLocalHost(); 123 } 124 catch (UnknownHostException e) 125 { 126 fail(e.toString()); 127 } 128 129 try 131 { 132 String hostName = localhost.getHostName(); 133 client = new XmlRpcClient(hostName, SERVER_PORT); 134 } 136 catch (Exception e) 137 { 138 e.printStackTrace(); 139 fail(e.toString()); 140 } 141 } 142 143 146 public void tearDown() 147 { 148 try 149 { 150 stopWebServer(); 151 } 152 catch (Exception e) 153 { 154 e.printStackTrace(); 155 fail(e.toString()); 156 } 157 } 158 159 162 public void testServer() 163 { 164 try 165 { 166 long time = System.currentTimeMillis(); 167 for (int i = 0; i < NBR_REQUESTS; i++) 168 { 169 InputStream in = 170 new ByteArrayInputStream (RPC_REQUEST.getBytes()); 171 String response = new String (server.execute(in)); 172 assertTrue("Response did not contain " + REQUEST_PARAM_XML, 173 response.indexOf(REQUEST_PARAM_XML) != -1); 174 } 175 time = System.currentTimeMillis() - time; 176 System.out.println("Total time elapsed for " + NBR_REQUESTS + 177 " iterations was " + time + " milliseconds, " + 178 "averaging " + (time / NBR_REQUESTS) + 179 " milliseconds per request"); 180 } 181 catch (Exception e) 182 { 183 e.printStackTrace(); 184 fail(e.getMessage()); 185 } 186 } 187 188 191 public void testRpc() 192 { 193 try 194 { 195 Vector params = new Vector (); 198 params.add(REQUEST_PARAM_VALUE); 199 Object response = client.execute(HANDLER_NAME + ".echo", params); 200 assertEquals(REQUEST_PARAM_VALUE, response); 201 } 203 catch (Exception e) 204 { 205 e.printStackTrace(); 206 fail(e.getMessage()); 207 } 208 } 209 210 public void testSystemMultiCall() 211 { 212 try 213 { 214 Vector calls = new Vector (); 215 216 for (int i = 0; i < NUM_MULTICALLS; i++) 217 { 218 Hashtable call = new Hashtable (); 219 Vector params = new Vector (); 220 221 params.add(REQUEST_PARAM_VALUE + i); 222 call.put("methodName", HANDLER_NAME + ".echo"); 223 call.put("params", params); 224 225 calls.addElement(call); 226 } 227 228 Vector paramWrapper = new Vector (); 229 paramWrapper.add(calls); 230 231 Object response = client.execute("system.multicall", paramWrapper); 232 233 for (int i = 0; i < NUM_MULTICALLS; i++) 234 { 235 Vector result = new Vector (); 236 result.add(REQUEST_PARAM_VALUE + i); 237 238 assertEquals(result, ((Vector )response).elementAt(i)); 239 } 240 } 241 catch (Exception e) 242 { 243 e.printStackTrace(); 244 fail(e.getMessage()); 245 } 246 } 247 } 248 | Popular Tags |