1 57 58 package docStyle; 59 60 import java.io.StringReader ; 61 import java.io.StringWriter ; 62 63 import junit.framework.Test; 64 import junit.framework.TestCase; 65 import junit.framework.TestSuite; 66 67 import org.apache.wsif.WSIFMessage; 68 import org.apache.wsif.WSIFOperation; 69 import org.apache.wsif.WSIFPort; 70 import org.apache.wsif.WSIFService; 71 import org.apache.wsif.WSIFServiceFactory; 72 import org.apache.xerces.parsers.DOMParser; 73 import org.apache.xml.serialize.OutputFormat; 74 import org.apache.xml.serialize.XMLSerializer; 75 import org.w3c.dom.Element ; 76 import org.w3c.dom.Node ; 77 import org.w3c.dom.NodeList ; 78 import org.xml.sax.InputSource ; 79 80 import docStyle.wsifservice.ZipCodeResolverSoap; 81 import docStyle.zipCodeNW.ShortZipCode; 82 import docStyle.zipCodeNW.ShortZipCodeResponse; 83 84 import util.TestUtilities; 85 86 89 public class ZipCodeAxisTest extends TestCase { 90 String wsdlLocation = 91 TestUtilities.getWsdlPath("java\\test\\docStyle\\wsifservice") 92 + "zipCodeResolver.wsdl"; 93 97 public ZipCodeAxisTest(String name) { 98 super(name); 99 } 100 101 public static void main(String [] args) { 102 TestUtilities.startListeners( 103 TestUtilities.ADDRESSBOOK_LISTENER 104 | TestUtilities.ASYNC_LISTENER 105 | TestUtilities.NATIVEJMS_LISTENER); 106 junit.textui.TestRunner.run(suite()); 107 TestUtilities.stopListeners(); 108 } 109 110 public static Test suite() { 111 return new TestSuite(ZipCodeAxisTest.class); 112 } 113 114 public void setUp() { 115 TestUtilities.setUpExtensionsAndProviders(); 116 } 117 118 public void testDynamicAxis() { 119 doitDyn("ZipCodeResolverSoap", "axis"); 120 } 121 public void testDynamicAxisWrapped() { 122 doitDynWrapped("ZipCodeResolverSoap", "axis"); 123 } 124 public void testStubsAxis() { 125 doitStub("ZipCodeResolverSoap", "axis"); 126 } 127 public void testStubsAxisWrapped() { 128 doitStubWrapped("ZipCodeResolverSoap", "axis"); 129 } 130 public void testMessagingAxis() { 131 doitMessaging("ZipCodeResolverSoap", "axis"); 132 } 133 public void testDyn() { 134 doitStub("ZipCodeResolverSoapJMS", "axis"); 135 } 136 137 private void doitDyn(String portName, String protocol) { 138 if (portName.toUpperCase().indexOf("JMS") != -1 139 && !TestUtilities.areWeTesting("jms")) 140 return; 141 142 TestUtilities.setProviderForProtocol(protocol); 143 144 try { 145 WSIFServiceFactory factory = WSIFServiceFactory.newInstance(); 146 WSIFService service = 147 factory.getService( 148 wsdlLocation, 149 null, 150 null, 151 "http://webservices.eraserver.net/", 152 "ZipCodeResolverSoap"); 153 154 WSIFPort port = service.getPort(portName); 155 156 WSIFOperation operation = port.createOperation("ShortZipCode"); 157 158 WSIFMessage inMsg = operation.createInputMessage(); 159 WSIFMessage outMsg = operation.createOutputMessage(); 160 WSIFMessage faultMsg = operation.createFaultMessage(); 161 inMsg.setObjectPart("accessCode", "9999"); 162 inMsg.setObjectPart("address", "607 Trinity"); 163 inMsg.setObjectPart("city", "Austin"); 164 inMsg.setObjectPart("state", "TX"); 165 166 boolean ok = 167 operation.executeRequestResponseOperation( 168 inMsg, 169 outMsg, 170 faultMsg); 171 172 assertTrue("operation returned false!!", ok); 173 String s = (String ) outMsg.getObjectPart("ShortZipCodeResult"); 174 assertTrue("wrong zipcode: " + s + "!!", "78701".equals(s)); 175 176 } catch (Exception ex) { 177 ex.printStackTrace(); 178 assertTrue( 179 "AddressBookTest(" 180 + portName 181 + ") caught exception " 182 + ex.getLocalizedMessage(), 183 false); 184 } 185 } 186 187 private void doitDynWrapped(String portName, String protocol) { 188 if (portName.toUpperCase().indexOf("JMS") != -1 189 && !TestUtilities.areWeTesting("jms")) 190 return; 191 192 TestUtilities.setProviderForProtocol(protocol); 193 194 try { 195 WSIFServiceFactory factory = WSIFServiceFactory.newInstance(); 196 WSIFService service = 197 factory.getService( 198 wsdlLocation, 199 null, 200 null, 201 "http://webservices.eraserver.net/", 202 "ZipCodeResolverSoap"); 203 204 service.mapType( 205 new javax.xml.namespace.QName ( 206 "http://webservices.eraserver.net/", 207 "ShortZipCode"), 208 ShortZipCode.class ); 209 service.mapType( 210 new javax.xml.namespace.QName ( 211 "http://webservices.eraserver.net/", 212 "ShortZipCodeResponse"), 213 ShortZipCodeResponse.class ); 214 215 WSIFPort port = service.getPort(portName); 216 217 WSIFOperation operation = port.createOperation("ShortZipCode"); 218 219 WSIFMessage inMsg = operation.createInputMessage(); 220 WSIFMessage outMsg = operation.createOutputMessage(); 221 WSIFMessage faultMsg = operation.createFaultMessage(); 222 223 ShortZipCode zc = new ShortZipCode(); 224 zc.setAccessCode("9999"); 225 zc.setAddress("607 Trinity"); 226 zc.setCity("Austin"); 227 zc.setState("TX"); 228 229 inMsg.setObjectPart("parameters", zc); 230 231 boolean ok = 232 operation.executeRequestResponseOperation( 233 inMsg, 234 outMsg, 235 faultMsg); 236 237 assertTrue("operation returned false!!", ok); 238 239 ShortZipCodeResponse zcResp = 240 (ShortZipCodeResponse) outMsg.getObjectPart("parameters"); 241 242 String s = zcResp.getShortZipCodeResult(); 243 assertTrue("wrong zipcode: " + s + "!!", "78701".equals(s)); 244 245 } catch (Exception ex) { 246 ex.printStackTrace(); 247 assertTrue( 248 "AddressBookTest(" 249 + portName 250 + ") caught exception " 251 + ex.getLocalizedMessage(), 252 false); 253 } 254 } 255 256 private void doitStub(String portName, String protocol) { 257 if (portName.toUpperCase().indexOf("JMS") != -1 258 && !TestUtilities.areWeTesting("jms")) 259 return; 260 261 TestUtilities.setProviderForProtocol(protocol); 262 263 try { 264 WSIFServiceFactory factory = WSIFServiceFactory.newInstance(); 265 WSIFService service = 266 factory.getService( 267 wsdlLocation, 268 null, 269 null, 270 "http://webservices.eraserver.net/", 271 "ZipCodeResolverSoap"); 272 273 ZipCodeResolverSoap stub = (ZipCodeResolverSoap) service.getStub(portName, ZipCodeResolverSoap.class); 274 275 String zipcode = stub.ShortZipCode( "9999", "607 Trinity", "Austin", "TX" ); 276 assertTrue("wrong zipcode: " + zipcode + "!!", "78701".equals(zipcode)); 277 278 } catch (Exception ex) { 279 ex.printStackTrace(); 280 assertTrue( 281 "AddressBookTest(" 282 + portName 283 + ") caught exception " 284 + ex.getLocalizedMessage(), 285 false); 286 } 287 } 288 289 private void doitStubWrapped(String portName, String protocol) { 290 if (portName.toUpperCase().indexOf("JMS") != -1 291 && !TestUtilities.areWeTesting("jms")) 292 return; 293 294 TestUtilities.setProviderForProtocol(protocol); 295 296 try { 297 WSIFServiceFactory factory = WSIFServiceFactory.newInstance(); 298 WSIFService service = 299 factory.getService( 300 wsdlLocation, 301 null, 302 null, 303 "http://webservices.eraserver.net/", 304 "ZipCodeResolverSoap"); 305 306 service.mapType( 308 new javax.xml.namespace.QName ( 309 "http://webservices.eraserver.net/", 310 "ShortZipCode"), 311 ShortZipCode.class ); 312 service.mapType( 313 new javax.xml.namespace.QName ( 314 "http://webservices.eraserver.net/", 315 "ShortZipCodeResponse"), 316 ShortZipCodeResponse.class ); 317 318 ShortZipCode zc = new ShortZipCode(); 319 zc.setAccessCode("9999"); 320 zc.setAddress("607 Trinity"); 321 zc.setCity("Austin"); 322 zc.setState("TX"); 323 324 docStyle.zipCodeNW.ZipCodeResolverSoap stub = (docStyle.zipCodeNW.ZipCodeResolverSoap) service.getStub(portName, docStyle.zipCodeNW.ZipCodeResolverSoap.class); 325 326 ShortZipCodeResponse zcResp = stub.shortZipCode(zc); 327 328 String s = zcResp.getShortZipCodeResult(); 329 assertTrue("wrong zipcode: " + s + "!!", "78701".equals(s)); 330 331 } catch (Exception ex) { 332 ex.printStackTrace(); 333 assertTrue( 334 "AddressBookTest(" 335 + portName 336 + ") caught exception " 337 + ex.getLocalizedMessage(), 338 false); 339 } 340 } 341 342 private void doitMessaging(String portName, String protocol) { 343 if (portName.toUpperCase().indexOf("JMS") != -1 344 && !TestUtilities.areWeTesting("jms")) 345 return; 346 347 TestUtilities.setProviderForProtocol(protocol); 348 349 try { 350 WSIFServiceFactory factory = WSIFServiceFactory.newInstance(); 351 WSIFService service = 352 factory.getService( 353 wsdlLocation, 354 null, 355 null, 356 "http://webservices.eraserver.net/", 357 "ZipCodeResolverSoap"); 358 359 WSIFPort port = service.getPort(portName); 360 WSIFOperation operation = port.createOperation("ShortZipCode"); 361 WSIFMessage inMsg = operation.createInputMessage(); 362 WSIFMessage outMsg = operation.createOutputMessage(); 363 WSIFMessage faultMsg = operation.createFaultMessage(); 364 365 String inputDocument = 366 "<ShortZipCode xmlns=\"http://webservices.eraserver.net/\">" 367 + "<accessCode>9999</accessCode>" 368 + "<address>607 Trinity</address>" 369 + "<city>Austin</city>" 370 + "<state>TX</state>" 371 + "</ShortZipCode>"; 372 373 DOMParser parser = new DOMParser(); 374 String xmlString = "<?xml version=\"1.0\"?>\n" + inputDocument; 375 parser.parse(new InputSource (new StringReader (xmlString))); 376 Element element = parser.getDocument().getDocumentElement(); 377 379 inMsg.setObjectPart("parameters", element); 380 381 boolean ok = 382 operation.executeRequestResponseOperation( 383 inMsg, 384 outMsg, 385 faultMsg); 386 assertTrue("operation returned false!!", ok); 387 388 Element responseElement = 389 (Element ) outMsg.getObjectPart("parameters"); 390 assertTrue("return element is null!!", responseElement != null); 391 393 NodeList nl = responseElement.getChildNodes(); 394 Node n = nl.item(0); nl = n.getChildNodes(); 396 n = nl.item(0); String s = n.getNodeValue(); 398 if (!"78701".equals(s)) { 399 printElement(responseElement); 400 } 401 assertTrue("wrong zipcode: " + s + "!!", "78701".equals(s)); 402 403 } catch (Exception ex) { 404 ex.printStackTrace(); 405 assertTrue( 406 "AddressBookTest(" 407 + portName 408 + ") caught exception " 409 + ex.getLocalizedMessage(), 410 false); 411 } 412 } 413 414 private void printElement(Element e) throws Exception { 415 OutputFormat of = new OutputFormat(e.getOwnerDocument(), "UTF-8", true); 416 XMLSerializer xmls = new XMLSerializer(of); 417 StringWriter sw = new StringWriter (); 418 xmls.setOutputCharStream(sw); 419 xmls.serialize(e); 420 System.err.println("element=" + sw.toString()); 421 } 422 } 423
| Popular Tags
|