File: TestAxisClient.java

package info (click to toggle)
axis 1.4-29
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 52,100 kB
  • sloc: java: 129,124; xml: 10,602; jsp: 983; sh: 84; cs: 36; makefile: 18
file content (549 lines) | stat: -rw-r--r-- 21,442 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
package test.jaxrpc;

import javax.xml.namespace.QName;
import javax.xml.rpc.JAXRPCException;

import org.apache.axis.AxisFault;
import org.apache.axis.ConfigurationException;
import org.apache.axis.Constants;
import org.apache.axis.EngineConfiguration;
import org.apache.axis.Handler;
import org.apache.axis.Message;
import org.apache.axis.MessageContext;
import org.apache.axis.client.AxisClient;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.configuration.EngineConfigurationFactoryFinder;
import org.apache.axis.configuration.FileProvider;
import org.apache.axis.deployment.wsdd.WSDDDeployment;
import org.apache.axis.deployment.wsdd.WSDDTransport;
import org.apache.axis.handlers.soap.SOAPService;


/**
 *
 *
 * @author Guillaume Sauthier
 */
public class TestAxisClient extends AJAXRPC {


    /**
     *
     *
     * @author Guillaume Sauthier
     */
    protected class AxisFaultWSDDTransport extends WSDDTransport {

        /**
         * @see org.apache.axis.deployment.wsdd.WSDDDeployableItem#makeNewInstance(org.apache.axis.EngineConfiguration)
         */
        public Handler makeNewInstance(EngineConfiguration registry) throws ConfigurationException {
            return new MockServiceHandler();
        }
        /**
         * @see org.apache.axis.deployment.wsdd.WSDDDeployableItem#getQName()
         */
        public QName getQName() {
            return new QName("faulter");
        }
}
    /*
     * @see TestCase#setUp()
     */
    protected void setUp() throws Exception {
        super.setUp();
    }

    /*
     * @see TestCase#tearDown()
     */
    protected void tearDown() throws Exception {
        super.tearDown();
    }
    /**
     * All Handlers in Chain return true for handleRequest and handleResponse
     * <p/>
     * <p/>
     * * Expected Chain invocation sequence looks like.....
     * H0.handleRequest
     * H1.handleRequest
     * H2.handleRequest
     * H2.handleResponse
     * H1.handleResponse
     * H0.handleResponse
     * 
     * @throws Exception 
     */
    public void testPositiveCourseFlow() throws Exception {

        TestHandlerInfoChainFactory factory = buildInfoChainFactory();
        SOAPService soapService = new SOAPService();
        soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
        soapService.setOption(Call.WSDL_SERVICE, new Service());
        soapService.setOption(Call.WSDL_PORT_NAME, new QName("Fake"));
        soapService.init();
        AxisClient client = new AxisClient();
        MessageContext context = new TestMessageContext(client);
        context.setTransportName("local");
        context.setService(soapService);
        client.invoke(context);

        AAAHandler handlerZero = factory.getHandlers()[0];
        AAAHandler handlerOne = factory.getHandlers()[1];
        AAAHandler handlerTwo = factory.getHandlers()[2];
        assertHandlerRuntime("handlerZero", handlerZero, 1, 1, 0);
        assertHandlerRuntime("handlerOne", handlerOne, 1, 1, 0);
        assertHandlerRuntime("handlerTwo", handlerTwo, 1, 1, 0);
    }

