File: TestClient.java

package info (click to toggle)
axis 1.4-25
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 51,864 kB
  • sloc: java: 129,119; xml: 10,573; jsp: 983; sh: 84; cs: 36; makefile: 26
file content (774 lines) | stat: -rw-r--r-- 24,694 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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
/*
 * Copyright 2001-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package samples.echo ;

import org.apache.axis.AxisFault;
import org.apache.axis.client.Stub;
import org.apache.axis.types.HexBinary;
import org.apache.axis.types.NegativeInteger;
import org.apache.axis.types.NonNegativeInteger;
import org.apache.axis.types.NonPositiveInteger;
import org.apache.axis.types.NormalizedString;
import org.apache.axis.types.PositiveInteger;
import org.apache.axis.types.Token;
import org.apache.axis.types.UnsignedByte;
import org.apache.axis.types.UnsignedInt;
import org.apache.axis.types.UnsignedLong;
import org.apache.axis.types.UnsignedShort;
import org.apache.axis.utils.JavaUtils;
import org.apache.axis.utils.Options;

import javax.xml.rpc.holders.FloatHolder;
import javax.xml.rpc.holders.IntHolder;
import javax.xml.rpc.holders.StringHolder;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;


/**
 * Test Client for the echo interop service.  See the main entrypoint
 * for more details on usage.
 *
 * @author Sam Ruby <rubys@us.ibm.com>
 * Modified to use WSDL2Java generated stubs and artifacts by
 * @author Rich Scheuerle <scheu@us.ibm.com>
 */
public abstract class TestClient {

    private static InteropTestPortType binding = null;

    /**
     * When testMode is true, we throw exceptions up to the caller
     * instead of recording them and continuing.
     */
    private boolean testMode = false;

    public TestClient() {
    }

    /**
     * Constructor which sets testMode
     */
    public TestClient(boolean testMode) {
        this.testMode = testMode;
    }

    /**
     * Determine if two objects are equal.  Handles nulls and recursively
     * verifies arrays are equal.  Accepts dates within a tolerance of
     * 999 milliseconds.
     */
    protected boolean equals(Object obj1, Object obj2) {
       if (obj1 == null || obj2 == null) return (obj1 == obj2);
       if (obj1.equals(obj2)) return true;

       // For comparison purposes, get the array of bytes representing
        // the HexBinary object.
       if (obj1 instanceof HexBinary) {
           obj1 = ((HexBinary) obj1).getBytes();
       }
       if (obj2 instanceof HexBinary) {
           obj2 = ((HexBinary) obj2).getBytes();
       }

       if (obj1 instanceof Calendar && obj2 instanceof Calendar) {
           if (Math.abs(((Calendar)obj1).getTime().getTime() - ((Calendar)obj2).getTime().getTime()) < 1000) {
               return true;
           }
       }

       if ((obj1 instanceof Map) && (obj2 instanceof Map)) {
           Map map1 = (Map)obj1;
           Map map2 = (Map)obj2;
           Set keys1 = map1.keySet();
           Set keys2 = map2.keySet();
           if (!(keys1.equals(keys2))) return false;

           // Check map1 is a subset of map2.
           Iterator i = keys1.iterator();
           while (i.hasNext()) {
               Object key = i.next();
               if (!equals(map1.get(key), map2.get(key)))
                   return false;
           }

           // Check map2 is a subset of map1.
           Iterator j = keys2.iterator();
           while (j.hasNext()) {
               Object key = j.next();
               if (!equals(map1.get(key), map2.get(key)))
                   return false;
           }
           return true;
       }

       if (obj1 instanceof List)
         obj1 = JavaUtils.convert(obj1, Object[].class);
       if (obj2 instanceof List)
         obj2 = JavaUtils.convert(obj2, Object[].class);

       if (!obj2.getClass().isArray()) return false;
       if (!obj1.getClass().isArray()) return false;
       if (Array.getLength(obj1) != Array.getLength(obj2)) return false;
       for (int i=0; i<Array.getLength(obj1); i++)
           if (!equals(Array.get(obj1,i),Array.get(obj2,i))) return false;
       return true;
    }

