KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > jaxrpc > TestAxisClient


1 package test.jaxrpc;
2
3 import javax.xml.namespace.QName JavaDoc;
4 import javax.xml.rpc.JAXRPCException JavaDoc;
5
6 import org.apache.axis.AxisFault;
7 import org.apache.axis.ConfigurationException;
8 import org.apache.axis.Constants;
9 import org.apache.axis.EngineConfiguration;
10 import org.apache.axis.Handler;
11 import org.apache.axis.Message;
12 import org.apache.axis.MessageContext;
13 import org.apache.axis.client.AxisClient;
14 import org.apache.axis.client.Call;
15 import org.apache.axis.client.Service;
16 import org.apache.axis.configuration.EngineConfigurationFactoryFinder;
17 import org.apache.axis.configuration.FileProvider;
18 import org.apache.axis.deployment.wsdd.WSDDDeployment;
19 import org.apache.axis.deployment.wsdd.WSDDTransport;
20 import org.apache.axis.handlers.soap.SOAPService;
21
22
23 /**
24  *
25  *
26  * @author Guillaume Sauthier
27  */

28 public class TestAxisClient extends AJAXRPC {
29
30
31     /**
32      *
33      *
34      * @author Guillaume Sauthier
35      */

36     protected class AxisFaultWSDDTransport extends WSDDTransport {
37
38         /**
39          * @see org.apache.axis.deployment.wsdd.WSDDDeployableItem#makeNewInstance(org.apache.axis.EngineConfiguration)
40          */

41         public Handler makeNewInstance(EngineConfiguration registry) throws ConfigurationException {
42             return new MockServiceHandler();
43         }
44         /**
45          * @see org.apache.axis.deployment.wsdd.WSDDDeployableItem#getQName()
46          */

47         public QName JavaDoc getQName() {
48             return new QName JavaDoc("faulter");
49         }
50 }
51     /*
52      * @see TestCase#setUp()
53      */

54     protected void setUp() throws Exception JavaDoc {
55         super.setUp();
56     }
57
58     /*
59      * @see TestCase#tearDown()
60      */

61     protected void tearDown() throws Exception JavaDoc {
62         super.tearDown();
63     }
64     /**
65      * All Handlers in Chain return true for handleRequest and handleResponse
66      * <p/>
67      * <p/>
68      * * Expected Chain invocation sequence looks like.....
69      * H0.handleRequest
70      * H1.handleRequest
71      * H2.handleRequest
72      * H2.handleResponse
73      * H1.handleResponse
74      * H0.handleResponse
75      *
76      * @throws Exception
77      */

78     public void testPositiveCourseFlow() throws Exception JavaDoc {
79
80         TestHandlerInfoChainFactory factory = buildInfoChainFactory();
81         SOAPService soapService = new SOAPService();
82         soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
83         soapService.setOption(Call.WSDL_SERVICE, new Service());
84         soapService.setOption(Call.WSDL_PORT_NAME, new QName JavaDoc("Fake"));
85         soapService.init();
86         AxisClient client = new AxisClient();
87         MessageContext context = new TestMessageContext(client);
88         context.setTransportName("local");
89         context.setService(soapService);
90         client.invoke(context);
91
92         AAAHandler handlerZero = factory.getHandlers()[0];
93         AAAHandler handlerOne = factory.getHandlers()[1];
94         AAAHandler handlerTwo = factory.getHandlers()[2];
95         assertHandlerRuntime("handlerZero", handlerZero, 1, 1, 0);
96         assertHandlerRuntime("handlerOne", handlerOne, 1, 1, 0);
97         assertHandlerRuntime("handlerTwo", handlerTwo, 1, 1, 0);
98     }
99
100     /**
101      * Tests scenario where one handler returns false on a call
102      * to handleRequest(...).
103      * <p/>
104      * Expected Chain invocation sequence looks like.....
105      * H0.handleRequest
106      * H1.handleRequest returns false
107      * H1.handleResponse
108      * H0.handleResponse
109      *
110      * @throws Exception
111      */

112     public void testRequestHandlerReturnsFalse() throws Exception JavaDoc {
113         // SETUP THE 2nd HANDLER IN THE REQUEST CHAIN TO RETURN FALSE
114
handler1Config.put("HANDLE_REQUEST_RETURN_VALUE", Boolean.FALSE);
115
116         TestHandlerInfoChainFactory factory = buildInfoChainFactory();
117         SOAPService soapService = new SOAPService();
118         soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
119         soapService.setOption(Call.WSDL_SERVICE, new Service());
120         soapService.setOption(Call.WSDL_PORT_NAME, new QName JavaDoc("Fake"));
121         soapService.init();
122         AxisClient client = new AxisClient();
123         MessageContext context = new TestMessageContext(client);
124         context.setTransportName("local");
125         context.setService(soapService);
126         client.invoke(context);
127
128         AAAHandler handlerZero = factory.getHandlers()[0];
129         AAAHandler handlerOne = factory.getHandlers()[1];
130         AAAHandler handlerTwo = factory.getHandlers()[2];
131         assertHandlerRuntime("handlerZero", handlerZero, 1, 1, 0);
132         assertHandlerRuntime("handlerOne", handlerOne, 1, 1, 0);
133         assertHandlerRuntime("handlerTwo", handlerTwo, 0, 0, 0);
134     }
135
136     /**
137      * Tests scenario where one handler throws a JAXRPCException
138      * to handleRequest(...).
139      * <p/>
140      * Expected Chain invocation sequence looks like.....
141      * H0.handleRequest
142      * H1.handleRequest throws JAXRPCException
143      *
144      * @throws Exception
145      */

146     public void testRequestHandlerThrowsJAXRPCException() throws Exception JavaDoc {
147         // SETUP THE 2nd HANDLER IN THE REQUEST CHAIN TO THROW JAXRPCException
148
handler1Config.put("HANDLE_REQUEST_RETURN_VALUE",
149                 new JAXRPCException JavaDoc());
150
151         TestHandlerInfoChainFactory factory = buildInfoChainFactory();
152         SOAPService soapService = new SOAPService();
153         soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
154         soapService.setOption(Call.WSDL_SERVICE, new Service());
155         soapService.setOption(Call.WSDL_PORT_NAME, new QName JavaDoc("Fake"));
156         soapService.init();
157         AxisClient client = new AxisClient();
158         MessageContext context = new TestMessageContext(client);
159         context.setTransportName("local");
160         context.setService(soapService);
161         
162         try {
163             client.invoke(context);
164         } catch (AxisFault e) {
165             AAAHandler handlerZero = factory.getHandlers()[0];
166             AAAHandler handlerOne = factory.getHandlers()[1];
167             AAAHandler handlerTwo = factory.getHandlers()[2];
168             assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0);
169             assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 0);
170             assertHandlerRuntime("handlerTwo", handlerTwo, 0, 0, 0);
171         }
172     }
173
174     /**
175      * Tests scenario where one handler throws a RuntimeException
176      * to handleRequest(...).
177      * <p/>
178      * Expected Chain invocation sequence looks like.....
179      * H0.handleRequest
180      * H1.handleRequest throws JAXRPCException
181      *
182      * @throws Exception
183      */

