KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > jaxrpc > TestSOAPService


1 package test.jaxrpc;
2
3 import junit.framework.TestCase;
4 import org.apache.axis.AxisFault;
5 import org.apache.axis.Constants;
6 import org.apache.axis.Handler;
7 import org.apache.axis.Message;
8 import org.apache.axis.MessageContext;
9 import org.apache.axis.handlers.BasicHandler;
10 import org.apache.axis.handlers.HandlerInfoChainFactory;
11 import org.apache.axis.handlers.soap.SOAPService;
12 import org.apache.axis.message.Detail;
13 import org.apache.axis.server.AxisServer;
14
15 import javax.xml.rpc.JAXRPCException JavaDoc;
16 import javax.xml.rpc.handler.HandlerChain JavaDoc;
17 import javax.xml.rpc.handler.HandlerInfo JavaDoc;
18 import javax.xml.rpc.soap.SOAPFaultException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 public class TestSOAPService
25         extends TestCase {
26     HandlerInfo JavaDoc handlerInfo0 = null;
27     HandlerInfo JavaDoc handlerInfo1 = null;
28     HandlerInfo JavaDoc handlerInfo2 = null;
29
30     Map JavaDoc handler0Config = null;
31     Map JavaDoc handler1Config = null;
32     Map JavaDoc handler2Config = null;
33
34     /**
35      * Sets up the handlerInfo and handlerConfigs for all tests.
36      * Each test has 3 JAX-RPC Handlers of the same type to keep things
37      * simple.
38      *
39      * @throws Exception
40      */

41     protected void setUp() throws Exception JavaDoc {
42         handlerInfo0 = new HandlerInfo JavaDoc();
43         handlerInfo0.setHandlerClass(AAAHandler.class);
44         handlerInfo1 = new HandlerInfo JavaDoc();
45         handlerInfo1.setHandlerClass(AAAHandler.class);
46         handlerInfo2 = new HandlerInfo JavaDoc();
47         handlerInfo2.setHandlerClass(AAAHandler.class);
48         handler0Config = new HashMap JavaDoc();
49         handler1Config = new HashMap JavaDoc();
50         handler2Config = new HashMap JavaDoc();
51         handlerInfo0.setHandlerConfig(handler0Config);
52         handlerInfo1.setHandlerConfig(handler1Config);
53         handlerInfo2.setHandlerConfig(handler2Config);
54     }
55
56     /**
57      * All Handlers in Chain return true for handleRequest and handleResponse
58      * <p/>
59      * <p/>
60      * * Expected Chain invocation sequence looks like.....
61      * H0.handleRequest
62      * H1.handleRequest
63      * H2.handleRequest
64      * H2.handleResponse
65      * H1.handleResponse
66      * H0.handleResponse
67      *
68      * @throws Exception
69      */

70     public void testPositiveCourseFlow() throws Exception JavaDoc {
71         TestHandlerInfoChainFactory factory = buildInfoChainFactory();
72         SOAPService soapService = new SOAPService();
73         soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
74         soapService.init();
75         soapService.invoke(new TestMessageContext());
76         AAAHandler handlerZero = factory.getHandlers()[0];
77         AAAHandler handlerOne = factory.getHandlers()[1];
78         AAAHandler handlerTwo = factory.getHandlers()[2];
79         assertHandlerRuntime("handlerZero", handlerZero, 1, 1, 0);
80         assertHandlerRuntime("handlerOne", handlerOne, 1, 1, 0);
81         assertHandlerRuntime("handlerTwo", handlerTwo, 1, 1, 0);
82     }
83
84     /**
85      * Tests scenario where one handler returns false on a call
86      * to handleRequest(...).
87      * <p/>
88      * Expected Chain invocation sequence looks like.....
89      * H0.handleRequest
90      * H1.handleRequest returns false
91      * H1.handleResponse
92      * H0.handleResponse
93      *
94      * @throws Exception
95      */

96     public void testRequestHandlerReturnsFalse() throws Exception JavaDoc {
97         SOAPService soapService = new SOAPService();
98
99         // SETUP THE 2nd HANDLER IN THE REQUEST CHAIN TO RETURN FALSE
100
handler1Config.put("HANDLE_REQUEST_RETURN_VALUE", Boolean.FALSE);
101         TestHandlerInfoChainFactory factory = buildInfoChainFactory();
102         soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
103         soapService.init();
104         MessageContext msgContext = new TestMessageContext();
105         soapService.invoke(msgContext);
106         AAAHandler handlerZero = factory.getHandlers()[0];
107         AAAHandler handlerOne = factory.getHandlers()[1];
108         AAAHandler handlerTwo = factory.getHandlers()[2];
109         assertHandlerRuntime("handlerZero", handlerZero, 1, 1, 0);
110         assertHandlerRuntime("handlerOne", handlerOne, 1, 1, 0);
111         assertHandlerRuntime("handlerTwo", handlerTwo, 0, 0, 0);
112     }
113
114     /**
115      * @throws Exception
116      */

117     public void testRequestHandlerThrowsSFE() throws Exception JavaDoc {
118         SOAPService soapService = new SOAPService();
119
120         // SETUP THE 2nd HANDLER IN THE REQUEST CHAIN TO THROW SOAPFaultException
121
handler1Config.put("HANDLE_REQUEST_RETURN_VALUE",
122                 new SOAPFaultException JavaDoc(null, "f", "f", new Detail()));
123         TestHandlerInfoChainFactory factory = buildInfoChainFactory();
124         soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
125         soapService.init();
126         MessageContext msgContext = new TestMessageContext();
127         soapService.invoke(msgContext);
128         AAAHandler handlerZero = factory.getHandlers()[0];
129         AAAHandler handlerOne = factory.getHandlers()[1];
130         AAAHandler handlerTwo = factory.getHandlers()[2];
131         assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 1);
132         assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 1);
133         assertHandlerRuntime("handlerTwo", handlerTwo, 0, 0, 0);
134     }
135
136     /**
137      * @throws Exception
138      */