    /**
     * Tests scenario where one handler returns false on a call
     * to handleRequest(...).
     * <p/>
     * Expected Chain invocation sequence looks like.....
     * H0.handleRequest
     * H1.handleRequest returns false
     * H1.handleResponse
     * H0.handleResponse
     * 
     * @throws Exception 
     */
    public void testRequestHandlerReturnsFalse() throws Exception {
        // SETUP THE 2nd HANDLER IN THE REQUEST CHAIN TO RETURN FALSE
        handler1Config.put("HANDLE_REQUEST_RETURN_VALUE", Boolean.FALSE);

        TestHandlerInfoChainFactory factory = buildInfoChainFactory();
        SOAPService soapService = new SOAPService();
        soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
        soapService.setOption(Call.WSDL_SERVICE, new Service());
        soapService.setOption(Call.WSDL_PORT_NAME, new QName("Fake"));
        soapService.init();
        AxisClient client = new AxisClient();
        MessageContext context = new TestMessageContext(client);
        context.setTransportName("local");
        context.setService(soapService);
        client.invoke(context);

        AAAHandler handlerZero = factory.getHandlers()[0];
        AAAHandler handlerOne = factory.getHandlers()[1];
        AAAHandler handlerTwo = factory.getHandlers()[2];
        assertHandlerRuntime("handlerZero", handlerZero, 1, 1, 0);
        assertHandlerRuntime("handlerOne", handlerOne, 1, 1, 0);
        assertHandlerRuntime("handlerTwo", handlerTwo, 0, 0, 0);
    }

