1 57 58 package async; 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.WSIFMessage; 67 import org.apache.wsif.WSIFOperation; 68 import org.apache.wsif.WSIFPort; 69 import org.apache.wsif.WSIFService; 70 import org.apache.wsif.WSIFServiceFactory; 71 import util.TestUtilities; 72 73 import addressbook.wsiftypes.Address; 74 import addressbook.wsiftypes.Phone; 75 76 82 public class AsyncTests extends TestCase { 83 84 public static String name1 = "Purdue Boilermaker"; 85 public static Address addr1 = 86 new Address( 87 1, 88 "University Drive", 89 "West Lafayette", 90 "IN", 91 47907, 92 new Phone(765, "494", "4900")); 93 94 public AsyncTests(String name) { 95 super(name); 96 } 97 98 public static void main(String [] args) { 99 TestUtilities.startListeners(); 100 junit.textui.TestRunner.run(suite()); 101 TestUtilities.stopListeners(); 102 } 103 104 public static Test suite() { 105 return new TestSuite(AsyncTests.class); 106 } 107 108 public void setUp() { 109 TestUtilities.setUpExtensionsAndProviders(); 110 } 111 112 public void testSoapJms() { 113 if ( TestUtilities.areWeTesting("jms") ) { 114 doitStockquote( "SOAPJMSPort", "soap" ); 115 doitAddressBook_addEntry( "SOAPJMSPort", "soap" ); 116 doitAddressBook_getAddressFromName( "SOAPJMSPort", "soap" ); 117 } 118 } 119 120 public void testAxisJms() { 121 if ( TestUtilities.areWeTesting("jms") ) { 122 doitStockquote( "SOAPJMSPort", "axis" ); 123 doitAddressBook_addEntry( "SOAPJMSPort", "axis" ); 124 doitAddressBook_getAddressFromName( "SOAPJMSPort", "axis" ); 125 } 126 } 127 128 public void testNativeJms() { 129 if ( TestUtilities.areWeTesting("jms") ) { 130 doitStockquote( "NativeJmsPort", "" ); 131 doitAddressBook_addEntry( "NativeJmsPort", "" ); 132 doitAddressBook_getAddressFromName( "NativeJmsPort", "" ); 133 } 134 } 135 136 139 public void doitStockquote(String portName, String protocol) { 140 if (portName.toUpperCase().indexOf("JMS") != -1 141 && !TestUtilities.areWeTesting("jms")) { 142 return; 143 } 144 145 TestUtilities.setProviderForProtocol( protocol ); 146 147 String portType = "StockquotePT"; 148 String operationName = "getQuote"; 149 150 String wsdlLocation = 151 TestUtilities.getWsdlPath("java\\test\\stockquote\\wsifservice") + "StockQuote.wsdl"; 152 153 System.out.println("\n=== StockQuote"); 154 try { 155 WSIFServiceFactory factory = WSIFServiceFactory.newInstance(); 156 WSIFService service = factory.getService(wsdlLocation, 157 null, null, "http://wsifservice.stockquote/", portType); 162 WSIFPort port = service.getPort(portName); 163 164 WSIFOperation operation = port.createOperation(operationName); 166 167 WSIFMessage context = operation.getContext(); 168 context.setObjectPart( WSIFConstants.CONTEXT_JMS_PREFIX + "JMSReplyTo", 169 TestUtilities.getWsifProperty("wsif.nativejms.responseq") ); 170 operation.setContext( context ); 171 172 WSIFMessage input = operation.createInputMessage(); 173 input.setName("GetQuoteInput"); 174 input.setObjectPart("symbol", "" ); 175 176 WSIFMessage output = operation.createOutputMessage(); 177 WSIFMessage fault = operation.createFaultMessage(); 178 179 doSyncOp( operation, input, output, context ); 180 float value = ((Float )output.getObjectPart( "quote" )).floatValue(); 181 System.out.println("sync stockquote found value = " + value); 182 assertTrue( "doSyncOp stockquote value incorrect!", 183 value == -1.0F ); 184 185 operation = port.createOperation(operationName); 187 188 operation.setContext( context ); 189 190 input = operation.createInputMessage(); 191 input.setName("GetQuoteInput"); 192 input.setObjectPart("symbol", "" ); 193 194 output = doAsyncOp( operation, input, context ); 195 value = ((Float )output.getObjectPart( "quote" )).floatValue(); 196 System.out.println("async stockquote found value = " + value); 197 assertTrue( "doAsyncOp stockquote value incorrect!", 198 value == -1.0F ); 199 200 operation = port.createOperation(operationName); 202 203 operation.setContext( context ); 204 205 input = operation.createInputMessage(); 206 input.setName("GetQuoteInput"); 207 input.setObjectPart("symbol", "" ); 208 209 output = doAsyncOpNoHandler( operation, input, context ); 210 value = ((Float )output.getObjectPart( "quote" )).floatValue(); 211 System.out.println("async stockquote found value = " + value); 212 assertTrue( "doAsyncOpNoHandler stockquote value incorrect!", 213 value == -1.0F ); 214 215 } catch (Exception e) { 216 e.printStackTrace(); 217 assertTrue("exception during stockquote test: " + e.getMessage(), false); 218 } 219 } 220 221 224 public void doitAddressBook_addEntry(String portName, String protocol) { 225 226 if ( portName.indexOf( "JMS" ) != -1 && !TestUtilities.areWeTesting("jms") ) { 227 return; 228 } 229 230 TestUtilities.setProviderForProtocol( protocol ); 231 232 String portType = "AddressBook"; 233 String operationName = "addEntry"; 234 235 String wsdlLocation = 236 TestUtilities.getWsdlPath("java\\test\\addressbook\\wsifservice") 237 + "AddressBook.wsdl"; 238 239 System.out.println("\n=== AddressBook_addEntry"); 240 241 try { 242 WSIFServiceFactory factory = WSIFServiceFactory.newInstance(); 243 WSIFService service = factory.getService(wsdlLocation, 244 null, null, "http://wsifservice.addressbook/", portType); 249 service.mapType( 250 new javax.xml.namespace.QName ("http://wsiftypes.addressbook/", "address"), 251 Class.forName("addressbook.wsiftypes.Address")); 252 253 service.mapType( 254 new javax.xml.namespace.QName ("http://wsiftypes.addressbook/", "phone"), 255 Class.forName("addressbook.wsiftypes.Phone")); 256 257 WSIFPort port = service.getPort(portName); 258 259 WSIFOperation operation = port.createOperation(operationName, 260 "AddEntryWholeNameRequest", null); 261 262 WSIFMessage input = operation.createInputMessage(); 263 input.setObjectPart("name", name1); 264 input.setObjectPart("address", addr1); 265 266 operation.executeInputOnlyOperation(input); 267 268 } catch (Exception e) { 269 e.printStackTrace(); 270 assertTrue("exception during addressbook addEntry: " + e.getMessage(), 271 false); 272 } 273 274 } 275 276 279 public void doitAddressBook_getAddressFromName(String portName, String protocol) { 280 281 if ( portName.indexOf( "JMS" ) != -1 && !TestUtilities.areWeTesting("jms") ) { 282 return; 283 } 284 TestUtilities.setProviderForProtocol( protocol ); 285 286 String portType = "AddressBook"; 287 String operationName = "getAddressFromName"; 288 289 String wsdlLocation = 290 TestUtilities.getWsdlPath("java\\test\\addressbook\\wsifservice") 291 + "AddressBook.wsdl"; 292 293 System.out.println("\n=== AddressBook_getAddressFromName"); 294 295 try { 296 WSIFServiceFactory factory = WSIFServiceFactory.newInstance(); 297 WSIFService service = factory.getService(wsdlLocation, 298 null, null, "http://wsifservice.addressbook/", portType); 303 service.mapType( 304 new javax.xml.namespace.QName ("http://wsiftypes.addressbook/", "address"), 305 Class.forName("addressbook.wsiftypes.Address")); 306 307 service.mapType( 308 new javax.xml.namespace.QName ("http://wsiftypes.addressbook/", "phone"), 309 Class.forName("addressbook.wsiftypes.Phone")); 310 311 WSIFPort port = service.getPort(portName); 312 313 String inputMsgName = "GetAddressFromNameRequest"; 315 String outputMsgName = "GetAddressFromNameResponse"; 316 WSIFOperation operation = 317 port.createOperation(operationName, inputMsgName, outputMsgName); 318 319 WSIFMessage context = operation.getContext(); 320 context.setObjectPart( WSIFConstants.CONTEXT_JMS_PREFIX + "JMSReplyTo", 321 TestUtilities.getWsifProperty("wsif.nativejms.responseq") ); 322 operation.setContext( context ); 323 324 WSIFMessage input = operation.createInputMessage(); 325 input.setObjectPart("name", name1); 326 327 WSIFMessage output = operation.createOutputMessage(); 328 WSIFMessage fault = operation.createFaultMessage(); 329 330 doSyncOp( operation, input, output, context ); 331 332 Address addressResponse = (Address) output.getObjectPart("address"); 333 System.out.println("Found address = " + addressResponse); 334 assertTrue( "doSyncOp addresses not equal!", addr1.equals(addressResponse) ); 335 336 operation = 338 port.createOperation(operationName, inputMsgName, outputMsgName); 339 340 operation.setContext( context ); 341 342 input = operation.createInputMessage(); 343 input.setObjectPart("name", name1); 344 345 output = operation.createOutputMessage(); 346 347 output = doAsyncOp( operation, input, context ); 348 349 addressResponse = (Address) output.getObjectPart("address"); 350 System.out.println("Found address = " + addressResponse); 351 assertTrue( "doAsyncOp addresses not equal!", addr1.equals(addressResponse) ); 352 353 operation = 355 port.createOperation(operationName, inputMsgName, outputMsgName); 356 357 operation.setContext( context ); 358 359 input = operation.createInputMessage(); 360 input.setObjectPart("name", name1); 361 362 output = doAsyncOpNoHandler( operation, input, context ); 363 addressResponse = (Address) output.getObjectPart("address"); 364 System.out.println("Found address = " + addressResponse); 365 assertTrue( "doAsyncOpNoHandler addresses not equal!", addr1.equals(addressResponse) ); 366 367 } catch (Exception e) { 368 e.printStackTrace(); 369 assertTrue("exception during addressbook getAddressFromName test: " + e.getMessage(), false); 370 } 371 372 } 373 374 private void doSyncOp(WSIFOperation op, 375 WSIFMessage input, WSIFMessage output, WSIFMessage context) { 376 try { 377 op.setContext( context ); 378 WSIFMessage fault = op.createFaultMessage(); 379 boolean ok = op.executeRequestResponseOperation(input, output, fault ); 380 assertTrue( "executeRequestResponseOperation returned false!", ok ); 381 } catch (Exception ex) { 382 ex.printStackTrace(); 383 assertTrue( "exception executing request: " + ex.getMessage(), false ); 384 } 385 } 386 387 private WSIFMessage doAsyncOp(WSIFOperation op, WSIFMessage input, WSIFMessage context) { 388 AsyncResponseHandler handler = new AsyncResponseHandler(1); try { 390 context.setObjectPart( WSIFConstants.CONTEXT_JMS_PREFIX + "JMSReplyTo", 391 TestUtilities.getWsifProperty("wsif.async.replytoq") ); 392 op.setContext( context ); 393 WSIFCorrelationId id = op.executeRequestResponseAsync(input, handler); 394 System.out.println( "async operation done, correlation id=" + id.getCorrelationId() ); 395 } catch (Exception ex) { 396 ex.printStackTrace(); 397 assertTrue( "exception executing async op: " + ex.getMessage(), false ); 398 } 399 int i = 5; while ( i-- > 0 && !handler.isDone() ) { 401 System.out.println( "async requests sent, waiting for responses - " + i ); 402 try { 403 Thread.sleep(3000); 404 } catch (InterruptedException ex) {} 405 } 406 assertTrue( "no response to async operation!", i > 0 ); return handler.getOutputs()[0]; 408 } 409 410 private WSIFMessage doAsyncOpNoHandler(WSIFOperation op, WSIFMessage input, WSIFMessage context) { 411 WSIFMessage output = null; 412 try { 413 context.setObjectPart( WSIFConstants.CONTEXT_JMS_PREFIX + "JMSReplyTo", 414 TestUtilities.getWsifProperty("wsif.async.replytoq2") ); 415 context.setObjectPart( "testJMSnoHandler", "true" ); 416 op.setContext( context ); 417 WSIFCorrelationId id = op.executeRequestResponseAsync( input ); 418 System.out.println( "async operation done, correlation id=" + id.getCorrelationId() ); 419 420 Object jmsResponse = 421 TestUtilities.getJMSAsyncResponse( 422 id.getCorrelationId(), 423 TestUtilities.getWsifProperty("wsif.async.replytoq2")); 424 425 output = op.createOutputMessage(); 426 WSIFMessage fault = op.createFaultMessage(); 427 op.processAsyncResponse( jmsResponse, output, fault ); 428 } catch (Exception ex) { 429 ex.printStackTrace(); 430 assertTrue( "exception executing async op: " + ex.getMessage(), false ); 431 } 432 return output; 433 } 434 435 436 }
| Popular Tags
|