    /**
     * Set up the call object.
     */
    public void setURL(String url)
        throws AxisFault
    {
        try {
            binding = new InteropTestServiceLocator().
                getecho(new java.net.URL(url));

            // safety first
            ((InteropTestSoapBindingStub)binding).setTimeout(60000);
            ((InteropTestSoapBindingStub)binding).setMaintainSession(true);
        } catch (Exception exp) {
            throw AxisFault.makeFault(exp);
        }
    }

    void setUser(String user) {
        ((Stub)binding).setUsername(user);
    }

    void setPassword(String password) {
        ((Stub)binding).setPassword(password);        
    }

    /**
     * Execute all tests.
     */
    public void executeAll() throws Exception {
        execute2A();
        execute2B();
        executeAxisXSD();
    }

    /**
     * Test custom mapping of xsd types not standardized:  xsd:token and
     * xsd:normalizedString.
     */
    public void executeAxisXSD() throws Exception {
        Object output = null;

        // Test xsd:token
        Token tInput = new Token("abccdefg");
        try {
            output = binding.echoToken(tInput);
            verify("echoToken", tInput, output);
        } catch (Exception e) {
            if (!testMode) {
                verify("echoToken", tInput, e);
            } else {
                throw e;
            }
        }

        // Test xsd:normalizedString
        NormalizedString nsInput = new NormalizedString("abccdefg");
        try {
            output = binding.echoNormalizedString(nsInput);
            verify("echoNormalizedString", nsInput, output);
        } catch (Exception e) {
            if (!testMode) {
                verify("echoNormalizedString", nsInput, e);
            } else {
                throw e;
            }
        }

        // Test xsd:unsignedLong
        UnsignedLong ulInput = new UnsignedLong(100);
        try {
            output = binding.echoUnsignedLong(ulInput);
            verify("echoUnsignedLong", ulInput, output);
        } catch (Exception e) {
            if (!testMode) {
                verify("echoUnsignedLong", ulInput, e);
            } else {
                throw e;
            }
        }

        // Test xsd:unsignedInt
        UnsignedInt uiInput = new UnsignedInt(101);
        try {
            output = binding.echoUnsignedInt(uiInput);
            verify("echoUnsignedInt", uiInput, output);
        } catch (Exception e) {
            if (!testMode) {
                verify("echoUnsignedInt", uiInput, e);
            } else {
                throw e;
            }
        }

        // Test xsd:unsignedShort
        UnsignedShort usInput = new UnsignedShort(102);
        try {
            output = binding.echoUnsignedShort(usInput);
            verify("echoUnsignedShort", usInput, output);
        } catch (Exception e) {
            if (!testMode) {
                verify("echoUnsignedShort", usInput, e);
            } else {
                throw e;
            }
        }

        // Test xsd:unsignedByte
        UnsignedByte ubInput = new UnsignedByte(103);
        try {
            output = binding.echoUnsignedByte(ubInput);
            verify("echoUnsignedByte", ubInput, output);
        } catch (Exception e) {
            if (!testMode) {
                verify("echoUnsignedByte", ubInput, e);
            } else {
                throw e;
            }
        }

        // Test xsd:nonNegativeInteger
        NonNegativeInteger nniInput = new NonNegativeInteger("12345678901234567890");
        try {
            output = binding.echoNonNegativeInteger(nniInput);
            verify("echoNonNegativeInteger", nniInput, output);
        } catch (Exception e) {
            if (!testMode) {
                verify("echoNonNegativeInteger", nniInput, e);
            } else {
                throw e;
            }
        }

        // Test xsd:positiveInteger
        PositiveInteger piInput = new PositiveInteger("12345678901234567890");
        try {
            output = binding.echoPositiveInteger(piInput);
            verify("echoPositiveInteger", piInput, output);
        } catch (Exception e) {
            if (!testMode) {
                verify("echoPositiveInteger", piInput, e);
            } else {
                throw e;
            }
        }

        // Test xsd:nonPositiveInteger
        NonPositiveInteger npiInput = new NonPositiveInteger("-12345678901234567890");
        try {
            output = binding.echoNonPositiveInteger(npiInput);
            verify("echoNonPositiveInteger", npiInput, output);
        } catch (Exception e) {
            if (!testMode) {
                verify("echoNonPositiveInteger", npiInput, e);
            } else {
                throw e;
            }
        }
        
        // Test xsd:negativeInteger
        NegativeInteger niInput = new NegativeInteger("-12345678901234567890");
        try {
            output = binding.echoNegativeInteger(niInput);
            verify("echoNegativeInteger", niInput, output);
        } catch (Exception e) {
            if (!testMode) {
                verify("echoNegativeInteger", niInput, e);
            } else {
                throw e;
            }
        }

    }