    /**
     * Tests scenario where one handler throws a JAXRPCException
     * to handleRequest(...).
     * <p/>
     * Expected Chain invocation sequence looks like.....
     * H0.handleRequest
     * H1.handleRequest throws JAXRPCException
     *
     * @throws Exception 
     */
    public void testRequestHandlerThrowsJAXRPCException() throws Exception {
        // SETUP THE 2nd HANDLER IN THE REQUEST CHAIN TO THROW JAXRPCException
        handler1Config.put("HANDLE_REQUEST_RETURN_VALUE",
                new JAXRPCException());

        TestHandlerInfoChainFactory factory = buildInfoChainFactory();
        SOAPService soapService = new SOAPService();
        soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
        soapService.setOption(Call.WSDL_SERVICE, new Service());
        soapService.setOption(Call.WSDL_PORT_NAME, new QName("Fake"));
        soapService.init();
        AxisClient client = new AxisClient();
        MessageContext context = new TestMessageContext(client);
        context.setTransportName("local");
        context.setService(soapService);
        
        try {
            client.invoke(context);
        } catch (AxisFault e) {
            AAAHandler handlerZero = factory.getHandlers()[0];
            AAAHandler handlerOne = factory.getHandlers()[1];
            AAAHandler handlerTwo = factory.getHandlers()[2];
            assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0);
            assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 0);
            assertHandlerRuntime("handlerTwo", handlerTwo, 0, 0, 0);
        }
    }

    /**
     * Tests scenario where one handler throws a RuntimeException
     * to handleRequest(...).
     * <p/>
     * Expected Chain invocation sequence looks like.....
     * H0.handleRequest
     * H1.handleRequest throws JAXRPCException
     *
     * @throws Exception 
     */
    public void testRequestHandlerThrowsRuntimeException() throws Exception {
        // SETUP THE 2nd HANDLER IN THE REQUEST CHAIN TO THROW JAXRPCException
        handler1Config.put("HANDLE_REQUEST_RETURN_VALUE",
                new RuntimeException());

        TestHandlerInfoChainFactory factory = buildInfoChainFactory();
        SOAPService soapService = new SOAPService();
        soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
        soapService.setOption(Call.WSDL_SERVICE, new Service());
        soapService.setOption(Call.WSDL_PORT_NAME, new QName("Fake"));
        soapService.init();
        AxisClient client = new AxisClient();
        MessageContext context = new TestMessageContext(client);
        context.setTransportName("local");
        context.setService(soapService);

        try {
            client.invoke(context);
            fail("Expected AxisFault to be thrown");
        } catch (AxisFault e) {
            AAAHandler handlerZero = factory.getHandlers()[0];
            AAAHandler handlerOne = factory.getHandlers()[1];
            AAAHandler handlerTwo = factory.getHandlers()[2];
            assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0);
            assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 0);
            assertHandlerRuntime("handlerTwo", handlerTwo, 0, 0, 0);
        }
    }

    /**
     * Tests scenario where one handler returns false on a call
     * to handleResponse(...).
     * <p/>
     * Expected Chain invocation sequence looks like.....
     * H0.handleRequest
     * H1.handleRequest
     * H2.handleRequest
     * H2.handleResponse return false
     * 
     * @throws Exception 
     */
    public void testResponseHandlerReturnsFalse() throws Exception {
        // SETUP THE 3rd HANDLER IN THE CHAIN TO RETURN FALSE on handleResponse
        handler2Config.put("HANDLE_RESPONSE_RETURN_VALUE", Boolean.FALSE);

        TestHandlerInfoChainFactory factory = buildInfoChainFactory();
        SOAPService soapService = new SOAPService();
        soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
        soapService.setOption(Call.WSDL_SERVICE, new Service());
        soapService.setOption(Call.WSDL_PORT_NAME, new QName("Fake"));
        soapService.init();
        AxisClient client = new AxisClient();
        MessageContext context = new TestMessageContext(client);
        context.setTransportName("local");
        context.setService(soapService);

        client.invoke(context);
        
        AAAHandler handlerZero = factory.getHandlers()[0];
        AAAHandler handlerOne = factory.getHandlers()[1];
        AAAHandler handlerTwo = factory.getHandlers()[2];
        assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0);
        assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 0);
        assertHandlerRuntime("handlerTwo", handlerTwo, 1, 1, 0);
    }

    /**
     * Tests scenario where one handler throws JAXRPCException on a call
     * to handleResponse(...).
     * <p/>
     * Expected Chain invocation sequence looks like.....
     * H0.handleRequest
     * H1.handleRequest
     * H2.handleRequest
     * H2.handleResponse
     * H1.handlerResponse throws JAXRPCException
     * 
     * @throws Exception 
     */
    public void testResponseHandlerThrowsJAXRPCException() throws Exception {
        // SETUP THE 2nd HANDLER IN THE CHAIN TO THROW JAXRPCException on handleResponse
        handler1Config.put("HANDLE_RESPONSE_RETURN_VALUE",
                new JAXRPCException());

        TestHandlerInfoChainFactory factory = buildInfoChainFactory();
        SOAPService soapService = new SOAPService();
        soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
        soapService.setOption(Call.WSDL_SERVICE, new Service());
        soapService.setOption(Call.WSDL_PORT_NAME, new QName("Fake"));
        soapService.init();
        AxisClient client = new AxisClient();
        MessageContext context = new TestMessageContext(client);
        context.setTransportName("local");
        context.setService(soapService);

        try {
            client.invoke(context);
            fail("Expected AxisFault to be thrown");
        } catch (AxisFault e) {
            AAAHandler handlerZero = factory.getHandlers()[0];
            AAAHandler handlerOne = factory.getHandlers()[1];
            AAAHandler handlerTwo = factory.getHandlers()[2];
            assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0);
            assertHandlerRuntime("handlerOne", handlerOne, 1, 1, 0);
            assertHandlerRuntime("handlerTwo", handlerTwo, 1, 1, 0);
        }
    }

    /**
     * Tests scenario where one handler throws RuntimeException on a call
     * to handleResponse(...).
     * <p/>
     * Expected Chain invocation sequence looks like.....
     * H0.handleRequest
     * H1.handleRequest
     * H2.handleRequest
     * H2.handleResponse
     * H1.handlerResponse throws RuntimeException
     * 
     * @throws Exception 
     */
    public void testResponseHandlerThrowsRuntimeException() throws Exception {
        // SETUP THE 2nd HANDLER IN THE CHAIN TO THROW RuntimeException on handleResponse
        handler1Config.put("HANDLE_RESPONSE_RETURN_VALUE",
                new RuntimeException());

        TestHandlerInfoChainFactory factory = buildInfoChainFactory();
        SOAPService soapService = new SOAPService();
        soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
        soapService.setOption(Call.WSDL_SERVICE, new Service());
        soapService.setOption(Call.WSDL_PORT_NAME, new QName("Fake"));
        soapService.init();
        AxisClient client = new AxisClient();
        MessageContext context = new TestMessageContext(client);
        context.setTransportName("local");
        context.setService(soapService);

        try {
            client.invoke(context);
            fail("Expected AxisFault to be thrown");
        } catch (AxisFault e) {
            AAAHandler handlerZero = factory.getHandlers()[0];
            AAAHandler handlerOne = factory.getHandlers()[1];
            AAAHandler handlerTwo = factory.getHandlers()[2];
            assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0);
            assertHandlerRuntime("handlerOne", handlerOne, 1, 1, 0);
            assertHandlerRuntime("handlerTwo", handlerTwo, 1, 1, 0);
        }
    }

    /**
     * Tests scenario where one handler returns false on a call
     * to handleFault(...).
     * <p/>
     * Expected Chain invocation sequence looks like.....
     * H0.handleRequest
     * H1.handleRequest
     * H2.handleRequest
     * H2.handleFault
     * H1.handleFault return false
     * 
     * @throws Exception 
     */
    public void testHandleFaultReturnsFalse() throws Exception {
        // SETUP A MOCK SERVICE THAT SIMULATE A SOAPFAULT THROWN BY ENDPOINT
        handler1Config.put("HANDLE_FAULT_RETURN_VALUE", Boolean.FALSE);

        TestHandlerInfoChainFactory factory = buildInfoChainFactory();
        SOAPService soapService = new SOAPService();
        soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
        soapService.setOption(Call.WSDL_SERVICE, new Service());
        soapService.setOption(Call.WSDL_PORT_NAME, new QName("Fake"));
        soapService.init();
        AxisClient client = new AxisClient();        
        addFaultTransport(client);

        MessageContext context = new TestMessageContext(client);
        context.setTransportName("faulter");
        context.setService(soapService);

        try {
            client.invoke(context);
            fail("Expecting an AxisFault");
        } catch (AxisFault f) {
            AAAHandler handlerZero = factory.getHandlers()[0];
            AAAHandler handlerOne = factory.getHandlers()[1];
            AAAHandler handlerTwo = factory.getHandlers()[2];
            assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0);
            assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 0);
            assertHandlerRuntime("handlerTwo", handlerTwo, 1, 0, 0);
        }
    }

    /**
     * Tests to see if we handle the scenario of a handler throwing a
     * runtime exception during the handleFault(...) processing correctly
     * <p/>
     * Expected chain sequence:
     * H0.handleRequest
     * H1.handleRequest
     * H2.handleRequest
     * H2.handleFault
     * H1.handleFault throws JAXRPCException
     * 
     * @throws Exception 
     */
    public void testFaultHandlerThrowsJAXRPCException() throws Exception {
        handler1Config.put("HANDLE_FAULT_RETURN_VALUE", new JAXRPCException());

        TestHandlerInfoChainFactory factory = buildInfoChainFactory();
        Handler serviceHandler = new MockServiceHandler();
        SOAPService soapService = new SOAPService(null, serviceHandler, null);
        soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
        soapService.setOption(Call.WSDL_SERVICE, new Service());
        soapService.setOption(Call.WSDL_PORT_NAME, new QName("Fake"));
        soapService.init();
        AxisClient client = new AxisClient();        
        addFaultTransport(client);

        MessageContext context = new TestMessageContext(client);
        context.setTransportName("faulter");
        context.setService(soapService);

        try {
            client.invoke(context);
            fail("Expected AxisFault to be thrown");
        } catch (AxisFault e) {
            AAAHandler handlerZero = factory.getHandlers()[0];
            AAAHandler handlerOne = factory.getHandlers()[1];
            AAAHandler handlerTwo = factory.getHandlers()[2];
            assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0);
            assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 0);
            assertHandlerRuntime("handlerTwo", handlerTwo, 1, 0, 0);
        }
    }

    /**
     * Tests to see if we handle the scenario of a handler throwing a
     * runtime exception during the handleFault(...) processing correctly
     * <p/>
     * Expected chain sequence:
     * H0.handleRequest
     * H1.handleRequest
     * H2.handleRequest
     * H2.handleFault
     * H1.handleFault throws RuntimeException
     * 
     * @throws Exception 
     */
    public void testFaultHandlerThrowsRuntimeException() throws Exception {
        // SETUP THE LAST HANDLER IN THE REQUEST CHAIN TO THROW SOAPFaultException
        handler1Config.put("HANDLE_FAULT_RETURN_VALUE", new RuntimeException());

        TestHandlerInfoChainFactory factory = buildInfoChainFactory();
        Handler serviceHandler = new MockServiceHandler();
        SOAPService soapService = new SOAPService(null, serviceHandler, null);
        soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
        soapService.setOption(Call.WSDL_SERVICE, new Service());
        soapService.setOption(Call.WSDL_PORT_NAME, new QName("Fake"));
        soapService.init();
        AxisClient client = new AxisClient();        
        addFaultTransport(client);

        MessageContext context = new TestMessageContext(client);
        context.setTransportName("faulter");
        context.setService(soapService);

        try {
            client.invoke(context);
            fail("Expected AxisFault to be thrown");
        } catch (AxisFault e) {
            AAAHandler handlerZero = factory.getHandlers()[0];
            AAAHandler handlerOne = factory.getHandlers()[1];
            AAAHandler handlerTwo = factory.getHandlers()[2];
            assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0);
            assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 0);
            assertHandlerRuntime("handlerTwo", handlerTwo, 1, 0, 0);
        }
    }

    /**
     * Tests scenario where service object throws Axis Fault.
     * <p/>
     * Expected chain sequence:
     * H0.handleRequest
     * H1.handleRequest
     * H2.handleRequest
     * ServiceObject.invoke() throws AxisFault
     * H2.handleFault
     * H1.handleFault
     * H0.handleFault
     * 
     * @throws Exception 
     */
    public void testServiceObjectThrowsAxisFault() throws Exception {
        TestHandlerInfoChainFactory factory = buildInfoChainFactory();
        Handler serviceHandler = new MockServiceHandler();
        SOAPService soapService = new SOAPService(null, serviceHandler, null);
        soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory);
        soapService.setOption(Call.WSDL_SERVICE, new Service());
        soapService.setOption(Call.WSDL_PORT_NAME, new QName("Fake"));
        soapService.init();
        AxisClient client = new AxisClient();        
        addFaultTransport(client);

        MessageContext context = new TestMessageContext(client);
        context.setTransportName("faulter");
        context.setService(soapService);

        try {
            client.invoke(context);
            fail("Expected AxisFault to be thrown");
        } catch (AxisFault e) {
            AAAHandler handlerZero = factory.getHandlers()[0];
            AAAHandler handlerOne = factory.getHandlers()[1];
            AAAHandler handlerTwo = factory.getHandlers()[2];
            assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0);
            assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 0);
            assertHandlerRuntime("handlerTwo", handlerTwo, 1, 0, 0);
        }
    }
    
    /**
     * Configure a transport handler that simulate a Fault on server-side
     * @param client AxisClient
     */
    private void addFaultTransport(AxisClient client) {

        FileProvider config = (FileProvider) client.getConfig();

        WSDDDeployment depl = config.getDeployment();
        if (depl == null) {
            depl = new WSDDDeployment();
        }
        WSDDTransport trans = new AxisFaultWSDDTransport();
        depl.deployTransport(trans);
        
    }

    private class TestMessageContext extends org.apache.axis.MessageContext {

        public String listByAreaCode = "<soap:Envelope\n" +
                "xmlns:s0=\"http://www.tilisoft.com/ws/LocInfo/literalTypes\"\n" +
                "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
                "xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" +
                "<soap:Header>\n" +
                "<WSABIHeader>\n" +
                "<SubscriptionId>192168001100108165800640600008</SubscriptionId>\n" +
                "</WSABIHeader>\n" +
                "</soap:Header>\n" +
                "<soap:Body>\n" +
                "<s0:ListByAreaCode>\n" +
                "<s0:AreaCode>617</s0:AreaCode>\n" +
                "</s0:ListByAreaCode>\n" +
                "</soap:Body>\n" +
                "</soap:Envelope>\n";

        public TestMessageContext(AxisClient client) {
            super(client);
            Message message = new Message(listByAreaCode);
            message.setMessageType(Message.REQUEST);
            setRequestMessage(message);
        }
    }

}