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 addressbook;
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.WSIFException;
067: import org.apache.wsif.WSIFMessage;
068: import org.apache.wsif.WSIFOperation;
069: import org.apache.wsif.WSIFPort;
070: import org.apache.wsif.WSIFService;
071: import org.apache.wsif.WSIFServiceFactory;
072: import org.apache.wsif.providers.soap.apacheaxis.WSIFDynamicProvider_ApacheAxis;
073: import org.apache.wsif.util.WSIFPluggableProviders;
074: import util.AddressUtility;
075: import util.TestUtilities;
076:
077: import addressbook.wsifservice.AddressBook;
078: import addressbook.wsiftypes.Address;
079: import addressbook.wsiftypes.Phone;
080: import async.AsyncResponseHandler;
081:
082: /**
083: * Junit test to test out the AddressBook sample.
084: * @author Mark Whitlock <whitlock@apache.org>
085: */
086: public class AddressBookTest extends TestCase {
087: String wsdlLocation = TestUtilities
088: .getWsdlPath("java\\test\\addressbook\\wsifservice")
089: + "AddressBook.wsdl";
090: static String server = TestUtilities.getSoapServer().toUpperCase();
091:
092: static String name1 = "Purdue Boilermaker";
093: static Address addr1 = new Address(1, "University Drive",
094: "West Lafayette", "IN", 47907,
095: new Phone(765, "494", "4900"));
096:
097: static String firstName2 = "Someone";
098: static String lastName2 = "Else";
099: static Address addr2 = new Address(0, "Somewhere Else", "No Where",
100: "NO", 71983, new Phone(600, "391", "5682"));
101:
102: public AddressBookTest(String name) {
103: super (name);
104: }
105:
106: public static void main(String[] args) {
107: TestUtilities.startListeners(TestUtilities.ADDRESSBOOK_LISTENER
108: | TestUtilities.ASYNC_LISTENER
109: | TestUtilities.NATIVEJMS_LISTENER);
110:
111: junit.textui.TestRunner.run(suite());
112: TestUtilities.stopListeners();
113: }
114:
115: public static Test suite() {
116: return new TestSuite(AddressBookTest.class);
117: }
118:
119: public void setUp() {
120: TestUtilities.setUpExtensionsAndProviders();
121: }
122:
123: public void testAxis() {
124: doit(server + "Port", "axis");
125: }
126:
127: public void testSoap() {
128: doit(server + "Port", "soap");
129: }
130:
131: public void testJava() {
132: doit("JavaPort", "java");
133: }
134:
135: public void testSoapJms() {
136: doit("SOAPJMSPort", "soap");
137: }
138:
139: public void testAxisJms() {
140: doit("SOAPJMSPort", "axis");
141: }
142:
143: public void testNativeJms() {
144: doit("NativeJmsPort", "");
145: }
146:
147: public void testDynamicSOAP() {
148: doitDyn(server + "Port", "soap");
149: }
150:
151: public void testDynamicAxis() {
152: doitDyn(server + "Port", "axis");
153: }
154:
155: public void testDynamicJava() {
156: doitDyn("JavaPort", "java");
157: }
158:
159: public void testDynamicSoapJms() {
160: doitDyn("SOAPJMSPort", "soap");
161: }
162:
163: public void testDynamicAxisJms() {
164: doitDyn("SOAPJMSPort", "axis");
165: }
166:
167: public void testDynamicNativeJms() {
168: doitDyn("NativeJmsPort", "");
169: }
170:
171: private void doit(String portName, String protocol) {
172: if (portName.toUpperCase().indexOf("JMS") != -1
173: && !TestUtilities.areWeTesting("jms"))
174: return;
175:
176: TestUtilities.setProviderForProtocol(protocol);
177:
178: try {
179: WSIFServiceFactory factory = WSIFServiceFactory
180: .newInstance();
181: WSIFService service = factory.getService(wsdlLocation,
182: null, // serviceNS
183: null, // serviceName
184: "http://wsifservice.addressbook/", // portTypeNS
185: "AddressBook"); // portTypeName
186:
187: AddressBook abStub = (AddressBook) service.getStub(
188: portName, AddressBook.class);
189:
190: abStub.addEntry(name1, addr1);
191: abStub.addEntry(firstName2, lastName2, addr2);
192:
193: Address resp1 = abStub.getAddressFromName(name1);
194: assertTrue(new AddressUtility(addr1).equals(resp1));
195:
196: Address resp2 = abStub.getAddressFromName(firstName2 + " "
197: + lastName2);
198: assertTrue(new AddressUtility(addr2).equals(resp2));
199:
200: if (TestUtilities.areWeTesting("async")) {
201: testAsyncOPs(service, portName, name1, addr1);
202: testAsyncOPs(service, portName, firstName2 + " "
203: + lastName2, addr2);
204: }
205:
206: } catch (Exception e) {
207: System.err.println("AddressBookTest(" + portName
208: + ") caught exception " + e);
209: e.printStackTrace();
210: assertTrue(false);
211: } finally {
212: TestUtilities.resetDefaultProviders();
213: }
214: }
215:
216: private void doitDyn(String portName, String protocol) {
217: if (portName.toUpperCase().indexOf("JMS") != -1
218: && !TestUtilities.areWeTesting("jms"))
219: return;
220:
221: TestUtilities.setProviderForProtocol(protocol);
222:
223: try {
224: WSIFServiceFactory factory = WSIFServiceFactory
225: .newInstance();
226: WSIFService service = factory.getService(wsdlLocation,
227: null, // serviceNS
228: null, // serviceName
229: "http://wsifservice.addressbook/", // portTypeNS
230: "AddressBook"); // portTypeName
231:
232: service.mapType(new javax.xml.namespace.QName(
233: "http://wsiftypes.addressbook/", "address"), Class
234: .forName("addressbook.wsiftypes.Address"));
235:
236: service.mapType(new javax.xml.namespace.QName(
237: "http://wsiftypes.addressbook/", "phone"), Class
238: .forName("addressbook.wsiftypes.Phone"));
239:
240: WSIFPort port = null;
241:
242: port = service.getPort(portName);
243:
244: WSIFOperation operation = port.createOperation("addEntry",
245: "AddEntryWholeNameRequest", null);
246:
247: WSIFMessage inputMessage = operation.createInputMessage();
248: WSIFMessage outputMessage = operation.createOutputMessage();
249: WSIFMessage faultMessage = operation.createFaultMessage();
250:
251: // Create a name and address to add to the addressbook
252: String nameToAdd = "Chris P. Bacon";
253: Address addressToAdd = new Address(1, "The Waterfront",
254: "Some City", "NY", 47907, new Phone(765, "494",
255: "4900"));
256:
257: // Add the name and address to the input message
258: inputMessage.setObjectPart("name", nameToAdd);
259: inputMessage.setObjectPart("address", addressToAdd);
260:
261: // Execute the operation, obtaining a flag to indicate its success
262: operation.executeInputOnlyOperation(inputMessage);
263:
264: // Start from fresh
265: operation = null;
266: inputMessage = null;
267: outputMessage = null;
268: faultMessage = null;
269:
270: operation = port.createOperation("getAddressFromName");
271:
272: // Create the messages
273: inputMessage = operation.createInputMessage();
274: outputMessage = operation.createOutputMessage();
275: faultMessage = operation.createFaultMessage();
276:
277: // Set the name to find in the addressbook
278: String nameToLookup = "Chris P. Bacon";
279: inputMessage.setObjectPart("name", nameToLookup);
280:
281: // Execute the operation
282: boolean operationSucceeded = operation
283: .executeRequestResponseOperation(inputMessage,
284: outputMessage, faultMessage);
285:
286: if (operationSucceeded) {
287: System.out.println("Successfull lookup of name '"
288: + nameToLookup + "' in addressbook");
289:
290: // We can obtain the address that was found by querying the output message
291: Address addressFound = (Address) outputMessage
292: .getObjectPart("address");
293: System.out.println("The address found was:");
294: System.out.println(addressFound);
295: } else {
296: System.out
297: .println("Failed to lookup name in addressbook");
298: }
299:
300: // Check that we can't reuse an operation.
301: boolean caughtException = false;
302: try {
303: operationSucceeded = operation
304: .executeRequestResponseOperation(inputMessage,
305: outputMessage, faultMessage);
306: } catch (WSIFException we) {
307: caughtException = true;
308: }
309: assertTrue(caughtException);
310:
311: } catch (Exception e) {
312: System.err.println("AddressBookTest(" + portName
313: + ") caught exception " + e);
314: e.printStackTrace();
315: assertTrue(false);
316: } finally {
317: TestUtilities.resetDefaultProviders();
318: }
319: }
320:
321: private void testAsyncOPs(WSIFService service, String portName,
322: String name, Address addr) {
323: try {
324: WSIFPort port = (portName == null) ? service.getPort()
325: : service.getPort(portName);
326:
327: if (!port.supportsAsync()) {
328: return;
329: }
330:
331: WSIFOperation op = port
332: .createOperation("getAddressFromName");
333:
334: AsyncResponseHandler abHandler = new AsyncResponseHandler(1); // 1 async call
335:
336: WSIFMessage inMsg = op.createInputMessage();
337: inMsg.setObjectPart("name", name);
338:
339: WSIFMessage outmsg = op.createOutputMessage();
340: WSIFMessage faultMsg = op.createFaultMessage();
341:
342: WSIFMessage context = op.getContext();
343: context.setObjectPart(WSIFConstants.CONTEXT_JMS_PREFIX
344: + "JMSReplyTo", TestUtilities
345: .getWsifProperty("wsif.async.replytoq"));
346: op.setContext(context);
347:
348: WSIFCorrelationId id = op.executeRequestResponseAsync(
349: inMsg, abHandler);
350: assertTrue(
351: "null correlation id returned from async request!",
352: id != null);
353:
354: int i = 10;
355: while (i-- > 0 && !abHandler.isDone()) {
356: System.out
357: .println("waiting for async responses - " + i);
358: try {
359: Thread.sleep(3000);
360: } catch (InterruptedException ex) {
361: }
362: }
363: assertTrue("no response from async operation!", i > 0); // no responses in time
364:
365: WSIFMessage[] faults = abHandler.getFaults();
366: WSIFMessage[] outputs = abHandler.getOutputs();
367: Address respAddr = (Address) outputs[0]
368: .getObjectPart("address");
369: assertTrue("incorrect address response!", addr
370: .equals(respAddr));
371:
372: } catch (Exception ex) {
373: ex.printStackTrace();
374: assertTrue("exception making async request!!", false);
375: }
376: }
377:
378: }
|