139     public void testRequestHandlerThrowsJAXRPCException() throws Exception JavaDoc {
140         SOAPService soapService = new SOAPService();
141
142         // SETUP THE 2nd HANDLER IN THE REQUEST CHAIN TO THROW JAXRPCException
143
handler1Config.put("HANDLE_REQUEST_RETURN_VALUE",
144                 new JAXRPCException JavaDoc());
145         TestHandlerInfoChainFactory factory = buildInfoChainFactory();
146         soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
147         soapService.init();
148         MessageContext msgContext = new TestMessageContext();
149         try {
150             soapService.invoke(msgContext);
151         } catch (AxisFault e) {
152             AAAHandler handlerZero = factory.getHandlers()[0];
153             AAAHandler handlerOne = factory.getHandlers()[1];
154             AAAHandler handlerTwo = factory.getHandlers()[2];
155             assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0);
156             assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 0);
157             assertHandlerRuntime("handlerTwo", handlerTwo, 0, 0, 0);
158         }
159     }
160
161     public void testRequestHandlerThrowsRuntimeException() throws Exception JavaDoc {
162         SOAPService soapService = new SOAPService();
163
164         // SETUP THE 2nd HANDLER IN THE REQUEST CHAIN TO THROW JAXRPCException
165
handler1Config.put("HANDLE_REQUEST_RETURN_VALUE",
166                 new RuntimeException JavaDoc());
167         TestHandlerInfoChainFactory factory = buildInfoChainFactory();
168         soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
169         soapService.init();
170         MessageContext msgContext = new TestMessageContext();
171         try {
172             soapService.invoke(msgContext);
173         } catch (AxisFault e) {
174             AAAHandler handlerZero = factory.getHandlers()[0];
175             AAAHandler handlerOne = factory.getHandlers()[1];
176             AAAHandler handlerTwo = factory.getHandlers()[2];
177             assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0);
178             assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 0);
179             assertHandlerRuntime("handlerTwo", handlerTwo, 0, 0, 0);
180         }
181     }
182
183     public void testResponseHandlerReturnsFalse() throws Exception JavaDoc {
184         SOAPService soapService = new SOAPService();
185
186         // SETUP THE 3rd HANDLER IN THE CHAIN TO RETURN FALSE on handleResponse
187
handler2Config.put("HANDLE_RESPONSE_RETURN_VALUE", Boolean.FALSE);
188         TestHandlerInfoChainFactory factory = buildInfoChainFactory();
189         soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
190         soapService.init();
191         MessageContext msgContext = new TestMessageContext();
192         soapService.invoke(msgContext);
193         AAAHandler handlerZero = factory.getHandlers()[0];
194         AAAHandler handlerOne = factory.getHandlers()[1];
195         AAAHandler handlerTwo = factory.getHandlers()[2];
196         assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0);
197         assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 0);
198         assertHandlerRuntime("handlerTwo", handlerTwo, 1, 1, 0);
199     }
200
201     public void testResponseHandlerThrowsJAXRPCException() throws Exception JavaDoc {
202         SOAPService soapService = new SOAPService();
203
204         // SETUP THE 2nd HANDLER IN THE CHAIN TO THROW JAXRPCException on handleResponse
205
handler1Config.put("HANDLE_RESPONSE_RETURN_VALUE",
206                 new JAXRPCException JavaDoc());
207         TestHandlerInfoChainFactory factory = buildInfoChainFactory();
208         soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
209         soapService.init();
210         MessageContext msgContext = new TestMessageContext();
211         try {
212             soapService.invoke(msgContext);
213             fail("Expected AxisFault to be thrown");
214         } catch (AxisFault e) {
215             AAAHandler handlerZero = factory.getHandlers()[0];
216             AAAHandler handlerOne = factory.getHandlers()[1];
217             AAAHandler handlerTwo = factory.getHandlers()[2];
218             assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0);
219             assertHandlerRuntime("handlerOne", handlerOne, 1, 1, 0);
220             assertHandlerRuntime("handlerTwo", handlerTwo, 1, 1, 0);
221         }
222     }
223
224     public void testResponseHandlerThrowsRuntimeException() throws Exception JavaDoc {
225         SOAPService soapService = new SOAPService();
226
227         // SETUP THE 2nd HANDLER IN THE CHAIN TO THROW RuntimeException on handleResponse
228
handler1Config.put("HANDLE_RESPONSE_RETURN_VALUE",
229                 new RuntimeException JavaDoc());
230         TestHandlerInfoChainFactory factory = buildInfoChainFactory();
231         soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
232         soapService.init();
233         MessageContext msgContext = new TestMessageContext();
234         try {
235             soapService.invoke(msgContext);
236             fail("Expected AxisFault to be thrown");
237         } catch (AxisFault e) {
238             AAAHandler handlerZero = factory.getHandlers()[0];
239             AAAHandler handlerOne = factory.getHandlers()[1];
240             AAAHandler handlerTwo = factory.getHandlers()[2];
241             assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0);
242             assertHandlerRuntime("handlerOne", handlerOne, 1, 1, 0);
243             assertHandlerRuntime("handlerTwo", handlerTwo, 1, 1, 0);
244         }
245     }
246
247     public void testHandleFaultReturnsFalse() throws Exception JavaDoc {
248         SOAPService soapService = new SOAPService();
249
250         // SETUP THE LAST HANDLER IN THE REQUEST CHAIN TO THROW SOAPFaultException
251
handler2Config.put("HANDLE_REQUEST_RETURN_VALUE",
252                 new SOAPFaultException JavaDoc(null, "f", "f", new Detail()));
253         handler1Config.put("HANDLE_FAULT_RETURN_VALUE", Boolean.FALSE);
254         TestHandlerInfoChainFactory factory = buildInfoChainFactory();
255         soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
256         soapService.init();
257         MessageContext msgContext = new TestMessageContext();
258         soapService.invoke(msgContext);
259         AAAHandler handlerZero = factory.getHandlers()[0];
260         AAAHandler handlerOne = factory.getHandlers()[1];
261         AAAHandler handlerTwo = factory.getHandlers()[2];
262         assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0);
263         assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 1);
264         assertHandlerRuntime("handlerTwo", handlerTwo, 1, 0, 1);
265     }
266
267     /**
268      * Tests to see if we handle the scenario of a handler throwing a
269      * runtime exception during the handleFault(...) processing correctly
270      * <p/>
271      * Expected chain sequence:
272      * H0.handleRequest
273      * H1.handleRequest
274      * H2.handleRequest SOAPFaultException
275      * H2.handleFault
276      * H1.handleFault throws JAXRPCException
277      *
278      * @throws Exception
279      */

