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