KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jms > JMSTimeoutTest


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 jms;
59
60 import java.text.DateFormat JavaDoc;
61 import java.util.Date JavaDoc;
62
63 import junit.framework.Test;
64 import junit.framework.TestCase;
65 import junit.framework.TestSuite;
66
67 import org.apache.wsif.WSIFConstants;
68 import org.apache.wsif.WSIFCorrelationId;
69 import org.apache.wsif.WSIFException;
70 import org.apache.wsif.WSIFMessage;
71 import org.apache.wsif.WSIFOperation;
72 import org.apache.wsif.WSIFPort;
73 import org.apache.wsif.WSIFService;
74 import org.apache.wsif.WSIFServiceFactory;
75 import org.apache.wsif.util.WSIFProperties;
76 import util.AddressUtility;
77 import util.TestUtilities;
78
79 import addressbook.wsifservice.AddressBook;
80 import addressbook.wsiftypes.Address;
81 import addressbook.wsiftypes.Phone;
82 import async.AsyncResponseHandler;
83
84 /**
85  * Junit test to test out the JMS sync and async timeouts.
86  * This doesn't work so well yet and you have to run each test
87  * manually and check the timeout is correct. the only way to
88  * do this for async ops is to add a system.out.println to the
89  * correlation service to show what timeout value its using.
90  *
91  * @author <a HREF="mailto:ant.elder@uk.ibm.com">Ant Elder</a>
92  */