280     public void testFaultHandlerThrowsJAXRPCException() throws Exception JavaDoc {
281         SOAPService soapService = new SOAPService();
282
283         // SETUP THE LAST HANDLER IN THE REQUEST CHAIN TO THROW SOAPFaultException
284
handler2Config.put("HANDLE_REQUEST_RETURN_VALUE",
285                 new SOAPFaultException JavaDoc(null, "f", "f", new Detail()));
286         handler1Config.put("HANDLE_FAULT_RETURN_VALUE", new JAXRPCException JavaDoc());
287         TestHandlerInfoChainFactory factory = buildInfoChainFactory();
288         soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
289         soapService.init();
290         MessageContext msgContext = new TestMessageContext();
291         try {
292             soapService.invoke(msgContext);
293             fail("Expected AxisFault to be thrown");
294         } catch (AxisFault e) {
295             AAAHandler handlerZero = factory.getHandlers()[0];
296             AAAHandler handlerOne = factory.getHandlers()[1];
297             AAAHandler handlerTwo = factory.getHandlers()[2];
298             assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0);
299             assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 1);
300             assertHandlerRuntime("handlerTwo", handlerTwo, 1, 0, 1);
301         }
302     }
303
304     /**
305      * Tests to see if we handle the scenario of a handler throwing a
306      * runtime exception during the handleFault(...) processing correctly
307      * <p/>
308      * Expected chain sequence:
309      * H0.handleRequest
310      * H1.handleRequest
311      * H2.handleRequest throws SOAPFaultException
312      * H2.handleFault
313      * H1.handleFault throws RuntimeException
314      *
315      * @throws Exception
316      */

