KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xmlrpc > ClientServerRpcTest


1 /*
2  * Copyright 1999,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not 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.
15  */

16
17
18 package org.apache.xmlrpc;
19
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.net.InetAddress JavaDoc;
23 import java.net.UnknownHostException JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 import junit.framework.Test;
28 import junit.framework.TestSuite;
29
30 /**
31  * Tests XmlRpc run-time.
32  *
33  * @author Daniel L. Rall
34  * @version $Id: ClientServerRpcTest.java,v 1.19 2005/04/22 10:26:17 hgomez Exp $
35  */

36 public class ClientServerRpcTest
37     extends LocalServerRpcTest
38 {
39     /**
40      * The identifier or fully qualified class name of the SAX driver
41      * to use. This is generally <code>uk.co.wilson.xml.MinML</code>,
42      * but could be changed to
43      * <code>org.apache.xerces.parsers.SAXParser</code> for timing
44      * comparisons.
45      */

46     private static final String JavaDoc SAX_DRIVER = "uk.co.wilson.xml.MinML";
47
48     /**
49      * The number of RPCs to make for each test.
50      */

51     private static final int NBR_REQUESTS = 1000;
52
53     /**
54      * The number of calls to batch in the multicall.
55      */

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     /**
65      * Constructor
66      */

67     public ClientServerRpcTest(String JavaDoc testName)
68     {
69         super(testName);
70
71         XmlRpc.setDebug(true);
72         try
73         {
74             XmlRpc.setDriver(SAX_DRIVER);
75         }
76         catch (ClassNotFoundException JavaDoc e)
77         {
78             fail(e.toString());
79         }
80
81         // Server (only)
82
server = new XmlRpcServer();
83         server.addHandler(HANDLER_NAME, new TestHandler());
84
85         // Setup system handler
86
SystemHandler webServerSysHandler = new SystemHandler();
87         webServerSysHandler.addSystemHandler("multicall", new MultiCall());
88
89         // WebServer (contains its own XmlRpcServer instance)
90
setUpWebServer();
91         webServer.addHandler("system", webServerSysHandler);
92     }
93
94     /**
95      * Return the Test
96      */

97     public static Test suite()
98     {
99         return new TestSuite(ClientServerRpcTest.class);
100     }
101
102     /**
103      * Setup the server and clients.
104      */

105     public void setUp()
106     {
107         try
108         {
109             startWebServer();
110         }
111         catch (RuntimeException JavaDoc e)
112         {
113             e.printStackTrace();
114             fail(e.toString());
115         }
116
117         InetAddress JavaDoc localhost = null;
118         try
119         {
120             // localhost will be a random network interface on a
121
// multi-homed host.
122
localhost = InetAddress.getLocalHost();
123         }
124         catch (UnknownHostException JavaDoc e)
125         {
126             fail(e.toString());
127         }
128
129         // XML-RPC client(s)
130
try
131         {
132             String JavaDoc hostName = localhost.getHostName();
133             client = new XmlRpcClient(hostName, SERVER_PORT);
134             //liteClient = new XmlRpcClientLite(hostName, SERVER_PORT);
135
}
136         catch (Exception JavaDoc e)
137         {
138             e.printStackTrace();
139             fail(e.toString());
140         }
141     }
142    
143     /**
144      * Tear down the test.
145      */

146     public void tearDown()
147     {
148         try
149         {
150             stopWebServer();
151         }
152         catch (Exception JavaDoc e)
153         {
154             e.printStackTrace();
155             fail(e.toString());
156         }
157     }
158
159     /**
160      * Tests server's RPC capabilities directly.
161      */

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 JavaDoc in =
170                     new ByteArrayInputStream JavaDoc(RPC_REQUEST.getBytes());
171                 String JavaDoc response = new String JavaDoc(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 JavaDoc e)
182         {
183             e.printStackTrace();
184             fail(e.getMessage());
185         }
186     }
187
188     /**
189      * Tests client/server RPC (via {@link WebServer}).
190      */

191     public void testRpc()
192     {
193         try
194         {
195             // Test the web server (which also tests the rpc server)
196
// by connecting via the clients
197
Vector JavaDoc params = new Vector JavaDoc();
198             params.add(REQUEST_PARAM_VALUE);
199             Object JavaDoc response = client.execute(HANDLER_NAME + ".echo", params);
200             assertEquals(REQUEST_PARAM_VALUE, response);
201             //params.removeAllElements();
202
}
203         catch (Exception JavaDoc e)
204         {
205             e.printStackTrace();
206             fail(e.getMessage());
207         }
208     }
209
210     public void testSystemMultiCall()
211     {
212         try
213         {
214             Vector JavaDoc calls = new Vector JavaDoc();
215
216             for (int i = 0; i < NUM_MULTICALLS; i++)
217             {
218                 Hashtable JavaDoc call = new Hashtable JavaDoc();
219                 Vector JavaDoc params = new Vector JavaDoc();
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 JavaDoc paramWrapper = new Vector JavaDoc();
229             paramWrapper.add(calls);
230             
231             Object JavaDoc response = client.execute("system.multicall", paramWrapper);
232
233             for (int i = 0; i < NUM_MULTICALLS; i++)
234             {
235                Vector JavaDoc result = new Vector JavaDoc();
236                result.add(REQUEST_PARAM_VALUE + i);
237
238                assertEquals(result, ((Vector JavaDoc)response).elementAt(i));
239             }
240         }
241         catch (Exception JavaDoc e)
242         {
243             e.printStackTrace();
244             fail(e.getMessage());
245         }
246     }
247 }
248
Popular Tags