KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > systest > basic > ClientServerTest


1 package org.objectweb.celtix.systest.basic;
2
3 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
4 import java.net.URL JavaDoc;
5 import java.util.concurrent.ExecutionException JavaDoc;
6 import java.util.concurrent.ExecutorService JavaDoc;
7 import java.util.concurrent.Executors JavaDoc;
8 import java.util.concurrent.Future JavaDoc;
9
10 import javax.xml.namespace.QName JavaDoc;
11 //import javax.xml.ws.AsyncHandler;
12
import javax.xml.ws.AsyncHandler;
13 import javax.xml.ws.Response;
14
15 import junit.framework.Test;
16 import junit.framework.TestSuite;
17
18 import org.objectweb.celtix.systest.common.ClientServerSetupBase;
19 import org.objectweb.celtix.systest.common.ClientServerTestBase;
20 import org.objectweb.hello_world_soap_http.BadRecordLitFault;
21 import org.objectweb.hello_world_soap_http.Greeter;
22 import org.objectweb.hello_world_soap_http.NoSuchCodeLitFault;
23 import org.objectweb.hello_world_soap_http.SOAPService;
24 import org.objectweb.hello_world_soap_http.types.BareDocumentResponse;
25 import org.objectweb.hello_world_soap_http.types.GreetMeSometimeResponse;
26
27 public class ClientServerTest extends ClientServerTestBase {
28
29     private final QName JavaDoc serviceName = new QName JavaDoc("http://objectweb.org/hello_world_soap_http",
30                                                 "SOAPService");
31     private final QName JavaDoc portName = new QName JavaDoc("http://objectweb.org/hello_world_soap_http",
32                                              "SoapPort");
33
34     public static Test suite() throws Exception JavaDoc {
35         TestSuite suite = new TestSuite(ClientServerTest.class);
36         return new ClientServerSetupBase(suite) {
37             public void startServers() throws Exception JavaDoc {
38                 assertTrue("server did not launch correctly", launchServer(Server.class));
39             }
40         };
41     }
42     public void testBasicConnection() throws Exception JavaDoc {
43         URL JavaDoc wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
44         assertNotNull(wsdl);
45         
46         SOAPService service = new SOAPService(wsdl, serviceName);
47         assertNotNull(service);
48         
49         Greeter greeter = service.getPort(portName, Greeter.class);
50         
51         String JavaDoc response1 = new String JavaDoc("Hello Milestone-");
52         String JavaDoc response2 = new String JavaDoc("Bonjour");
53         try {
54             for (int idx = 0; idx < 5; idx++) {
55                 String JavaDoc greeting = greeter.greetMe("Milestone-" + idx);
56                 assertNotNull("no response received from service", greeting);
57                 String JavaDoc exResponse = response1 + idx;
58                 assertEquals(exResponse, greeting);
59                 
60                 String JavaDoc reply = greeter.sayHi();
61                 assertNotNull("no response received from service", reply);
62                 assertEquals(response2, reply);
63
64                 greeter.greetMeOneWay("Milestone-" + idx);
65                 
66                 BareDocumentResponse bareres = greeter.testDocLitBare("MySimpleDocument");
67                 assertNotNull("no response for operation testDocLitBare", bareres);
68                 assertEquals("Celtix", bareres.getCompany());
69                 assertTrue(bareres.getId() == 1);
70                 
71             }
72         } catch (UndeclaredThrowableException JavaDoc ex) {
73             throw (Exception JavaDoc)ex.getCause();
74         }
75     }
76
77     public void testAsyncPollingCall() throws Exception JavaDoc {
78         URL JavaDoc wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
79         assertNotNull(wsdl);
80         
81         SOAPService service = new SOAPService(wsdl, serviceName);
82         assertNotNull(service);
83         ExecutorService JavaDoc executor = Executors.newFixedThreadPool(5);
84         service.setExecutor(executor);
85         assertNotNull(service);
86
87         String JavaDoc expectedString = new String JavaDoc("How are you Joe");
88         try {
89             Greeter greeter = (Greeter)service.getPort(portName, Greeter.class);
90             
91             Response<GreetMeSometimeResponse> response = greeter.greetMeSometimeAsync("Joe");
92             while (!response.isDone()) {
93                 Thread.sleep(100);
94             }
95             GreetMeSometimeResponse reply = response.get();
96             assertNotNull("no response received from service", reply);
97             String JavaDoc s = reply.getResponseType();
98             assertEquals(expectedString, s);
99         } catch (UndeclaredThrowableException JavaDoc ex) {
100             throw (Exception JavaDoc)ex.getCause();
101         }
102         executor.shutdown();
103     }
104     
105     public void testAsyncSynchronousPolling() throws Exception JavaDoc {
106         URL JavaDoc wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
107         assertNotNull(wsdl);
108         
109         SOAPService service = new SOAPService(wsdl, serviceName);
110         assertNotNull(service);
111         ExecutorService JavaDoc executor = Executors.newFixedThreadPool(5);
112         service.setExecutor(executor);
113         assertNotNull(service);
114
115         final String JavaDoc expectedString = new String JavaDoc("How are you Joe");
116           
117         class Poller extends Thread JavaDoc {
118             Response<GreetMeSometimeResponse> response;
119             int tid;
120             
121             Poller(Response<GreetMeSometimeResponse> r, int t) {
122                 response = r;
123                 tid = t;
124             }
125             public void run() {
126                 if (tid % 2 > 0) {
127                     while (!response.isDone()) {
128                         try {
129                             Thread.sleep(100);
130                         } catch (InterruptedException JavaDoc ex) {
131                             // ignore
132
}
133                     }
134                 }
135                 GreetMeSometimeResponse reply = null;
136                 try {
137                     reply = response.get();
138                 } catch (Exception JavaDoc ex) {
139                     fail("Poller " + tid + " failed with " + ex);
140                 }
141                 assertNotNull("Poller " + tid + ": no response received from service", reply);
142                 String JavaDoc s = reply.getResponseType();
143                 assertEquals(expectedString, s);
144             }
145         }
146         
147         Greeter greeter = (Greeter)service.getPort(portName, Greeter.class);
148         Response<GreetMeSometimeResponse> response = greeter.greetMeSometimeAsync("Joe");
149         
150         Poller[] pollers = new Poller[4];
151         for (int i = 0; i < pollers.length; i++) {
152             pollers[i] = new Poller(response, i);
153         }
154         for (Poller p : pollers) {
155             p.start();
156         }
157         
158         for (Poller p : pollers) {
159             p.join();
160         }
161         
162         executor.shutdown();
163     }
164     static class MyHandler implements AsyncHandler<GreetMeSometimeResponse> {
165         static int invocationCount;
166         private String JavaDoc replyBuffer;
167         
168         public void handleResponse(Response<GreetMeSometimeResponse> response) {
169             invocationCount++;
170             try {
171                 GreetMeSometimeResponse reply = response.get();
172                 replyBuffer = reply.getResponseType();
173             } catch (InterruptedException JavaDoc ex) {
174                 ex.printStackTrace();
175             } catch (ExecutionException JavaDoc ex) {
176                 ex.printStackTrace();
177             }
178         }
179         
180         String JavaDoc getReplyBuffer() {
181             return replyBuffer;
182         }
183     }
184     
185     public void testAsyncCallWithHandler() throws Exception JavaDoc {
186         URL JavaDoc wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
187         assertNotNull(wsdl);
188         
189         SOAPService service = new SOAPService(wsdl, serviceName);
190         ExecutorService JavaDoc executor = Executors.newFixedThreadPool(5);
191         service.setExecutor(executor);
192         assertNotNull(service);
193         
194         MyHandler h = new MyHandler();
195         MyHandler.invocationCount = 0;
196
197         String JavaDoc expectedString = new String JavaDoc("How are you Joe");
198         try {
199             Greeter greeter = (Greeter)service.getPort(portName, Greeter.class);
200             Future JavaDoc<?> f = greeter.greetMeSometimeAsync("Joe", h);
201             int i = 0;
202             while (!f.isDone() && i < 20) {
203                 Thread.sleep(100);
204                 i++;
205             }
206             assertEquals("callback was not executed or did not return the expected result",
207                          expectedString, h.getReplyBuffer());
208         } catch (UndeclaredThrowableException JavaDoc ex) {
209             throw (Exception JavaDoc)ex.getCause();
210         }
211         assertEquals(1, MyHandler.invocationCount);
212         executor.shutdown();
213     }
214     public void testAsyncCallWithHandlerAndMultipleClients() throws Exception JavaDoc {
215         URL JavaDoc wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
216         assertNotNull(wsdl);
217         
218         SOAPService service = new SOAPService(wsdl, serviceName);
219         ExecutorService JavaDoc executor = Executors.newFixedThreadPool(5);
220         service.setExecutor(executor);
221         assertNotNull(service);
222         
223         final MyHandler h = new MyHandler();
224         MyHandler.invocationCount = 0;
225
226         final String JavaDoc expectedString = new String JavaDoc("How are you Joe");
227         
228         class Poller extends Thread JavaDoc {
229             Future JavaDoc<?> future;
230             int tid;
231             
232             Poller(Future JavaDoc<?> f, int t) {
233                 future = f;
234                 tid = t;
235             }
236             public void run() {
237                 if (tid % 2 > 0) {
238                     while (!future.isDone()) {
239                         try {
240                             Thread.sleep(100);
241                         } catch (InterruptedException JavaDoc ex) {
242                             // ignore
243
}
244                     }
245                 }
246                 try {
247                     future.get();
248                 } catch (Exception JavaDoc ex) {
249                     fail("Poller " + tid + " failed with " + ex);
250                 }
251                 assertEquals("callback was not executed or did not return the expected result",
252                              expectedString, h.getReplyBuffer());
253             }
254         }
255         
256         Greeter greeter = (Greeter)service.getPort(portName, Greeter.class);
257         Future JavaDoc<?> f = greeter.greetMeSometimeAsync("Joe", h);
258         
259         Poller[] pollers = new Poller[4];
260         for (int i = 0; i < pollers.length; i++) {
261             pollers[i] = new Poller(f, i);
262         }
263         for (Poller p : pollers) {
264             p.start();
265         }
266         
267         for (Poller p : pollers) {
268             p.join();
269         }
270         assertEquals(1, MyHandler.invocationCount);
271         executor.shutdown();
272     }
273     
274     
275  
276     public void testFaults() throws Exception JavaDoc {
277         URL JavaDoc wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
278         assertNotNull(wsdl);
279         
280         SOAPService service = new SOAPService(wsdl, serviceName);
281         ExecutorService JavaDoc ex = Executors.newFixedThreadPool(1);
282         service.setExecutor(ex);
283         assertNotNull(service);
284
285         String JavaDoc noSuchCodeFault = "NoSuchCodeLitFault";
286         String JavaDoc badRecordFault = "BadRecordLitFault";
287
288         Greeter greeter = service.getPort(portName, Greeter.class);
289         for (int idx = 0; idx < 2; idx++) {
290             try {
291                 greeter.testDocLitFault(noSuchCodeFault);
292                 fail("Should have thrown NoSuchCodeLitFault exception");
293             } catch (NoSuchCodeLitFault nslf) {
294                 assertNotNull(nslf.getFaultInfo());
295                 assertNotNull(nslf.getFaultInfo().getCode());
296             }
297             
298             try {
299                 greeter.testDocLitFault(badRecordFault);
300                 fail("Should have thrown BadRecordLitFault exception");
301             } catch (BadRecordLitFault brlf) {
302                 assertNotNull(brlf.getFaultInfo());
303             }
304         }
305
306     }
307
308     public static void main(String JavaDoc[] args) {
309         junit.textui.TestRunner.run(ClientServerTest.class);
310     }
311     
312
313
314     /*
315     
316     public static void main(String[] args) {
317         ClientServerTest cst = new ClientServerTest();
318         
319         if ("client".equals(args[0])) {
320             try {
321                 cst.testAsyncPollingCall();
322             } catch (Exception ex) {
323                 ex.printStackTrace();
324             }
325             System.err.println("Exiting...........");
326             System.exit(0);
327         } else if ("server".equals(args[0])) {
328             try {
329                // cst.setUp();
330                 cst.onetimeSetUp();
331             } catch (Exception ex) {
332                 ex.printStackTrace();
333             }
334         } else {
335             System.out.println("Invaid arg");
336         }
337
338     }
339     
340     */

341     
342     
343     
344     
345 }
346
Popular Tags