    /**
     * Execute the 2A tests
     */
    public void execute2A() throws Exception {
        // execute the tests
        Object output = null;

        {
            String input = "abccdefg";
            try {
                output = binding.echoString(input);
                verify("echoString", input, output);
            } catch (Exception e) {
                if (!testMode) {
                    verify("echoString", input, e);
                } else {
                    throw e;
                }
            }
        }

        {
            String[] input = new String[] {"abc", "def"};
            try {
                output = binding.echoStringArray(input);
                verify("echoStringArray", input, output);
            } catch (Exception e) {
                if (!testMode) {
                    verify("echoStringArray", input, e);
                } else {
                    throw e;
                }
            }
        }

        {
            Integer input = new Integer(42);
            try {
                output = new Integer( binding.echoInteger(input.intValue()));
                verify("echoInteger", input, output);
            } catch (Exception e) {
                if (!testMode) {
                    verify("echoInteger", input, e);
                } else {
                    throw e;
                }
            }
        }

        {
            int[] input = new int[] {42};
            try {
                output = binding.echoIntegerArray(input);
                verify("echoIntegerArray", input, output);
            } catch (Exception e) {
                if (!testMode) {
                    verify("echoIntegerArray", input, e);
                } else {
                    throw e;
                }
            }
        }

        {
            Float input = new Float(3.7F);
            try {
                output = new Float(binding.echoFloat(input.floatValue()));
                verify("echoFloat", input, output);
            } catch (Exception e) {
                if (!testMode) {
                    verify("echoFloat", input, e);
                } else {
                    throw e;
                }
            }
        }

        {
            float[] input = new float[] {3.7F, 7F};
            try {
                output = binding.echoFloatArray(input);
                verify("echoFloatArray", input, output);
            } catch (Exception e) {
                if (!testMode) {
                    verify("echoFloatArray", input, e);
                } else {
                    throw e;
                }
            }
        }

        {
            SOAPStruct input = new SOAPStruct();
            input.setVarInt(5);
            input.setVarString("Hello");
            input.setVarFloat(103F);
            try {
                output = binding.echoStruct(input);
                verify("echoStruct", input, output);
            } catch (Exception e) {
                if (!testMode) {
                    verify("echoStruct", input, e);
                } else {
                    throw e;
                }
            }
        }

        {
            SOAPStruct[] input = new SOAPStruct[] {
                new SOAPStruct(),
                new SOAPStruct(),
                new SOAPStruct()};
            input[0].setVarInt(1);
            input[0].setVarString("one");
            input[0].setVarFloat(1.1F);
            input[1].setVarInt(2);
            input[1].setVarString("two");
            input[1].setVarFloat(2.2F);
            input[2].setVarInt(3);
            input[2].setVarString("three");
            input[2].setVarFloat(3.3F);

            try {
                output = binding.echoStructArray(input);
                verify("echoStructArray", input, output);
            } catch (Exception e) {
                if (!testMode) {
                    verify("echoStructArray", input, e);
                } else {
                    throw e;
                }
            }
        }

        {
            try {
                binding.echoVoid();
                verify("echoVoid", null, null);
            } catch (Exception e) {
                if (!testMode) {
                    verify("echoVoid", null, e);
                } else {
                    throw e;
                }
            }
        }

        {
            byte[] input = "Base64".getBytes();
            try {
                output = binding.echoBase64(input);
                verify("echoBase64", input, output);
            } catch (Exception e) {
                if (!testMode) {
                    verify("echoBase64", input, e);
                } else {
                    throw e;
                }
            }
        }

        {
            HexBinary input = new HexBinary("3344");
            try {
                output = binding.echoHexBinary(input.getBytes());
                verify("echoHexBinary", input, output);
            } catch (Exception e) {
                if (!testMode) {
                    verify("echoHexBinary", input, e);
                } else {
                    throw e;
                }
            }
        }
        Calendar inputDate = Calendar.getInstance();
        inputDate.setTimeZone(TimeZone.getTimeZone("GMT"));
        inputDate.setTime(new Date());
        {
            try {
                output = binding.echoDate(inputDate);
                verify("echoDate", inputDate, output);
            } catch (Exception e) {
                if (!testMode) {
                    verify("echoDate", inputDate, e);
                } else {
                    throw e;
                }
            }
        }

        {
            BigDecimal input = new BigDecimal("3.14159");
            try {
                output = binding.echoDecimal(input);
                verify("echoDecimal", input, output);
            } catch (Exception e) {
                if (!testMode) {
                    verify("echoDecimal", input, e);
                } else {
                    throw e;
                }
            }
        }

        {
            Boolean input = Boolean.TRUE;
            try {
                output = new Boolean( binding.echoBoolean(input.booleanValue()));
                verify("echoBoolean", input, output);
            } catch (Exception e) {
                if (!testMode) {
                    verify("echoBoolean", input, e);
                } else {
                    throw e;
                }
            }
        }

        HashMap map = new HashMap();
        map.put(new Integer(5), "String Value");
        map.put("String Key", inputDate);
        {
            HashMap input = map;
            try {
                output = binding.echoMap(input);
                verify("echoMap", input, output);
            } catch (Exception e) {
                if (!testMode) {
                    verify("echoMap", input, e);
                } else {
                    throw e;
                }
            }
        }

        HashMap map2 = new HashMap();
        map2.put("this is the second map", new Boolean(true));
        map2.put("test", new Float(411));
        {
            HashMap[] input = new HashMap [] {map, map2 };
            try {
                output = binding.echoMapArray(input);
                verify("echoMapArray", input, output);
            } catch (Exception e) {
                if (!testMode) {
                    verify("echoMapArray", input, e);
                } else {
                    throw e;
                }
            }
        }
    }