184     public void testRequestHandlerThrowsRuntimeException() throws Exception JavaDoc {
185         // SETUP THE 2nd HANDLER IN THE REQUEST CHAIN TO THROW JAXRPCException
186
handler1Config.put("HANDLE_REQUEST_RETURN_VALUE",
187                 new RuntimeException JavaDoc());
188
189         TestHandlerInfoChainFactory factory = buildInfoChainFactory();
190         SOAPService soapService = new SOAPService();
191         soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
192         soapService.setOption(Call.WSDL_SERVICE, new Service());
193         soapService.setOption(Call.WSDL_PORT_NAME, new QName JavaDoc("Fake"));
194         soapService.init();
195         AxisClient client = new AxisClient();
196         MessageContext context = new TestMessageContext(client);
197         context.setTransportName("local");
198         context.setService(soapService);
199
200         try {
201             client.invoke(context);
202             fail("Expected AxisFault to be thrown");
203         } catch (AxisFault e) {
204             AAAHandler handlerZero = factory.getHandlers()[0];
205             AAAHandler handlerOne = factory.getHandlers()[1];
206             AAAHandler handlerTwo = factory.getHandlers()[2];
207             assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0);
208             assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 0);
209             assertHandlerRuntime("handlerTwo", handlerTwo, 0, 0, 0);
210         }
211     }
212
213     /**
214      * Tests scenario where one handler returns false on a call
215      * to handleResponse(...).
216      * <p/>
217      * Expected Chain invocation sequence looks like.....
218      * H0.handleRequest
219      * H1.handleRequest
220      * H2.handleRequest
221      * H2.handleResponse return false
222      *
223      * @throws Exception
224      */

