KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > async > AsyncTests


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "WSIF" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

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 /**
77  * Junit test to test the NativeJMSProvider
78  *
79  * need to use the wsifjmssetup.bat and wsifmqset.bat
80  *
81  */

82 public class AsyncTests extends TestCase {
83
84   public static String JavaDoc 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 JavaDoc name) {
95     super(name);
96   }
97
98   public static void main(String JavaDoc[] 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   /**
137    * Query Stockquote sample
138    */

139   public void doitStockquote(String JavaDoc portName, String JavaDoc protocol) {
140     if (portName.toUpperCase().indexOf("JMS") != -1
141         && !TestUtilities.areWeTesting("jms")) {
142         return;
143     }
144
145     TestUtilities.setProviderForProtocol( protocol );
146
147     String JavaDoc portType = "StockquotePT";
148     String JavaDoc operationName = "getQuote";
149
150     String JavaDoc 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, // serviceNS
158
null, // serviceName
159
"http://wsifservice.stockquote/", // portTypeNS
160
portType); // portTypeName
161

162       WSIFPort port = service.getPort(portName);
163
164       // Executing synchronous executeRequestResponseOperation(input, output, fault )
165
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 JavaDoc)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       // Executing executeRequestResponseAsync(input, handler)
186
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 JavaDoc)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       // Executing executeRequestResponseAsync(input)
201
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 JavaDoc)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 JavaDoc e) {
216         e.printStackTrace();
217         assertTrue("exception during stockquote test: " + e.getMessage(), false);
218     }
219   }
220
221   /**
222    * AddressBook sample - addEntry
223    */

224   public void doitAddressBook_addEntry(String JavaDoc portName, String JavaDoc protocol) {
225
226     if ( portName.indexOf( "JMS" ) != -1 && !TestUtilities.areWeTesting("jms") ) {
227        return;
228     }
229
230     TestUtilities.setProviderForProtocol( protocol );
231
232     String JavaDoc portType = "AddressBook";
233     String JavaDoc operationName = "addEntry";
234
235     String JavaDoc 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, // serviceNS
245
null, // serviceName
246
"http://wsifservice.addressbook/", // portTypeNS
247
portType); // portTypeName
248

249       service.mapType(
250          new javax.xml.namespace.QName JavaDoc("http://wsiftypes.addressbook/", "address"),
251          Class.forName("addressbook.wsiftypes.Address"));
252
253       service.mapType(
254          new javax.xml.namespace.QName JavaDoc("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 JavaDoc e) {
269       e.printStackTrace();
270       assertTrue("exception during addressbook addEntry: " + e.getMessage(),
271                  false);
272     }
273
274   }
275
276   /**
277    * AddressBook sample - getAddressFromName
278    */

279   public void doitAddressBook_getAddressFromName(String JavaDoc portName, String JavaDoc protocol) {
280
281     if ( portName.indexOf( "JMS" ) != -1 && !TestUtilities.areWeTesting("jms") ) {
282        return;
283     }
284     TestUtilities.setProviderForProtocol( protocol );
285
286     String JavaDoc portType = "AddressBook";
287     String JavaDoc operationName = "getAddressFromName";
288
289     String JavaDoc 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, // serviceNS
299
null, // serviceName
300
"http://wsifservice.addressbook/", // portTypeNS
301
portType); // portTypeName
302

303       service.mapType(
304          new javax.xml.namespace.QName JavaDoc("http://wsiftypes.addressbook/", "address"),
305          Class.forName("addressbook.wsiftypes.Address"));
306
307       service.mapType(
308          new javax.xml.namespace.QName JavaDoc("http://wsiftypes.addressbook/", "phone"),
309          Class.forName("addressbook.wsiftypes.Phone"));
310
311       WSIFPort port = service.getPort(portName);
312
313       // Executing synchronous executeRequestResponseOperation(input, output, fault )
314
String JavaDoc inputMsgName = "GetAddressFromNameRequest";
315       String JavaDoc 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       // Executing executeRequestResponseOperation(input, output, fault )
337
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       // Executing executeRequestResponseAsync(input)
354
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 JavaDoc 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 JavaDoc 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); // 2 async calls
389
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 JavaDoc ex) {
396          ex.printStackTrace();
397          assertTrue( "exception executing async op: " + ex.getMessage(), false );
398       }
399       int i = 5; // 15 seconds timout
400
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 JavaDoc ex) {}
405       }
406       assertTrue( "no response to async operation!", i > 0 ); // no responses in time
407
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 JavaDoc 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 JavaDoc ex) {
429          ex.printStackTrace();
430          assertTrue( "exception executing async op: " + ex.getMessage(), false );
431       }
432       return output;
433   }
434
435     
436 }
Free Books   Free Magazines  
Popular Tags