    /**
     * Execute the 2B tests
     */
    public void execute2B() throws Exception {
        // execute the tests
        Object output = null;
        {
            SOAPStruct input = new SOAPStruct();
            input.setVarInt(5);
            input.setVarString("Hello");
            input.setVarFloat(103F);
            try {
                StringHolder outputString = new StringHolder();
                IntHolder outputInteger = new IntHolder();
                FloatHolder outputFloat = new FloatHolder();
                binding.echoStructAsSimpleTypes(input, outputString,
                                                 outputInteger, outputFloat);
                output = new SOAPStruct();
                ((SOAPStruct)output).setVarInt(outputInteger.value);
                ((SOAPStruct)output).setVarString(outputString.value);
                ((SOAPStruct)output).setVarFloat(outputFloat.value);
                verify("echoStructAsSimpleTypes",
                       input, output);
            } catch (Exception e) {
                if (!testMode) {
                    verify("echoStructAsSimpleTypes", input, e);
                } else {
                    throw e;
                }
            }
        }

        {
            SOAPStruct input = new SOAPStruct();
            input.setVarInt(5);
            input.setVarString("Hello");
            input.setVarFloat(103F);
            try {
                output = binding.echoSimpleTypesAsStruct(
                   input.getVarString(), input.getVarInt(), input.getVarFloat());
                verify("echoSimpleTypesAsStruct",
                       input,
                       output);
            } catch (Exception e) {
                if (!testMode) {
                    verify("echoSimpleTypesAsStruct", input, e);
                } else {
                    throw e;
                }
            }
        }

        {
            String[][] input = new String[2][2];
            input[0][0] = "00";
            input[0][1] = "01";
            input[1][0] = "10";
            input[1][1] = "11";
            try {
                output = binding.echo2DStringArray(input);
                verify("echo2DStringArray",
                       input,
                       output);
            } catch (Exception e) {
                if (!testMode) {
                    verify("echo2DStringArray", input, e);
                } else {
                    throw e;
                }
            }
        }

        {
            SOAPStruct inputS =new SOAPStruct();
            inputS.setVarInt(5);
            inputS.setVarString("Hello");
            inputS.setVarFloat(103F);
            SOAPStructStruct input = new SOAPStructStruct();
            input.setVarString("AXIS");
            input.setVarInt(1);
            input.setVarFloat(3F);
            input.setVarStruct(inputS);
            try {
                output = binding.echoNestedStruct(input);
                verify("echoNestedStruct", input, output);
            } catch (Exception e) {
                if (!testMode) {
                    verify("echoNestedStruct", input, e);
                } else {
                    throw e;
                }
            }
        }
        {
            SOAPArrayStruct input = new SOAPArrayStruct();
            input.setVarString("AXIS");
            input.setVarInt(1);
            input.setVarFloat(3F);
            input.setVarArray(new String[] {"one", "two", "three"});
            try {
                output = binding.echoNestedArray(input);
                verify("echoNestedArray", input, output);
            } catch (Exception e) {
                if (!testMode) {
                    verify("echoNestedArray", input, e);
                } else {
                    throw e;
                }
            }
        }
    }