225     public void testResponseHandlerReturnsFalse() throws Exception JavaDoc {
226         // SETUP THE 3rd HANDLER IN THE CHAIN TO RETURN FALSE on handleResponse
227
handler2Config.put("HANDLE_RESPONSE_RETURN_VALUE", Boolean.FALSE);
228
229         TestHandlerInfoChainFactory factory = buildInfoChainFactory();
230         SOAPService soapService = new SOAPService();
231         soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
232         soapService.setOption(Call.WSDL_SERVICE, new Service());
233         soapService.setOption(Call.WSDL_PORT_NAME, new QName JavaDoc("Fake"));
234         soapService.init();
235         AxisClient client = new AxisClient();
236         MessageContext context = new TestMessageContext(client);
237         context.setTransportName("local");
238         context.setService(soapService);
239
240         client.invoke(context);
241         
242         AAAHandler handlerZero = factory.getHandlers()[0];
243         AAAHandler handlerOne = factory.getHandlers()[1];
244         AAAHandler handlerTwo = factory.getHandlers()[2];
245         assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0);
246         assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 0);
247         assertHandlerRuntime("handlerTwo", handlerTwo, 1, 1, 0);
248     }
249
250     /**
251      * Tests scenario where one handler throws JAXRPCException on a call
252      * to handleResponse(...).
253      * <p/>
254      * Expected Chain invocation sequence looks like.....
255      * H0.handleRequest
256      * H1.handleRequest
257      * H2.handleRequest
258      * H2.handleResponse
259      * H1.handlerResponse throws JAXRPCException
260      *
261      * @throws Exception
262      */

263     public void testResponseHandlerThrowsJAXRPCException() throws Exception JavaDoc {
264         // SETUP THE 2nd HANDLER IN THE CHAIN TO THROW JAXRPCException on handleResponse
265
handler1Config.put("HANDLE_RESPONSE_RETURN_VALUE",
266                 new JAXRPCException JavaDoc());
267
268         TestHandlerInfoChainFactory factory = buildInfoChainFactory();
269         SOAPService soapService = new SOAPService();
270         soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
271         soapService.setOption(Call.WSDL_SERVICE, new Service());
272         soapService.setOption(Call.WSDL_PORT_NAME, new QName JavaDoc("Fake"));
273         soapService.init();
274         AxisClient client = new AxisClient();
275         MessageContext context = new TestMessageContext(client);
276         context.setTransportName("local");
277         context.setService(soapService);
278
279         try {
280             client.invoke(context);
281             fail("Expected AxisFault to be thrown");
282         } catch (AxisFault e) {
283             AAAHandler handlerZero = factory.getHandlers()[0];
284             AAAHandler handlerOne = factory.getHandlers()[1];
285             AAAHandler handlerTwo = factory.getHandlers()[2];
286             assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0);
287             assertHandlerRuntime("handlerOne", handlerOne, 1, 1, 0);
288             assertHandlerRuntime("handlerTwo", handlerTwo, 1, 1, 0);
289         }
290     }
291
292     /**
293      * Tests scenario where one handler throws RuntimeException on a call
294      * to handleResponse(...).
295      * <p/>
296      * Expected Chain invocation sequence looks like.....
297      * H0.handleRequest
298      * H1.handleRequest
299      * H2.handleRequest
300      * H2.handleResponse
301      * H1.handlerResponse throws RuntimeException
302      *
303      * @throws Exception
304      */

305     public void testResponseHandlerThrowsRuntimeException() throws Exception JavaDoc {
306         // SETUP THE 2nd HANDLER IN THE CHAIN TO THROW RuntimeException on handleResponse
307
handler1Config.put("HANDLE_RESPONSE_RETURN_VALUE",
308                 new RuntimeException JavaDoc());
309
310         TestHandlerInfoChainFactory factory = buildInfoChainFactory();
311         SOAPService soapService = new SOAPService();
312         soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
313         soapService.setOption(Call.WSDL_SERVICE, new Service());
314         soapService.setOption(Call.WSDL_PORT_NAME, new QName JavaDoc("Fake"));
315         soapService.init();
316         AxisClient client = new AxisClient();
317         MessageContext context = new TestMessageContext(client);
318         context.setTransportName("local");
319         context.setService(soapService);
320
321         try {
322             client.invoke(context);
323             fail("Expected AxisFault to be thrown");
324         } catch (AxisFault e) {
325             AAAHandler handlerZero = factory.getHandlers()[0];
326             AAAHandler handlerOne = factory.getHandlers()[1];
327             AAAHandler handlerTwo = factory.getHandlers()[2];
328             assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0);
329             assertHandlerRuntime("handlerOne", handlerOne, 1, 1, 0);
330             assertHandlerRuntime("handlerTwo", handlerTwo, 1, 1, 0);
331         }
332     }
333
334     /**
335      * Tests scenario where one handler returns false on a call
336      * to handleFault(...).
337      * <p/>
338      * Expected Chain invocation sequence looks like.....
339      * H0.handleRequest
340      * H1.handleRequest
341      * H2.handleRequest
342      * H2.handleFault
343      * H1.handleFault return false
344      *
345      * @throws Exception
346      */

