| 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; |
| 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 |
/** |
| 41 |
* Test Client for part of interop group 3E. See the main entrypoint |
| 42 |
* for more details on usage. |
| 43 |
* |
| 44 |
* @author Glyn Normington <[email protected]> |
| 45 |
*/ |
| 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 |
/** |
| 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 obj1, Object 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 && 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 |
// Check map1 is a subset of map2. |
| 91 |
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 |
// Check map2 is a subset of map1. |
| 99 |
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; // type mismatch or unsupported type |
| 127 |
} |
| 128 |
return true; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Set up the call object. |
| 133 |
*/ |
| 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 |
/** |
| 159 |
* Introspect the WSDL to obtain a service name. |
| 160 |
* The WSDL must define one service. |
| 161 |
*/ |
| 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 |
/** |
| 170 |
* Introspect the specified WSDL to obtain a port name. |
| 171 |
* The WSDL must define one port name. |
| 172 |
*/ |
| 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 |
/** |
| 183 |
* Execute the test |
| 184 |
*/ |
| 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 |
/** |
| 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 method, Object sent, Object 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 args[]) throws Exception { |
| 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 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 |
// Line up the output |
| 275 |
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 |
// 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 |
} |