1 57 58 package addressbook; 59 60 import junit.framework.Test; 61 import junit.framework.TestCase; 62 import junit.framework.TestSuite; 63 64 import org.apache.wsif.WSIFConstants; 65 import org.apache.wsif.WSIFCorrelationId; 66 import org.apache.wsif.WSIFException; 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.wsif.providers.soap.apacheaxis.WSIFDynamicProvider_ApacheAxis; 73 import org.apache.wsif.util.WSIFPluggableProviders; 74 import util.AddressUtility; 75 import util.TestUtilities; 76 77 import addressbook.wsifservice.AddressBook; 78 import addressbook.wsiftypes.Address; 79 import addressbook.wsiftypes.Phone; 80 import async.AsyncResponseHandler; 81 82 86 public class AddressBookTest extends TestCase { 87 String wsdlLocation = 88 TestUtilities.getWsdlPath("java\\test\\addressbook\\wsifservice") 89 + "AddressBook.wsdl"; 90 static String server = TestUtilities.getSoapServer().toUpperCase(); 91 92 static String name1 = "Purdue Boilermaker"; 93 static Address addr1 = 94 new Address( 95 1, 96 "University Drive", 97 "West Lafayette", 98 "IN", 99 47907, 100 new Phone(765, "494", "4900")); 101 102 static String firstName2 = "Someone"; 103 static String lastName2 = "Else"; 104 static Address addr2 = 105 new Address( 106 0, 107 "Somewhere Else", 108 "No Where", 109 "NO", 110 71983, 111 new Phone(600, "391", "5682")); 112 113 public AddressBookTest(String name) { 114 super(name); 115 } 116 117 public static void main(String [] args) { 118 TestUtilities.startListeners( 119 TestUtilities.ADDRESSBOOK_LISTENER 120 | TestUtilities.ASYNC_LISTENER 121 | TestUtilities.NATIVEJMS_LISTENER); 122 123 junit.textui.TestRunner.run(suite()); 124 TestUtilities.stopListeners(); 125 } 126 127 public static Test suite() { 128 return new TestSuite(AddressBookTest.class); 129 } 130 131 public void setUp() { 132 TestUtilities.setUpExtensionsAndProviders(); 133 } 134 135 public void testAxis() { 136 doit(server+"Port", "axis"); 137 } 138 public void testSoap() { 139 doit(server+"Port", "soap"); 140 } 141 public void testJava() { 142 doit("JavaPort", "java"); 143 } 144 public void testSoapJms() { 145 doit("SOAPJMSPort", "soap"); 146 } 147 public void testAxisJms() { 148 doit("SOAPJMSPort", "axis"); 149 } 150 public void testNativeJms() { 151 doit("NativeJmsPort", "" ); 152 } 153 public void testDynamicSOAP() { 154 doitDyn(server+"Port", "soap"); 155 } 156 public void testDynamicAxis() { 157 doitDyn(server+"Port", "axis"); 158 } 159 public void testDynamicJava() { 160 doitDyn("JavaPort", "java"); 161 } 162 public void testDynamicSoapJms() { 163 doitDyn("SOAPJMSPort", "soap"); 164 } 165 public void testDynamicAxisJms() { 166 doitDyn("SOAPJMSPort", "axis"); 167 } 168 public void testDynamicNativeJms() { 169 doitDyn("NativeJmsPort", "" ); 170 } 171 172 private void doit(String portName, String protocol) { 173 if (portName.toUpperCase().indexOf("JMS") != -1 174 && !TestUtilities.areWeTesting("jms")) 175 return; 176 177 TestUtilities.setProviderForProtocol( protocol ); 178 179 try { 180 WSIFServiceFactory factory = WSIFServiceFactory.newInstance(); 181 WSIFService service = factory.getService(wsdlLocation, null, null, "http://wsifservice.addressbook/", "AddressBook" ); 186 AddressBook abStub = (AddressBook) service.getStub(portName, AddressBook.class); 187 188 abStub.addEntry(name1, addr1); 189 abStub.addEntry(firstName2, lastName2, addr2); 190 191 Address resp1 = abStub.getAddressFromName(name1); 192 assertTrue(new AddressUtility(addr1).equals(resp1)); 193 194 Address resp2 = abStub.getAddressFromName(firstName2 + " " + lastName2); 195 assertTrue(new AddressUtility(addr2).equals(resp2)); 196 197 if (TestUtilities.areWeTesting("async")) { 198 testAsyncOPs( service, portName, name1, addr1 ); 199 testAsyncOPs( service, portName, firstName2 + " " + lastName2, addr2 ); 200 } 201 202 } catch (Exception e) { 203 System.err.println("AddressBookTest(" + portName + ") caught exception " + e); 204 e.printStackTrace(); 205 assertTrue(false); 206 } finally { 207 TestUtilities.resetDefaultProviders(); 208 } 209 } 210 211 private void doitDyn(String portName, String protocol) { 212 if (portName.toUpperCase().indexOf("JMS") != -1 && !TestUtilities.areWeTesting("jms")) 213 return; 214 215 TestUtilities.setProviderForProtocol( protocol ); 216 217 try { 218 WSIFServiceFactory factory = WSIFServiceFactory.newInstance(); 219 WSIFService service = factory.getService(wsdlLocation, null, null, "http://wsifservice.addressbook/", "AddressBook"); 224 service.mapType( 225 new javax.xml.namespace.QName ( 226 "http://wsiftypes.addressbook/", 227 "address"), 228 Class.forName("addressbook.wsiftypes.Address")); 229 230 service.mapType( 231 new javax.xml.namespace.QName ( 232 "http://wsiftypes.addressbook/", 233 "phone"), 234 Class.forName("addressbook.wsiftypes.Phone")); 235 236 WSIFPort port = null; 237 238 port = service.getPort(portName); 239 240 WSIFOperation operation = 241 port.createOperation("addEntry", "AddEntryWholeNameRequest", null); 242 243 WSIFMessage inputMessage = operation.createInputMessage(); 244 WSIFMessage outputMessage = operation.createOutputMessage(); 245 WSIFMessage faultMessage = operation.createFaultMessage(); 246 247 String nameToAdd = "Chris P. Bacon"; 249 Address addressToAdd = 250 new Address( 251 1, 252 "The Waterfront", 253 "Some City", 254 "NY", 255 47907, 256 new Phone(765, "494", "4900")); 257 258 inputMessage.setObjectPart("name", nameToAdd); 260 inputMessage.setObjectPart("address", addressToAdd); 261 262 operation.executeInputOnlyOperation(inputMessage); 264 265 operation = null; 267 inputMessage = null; 268 outputMessage = null; 269 faultMessage = null; 270 271 operation = port.createOperation("getAddressFromName"); 272 273 inputMessage = operation.createInputMessage(); 275 outputMessage = operation.createOutputMessage(); 276 faultMessage = operation.createFaultMessage(); 277 278 String nameToLookup = "Chris P. Bacon"; 280 inputMessage.setObjectPart("name", nameToLookup); 281 282 boolean operationSucceeded = 284 operation.executeRequestResponseOperation( 285 inputMessage, 286 outputMessage, 287 faultMessage); 288 289 if (operationSucceeded) { 290 System.out.println( 291 "Successfull lookup of name '" + nameToLookup + "' in addressbook"); 292 293 Address addressFound = (Address) outputMessage.getObjectPart("address"); 295 System.out.println("The address found was:"); 296 System.out.println(addressFound); 297 } else { 298 System.out.println("Failed to lookup name in addressbook"); 299 } 300 301 boolean caughtException = false; 303 try { 304 operationSucceeded = 305 operation.executeRequestResponseOperation( 306 inputMessage, 307 outputMessage, 308 faultMessage); 309 } catch (WSIFException we) { 310 caughtException = true; 311 } 312 assertTrue(caughtException); 313 314 } catch (Exception e) { 315 System.err.println("AddressBookTest(" + portName + ") caught exception " + e); 316 e.printStackTrace(); 317 assertTrue(false); 318 } finally { 319 TestUtilities.resetDefaultProviders(); 320 } 321 } 322 323 private void testAsyncOPs(WSIFService service, String portName, String name, Address addr) { 324 try { 325 WSIFPort port = 326 (portName == null) ? service.getPort() : service.getPort(portName); 327 328 if (!port.supportsAsync() ) { 329 return; 330 } 331 332 WSIFOperation op = port.createOperation( "getAddressFromName" ); 333 334 AsyncResponseHandler abHandler = new AsyncResponseHandler( 1 ); 336 WSIFMessage inMsg = op.createInputMessage(); 337 inMsg.setObjectPart( "name", name ); 338 339 WSIFMessage outmsg = op.createOutputMessage(); 340 WSIFMessage faultMsg = op.createFaultMessage(); 341 342 WSIFMessage context = op.getContext(); 343 context.setObjectPart( WSIFConstants.CONTEXT_JMS_PREFIX + "JMSReplyTo", 344 TestUtilities.getWsifProperty("wsif.async.replytoq") ); 345 op.setContext( context ); 346 347 WSIFCorrelationId id = op.executeRequestResponseAsync(inMsg, abHandler); 348 assertTrue("null correlation id returned from async request!", id != null ); 349 350 int i = 10; 351 while (i-- > 0 && !abHandler.isDone()) { 352 System.out.println( "waiting for async responses - " + i ); 353 try { 354 Thread.sleep(3000); 355 } catch (InterruptedException ex) { 356 } 357 } 358 assertTrue("no response from async operation!",i > 0); 360 WSIFMessage[] faults = abHandler.getFaults(); 361 WSIFMessage[] outputs = abHandler.getOutputs(); 362 Address respAddr = (Address) outputs[0].getObjectPart("address"); 363 assertTrue("incorrect address response!", addr.equals(respAddr)); 364 365 } catch (Exception ex) { 366 ex.printStackTrace(); 367 assertTrue( "exception making async request!!", false ); 368 } 369 } 370 371 } 372
| Popular Tags
|