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 async;
059:
060: import junit.framework.Test;
061: import junit.framework.TestCase;
062: import junit.framework.TestSuite;
063:
064: import org.apache.wsif.WSIFConstants;
065: import org.apache.wsif.WSIFCorrelationId;
066: import org.apache.wsif.WSIFMessage;
067: import org.apache.wsif.WSIFOperation;
068: import org.apache.wsif.WSIFPort;
069: import org.apache.wsif.WSIFService;
070: import org.apache.wsif.WSIFServiceFactory;
071: import util.TestUtilities;
072:
073: import addressbook.wsiftypes.Address;
074: import addressbook.wsiftypes.Phone;
075:
076: /**
077: * Junit test to test the NativeJMSProvider
078: *
079: * need to use the wsifjmssetup.bat and wsifmqset.bat
080: *
081: */
082: public class AsyncTests extends TestCase {
083:
084: public static String name1 = "Purdue Boilermaker";
085: public static Address addr1 = new Address(1, "University Drive",
086: "West Lafayette", "IN", 47907,
087: new Phone(765, "494", "4900"));
088:
089: public AsyncTests(String name) {
090: super (name);
091: }
092:
093: public static void main(String[] args) {
094: TestUtilities.startListeners();
095: junit.textui.TestRunner.run(suite());
096: TestUtilities.stopListeners();
097: }
098:
099: public static Test suite() {
100: return new TestSuite(AsyncTests.class);
101: }
102:
103: public void setUp() {
104: TestUtilities.setUpExtensionsAndProviders();
105: }
106:
107: public void testSoapJms() {
108: if (TestUtilities.areWeTesting("jms")) {
109: doitStockquote("SOAPJMSPort", "soap");
110: doitAddressBook_addEntry("SOAPJMSPort", "soap");
111: doitAddressBook_getAddressFromName("SOAPJMSPort", "soap");
112: }
113: }
114:
115: public void testAxisJms() {
116: if (TestUtilities.areWeTesting("jms")) {
117: doitStockquote("SOAPJMSPort", "axis");
118: doitAddressBook_addEntry("SOAPJMSPort", "axis");
119: doitAddressBook_getAddressFromName("SOAPJMSPort", "axis");
120: }
121: }
122:
123: public void testNativeJms() {
124: if (TestUtilities.areWeTesting("jms")) {
125: doitStockquote("NativeJmsPort", "");
126: doitAddressBook_addEntry("NativeJmsPort", "");
127: doitAddressBook_getAddressFromName("NativeJmsPort", "");
128: }
129: }
130:
131: /**
132: * Query Stockquote sample
133: */
134: public void doitStockquote(String portName, String protocol) {
135: if (portName.toUpperCase().indexOf("JMS") != -1
136: && !TestUtilities.areWeTesting("jms")) {
137: return;
138: }
139:
140: TestUtilities.setProviderForProtocol(protocol);
141:
142: String portType = "StockquotePT";
143: String operationName = "getQuote";
144:
145: String wsdlLocation = TestUtilities
146: .getWsdlPath("java\\test\\stockquote\\wsifservice")
147: + "StockQuote.wsdl";
148:
149: System.out.println("\n=== StockQuote");
150: try {
151: WSIFServiceFactory factory = WSIFServiceFactory
152: .newInstance();
153: WSIFService service = factory.getService(wsdlLocation,
154: null, // serviceNS
155: null, // serviceName
156: "http://wsifservice.stockquote/", // portTypeNS
157: portType); // portTypeName
158:
159: WSIFPort port = service.getPort(portName);
160:
161: // Executing synchronous executeRequestResponseOperation(input, output, fault )
162: WSIFOperation operation = port
163: .createOperation(operationName);
164:
165: WSIFMessage context = operation.getContext();
166: context.setObjectPart(WSIFConstants.CONTEXT_JMS_PREFIX
167: + "JMSReplyTo", TestUtilities
168: .getWsifProperty("wsif.nativejms.responseq"));
169: operation.setContext(context);
170:
171: WSIFMessage input = operation.createInputMessage();
172: input.setName("GetQuoteInput");
173: input.setObjectPart("symbol", "");
174:
175: WSIFMessage output = operation.createOutputMessage();
176: WSIFMessage fault = operation.createFaultMessage();
177:
178: doSyncOp(operation, input, output, context);
179: float value = ((Float) output.getObjectPart("quote"))
180: .floatValue();
181: System.out
182: .println("sync stockquote found value = " + value);
183: assertTrue("doSyncOp stockquote value incorrect!",
184: value == -1.0F);
185:
186: // Executing executeRequestResponseAsync(input, handler)
187: operation = port.createOperation(operationName);
188:
189: operation.setContext(context);
190:
191: input = operation.createInputMessage();
192: input.setName("GetQuoteInput");
193: input.setObjectPart("symbol", "");
194:
195: output = doAsyncOp(operation, input, context);
196: value = ((Float) output.getObjectPart("quote"))
197: .floatValue();
198: System.out.println("async stockquote found value = "
199: + value);
200: assertTrue("doAsyncOp stockquote value incorrect!",
201: value == -1.0F);
202:
203: // Executing executeRequestResponseAsync(input)
204: operation = port.createOperation(operationName);
205:
206: operation.setContext(context);
207:
208: input = operation.createInputMessage();
209: input.setName("GetQuoteInput");
210: input.setObjectPart("symbol", "");
211:
212: output = doAsyncOpNoHandler(operation, input, context);
213: value = ((Float) output.getObjectPart("quote"))
214: .floatValue();
215: System.out.println("async stockquote found value = "
216: + value);
217: assertTrue(
218: "doAsyncOpNoHandler stockquote value incorrect!",
219: value == -1.0F);
220:
221: } catch (Exception e) {
222: e.printStackTrace();
223: assertTrue("exception during stockquote test: "
224: + e.getMessage(), false);
225: }
226: }
227:
228: /**
229: * AddressBook sample - addEntry
230: */
231: public void doitAddressBook_addEntry(String portName,
232: String protocol) {
233:
234: if (portName.indexOf("JMS") != -1
235: && !TestUtilities.areWeTesting("jms")) {
236: return;
237: }
238:
239: TestUtilities.setProviderForProtocol(protocol);
240:
241: String portType = "AddressBook";
242: String operationName = "addEntry";
243:
244: String wsdlLocation = TestUtilities
245: .getWsdlPath("java\\test\\addressbook\\wsifservice")
246: + "AddressBook.wsdl";
247:
248: System.out.println("\n=== AddressBook_addEntry");
249:
250: try {
251: WSIFServiceFactory factory = WSIFServiceFactory
252: .newInstance();
253: WSIFService service = factory.getService(wsdlLocation,
254: null, // serviceNS
255: null, // serviceName
256: "http://wsifservice.addressbook/", // portTypeNS
257: portType); // portTypeName
258:
259: service.mapType(new javax.xml.namespace.QName(
260: "http://wsiftypes.addressbook/", "address"), Class
261: .forName("addressbook.wsiftypes.Address"));
262:
263: service.mapType(new javax.xml.namespace.QName(
264: "http://wsiftypes.addressbook/", "phone"), Class
265: .forName("addressbook.wsiftypes.Phone"));
266:
267: WSIFPort port = service.getPort(portName);
268:
269: WSIFOperation operation = port.createOperation(
270: operationName, "AddEntryWholeNameRequest", null);
271:
272: WSIFMessage input = operation.createInputMessage();
273: input.setObjectPart("name", name1);
274: input.setObjectPart("address", addr1);
275:
276: operation.executeInputOnlyOperation(input);
277:
278: } catch (Exception e) {
279: e.printStackTrace();
280: assertTrue("exception during addressbook addEntry: "
281: + e.getMessage(), false);
282: }
283:
284: }
285:
286: /**
287: * AddressBook sample - getAddressFromName
288: */
289: public void doitAddressBook_getAddressFromName(String portName,
290: String protocol) {
291:
292: if (portName.indexOf("JMS") != -1
293: && !TestUtilities.areWeTesting("jms")) {
294: return;
295: }
296: TestUtilities.setProviderForProtocol(protocol);
297:
298: String portType = "AddressBook";
299: String operationName = "getAddressFromName";
300:
301: String wsdlLocation = TestUtilities
302: .getWsdlPath("java\\test\\addressbook\\wsifservice")
303: + "AddressBook.wsdl";
304:
305: System.out.println("\n=== AddressBook_getAddressFromName");
306:
307: try {
308: WSIFServiceFactory factory = WSIFServiceFactory
309: .newInstance();
310: WSIFService service = factory.getService(wsdlLocation,
311: null, // serviceNS
312: null, // serviceName
313: "http://wsifservice.addressbook/", // portTypeNS
314: portType); // portTypeName
315:
316: service.mapType(new javax.xml.namespace.QName(
317: "http://wsiftypes.addressbook/", "address"), Class
318: .forName("addressbook.wsiftypes.Address"));
319:
320: service.mapType(new javax.xml.namespace.QName(
321: "http://wsiftypes.addressbook/", "phone"), Class
322: .forName("addressbook.wsiftypes.Phone"));
323:
324: WSIFPort port = service.getPort(portName);
325:
326: // Executing synchronous executeRequestResponseOperation(input, output, fault )
327: String inputMsgName = "GetAddressFromNameRequest";
328: String outputMsgName = "GetAddressFromNameResponse";
329: WSIFOperation operation = port.createOperation(
330: operationName, inputMsgName, outputMsgName);
331:
332: WSIFMessage context = operation.getContext();
333: context.setObjectPart(WSIFConstants.CONTEXT_JMS_PREFIX
334: + "JMSReplyTo", TestUtilities
335: .getWsifProperty("wsif.nativejms.responseq"));
336: operation.setContext(context);
337:
338: WSIFMessage input = operation.createInputMessage();
339: input.setObjectPart("name", name1);
340:
341: WSIFMessage output = operation.createOutputMessage();
342: WSIFMessage fault = operation.createFaultMessage();
343:
344: doSyncOp(operation, input, output, context);
345:
346: Address addressResponse = (Address) output
347: .getObjectPart("address");
348: System.out.println("Found address = " + addressResponse);
349: assertTrue("doSyncOp addresses not equal!", addr1
350: .equals(addressResponse));
351:
352: // Executing executeRequestResponseOperation(input, output, fault )
353: operation = port.createOperation(operationName,
354: inputMsgName, outputMsgName);
355:
356: operation.setContext(context);
357:
358: input = operation.createInputMessage();
359: input.setObjectPart("name", name1);
360:
361: output = operation.createOutputMessage();
362:
363: output = doAsyncOp(operation, input, context);
364:
365: addressResponse = (Address) output.getObjectPart("address");
366: System.out.println("Found address = " + addressResponse);
367: assertTrue("doAsyncOp addresses not equal!", addr1
368: .equals(addressResponse));
369:
370: // Executing executeRequestResponseAsync(input)
371: operation = port.createOperation(operationName,
372: inputMsgName, outputMsgName);
373:
374: operation.setContext(context);
375:
376: input = operation.createInputMessage();
377: input.setObjectPart("name", name1);
378:
379: output = doAsyncOpNoHandler(operation, input, context);
380: addressResponse = (Address) output.getObjectPart("address");
381: System.out.println("Found address = " + addressResponse);
382: assertTrue("doAsyncOpNoHandler addresses not equal!", addr1
383: .equals(addressResponse));
384:
385: } catch (Exception e) {
386: e.printStackTrace();
387: assertTrue(
388: "exception during addressbook getAddressFromName test: "
389: + e.getMessage(), false);
390: }
391:
392: }
393:
394: private void doSyncOp(WSIFOperation op, WSIFMessage input,
395: WSIFMessage output, WSIFMessage context) {
396: try {
397: op.setContext(context);
398: WSIFMessage fault = op.createFaultMessage();
399: boolean ok = op.executeRequestResponseOperation(input,
400: output, fault);
401: assertTrue(
402: "executeRequestResponseOperation returned false!",
403: ok);
404: } catch (Exception ex) {
405: ex.printStackTrace();
406: assertTrue("exception executing request: "
407: + ex.getMessage(), false);
408: }
409: }
410:
411: private WSIFMessage doAsyncOp(WSIFOperation op, WSIFMessage input,
412: WSIFMessage context) {
413: AsyncResponseHandler handler = new AsyncResponseHandler(1); // 2 async calls
414: try {
415: context.setObjectPart(WSIFConstants.CONTEXT_JMS_PREFIX
416: + "JMSReplyTo", TestUtilities
417: .getWsifProperty("wsif.async.replytoq"));
418: op.setContext(context);
419: WSIFCorrelationId id = op.executeRequestResponseAsync(
420: input, handler);
421: System.out.println("async operation done, correlation id="
422: + id.getCorrelationId());
423: } catch (Exception ex) {
424: ex.printStackTrace();
425: assertTrue("exception executing async op: "
426: + ex.getMessage(), false);
427: }
428: int i = 5; // 15 seconds timout
429: while (i-- > 0 && !handler.isDone()) {
430: System.out
431: .println("async requests sent, waiting for responses - "
432: + i);
433: try {
434: Thread.sleep(3000);
435: } catch (InterruptedException ex) {
436: }
437: }
438: assertTrue("no response to async operation!", i > 0); // no responses in time
439: return handler.getOutputs()[0];
440: }
441:
442: private WSIFMessage doAsyncOpNoHandler(WSIFOperation op,
443: WSIFMessage input, WSIFMessage context) {
444: WSIFMessage output = null;
445: try {
446: context.setObjectPart(WSIFConstants.CONTEXT_JMS_PREFIX
447: + "JMSReplyTo", TestUtilities
448: .getWsifProperty("wsif.async.replytoq2"));
449: context.setObjectPart("testJMSnoHandler", "true");
450: op.setContext(context);
451: WSIFCorrelationId id = op
452: .executeRequestResponseAsync(input);
453: System.out.println("async operation done, correlation id="
454: + id.getCorrelationId());
455:
456: Object jmsResponse = TestUtilities.getJMSAsyncResponse(id
457: .getCorrelationId(), TestUtilities
458: .getWsifProperty("wsif.async.replytoq2"));
459:
460: output = op.createOutputMessage();
461: WSIFMessage fault = op.createFaultMessage();
462: op.processAsyncResponse(jmsResponse, output, fault);
463: } catch (Exception ex) {
464: ex.printStackTrace();
465: assertTrue("exception executing async op: "
466: + ex.getMessage(), false);
467: }
468: return output;
469: }
470:
471: }
|