317     public void testFaultHandlerThrowsRuntimeException() throws Exception JavaDoc {
318         SOAPService soapService = new SOAPService();
319
320         // SETUP THE LAST HANDLER IN THE REQUEST CHAIN TO THROW SOAPFaultException
321
handler2Config.put("HANDLE_REQUEST_RETURN_VALUE",
322                 new SOAPFaultException JavaDoc(null, "f", "f", new Detail()));
323         handler1Config.put("HANDLE_FAULT_RETURN_VALUE", new RuntimeException JavaDoc());
324         TestHandlerInfoChainFactory factory = buildInfoChainFactory();
325         soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
326         soapService.init();
327         MessageContext msgContext = new TestMessageContext();
328         try {
329             soapService.invoke(msgContext);
330             fail("Expected AxisFault to be thrown");
331         } catch (AxisFault e) {
332             AAAHandler handlerZero = factory.getHandlers()[0];
333             AAAHandler handlerOne = factory.getHandlers()[1];
334             AAAHandler handlerTwo = factory.getHandlers()[2];
335             assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0);
336             assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 1);
337             assertHandlerRuntime("handlerTwo", handlerTwo, 1, 0, 1);
338         }
339     }
340
341     /**
342      * Tests scenario where service object throws Axis Fault.
343      * <p/>
344      * Expected chain sequence:
345      * H0.handleRequest
346      * H1.handleRequest
347      * H2.handleRequest
348      * ServiceObject.invoke() throws AxisFault
349      * H2.handleFault
350      * H1.handleFault
351      * H0.handleFault
352      *
353      * @throws Exception
354      */

355     public void testServiceObjectThrowsAxisFault() throws Exception JavaDoc {
356         Handler serviceHandler = new MockServiceHandler();
357         SOAPService soapService = new SOAPService(null, null, serviceHandler);
358         TestHandlerInfoChainFactory factory = buildInfoChainFactory();
359         soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
360         soapService.init();
361         MessageContext msgContext = new TestMessageContext();
362         try {
363             soapService.invoke(msgContext);
364             fail("Expected AxisFault to be thrown");
365         } catch (AxisFault e) {
366             AAAHandler handlerZero = factory.getHandlers()[0];
367             AAAHandler handlerOne = factory.getHandlers()[1];
368             AAAHandler handlerTwo = factory.getHandlers()[2];
369             assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 1);
370             assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 1);
371             assertHandlerRuntime("handlerTwo", handlerTwo, 1, 0, 1);
372         }
373     }
374
375     /**
376      * Convenience method to organize all test checking for a particular
377      * handler.
378      * <p/>
379      * Checks to see if the expected number of calls to handleRequest, handleResponse
380      * and handleFault reconciles with actuals
381      *
382      * @param message
383      * @param handler the target handler to reconcile
384      * @param numHandleRequest # of expected calls to handleRequest
385      * @param numHandleResponse # of expected calls to handleResponse
386      * @param numHandleFault # of expected call to handleFault
387      */

