001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 2002 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "WSIF" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 2001, 2002, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package jms;
059:
060: import java.text.DateFormat;
061: import java.util.Date;
062:
063: import junit.framework.Test;
064: import junit.framework.TestCase;
065: import junit.framework.TestSuite;
066:
067: import org.apache.wsif.WSIFConstants;
068: import org.apache.wsif.WSIFCorrelationId;
069: import org.apache.wsif.WSIFException;
070: import org.apache.wsif.WSIFMessage;
071: import org.apache.wsif.WSIFOperation;
072: import org.apache.wsif.WSIFPort;
073: import org.apache.wsif.WSIFService;
074: import org.apache.wsif.WSIFServiceFactory;
075: import org.apache.wsif.util.WSIFProperties;
076: import util.AddressUtility;
077: import util.TestUtilities;
078:
079: import addressbook.wsifservice.AddressBook;
080: import addressbook.wsiftypes.Address;
081: import addressbook.wsiftypes.Phone;
082: import async.AsyncResponseHandler;
083:
084: /**
085: * Junit test to test out the JMS sync and async timeouts.
086: * This doesn't work so well yet and you have to run each test
087: * manually and check the timeout is correct. the only way to
088: * do this for async ops is to add a system.out.println to the
089: * correlation service to show what timeout value its using.
090: *
091: * @author <a href="mailto:ant.elder@uk.ibm.com">Ant Elder</a>
092: */
093: public class JMSTimeoutTest extends TestCase {
094: String wsdlLocation = TestUtilities.getWsdlPath("java\\test\\jms")
095: + "Timeouts.wsdl";
096:
097: private static final long sps1 = 7000;
098: private static final long spa1 = 11000;
099: 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 name) {
111: super (name);
112: }
113:
114: public static void main(String[] 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 portName, String 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
248: .newInstance();
249: WSIFService service = factory.getService(wsdlLocation,
250: null, null, "http://wsifservice.addressbook/",
251: "AddressBook");
252:
253: service.mapType(new javax.xml.namespace.QName(
254: "http://wsiftypes.addressbook/", "address"), Class
255: .forName("addressbook.wsiftypes.Address"));
256:
257: service.mapType(new javax.xml.namespace.QName(
258: "http://wsiftypes.addressbook/", "phone"), Class
259: .forName("addressbook.wsiftypes.Phone"));
260:
261: WSIFPort port = service.getPort(portName);
262:
263: WSIFOperation operation = port
264: .createOperation("getAddressFromName");
265:
266: // Create the messages
267: WSIFMessage inputMessage = operation.createInputMessage();
268: WSIFMessage outputMessage = operation.createOutputMessage();
269: WSIFMessage faultMessage = operation.createFaultMessage();
270:
271: // Set the name to find in the addressbook
272: String nameToLookup = "Chris P. Bacon";
273: inputMessage.setObjectPart("name", nameToLookup);
274:
275: if ("SOAPJMSPort2".equals(portName)
276: || "NativeJmsPort2".equals(portName)) {
277: inputMessage.setObjectPart("syncTimeout", "" + sps2);
278: }
279: if ("SOAPJMSPort3".equals(portName)
280: || "NativeJmsPort3".equals(portName)
281: || "SOAPJMSPort6".equals(portName)
282: || "NativeJmsPort6".equals(portName)) {
283: WSIFMessage context = operation.getContext();
284: context
285: .setObjectPart(
286: WSIFConstants.WSIF_PROP_SYNC_TIMEOUT,
287: "" + sps3);
288: operation.setContext(context);
289: }
290:
291: // Execute the operation
292: Date first = new Date();
293: boolean operationSucceeded = operation
294: .executeRequestResponseOperation(inputMessage,
295: outputMessage, faultMessage);
296:
297: if (operationSucceeded) {
298: assertTrue("it didn't timeout!!", false);
299: } else {
300: Date second = new Date();
301:
302: DateFormat df = DateFormat.getDateTimeInstance();
303: long diff = second.getTime() - first.getTime();
304: System.out.println("diff=" + diff);
305: // assertTrue("lookup op failed!!", false);
306: }
307:
308: } catch (Exception e) {
309: e.printStackTrace();
310: assertTrue("exception running tests-" + e.getMessage(),
311: false);
312: }
313:
314: }
315:
316: private void testAsyncOPs(String portName, String protocol) {
317: if (portName.toUpperCase().indexOf("JMS") != -1
318: && !TestUtilities.areWeTesting("jms"))
319: return;
320:
321: TestUtilities.setProviderForProtocol(protocol);
322:
323: try {
324: WSIFServiceFactory factory = WSIFServiceFactory
325: .newInstance();
326: WSIFService service = factory.getService(wsdlLocation,
327: null, null, "http://wsifservice.addressbook/",
328: "AddressBook");
329:
330: service.mapType(new javax.xml.namespace.QName(
331: "http://wsiftypes.addressbook/", "address"), Class
332: .forName("addressbook.wsiftypes.Address"));
333:
334: service.mapType(new javax.xml.namespace.QName(
335: "http://wsiftypes.addressbook/", "phone"), Class
336: .forName("addressbook.wsiftypes.Phone"));
337:
338: WSIFPort port = (portName == null) ? service.getPort()
339: : service.getPort(portName);
340:
341: if (!port.supportsAsync()) {
342: return;
343: }
344:
345: WSIFOperation op = port
346: .createOperation("getAddressFromName");
347:
348: AsyncResponseHandler abHandler = new AsyncResponseHandler(1);
349: // 1 async call
350:
351: WSIFMessage inMsg = op.createInputMessage();
352: inMsg.setObjectPart("name", "fred");
353:
354: WSIFMessage outmsg = op.createOutputMessage();
355: WSIFMessage faultMsg = op.createFaultMessage();
356:
357: WSIFMessage context = op.getContext();
358: context.setObjectPart(WSIFConstants.CONTEXT_JMS_PREFIX
359: + "JMSReplyTo", TestUtilities
360: .getWsifProperty("wsif.async.replytoq"));
361:
362: if ("SOAPJMSPort2".equals(portName)
363: || "NativeJmsPort2".equals(portName)) {
364: inMsg.setObjectPart("asyncTimeout", "" + spa2);
365: }
366: if ("SOAPJMSPort3".equals(portName)
367: || "NativeJmsPort3".equals(portName)
368: || "SOAPJMSPort6".equals(portName)
369: || "NativeJmsPort6".equals(portName)) {
370: context.setObjectPart(
371: WSIFConstants.WSIF_PROP_ASYNC_TIMEOUT, ""
372: + spa3);
373: }
374:
375: op.setContext(context);
376:
377: WSIFCorrelationId id = op.executeRequestResponseAsync(
378: inMsg, abHandler);
379: assertTrue(
380: "null correlation id returned from async request!",
381: id != null);
382:
383: int i = 30;
384: while (i-- > 0 && !abHandler.isDone()) {
385: System.out
386: .println("waiting for async responses - " + i);
387: try {
388: Thread.sleep(1000);
389: } catch (InterruptedException ex) {
390: }
391: }
392:
393: } catch (Exception ex) {
394: ex.printStackTrace();
395: assertTrue("exception making async request!!", false);
396: }
397: }
398: /*
399: Date first = new Date();
400: try {
401: stub.addEntry(name, addr, deliveryMode);
402: } catch (WSIFException we) {
403: System.out.println("Caught expected " + we);
404: caught = true;
405: }
406: assertTrue(caught);
407: Date second = new Date();
408:
409: DateFormat df = DateFormat.getDateTimeInstance();
410: System.out.println("first date was " + df.format(first));
411: System.out.println("second date was " + df.format(second));
412:
413: // getTime() returns milliseconds.
414: long firstTime = first.getTime();
415: long secondTime = second.getTime();
416: System.out.println("first time was " + firstTime);
417: System.out.println("second time was " + secondTime);
418:
419: long timeout = WSIFProperties.getSyncTimeout();
420: assertTrue(firstTime<secondTime);
421: assertTrue((secondTime-firstTime)>timeout);
422: assertTrue((secondTime-firstTime)<(timeout*2));
423: */
424:
425: }
|