347     public void testHandleFaultReturnsFalse() throws Exception JavaDoc {
348         // SETUP A MOCK SERVICE THAT SIMULATE A SOAPFAULT THROWN BY ENDPOINT
349
handler1Config.put("HANDLE_FAULT_RETURN_VALUE", Boolean.FALSE);
350
351         TestHandlerInfoChainFactory factory = buildInfoChainFactory();
352         SOAPService soapService = new SOAPService();
353         soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
354         soapService.setOption(Call.WSDL_SERVICE, new Service());
355         soapService.setOption(Call.WSDL_PORT_NAME, new QName JavaDoc("Fake"));
356         soapService.init();
357         AxisClient client = new AxisClient();
358         addFaultTransport(client);
359
360         MessageContext context = new TestMessageContext(client);
361         context.setTransportName("faulter");
362         context.setService(soapService);
363
364         try {
365             client.invoke(context);
366             fail("Expecting an AxisFault");
367         } catch (AxisFault f) {
368             AAAHandler handlerZero = factory.getHandlers()[0];
369             AAAHandler handlerOne = factory.getHandlers()[1];
370             AAAHandler handlerTwo = factory.getHandlers()[2];
371             assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0);
372             assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 0);
373             assertHandlerRuntime("handlerTwo", handlerTwo, 1, 0, 0);
374         }
375     }
376
377     /**
378      * Tests to see if we handle the scenario of a handler throwing a
379      * runtime exception during the handleFault(...) processing correctly
380      * <p/>
381      * Expected chain sequence:
382      * H0.handleRequest
383      * H1.handleRequest
384      * H2.handleRequest
385      * H2.handleFault
386      * H1.handleFault throws JAXRPCException
387      *
388      * @throws Exception
389      */

390     public void testFaultHandlerThrowsJAXRPCException() throws Exception JavaDoc {
391         handler1Config.put("HANDLE_FAULT_RETURN_VALUE", new JAXRPCException JavaDoc());
392
393         TestHandlerInfoChainFactory factory = buildInfoChainFactory();
394         Handler serviceHandler = new MockServiceHandler();
395         SOAPService soapService = new SOAPService(null, serviceHandler, null);
396         soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
397         soapService.setOption(Call.WSDL_SERVICE, new Service());
398         soapService.setOption(Call.WSDL_PORT_NAME, new QName JavaDoc("Fake"));
399         soapService.init();
400         AxisClient client = new AxisClient();
401         addFaultTransport(client);
402
403         MessageContext context = new TestMessageContext(client);
404         context.setTransportName("faulter");
405         context.setService(soapService);
406
407         try {
408             client.invoke(context);
409             fail("Expected AxisFault to be thrown");
410         } catch (AxisFault e) {
411             AAAHandler handlerZero = factory.getHandlers()[0];
412             AAAHandler handlerOne = factory.getHandlers()[1];
413             AAAHandler handlerTwo = factory.getHandlers()[2];
414             assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0);
415             assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 0);
416             assertHandlerRuntime("handlerTwo", handlerTwo, 1, 0, 0);
417         }
418     }
419
420     /**
421      * Tests to see if we handle the scenario of a handler throwing a
422      * runtime exception during the handleFault(...) processing correctly
423      * <p/>
424      * Expected chain sequence:
425      * H0.handleRequest
426      * H1.handleRequest
427      * H2.handleRequest
428      * H2.handleFault
429      * H1.handleFault throws RuntimeException
430      *
431      * @throws Exception
432      */