388     protected void assertHandlerRuntime(String JavaDoc message, AAAHandler handler,
389                                         int numHandleRequest,
390                                         int numHandleResponse,
391                                         int numHandleFault) {
392         assertEquals(message + ": handleRequest", numHandleRequest,
393                 handler.getHandleRequestInvocations());
394         assertEquals(message + ": handleResponse", numHandleResponse,
395                 handler.getHandleResponseInvocations());
396         assertEquals(message + ": handleFault", numHandleFault,
397                 handler.getHandleFaultInvocations());
398     }
399
400     /**
401      * Convenience method to create a HandlerInfoChainFactory
402      *
403      * @return
404      */

405     protected TestHandlerInfoChainFactory buildInfoChainFactory() {
406         List JavaDoc handlerInfos = new ArrayList JavaDoc();
407         handlerInfos.add(handlerInfo0);
408         handlerInfos.add(handlerInfo1);
409         handlerInfos.add(handlerInfo2);
410         TestHandlerInfoChainFactory factory = new TestHandlerInfoChainFactory(
411                 handlerInfos);
412         return factory;
413     }
414
415     /**
416      * Mock Service Handler used to simulate a service object throwing
417      * an AxisFault.
418      */

419     private class MockServiceHandler extends BasicHandler {
420         public void invoke(MessageContext msgContext) throws AxisFault {
421             throw new AxisFault();
422         }
423     }
424
425     /**
426      * Helper class for keeping references to the JAX-RPC Handlers
427      * that are created by the Factory.
428      * <p/>
429      * This class allows us to access the individual handlers after
430      * the test case has been executed in order to make sure that
431      * the expected methods of the handler instance have been invoked.
432      */

433     private class TestHandlerInfoChainFactory extends HandlerInfoChainFactory {
434         AAAHandler[] handlers;
435
436         public TestHandlerInfoChainFactory(List JavaDoc handlerInfos) {
437             super(handlerInfos);
438         }
439
440         public HandlerChain JavaDoc createHandlerChain() {
441             HandlerChain JavaDoc chain = super.createHandlerChain();
442             handlers = new AAAHandler[chain.size()];
443             for (int i = 0; i < chain.size(); i++) {
444                 handlers[i] = (AAAHandler) chain.get(i);
445             }
446             return chain;
447         }
448
449         public AAAHandler[] getHandlers() {
450             return handlers;
451         }
452
453     }
454
455     private class TestMessageContext extends org.apache.axis.MessageContext {
456
457         public String JavaDoc listByAreaCode = "<soap:Envelope\n" +
458                 "xmlns:s0=\"http://www.tilisoft.com/ws/LocInfo/literalTypes\"\n" +
459                 "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
460                 "xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" +
461                 "<soap:Header>\n" +
462                 "<WSABIHeader>\n" +
463                 "<SubscriptionId>192168001100108165800640600008</SubscriptionId>\n" +
464                 "</WSABIHeader>\n" +
465                 "</soap:Header>\n" +
466                 "<soap:Body>\n" +
467                 "<s0:ListByAreaCode>\n" +
468                 "<s0:AreaCode>617</s0:AreaCode>\n" +
469                 "</s0:ListByAreaCode>\n" +
470                 "</soap:Body>\n" +
471                 "</soap:Envelope>\n";
472
473         public TestMessageContext() {
474             super(new AxisServer());
475             Message message = new Message(listByAreaCode);
476             message.setMessageType(Message.REQUEST);
477             setRequestMessage(message);
478         }
479     }
480
481 }
482
Popular Tags