Debian Codegraph @

samples/echo/TestClient.java

raw
1 /*
2 * Copyright 2001-2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package samples.echo ;
18
19 import org.apache.axis.AxisFault;
20 import org.apache.axis.client.Stub;
21 import org.apache.axis.types.HexBinary;
22 import org.apache.axis.types.NegativeInteger;
23 import org.apache.axis.types.NonNegativeInteger;
24 import org.apache.axis.types.NonPositiveInteger;
25 import org.apache.axis.types.NormalizedString;
26 import org.apache.axis.types.PositiveInteger;
27 import org.apache.axis.types.Token;
28 import org.apache.axis.types.UnsignedByte;
29 import org.apache.axis.types.UnsignedInt;
30 import org.apache.axis.types.UnsignedLong;
31 import org.apache.axis.types.UnsignedShort;
32 import org.apache.axis.utils.JavaUtils;
33 import org.apache.axis.utils.Options;
34
35 import javax.xml.rpc.holders.FloatHolder;
36 import javax.xml.rpc.holders.IntHolder;
37 import javax.xml.rpc.holders.StringHolder;
38 import java.io.PrintWriter;
39 import java.io.StringWriter;
40 import java.lang.reflect.Array;
41 import java.math.BigDecimal;
42 import java.util.Calendar;
43 import java.util.Date;
44 import java.util.HashMap;
45 import java.util.Iterator;
46 import java.util.List;
47 import java.util.Map;
48 import java.util.Set;
49 import java.util.TimeZone;
50
51
52 /**
53 * Test Client for the echo interop service. See the main entrypoint
54 * for more details on usage.
55 *
56 * @author Sam Ruby <[email protected]>
57 * Modified to use WSDL2Java generated stubs and artifacts by
58 * @author Rich Scheuerle <[email protected]>
59 */
60 public abstract class TestClient {
61
62 private static InteropTestPortType binding = null;
63
64 /**
65 * When testMode is true, we throw exceptions up to the caller
66 * instead of recording them and continuing.
67 */
68 private boolean testMode = false;
69
70 public TestClient() {
71 }
72
73 /**
74 * Constructor which sets testMode
75 */
76 public TestClient(boolean testMode) {
77 this.testMode = testMode;
78 }
79
80 /**
81 * Determine if two objects are equal. Handles nulls and recursively
82 * verifies arrays are equal. Accepts dates within a tolerance of
83 * 999 milliseconds.
84 */
85 protected boolean equals(Object obj1, Object obj2) {
86 if (obj1 == null || obj2 == null) return (obj1 == obj2);
87 if (obj1.equals(obj2)) return true;
88
89 // For comparison purposes, get the array of bytes representing
90 // the HexBinary object.
91 if (obj1 instanceof HexBinary) {
92 obj1 = ((HexBinary) obj1).getBytes();
93 }
94 if (obj2 instanceof HexBinary) {
95 obj2 = ((HexBinary) obj2).getBytes();
96 }
97
98 if (obj1 instanceof Calendar && obj2 instanceof Calendar) {
99 if (Math.abs(((Calendar)obj1).getTime().getTime() - ((Calendar)obj2).getTime().getTime()) < 1000) {
100 return true;
101 }
102 }
103
104 if ((obj1 instanceof Map) && (obj2 instanceof Map)) {
105 Map map1 = (Map)obj1;
106 Map map2 = (Map)obj2;
107 Set keys1 = map1.keySet();
108 Set keys2 = map2.keySet();
109 if (!(keys1.equals(keys2))) return false;
110
111 // Check map1 is a subset of map2.
112 Iterator i = keys1.iterator();
113 while (i.hasNext()) {
114 Object key = i.next();
115 if (!equals(map1.get(key), map2.get(key)))
116 return false;
117 }
118
119 // Check map2 is a subset of map1.
120 Iterator j = keys2.iterator();
121 while (j.hasNext()) {
122 Object key = j.next();
123 if (!equals(map1.get(key), map2.get(key)))
124 return false;
125 }
126 return true;
127 }
128
129 if (obj1 instanceof List)
130 obj1 = JavaUtils.convert(obj1, Object[].class);
131 if (obj2 instanceof List)
132 obj2 = JavaUtils.convert(obj2, Object[].class);
133
134 if (!obj2.getClass().isArray()) return false;
135 if (!obj1.getClass().isArray()) return false;
136 if (Array.getLength(obj1) != Array.getLength(obj2)) return false;
137 for (int i=0; i<Array.getLength(obj1); i++)
138 if (!equals(Array.get(obj1,i),Array.get(obj2,i))) return false;
139 return true;
140 }
141
142 /**
143 * Set up the call object.
144 */
145 public void setURL(String url)
146 throws AxisFault
147 {
148 try {
149 binding = new InteropTestServiceLocator().
150 getecho(new java.net.URL(url));
151
152 // safety first
153 ((InteropTestSoapBindingStub)binding).setTimeout(60000);
154 ((InteropTestSoapBindingStub)binding).setMaintainSession(true);
155 } catch (Exception exp) {
156 throw AxisFault.makeFault(exp);
157 }
158 }
159
160 void setUser(String user) {
161 ((Stub)binding).setUsername(user);
162 }
163
164 void setPassword(String password) {
165 ((Stub)binding).setPassword(password);
166 }
167
168 /**
169 * Execute all tests.
170 */
171 public void executeAll() throws Exception {
172 execute2A();
173 execute2B();
174 executeAxisXSD();
175 }
176
177 /**
178 * Test custom mapping of xsd types not standardized: xsd:token and
179 * xsd:normalizedString.
180 */
181 public void executeAxisXSD() throws Exception {
182 Object output = null;
183
184 // Test xsd:token
185 Token tInput = new Token("abccdefg");
186 try {
187 output = binding.echoToken(tInput);
188 verify("echoToken", tInput, output);
189 } catch (Exception e) {
190 if (!testMode) {
191 verify("echoToken", tInput, e);
192 } else {
193 throw e;
194 }
195 }
196
197 // Test xsd:normalizedString
198 NormalizedString nsInput = new NormalizedString("abccdefg");
199 try {
200 output = binding.echoNormalizedString(nsInput);
201 verify("echoNormalizedString", nsInput, output);
202 } catch (Exception e) {
203 if (!testMode) {
204 verify("echoNormalizedString", nsInput, e);
205 } else {
206 throw e;
207 }
208 }
209
210 // Test xsd:unsignedLong
211 UnsignedLong ulInput = new UnsignedLong(100);
212 try {
213 output = binding.echoUnsignedLong(ulInput);
214 verify("echoUnsignedLong", ulInput, output);
215 } catch (Exception e) {
216 if (!testMode) {
217 verify("echoUnsignedLong", ulInput, e);
218 } else {
219 throw e;
220 }
221 }
222
223 // Test xsd:unsignedInt
224 UnsignedInt uiInput = new UnsignedInt(101);
225 try {
226 output = binding.echoUnsignedInt(uiInput);
227 verify("echoUnsignedInt", uiInput, output);
228 } catch (Exception e) {
229 if (!testMode) {
230 verify("echoUnsignedInt", uiInput, e);
231 } else {
232 throw e;
233 }
234 }
235
236 // Test xsd:unsignedShort
237 UnsignedShort usInput = new UnsignedShort(102);
238 try {
239 output = binding.echoUnsignedShort(usInput);
240 verify("echoUnsignedShort", usInput, output);
241 } catch (Exception e) {
242 if (!testMode) {
243 verify("echoUnsignedShort", usInput, e);
244 } else {
245 throw e;
246 }
247 }
248
249 // Test xsd:unsignedByte
250 UnsignedByte ubInput = new UnsignedByte(103);
251 try {
252 output = binding.echoUnsignedByte(ubInput);
253 verify("echoUnsignedByte", ubInput, output);
254 } catch (Exception e) {
255 if (!testMode) {
256 verify("echoUnsignedByte", ubInput, e);
257 } else {
258 throw e;
259 }
260 }
261
262 // Test xsd:nonNegativeInteger
263 NonNegativeInteger nniInput = new NonNegativeInteger("12345678901234567890");
264 try {
265 output = binding.echoNonNegativeInteger(nniInput);
266 verify("echoNonNegativeInteger", nniInput, output);
267 } catch (Exception e) {
268 if (!testMode) {
269 verify("echoNonNegativeInteger", nniInput, e);
270 } else {
271 throw e;
272 }
273 }
274
275 // Test xsd:positiveInteger
276 PositiveInteger piInput = new PositiveInteger("12345678901234567890");
277 try {
278 output = binding.echoPositiveInteger(piInput);
279 verify("echoPositiveInteger", piInput, output);
280 } catch (Exception e) {
281 if (!testMode) {
282 verify("echoPositiveInteger", piInput, e);
283 } else {
284 throw e;
285 }
286 }
287
288 // Test xsd:nonPositiveInteger
289 NonPositiveInteger npiInput = new NonPositiveInteger("-12345678901234567890");
290 try {
291 output = binding.echoNonPositiveInteger(npiInput);
292 verify("echoNonPositiveInteger", npiInput, output);
293 } catch (Exception e) {
294 if (!testMode) {
295 verify("echoNonPositiveInteger", npiInput, e);
296 } else {
297 throw e;
298 }
299 }
300
301 // Test xsd:negativeInteger
302 NegativeInteger niInput = new NegativeInteger("-12345678901234567890");
303 try {
304 output = binding.echoNegativeInteger(niInput);
305 verify("echoNegativeInteger", niInput, output);
306 } catch (Exception e) {
307 if (!testMode) {
308 verify("echoNegativeInteger", niInput, e);
309 } else {
310 throw e;
311 }
312 }
313
314 }
315
316 /**
317 * Execute the 2A tests
318 */
319 public void execute2A() throws Exception {
320 // execute the tests
321 Object output = null;
322
323 {
324 String input = "abccdefg";
325 try {
326 output = binding.echoString(input);
327 verify("echoString", input, output);
328 } catch (Exception e) {
329 if (!testMode) {
330 verify("echoString", input, e);
331 } else {
332 throw e;
333 }
334 }
335 }
336
337 {
338 String[] input = new String[] {"abc", "def"};
339 try {
340 output = binding.echoStringArray(input);
341 verify("echoStringArray", input, output);
342 } catch (Exception e) {
343 if (!testMode) {
344 verify("echoStringArray", input, e);
345 } else {
346 throw e;
347 }
348 }
349 }
350
351 {
352 Integer input = new Integer(42);
353 try {
354 output = new Integer( binding.echoInteger(input.intValue()));
355 verify("echoInteger", input, output);
356 } catch (Exception e) {
357 if (!testMode) {
358 verify("echoInteger", input, e);
359 } else {
360 throw e;
361 }
362 }
363 }
364
365 {
366 int[] input = new int[] {42};
367 try {
368 output = binding.echoIntegerArray(input);
369 verify("echoIntegerArray", input, output);
370 } catch (Exception e) {
371 if (!testMode) {
372 verify("echoIntegerArray", input, e);
373 } else {
374 throw e;
375 }
376 }
377 }
378
379 {
380 Float input = new Float(3.7F);
381 try {
382 output = new Float(binding.echoFloat(input.floatValue()));
383 verify("echoFloat", input, output);
384 } catch (Exception e) {
385 if (!testMode) {
386 verify("echoFloat", input, e);
387 } else {
388 throw e;
389 }
390 }
391 }
392
393 {
394 float[] input = new float[] {3.7F, 7F};
395 try {
396 output = binding.echoFloatArray(input);
397 verify("echoFloatArray", input, output);
398 } catch (Exception e) {
399 if (!testMode) {
400 verify("echoFloatArray", input, e);
401 } else {
402 throw e;
403 }
404 }
405 }
406
407 {
408 SOAPStruct input = new SOAPStruct();
409 input.setVarInt(5);
410 input.setVarString("Hello");
411 input.setVarFloat(103F);
412 try {
413 output = binding.echoStruct(input);
414 verify("echoStruct", input, output);
415 } catch (Exception e) {
416 if (!testMode) {
417 verify("echoStruct", input, e);
418 } else {
419 throw e;
420 }
421 }
422 }
423
424 {
425 SOAPStruct[] input = new SOAPStruct[] {
426 new SOAPStruct(),
427 new SOAPStruct(),
428 new SOAPStruct()};
429 input[0].setVarInt(1);
430 input[0].setVarString("one");
431 input[0].setVarFloat(1.1F);
432 input[1].setVarInt(2);
433 input[1].setVarString("two");
434 input[1].setVarFloat(2.2F);
435 input[2].setVarInt(3);
436 input[2].setVarString("three");
437 input[2].setVarFloat(3.3F);
438
439 try {
440 output = binding.echoStructArray(input);
441 verify("echoStructArray", input, output);
442 } catch (Exception e) {
443 if (!testMode) {
444 verify("echoStructArray", input, e);
445 } else {
446 throw e;
447 }
448 }
449 }
450
451 {
452 try {
453 binding.echoVoid();
454 verify("echoVoid", null, null);
455 } catch (Exception e) {
456 if (!testMode) {
457 verify("echoVoid", null, e);
458 } else {
459 throw e;
460 }
461 }
462 }
463
464 {
465 byte[] input = "Base64".getBytes();
466 try {
467 output = binding.echoBase64(input);
468 verify("echoBase64", input, output);
469 } catch (Exception e) {
470 if (!testMode) {
471 verify("echoBase64", input, e);
472 } else {
473 throw e;
474 }
475 }
476 }
477
478 {
479 HexBinary input = new HexBinary("3344");
480 try {
481 output = binding.echoHexBinary(input.getBytes());
482 verify("echoHexBinary", input, output);
483 } catch (Exception e) {
484 if (!testMode) {
485 verify("echoHexBinary", input, e);
486 } else {
487 throw e;
488 }
489 }
490 }
491 Calendar inputDate = Calendar.getInstance();
492 inputDate.setTimeZone(TimeZone.getTimeZone("GMT"));
493 inputDate.setTime(new Date());
494 {
495 try {
496 output = binding.echoDate(inputDate);
497 verify("echoDate", inputDate, output);
498 } catch (Exception e) {
499 if (!testMode) {
500 verify("echoDate", inputDate, e);
501 } else {
502 throw e;
503 }
504 }
505 }
506
507 {
508 BigDecimal input = new BigDecimal("3.14159");
509 try {
510 output = binding.echoDecimal(input);
511 verify("echoDecimal", input, output);
512 } catch (Exception e) {
513 if (!testMode) {
514 verify("echoDecimal", input, e);
515 } else {
516 throw e;
517 }
518 }
519 }
520
521 {
522 Boolean input = Boolean.TRUE;
523 try {
524 output = new Boolean( binding.echoBoolean(input.booleanValue()));
525 verify("echoBoolean", input, output);
526 } catch (Exception e) {
527 if (!testMode) {
528 verify("echoBoolean", input, e);
529 } else {
530 throw e;
531 }
532 }
533 }
534
535 HashMap map = new HashMap();
536 map.put(new Integer(5), "String Value");
537 map.put("String Key", inputDate);
538 {
539 HashMap input = map;
540 try {
541 output = binding.echoMap(input);
542 verify("echoMap", input, output);
543 } catch (Exception e) {
544 if (!testMode) {
545 verify("echoMap", input, e);
546 } else {
547 throw e;
548 }
549 }
550 }
551
552 HashMap map2 = new HashMap();
553 map2.put("this is the second map", new Boolean(true));
554 map2.put("test", new Float(411));
555 {
556 HashMap[] input = new HashMap [] {map, map2 };
557 try {
558 output = binding.echoMapArray(input);
559 verify("echoMapArray", input, output);
560 } catch (Exception e) {
561 if (!testMode) {
562 verify("echoMapArray", input, e);
563 } else {
564 throw e;
565 }
566 }
567 }
568 }
569
570 /**
571 * Execute the 2B tests
572 */
573 public void execute2B() throws Exception {
574 // execute the tests
575 Object output = null;
576 {
577 SOAPStruct input = new SOAPStruct();
578 input.setVarInt(5);
579 input.setVarString("Hello");
580 input.setVarFloat(103F);
581 try {
582 StringHolder outputString = new StringHolder();
583 IntHolder outputInteger = new IntHolder();
584 FloatHolder outputFloat = new FloatHolder();
585 binding.echoStructAsSimpleTypes(input, outputString,
586 outputInteger, outputFloat);
587 output = new SOAPStruct();
588 ((SOAPStruct)output).setVarInt(outputInteger.value);
589 ((SOAPStruct)output).setVarString(outputString.value);
590 ((SOAPStruct)output).setVarFloat(outputFloat.value);
591 verify("echoStructAsSimpleTypes",
592 input, output);
593 } catch (Exception e) {
594 if (!testMode) {
595 verify("echoStructAsSimpleTypes", input, e);
596 } else {
597 throw e;
598 }
599 }
600 }
601
602 {
603 SOAPStruct input = new SOAPStruct();
604 input.setVarInt(5);
605 input.setVarString("Hello");
606 input.setVarFloat(103F);
607 try {
608 output = binding.echoSimpleTypesAsStruct(
609 input.getVarString(), input.getVarInt(), input.getVarFloat());
610 verify("echoSimpleTypesAsStruct",
611 input,
612 output);
613 } catch (Exception e) {
614 if (!testMode) {
615 verify("echoSimpleTypesAsStruct", input, e);
616 } else {
617 throw e;
618 }
619 }
620 }
621
622 {
623 String[][] input = new String[2][2];
624 input[0][0] = "00";
625 input[0][1] = "01";
626 input[1][0] = "10";
627 input[1][1] = "11";
628 try {
629 output = binding.echo2DStringArray(input);
630 verify("echo2DStringArray",
631 input,
632 output);
633 } catch (Exception e) {
634 if (!testMode) {
635 verify("echo2DStringArray", input, e);
636 } else {
637 throw e;
638 }
639 }
640 }
641
642 {
643 SOAPStruct inputS =new SOAPStruct();
644 inputS.setVarInt(5);
645 inputS.setVarString("Hello");
646 inputS.setVarFloat(103F);
647 SOAPStructStruct input = new SOAPStructStruct();
648 input.setVarString("AXIS");
649 input.setVarInt(1);
650 input.setVarFloat(3F);
651 input.setVarStruct(inputS);
652 try {
653 output = binding.echoNestedStruct(input);
654 verify("echoNestedStruct", input, output);
655 } catch (Exception e) {
656 if (!testMode) {
657 verify("echoNestedStruct", input, e);
658 } else {
659 throw e;
660 }
661 }
662 }
663 {
664 SOAPArrayStruct input = new SOAPArrayStruct();
665 input.setVarString("AXIS");
666 input.setVarInt(1);
667 input.setVarFloat(3F);
668 input.setVarArray(new String[] {"one", "two", "three"});
669 try {
670 output = binding.echoNestedArray(input);
671 verify("echoNestedArray", input, output);
672 } catch (Exception e) {
673 if (!testMode) {
674 verify("echoNestedArray", input, e);
675 } else {
676 throw e;
677 }
678 }
679 }
680 }
681
682 /**
683 * Verify that the object sent was, indeed, the one you got back.
684 * Subclasses are sent to override this with their own output.
685 */
686 protected abstract void verify(String method, Object sent, Object gotBack);
687
688 /**
689 * Main entry point. Tests a variety of echo methods and reports
690 * on their results.
691 *
692 * Arguments are of the form:
693 * -h localhost -p 8080 -s /soap/servlet/rpcrouter
694 * -h indicats the host
695 */
696 public static void main(String args[]) throws Exception {
697 Options opts = new Options(args);
698
699 boolean testPerformance = opts.isFlagSet('k') > 0;
700 boolean allTests = opts.isFlagSet('A') > 0;
701 boolean onlyB = opts.isFlagSet('b') > 0;
702 boolean testMode = opts.isFlagSet('t') > 0;
703
704 // set up tests so that the results are sent to System.out
705 TestClient client;
706
707 if (testPerformance) {
708 client = new TestClient(testMode) {
709 public void verify(String method, Object sent, Object gotBack) {
710 }
711 };
712 } else {
713 client = new TestClient(testMode) {
714 public void verify(String method, Object sent, Object gotBack) {
715 String message;
716 if (this.equals(sent, gotBack)) {
717 message = "OK";
718 } else {
719 if (gotBack instanceof Exception) {
720 if (gotBack instanceof AxisFault) {
721 message = "Fault: " +
722 ((AxisFault)gotBack).getFaultString();
723 } else {
724 StringWriter sw = new StringWriter();
725 PrintWriter pw = new PrintWriter(sw);
726 message = "Exception: ";
727 ((Exception)gotBack).printStackTrace(pw);
728 message += sw.getBuffer().toString();
729 }
730 } else {
731 message = "Fail:" + gotBack + " expected " + sent;
732 }
733 }
734 // Line up the output
735 String tab = "";
736 int l = method.length();
737 while (l < 25) {
738 tab += " ";
739 l++;
740 }
741 System.out.println(method + tab + " " + message);
742 }
743 };
744 }
745
746 // set up the call object
747 client.setURL(opts.getURL());
748 client.setUser(opts.getUser());
749 client.setPassword(opts.getPassword());
750
751 if (testPerformance) {
752 long startTime = System.currentTimeMillis();
753 for (int i = 0; i < 10; i++) {
754 if (allTests) {
755 client.executeAll();
756 } else if (onlyB) {
757 client.execute2B();
758 } else {
759 client.execute2A();
760 }
761 }
762 long stopTime = System.currentTimeMillis();
763 System.out.println("That took " + (stopTime - startTime) + " milliseconds");
764 } else {
765 if (allTests) {
766 client.executeAll();
767 } else if (onlyB) {
768 client.execute2B();
769 } else {
770 client.execute2A();
771 }
772 }
773 }
774 }