1 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 ; 26 27 import javax.wsdl.Definition; 28 import javax.wsdl.factory.WSDLFactory; 29 import javax.wsdl.xml.WSDLReader; 30 import javax.xml.namespace.QName ; 31 import javax.xml.rpc.Service ; 32 import java.io.PrintWriter ; 33 import java.io.StringWriter ; 34 import java.net.URL ; 35 import java.util.Date ; 36 import java.util.Iterator ; 37 import java.util.Map ; 38 import java.util.Set ; 39 40 46 public abstract class InteropTestListServiceTestClient { 47 public static URL url; 48 49 private static final String NS = 50 "http://soapinterop.org/WSDLInteropTestList"; 51 private static final QName OPQN = new QName (NS, "echoLinkedList"); 52 private static final String LISTNS = "http://soapinterop.org/xsd"; 53 private static final QName LISTQN = new QName (LISTNS, "List"); 54 55 private boolean addMethodToAction = false; 56 private String soapAction = "http://soapinterop.org/"; 57 private org.apache.axis.client.Call call = null; 58 private Definition wsdl = null; 59 private QName serviceQN = null; 60 61 66 protected boolean equals(Object obj1, Object obj2) { 67 if (obj1 == null || obj2 == null) return (obj1 == obj2); 68 if (obj1.equals(obj2)) return true; 69 70 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 && obj2 instanceof Date ) 80 if (Math.abs(((Date )obj1).getTime()-((Date )obj2).getTime())<1000) 81 return true; 82 83 if ((obj1 instanceof Map ) && (obj2 instanceof Map )) { 84 Map map1 = (Map )obj1; 85 Map map2 = (Map )obj2; 86 Set keys1 = map1.keySet(); 87 Set keys2 = map2.keySet(); 88 if (!(keys1.equals(keys2))) return false; 89 90 Iterator i = keys1.iterator(); 92 while (i.hasNext()) { 93 Object key = i.next(); 94 if (!equals(map1.get(key), map2.get(key))) 95 return false; 96 } 97 98 Iterator j = keys2.iterator(); 100 while (j.hasNext()) { 101 Object 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; } 128 return true; 129 } 130 131 134 public void setURL(String url) throws AxisFault { 135 try { 136 Document doc = XMLUtils.newDocument(new URL (url).toString()); 137 WSDLReader reader = WSDLFactory.newInstance().newWSDLReader(); 138 reader.setFeature("javax.wsdl.verbose", false); 139 wsdl = reader.readWSDL(null, doc); 140 141 Service service = new org.apache.axis.client. 142 Service(new URL (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 exp) { 154 throw AxisFault.makeFault(exp); 155 } 156 } 157 158 162 private QName getServiceQName() { 163 serviceQN = (QName )wsdl.getServices(). 164 keySet().iterator().next(); 165 return new QName (serviceQN.getNamespaceURI(), 166 serviceQN.getLocalPart()); 167 } 168 169 173 private QName getPortQName(Definition wsdl) { 174 if (serviceQN == null) { 175 getServiceQName(); 176 } 177 String port = (String )wsdl.getService(serviceQN).getPorts().keySet(). 178 iterator().next(); 179 return new QName (serviceQN.getNamespaceURI(), port); 180 } 181 182 185 public void execute() throws Exception { 186 187 { 188 Object 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 output = call.invoke(new Object [] {input}); 204 verify("echoLinkedList", input, output); 205 } catch (Exception e) { 206 verify("echoLinkedList", input, e); 207 } 208 } 209 } 210 211 215 protected abstract void verify(String method, Object sent, Object gotBack); 216 217 230 public static void main(String args[]) throws Exception { 231 Options opts = new Options(args); 232 233 boolean testPerformance = opts.isFlagSet('k') > 0; 234 235 InteropTestListServiceTestClient client; 237 238 if (testPerformance) { 239 client = 240 new InteropTestListServiceTestClient() { 241 public void verify(String method, 242 Object sent, 243 Object gotBack) { 244 } 245 }; 246 } else { 247 client = 248 new InteropTestListServiceTestClient() { 249 public void verify(String method, 250 Object sent, 251 Object gotBack) { 252 String message; 253 if (this.equals(sent, gotBack)) { 254 message = "OK"; 255 } else { 256 if (gotBack instanceof Exception ) { 257 if (gotBack instanceof AxisFault) { 258 message = "Fault: " + 259 ((AxisFault)gotBack). 260 getFaultString(); 261 } else { 262 StringWriter sw = new StringWriter (); 263 PrintWriter pw = new PrintWriter (sw); 264 message = "Exception: "; 265 ((Exception )gotBack). 266 printStackTrace(pw); 267 message += sw.getBuffer().toString(); 268 } 269 } else { 270 message = "Fail:" + gotBack + 271 " expected " + sent; 272 } 273 } 274 String 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 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 |