    /**
     * Verify that the object sent was, indeed, the one you got back.
     * Subclasses are sent to override this with their own output.
     */
    protected abstract void verify(String method, Object sent, Object gotBack);

    /**
     * Main entry point.  Tests a variety of echo methods and reports
     * on their results.
     *
     * Arguments are of the form:
     *   -h localhost -p 8080 -s /soap/servlet/rpcrouter
     * -h indicats the host
     */
    public static void main(String args[]) throws Exception {
        Options opts = new Options(args);

        boolean testPerformance = opts.isFlagSet('k') > 0;
        boolean allTests = opts.isFlagSet('A') > 0;
        boolean onlyB    = opts.isFlagSet('b') > 0;
        boolean testMode = opts.isFlagSet('t') > 0;

        // set up tests so that the results are sent to System.out
        TestClient client;

        if (testPerformance) {
            client = new TestClient(testMode) {
               public void verify(String method, Object sent, Object gotBack) {
               }
            };
        } else {
            client = new TestClient(testMode) {
            public void verify(String method, Object sent, Object gotBack) {
                String message;
                if (this.equals(sent, gotBack)) {
                    message = "OK";
                } else {
                    if (gotBack instanceof Exception) {
                        if (gotBack instanceof AxisFault) {
                            message = "Fault: " +
                                ((AxisFault)gotBack).getFaultString();
                        } else {
                            StringWriter sw = new StringWriter();
                            PrintWriter pw = new PrintWriter(sw);
                            message = "Exception: ";
                            ((Exception)gotBack).printStackTrace(pw);
                            message += sw.getBuffer().toString();
                        }
                    } else {
                        message = "Fail:" + gotBack + " expected " + sent;
                    }
                }
                // Line up the output
                String tab = "";
                int l = method.length();
                while (l < 25) {
                    tab += " ";
                    l++;
                }
                System.out.println(method + tab + " " + message);
            }
        };
        }

        // set up the call object
        client.setURL(opts.getURL());
        client.setUser(opts.getUser());
        client.setPassword(opts.getPassword());

        if (testPerformance) {
            long startTime = System.currentTimeMillis();
            for (int i = 0; i < 10; i++) {
                if (allTests) {
                    client.executeAll();
                } else if (onlyB) {
                    client.execute2B();
                } else {
                    client.execute2A();
                }
            }
            long stopTime = System.currentTimeMillis();
            System.out.println("That took " + (stopTime - startTime) + " milliseconds");
        } else {
            if (allTests) {
                client.executeAll();
            } else if (onlyB) {
                client.execute2B();
            } else {
                client.execute2A();
            }
        }
    }
}