93 public class JMSTimeoutTest extends TestCase {
94     String JavaDoc wsdlLocation =
95         TestUtilities.getWsdlPath("java\\test\\jms") + "Timeouts.wsdl";
96
97     private static final long sps1 = 7000;
98     private static final long spa1 = 11000;
99     private static final long sps2 = 12000;
100     private static final long spa2 = 14000;
101     private static final long sps3 = 15000;
102     private static final long spa3 = 18000;
103     private static final long sps4 = 3000; // wsif.properties
104
private static final long spa4 = 10000; // wsif.properties
105
private static final long sps5 = 25000;
106     private static final long spa5 = 27000;
107     private static final long sps6 = 15000;
108     private static final long spa6 = 18000;
109
110     public JMSTimeoutTest(String JavaDoc name) {
111         super(name);
112     }
113
114     public static void main(String JavaDoc[] args) {
115         // TestUtilities.startListeners(); <***** don't listen so it times out
116
junit.textui.TestRunner.run(suite());
117         // TestUtilities.stopListeners();
118
}
119
120     public static Test suite() {
121         return new TestSuite(JMSTimeoutTest.class);
122     }
123
124     public void setUp() {
125         TestUtilities.setUpExtensionsAndProviders();
126     }
127
128     // public void testDynamicAxisJms1() {
129
// doitDyn("SOAPJMSPort1", "soap");
130
// }
131
//xxx public void testDynamicAxisJms2() {
132
// doitDyn("SOAPJMSPort2", "soap");
133
// }
134
// public void testDynamicAxisJms3() {
135
// doitDyn("SOAPJMSPort3", "soap");
136
// }
137
// public void testDynamicAxisJms4() {
138
// doitDyn("SOAPJMSPort4", "soap");
139
// }
140
// public void testDynamicAxisJms5() {
141
// doitDyn("SOAPJMSPort5", "soap");
142
// }
143
// public void testDynamicAxisJms6() {
144
// doitDyn("SOAPJMSPort6", "soap");
145
// }
146
// public void testDynamicAxisJms1() {
147
// testAsyncOPs("SOAPJMSPort1", "soap");
148
// }
149
// public void testDynamicAxisJms2() {
150
// testAsyncOPs("SOAPJMSPort2", "soap");
151
// }
152
// public void testDynamicAxisJms3() {
153
// testAsyncOPs("SOAPJMSPort3", "soap");
154
// }
155
// public void testDynamicAxisJms4() {
156
// testAsyncOPs("SOAPJMSPort4", "soap");
157
// }
158
// public void testDynamicAxisJms5() {
159
// testAsyncOPs("SOAPJMSPort5", "soap");
160
// }
161
// public void testDynamicAxisJms6() {
162
// testAsyncOPs("SOAPJMSPort6", "soap");
163
// }
164

165     // public void testDynamicAxisJms1() {
166
// doitDyn("SOAPJMSPort1", "axis");
167
// }
168
// public void testDynamicAxisJms2() {
169
// doitDyn("SOAPJMSPort2", "axis");
170
// }
171
// public void testDynamicAxisJms3() {
172
// doitDyn("SOAPJMSPort3", "axis");
173
// }
174
// public void testDynamicAxisJms4() {
175
// doitDyn("SOAPJMSPort4", "axis");
176
// }
177
// public void testDynamicAxisJms5() {
178
// doitDyn("SOAPJMSPort5", "axis");
179
// }
180
// public void testDynamicAxisJms6() {
181
// doitDyn("SOAPJMSPort6", "axis");
182
// }
183
// public void testDynamicAxisJms1() {
184
// testAsyncOPs("SOAPJMSPort1", "axis");
185
// }
186
// public void testDynamicAxisJms2() {
187
// testAsyncOPs("SOAPJMSPort2", "axis");
188
// }
189
// public void testDynamicAxisJms3() {
190
// testAsyncOPs("SOAPJMSPort3", "axis");
191
// }
192
// public void testDynamicAxisJms4() {
193
// testAsyncOPs("SOAPJMSPort4", "axis");
194
// }
195
// public void testDynamicAxisJms5() {
196
// testAsyncOPs("SOAPJMSPort5", "axis");
197
// }
198
public void testDynamicAxisJms6() {
199           testAsyncOPs("SOAPJMSPort6", "axis");
200        }
201
202     // public void testDynamicNativeJms() {
203
// doitDyn("NativeJmsPort1", "" );
204
// }
205
// public void testDynamicNativeJms() {
206
// doitDyn("NativeJmsPort2", "" );
207
// }
208
// public void testDynamicNativeJms() {
209
// doitDyn("NativeJmsPort3", "" );
210
// }
211
// public void testDynamicNativeJms() {
212
// doitDyn("NativeJmsPort4", "" );
213
// }
214
// public void testDynamicNativeJms() {
215
// doitDyn("NativeJmsPort5", "" );
216
// }
217
// public void testDynamicNativeJms() {
218
// doitDyn("NativeJmsPort6", "" );
219
// }
220
//public void testDynamicNativeJms() {
221
// testAsyncOPs("NativeJmsPort1", "");
222
//}
223
// public void testDynamicNativeJms() {
224
// testAsyncOPs("NativeJmsPort2", "" );
225
// }
226
// public void testDynamicNativeJms() {
227
// testAsyncOPs("NativeJmsPort3", "" );
228
// }
229
// public void testDynamicNativeJms() {
230
// testAsyncOPs("NativeJmsPort4", "" );
231
// }
232
// public void testDynamicNativeJms() {
233
// testAsyncOPs("NativeJmsPort5", "" );
234
// }
235
// public void testDynamicNativeJms() {
236
// testAsyncOPs("NativeJmsPort6", "" );
237
// }
238

239     private void doitDyn(String JavaDoc portName, String JavaDoc protocol) {
240         if (portName.toUpperCase().indexOf("JMS") != -1
241             && !TestUtilities.areWeTesting("jms"))
242             return;
243
244         TestUtilities.setProviderForProtocol(protocol);
245
246         try {
247             WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
248             WSIFService service =
249                 factory.getService(
250                     wsdlLocation,
251                     null,
252                     null,
253                     "http://wsifservice.addressbook/",
254                     "AddressBook");
255
256             service.mapType(
257                 new javax.xml.namespace.QName JavaDoc(
258                     "http://wsiftypes.addressbook/",
259                     "address"),
260                 Class.forName("addressbook.wsiftypes.Address"));
261
262             service.mapType(
263                 new javax.xml.namespace.QName JavaDoc(
264                     "http://wsiftypes.addressbook/",
265                     "phone"),
266                 Class.forName("addressbook.wsiftypes.Phone"));
267
268             WSIFPort port = service.getPort(portName);
269
270             WSIFOperation operation =
271                 port.createOperation("getAddressFromName");
272
273             // Create the messages
274
WSIFMessage inputMessage = operation.createInputMessage();
275             WSIFMessage outputMessage = operation.createOutputMessage();
276             WSIFMessage faultMessage = operation.createFaultMessage();
277
278             // Set the name to find in the addressbook
279
String JavaDoc nameToLookup = "Chris P. Bacon";
280             inputMessage.setObjectPart("name", nameToLookup);
281
282             if ("SOAPJMSPort2".equals(portName)
283                 || "NativeJmsPort2".equals(portName)) {
284                 inputMessage.setObjectPart("syncTimeout", "" + sps2);
285             }
286             if ("SOAPJMSPort3".equals(portName)
287                 || "NativeJmsPort3".equals(portName)
288                 || "SOAPJMSPort6".equals(portName)
289                 || "NativeJmsPort6".equals(portName)) {
290                 WSIFMessage context = operation.getContext();
291                 context.setObjectPart(
292                     WSIFConstants.WSIF_PROP_SYNC_TIMEOUT,
293                     "" + sps3);
294                 operation.setContext(context);
295             }
296
297             // Execute the operation
298
Date JavaDoc first = new Date JavaDoc();
299             boolean operationSucceeded =
300                 operation.executeRequestResponseOperation(
301                     inputMessage,
302                     outputMessage,
303                     faultMessage);
304
305             if (operationSucceeded) {
306                 assertTrue("it didn't timeout!!", false);
307             } else {
308                 Date JavaDoc second = new Date JavaDoc();
309
310                 DateFormat JavaDoc df = DateFormat.getDateTimeInstance();
311                 long diff = second.getTime() - first.getTime();
312                 System.out.println("diff=" + diff);
313                 // assertTrue("lookup op failed!!", false);
314
}
315
316         } catch (Exception JavaDoc e) {
317             e.printStackTrace();
318             assertTrue("exception running tests-" + e.getMessage(), false);
319         }
320
321     }
322
323     private void testAsyncOPs(String JavaDoc portName, String JavaDoc protocol) {
324         if (portName.toUpperCase().indexOf("JMS") != -1
325             && !TestUtilities.areWeTesting("jms"))
326             return;
327
328         TestUtilities.setProviderForProtocol(protocol);
329
330         try {
331             WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
332             WSIFService service =
333                 factory.getService(
334                     wsdlLocation,
335                     null,
336                     null,
337                     "http://wsifservice.addressbook/",
338                     "AddressBook");
339
340             service.mapType(
341                 new javax.xml.namespace.QName JavaDoc(
342                     "http://wsiftypes.addressbook/",
343                     "address"),
344                 Class.forName("addressbook.wsiftypes.Address"));
345
346             service.mapType(
347                 new javax.xml.namespace.QName JavaDoc(
348                     "http://wsiftypes.addressbook/",
349                     "phone"),
350                 Class.forName("addressbook.wsiftypes.Phone"));
351
352             WSIFPort port =
353                 (portName == null)
354                     ? service.getPort()
355                     : service.getPort(portName);
356
357             if (!port.supportsAsync()) {
358                 return;
359             }
360
361             WSIFOperation op = port.createOperation("getAddressFromName");
362
363             AsyncResponseHandler abHandler = new AsyncResponseHandler(1);
364             // 1 async call
365

366             WSIFMessage inMsg = op.createInputMessage();
367             inMsg.setObjectPart("name", "fred");
368
369             WSIFMessage outmsg = op.createOutputMessage();
370             WSIFMessage faultMsg = op.createFaultMessage();
371
372             WSIFMessage context = op.getContext();
373             context.setObjectPart(
374                 WSIFConstants.CONTEXT_JMS_PREFIX + "JMSReplyTo",
375                 TestUtilities.getWsifProperty("wsif.async.replytoq"));
376
377             if ("SOAPJMSPort2".equals(portName)
378                 || "NativeJmsPort2".equals(portName)) {
379                 inMsg.setObjectPart("asyncTimeout", "" + spa2);
380             }
381             if ("SOAPJMSPort3".equals(portName)
382                 || "NativeJmsPort3".equals(portName)
383                 || "SOAPJMSPort6".equals(portName)
384                 || "NativeJmsPort6".equals(portName)) {
385                 context.setObjectPart(
386                     WSIFConstants.WSIF_PROP_ASYNC_TIMEOUT,
387                     "" + spa3);
388             }
389
390             op.setContext(context);
391
392             WSIFCorrelationId id =
393                 op.executeRequestResponseAsync(inMsg, abHandler);
394             assertTrue(
395                 "null correlation id returned from async request!",
396                 id != null);
397
398             int i = 30;
399             while (i-- > 0 && !abHandler.isDone()) {
400                 System.out.println("waiting for async responses - " + i);
401                 try {
402                     Thread.sleep(1000);
403                 } catch (InterruptedException JavaDoc ex) {
404                 }
405             }
406
407         } catch (Exception JavaDoc ex) {
408             ex.printStackTrace();
409             assertTrue("exception making async request!!", false);
410         }
411     }
412     /*
413             Date first = new Date();
414             try {
415                 stub.addEntry(name, addr, deliveryMode);
416             } catch (WSIFException we) {
417                 System.out.println("Caught expected " + we);
418                 caught = true;
419             }
420             assertTrue(caught);
421             Date second = new Date();
422     
423             DateFormat df = DateFormat.getDateTimeInstance();
424             System.out.println("first date was " + df.format(first));
425             System.out.println("second date was " + df.format(second));
426     
427             // getTime() returns milliseconds.
428             long firstTime = first.getTime();
429             long secondTime = second.getTime();
430             System.out.println("first time was " + firstTime);
431             System.out.println("second time was " + secondTime);
432     
433             long timeout = WSIFProperties.getSyncTimeout();
434             assertTrue(firstTime<secondTime);
435             assertTrue((secondTime-firstTime)>timeout);
436             assertTrue((secondTime-firstTime)<(timeout*2));
437     */

438
439 }
440
Popular Tags