433     public void testFaultHandlerThrowsRuntimeException() throws Exception JavaDoc {
434         // SETUP THE LAST HANDLER IN THE REQUEST CHAIN TO THROW SOAPFaultException
435
handler1Config.put("HANDLE_FAULT_RETURN_VALUE", new RuntimeException JavaDoc());
436
437         TestHandlerInfoChainFactory factory = buildInfoChainFactory();
438         Handler serviceHandler = new MockServiceHandler();
439         SOAPService soapService = new SOAPService(null, serviceHandler, null);
440         soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
441         soapService.setOption(Call.WSDL_SERVICE, new Service());
442         soapService.setOption(Call.WSDL_PORT_NAME, new QName JavaDoc("Fake"));
443         soapService.init();
444         AxisClient client = new AxisClient();
445         addFaultTransport(client);
446
447         MessageContext context = new TestMessageContext(client);
448         context.setTransportName("faulter");
449         context.setService(soapService);
450
451         try {
452             client.invoke(context);
453             fail("Expected AxisFault to be thrown");
454         } catch (AxisFault e) {
455             AAAHandler handlerZero = factory.getHandlers()[0];
456             AAAHandler handlerOne = factory.getHandlers()[1];
457             AAAHandler handlerTwo = factory.getHandlers()[2];
458             assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0);
459             assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 0);
460             assertHandlerRuntime("handlerTwo", handlerTwo, 1, 0, 0);
461         }
462     }
463
464     /**
465      * Tests scenario where service object throws Axis Fault.
466      * <p/>
467      * Expected chain sequence:
468      * H0.handleRequest
469      * H1.handleRequest
470      * H2.handleRequest
471      * ServiceObject.invoke() throws AxisFault
472      * H2.handleFault
473      * H1.handleFault
474      * H0.handleFault
475      *
476      * @throws Exception
477      */

478     public void testServiceObjectThrowsAxisFault() throws Exception JavaDoc {
479         TestHandlerInfoChainFactory factory = buildInfoChainFactory();
480         Handler serviceHandler = new MockServiceHandler();
481         SOAPService soapService = new SOAPService(null, serviceHandler, null);
482         soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
483         soapService.setOption(Call.WSDL_SERVICE, new Service());
484         soapService.setOption(Call.WSDL_PORT_NAME, new QName JavaDoc("Fake"));
485         soapService.init();
486         AxisClient client = new AxisClient();
487         addFaultTransport(client);
488
489         MessageContext context = new TestMessageContext(client);
490         context.setTransportName("faulter");
491         context.setService(soapService);
492
493         try {
494             client.invoke(context);
495             fail("Expected AxisFault to be thrown");
496         } catch (AxisFault e) {
497             AAAHandler handlerZero = factory.getHandlers()[0];
498             AAAHandler handlerOne = factory.getHandlers()[1];
499             AAAHandler handlerTwo = factory.getHandlers()[2];
500             assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0);
501             assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 0);
502             assertHandlerRuntime("handlerTwo", handlerTwo, 1, 0, 0);
503         }
504     }
505     
506     /**
507      * Configure a transport handler that simulate a Fault on server-side
508      * @param client AxisClient
509      */

510     private void addFaultTransport(AxisClient client) {
511
512         FileProvider config = (FileProvider) client.getConfig();
513
514         WSDDDeployment depl = config.getDeployment();
515         if (depl == null) {
516             depl = new WSDDDeployment();
517         }
518         WSDDTransport trans = new AxisFaultWSDDTransport();
519         depl.deployTransport(trans);
520         
521     }
522
523     private class TestMessageContext extends org.apache.axis.MessageContext {
524
525         public String JavaDoc listByAreaCode = "<soap:Envelope\n" +
526                 "xmlns:s0=\"http://www.tilisoft.com/ws/LocInfo/literalTypes\"\n" +
527                 "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
528                 "xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" +
529                 "<soap:Header>\n" +
530                 "<WSABIHeader>\n" +
531                 "<SubscriptionId>192168001100108165800640600008</SubscriptionId>\n" +
532                 "</WSABIHeader>\n" +
533                 "</soap:Header>\n" +
534                 "<soap:Body>\n" +
535                 "<s0:ListByAreaCode>\n" +
536                 "<s0:AreaCode>617</s0:AreaCode>\n" +
537                 "</s0:ListByAreaCode>\n" +
538                 "</soap:Body>\n" +
539                 "</soap:Envelope>\n";
540
541         public TestMessageContext(AxisClient client) {
542             super(client);
543             Message message = new Message(listByAreaCode);
544             message.setMessageType(Message.REQUEST);
545             setRequestMessage(message);
546         }
547     }
548
549 }
550
Popular Tags