KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > wsdl > interop3 > groupE > client > InteropTestListServiceTestClient


1 /*
2  * Copyright 2002-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 test.wsdl.interop3.groupE.client;
18
19 import org.apache.axis.AxisFault;
20 import org.apache.axis.encoding.ser.BeanDeserializerFactory;
21 import org.apache.axis.encoding.ser.BeanSerializerFactory;
22 import org.apache.axis.types.HexBinary;
23 import org.apache.axis.utils.Options;
24 import org.apache.axis.utils.XMLUtils;
25 import org.w3c.dom.Document JavaDoc;
26
27 import javax.wsdl.Definition;
28 import javax.wsdl.factory.WSDLFactory;
29 import javax.wsdl.xml.WSDLReader;
30 import javax.xml.namespace.QName JavaDoc;
31 import javax.xml.rpc.Service JavaDoc;
32 import java.io.PrintWriter JavaDoc;
33 import java.io.StringWriter JavaDoc;
34 import java.net.URL JavaDoc;
35 import java.util.Date JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.Set JavaDoc;
39
40 /**
41  * Test Client for part of interop group 3E. See the main entrypoint
42  * for more details on usage.
43  *
44  * @author Glyn Normington <glyn@apache.org>
45  */

46 public abstract class InteropTestListServiceTestClient {
47     public static URL JavaDoc url;
48
49     private static final String JavaDoc NS =
50         "http://soapinterop.org/WSDLInteropTestList";
51     private static final QName JavaDoc OPQN = new QName JavaDoc(NS, "echoLinkedList");
52     private static final String JavaDoc LISTNS = "http://soapinterop.org/xsd";
53     private static final QName JavaDoc LISTQN = new QName JavaDoc(LISTNS, "List");
54
55     private boolean addMethodToAction = false;
56     private String JavaDoc soapAction = "http://soapinterop.org/";
57     private org.apache.axis.client.Call call = null;
58     private Definition wsdl = null;
59     private QName JavaDoc serviceQN = null;
60
61     /**
62      * Determine if two objects are equal. Handles nulls and recursively
63      * verifies arrays are equal. Accepts dates within a tolerance of
64      * 999 milliseconds.
65      */

66     protected boolean equals(Object JavaDoc obj1, Object JavaDoc obj2) {
67        if (obj1 == null || obj2 == null) return (obj1 == obj2);
68        if (obj1.equals(obj2)) return true;
69
70        // For comparison purposes, get the array of bytes representing
71
// the HexBinary object.
72
if (obj1 instanceof HexBinary) {
73            obj1 = ((HexBinary) obj1).getBytes();
74        }
75        if (obj2 instanceof HexBinary) {
76            obj2 = ((HexBinary) obj2).getBytes();
77        }
78
79        if (obj1 instanceof Date JavaDoc && obj2 instanceof Date JavaDoc)
80            if (Math.abs(((Date JavaDoc)obj1).getTime()-((Date JavaDoc)obj2).getTime())<1000)
81                return true;
82
83        if ((obj1 instanceof Map JavaDoc) && (obj2 instanceof Map JavaDoc)) {
84            Map JavaDoc map1 = (Map JavaDoc)obj1;
85            Map JavaDoc map2 = (Map JavaDoc)obj2;
86            Set JavaDoc keys1 = map1.keySet();
87            Set JavaDoc keys2 = map2.keySet();
88            if (!(keys1.equals(keys2))) return false;
89
90            // Check map1 is a subset of map2.
91
Iterator JavaDoc i = keys1.iterator();
92            while (i.hasNext()) {
93                Object JavaDoc key = i.next();
94                if (!equals(map1.get(key), map2.get(key)))
95                    return false;
96            }
97
98            // Check map2 is a subset of map1.
99
Iterator JavaDoc j = keys2.iterator();
100            while (j.hasNext()) {
101                Object JavaDoc key = j.next();
102                if (!equals(map1.get(key), map2.get(key)))
103                    return false;
104            }
105            return true;
106        }
107        
108        if ((obj1 instanceof List) && (obj2 instanceof List)) {
109            List l1 = (List)obj1;
110            List l2 = (List)obj2;
111
112            if (l1.getVarInt() != l2.getVarInt()) return false;
113            
114            if (l1.getVarString() == null) {
115                if (l2.getVarString() != null) return false;
116            } else {
117                if (!l1.getVarString().equals(l2.getVarString())) return false;
118            }
119
120            if (l1.getChild() == null) {
121                if (l2.getChild() != null) return false;
122            } else {
123                if (!equals(l1.getChild(), l2.getChild())) return false;
124            }
125        } else {
126            return false; // type mismatch or unsupported type
127
}
128        return true;
129     }
130
131     /**
132      * Set up the call object.
133      */

134     public void setURL(String JavaDoc url) throws AxisFault {
135         try {
136             Document JavaDoc doc = XMLUtils.newDocument(new URL JavaDoc(url).toString());
137             WSDLReader reader = WSDLFactory.newInstance().newWSDLReader();
138             reader.setFeature("javax.wsdl.verbose", false);
139             wsdl = reader.readWSDL(null, doc);
140
141             Service JavaDoc service = new org.apache.axis.client.
142
                Service(new URL JavaDoc(url), getServiceQName());
143
144             call = (org.apache.axis.client.Call)service.
145                 createCall(getPortQName(wsdl), OPQN);
146             
147             call.registerTypeMapping(List.class,
148                                      LISTQN,
149                                      BeanSerializerFactory.class,
150                                      BeanDeserializerFactory.class,
151                                      false);
152             call.setReturnType(LISTQN);
153         } catch (Exception JavaDoc exp) {
154             throw AxisFault.makeFault(exp);
155         }
156     }
157
158     /**
159      * Introspect the WSDL to obtain a service name.
160      * The WSDL must define one service.
161      */

162     private QName JavaDoc getServiceQName() {
163         serviceQN = (QName JavaDoc)wsdl.getServices().
164             keySet().iterator().next();
165         return new QName JavaDoc(serviceQN.getNamespaceURI(),
166                          serviceQN.getLocalPart());
167     }
168
169     /**
170      * Introspect the specified WSDL to obtain a port name.
171      * The WSDL must define one port name.
172      */

173     private QName JavaDoc getPortQName(Definition wsdl) {
174         if (serviceQN == null) {
175             getServiceQName();
176         }
177         String JavaDoc port = (String JavaDoc)wsdl.getService(serviceQN).getPorts().keySet().
178             iterator().next();
179         return new QName JavaDoc(serviceQN.getNamespaceURI(), port);
180     }
181
182     /**
183      * Execute the test
184      */

185     public void execute() throws Exception JavaDoc {
186
187         {
188             Object JavaDoc input = null;
189             try {
190                 List node1 = new List();
191                 node1.setVarInt(1);
192                 node1.setVarString("last");
193                 List node2 = new List();
194                 node2.setVarInt(2);
195                 node2.setVarString("middle");
196                 node2.setChild(node1);
197                 List list = new List();
198                 list.setVarInt(3);
199                 list.setVarString("first");
200                 list.setChild(node2);
201                 input = list;
202
203                 Object JavaDoc output = call.invoke(new Object JavaDoc[] {input});
204                 verify("echoLinkedList", input, output);
205             } catch (Exception JavaDoc e) {
206                 verify("echoLinkedList", input, e);
207             }
208         }
209     }
210
211     /**
212      * Verify that the object sent was, indeed, the one you got back.
213      * Subclasses are sent to override this with their own output.
214      */

215     protected abstract void verify(String JavaDoc method, Object JavaDoc sent, Object JavaDoc gotBack);
216
217     /**
218      * Main entry point. Tests a variety of echo methods and reports
219      * on their results.
220      *
221      * Arguments are of the form:
222      * -h localhost -p 8080 -s /soap/servlet/rpcrouter
223      * -h indicates the host
224      * -p indicates the port
225      * -s indicates the service part of the URI
226      *
227      * Alternatively a URI may be passed thus:
228      * -l completeuri
229      */

230     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
231         Options opts = new Options(args);
232         
233         boolean testPerformance = opts.isFlagSet('k') > 0;
234         
235         // set up tests so that the results are sent to System.out
236
InteropTestListServiceTestClient client;
237         
238         if (testPerformance) {
239             client =
240                 new InteropTestListServiceTestClient() {
241                         public void verify(String JavaDoc method,
242                                            Object JavaDoc sent,
243                                            Object JavaDoc gotBack) {
244                         }
245                 };
246         } else {
247             client =
248                 new InteropTestListServiceTestClient() {
249                         public void verify(String JavaDoc method,
250                                            Object JavaDoc sent,
251                                            Object JavaDoc gotBack) {
252                             String JavaDoc message;
253                             if (this.equals(sent, gotBack)) {
254                                 message = "OK";
255                             } else {
256                                 if (gotBack instanceof Exception JavaDoc) {
257                                     if (gotBack instanceof AxisFault) {
258                                         message = "Fault: " +
259                                             ((AxisFault)gotBack).
260                                             getFaultString();
261                                     } else {
262                                         StringWriter JavaDoc sw = new StringWriter JavaDoc();
263                                         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
264                                         message = "Exception: ";
265                                         ((Exception JavaDoc)gotBack).
266                                             printStackTrace(pw);
267                                         message += sw.getBuffer().toString();
268                                     }
269                                 } else {
270                                     message = "Fail:" + gotBack +
271                                         " expected " + sent;
272                                 }
273                             }
274                             // Line up the output
275
String JavaDoc tab = "";
276                             int l = method.length();
277                             while (l < 25) {
278                                 tab += " ";
279                                 l++;
280                             }
281                             System.out.println(method + tab + " " + message);
282                         }
283                     };
284         }
285
286         // set up the call object
287
client.setURL(opts.getURL());
288
289         if (testPerformance) {
290             long startTime = System.currentTimeMillis();
291             for (int i = 0; i < 10; i++) {
292                     client.execute();
293             }
294             long stopTime = System.currentTimeMillis();
295             System.out.println("That took " +
296                                (stopTime - startTime) +
297                                " milliseconds");
298         } else {
299             client.execute();
300         }
301     }
302 }
303
Popular Tags