KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > addressbook > AddressBookTest


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 addressbook;
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.WSIFException;
67 import org.apache.wsif.WSIFMessage;
68 import org.apache.wsif.WSIFOperation;
69 import org.apache.wsif.WSIFPort;
70 import org.apache.wsif.WSIFService;
71 import org.apache.wsif.WSIFServiceFactory;
72 import org.apache.wsif.providers.soap.apacheaxis.WSIFDynamicProvider_ApacheAxis;
73 import org.apache.wsif.util.WSIFPluggableProviders;
74 import util.AddressUtility;
75 import util.TestUtilities;
76
77 import addressbook.wsifservice.AddressBook;
78 import addressbook.wsiftypes.Address;
79 import addressbook.wsiftypes.Phone;
80 import async.AsyncResponseHandler;
81
82 /**
83  * Junit test to test out the AddressBook sample.
84  * @author Mark Whitlock <whitlock@apache.org>
85  */

86 public class AddressBookTest extends TestCase {
87     String JavaDoc wsdlLocation =
88         TestUtilities.getWsdlPath("java\\test\\addressbook\\wsifservice")
89             + "AddressBook.wsdl";
90     static String JavaDoc server = TestUtilities.getSoapServer().toUpperCase();
91
92     static String JavaDoc name1 = "Purdue Boilermaker";
93     static Address addr1 =
94         new Address(
95             1,
96             "University Drive",
97             "West Lafayette",
98             "IN",
99             47907,
100             new Phone(765, "494", "4900"));
101
102     static String JavaDoc firstName2 = "Someone";
103     static String JavaDoc lastName2 = "Else";
104     static Address addr2 =
105         new Address(
106             0,
107             "Somewhere Else",
108             "No Where",
109             "NO",
110             71983,
111             new Phone(600, "391", "5682"));
112
113     public AddressBookTest(String JavaDoc name) {
114         super(name);
115     }
116
117     public static void main(String JavaDoc[] args) {
118         TestUtilities.startListeners(
119             TestUtilities.ADDRESSBOOK_LISTENER
120                 | TestUtilities.ASYNC_LISTENER
121                 | TestUtilities.NATIVEJMS_LISTENER);
122
123         junit.textui.TestRunner.run(suite());
124         TestUtilities.stopListeners();
125     }
126
127     public static Test suite() {
128         return new TestSuite(AddressBookTest.class);
129     }
130
131     public void setUp() {
132         TestUtilities.setUpExtensionsAndProviders();
133     }
134
135     public void testAxis() {
136         doit(server+"Port", "axis");
137     }
138     public void testSoap() {
139         doit(server+"Port", "soap");
140     }
141     public void testJava() {
142         doit("JavaPort", "java");
143     }
144     public void testSoapJms() {
145         doit("SOAPJMSPort", "soap");
146     }
147     public void testAxisJms() {
148         doit("SOAPJMSPort", "axis");
149     }
150     public void testNativeJms() {
151         doit("NativeJmsPort", "" );
152     }
153     public void testDynamicSOAP() {
154         doitDyn(server+"Port", "soap");
155     }
156     public void testDynamicAxis() {
157         doitDyn(server+"Port", "axis");
158     }
159     public void testDynamicJava() {
160         doitDyn("JavaPort", "java");
161     }
162     public void testDynamicSoapJms() {
163         doitDyn("SOAPJMSPort", "soap");
164     }
165     public void testDynamicAxisJms() {
166         doitDyn("SOAPJMSPort", "axis");
167     }
168     public void testDynamicNativeJms() {
169         doitDyn("NativeJmsPort", "" );
170     }
171
172     private void doit(String JavaDoc portName, String JavaDoc protocol) {
173         if (portName.toUpperCase().indexOf("JMS") != -1
174             && !TestUtilities.areWeTesting("jms"))
175             return;
176
177         TestUtilities.setProviderForProtocol( protocol );
178
179         try {
180             WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
181             WSIFService service = factory.getService(wsdlLocation, null, // serviceNS
182
null, // serviceName
183
"http://wsifservice.addressbook/", // portTypeNS
184
"AddressBook" ); // portTypeName
185

186             AddressBook abStub = (AddressBook) service.getStub(portName, AddressBook.class);
187
188             abStub.addEntry(name1, addr1);
189             abStub.addEntry(firstName2, lastName2, addr2);
190
191             Address resp1 = abStub.getAddressFromName(name1);
192             assertTrue(new AddressUtility(addr1).equals(resp1));
193
194             Address resp2 = abStub.getAddressFromName(firstName2 + " " + lastName2);
195             assertTrue(new AddressUtility(addr2).equals(resp2));
196             
197             if (TestUtilities.areWeTesting("async")) {
198                 testAsyncOPs( service, portName, name1, addr1 );
199                 testAsyncOPs( service, portName, firstName2 + " " + lastName2, addr2 );
200             }
201             
202         } catch (Exception JavaDoc e) {
203             System.err.println("AddressBookTest(" + portName + ") caught exception " + e);
204             e.printStackTrace();
205             assertTrue(false);
206         } finally {
207             TestUtilities.resetDefaultProviders();
208         }
209     }
210
211     private void doitDyn(String JavaDoc portName, String JavaDoc protocol) {
212         if (portName.toUpperCase().indexOf("JMS") != -1 && !TestUtilities.areWeTesting("jms"))
213             return;
214
215         TestUtilities.setProviderForProtocol( protocol );
216
217         try {
218             WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
219                 WSIFService service = factory.getService(wsdlLocation, null, // serviceNS
220
null, // serviceName
221
"http://wsifservice.addressbook/", // portTypeNS
222
"AddressBook"); // portTypeName
223

224             service.mapType(
225                 new javax.xml.namespace.QName JavaDoc(
226                     "http://wsiftypes.addressbook/",
227                     "address"),
228                 Class.forName("addressbook.wsiftypes.Address"));
229
230             service.mapType(
231                 new javax.xml.namespace.QName JavaDoc(
232                     "http://wsiftypes.addressbook/",
233                     "phone"),
234                 Class.forName("addressbook.wsiftypes.Phone"));
235
236             WSIFPort port = null;
237
238             port = service.getPort(portName);
239
240             WSIFOperation operation =
241                 port.createOperation("addEntry", "AddEntryWholeNameRequest", null);
242
243             WSIFMessage inputMessage = operation.createInputMessage();
244             WSIFMessage outputMessage = operation.createOutputMessage();
245             WSIFMessage faultMessage = operation.createFaultMessage();
246
247             // Create a name and address to add to the addressbook
248
String JavaDoc nameToAdd = "Chris P. Bacon";
249             Address addressToAdd =
250                 new Address(
251                     1,
252                     "The Waterfront",
253                     "Some City",
254                     "NY",
255                     47907,
256                     new Phone(765, "494", "4900"));
257
258             // Add the name and address to the input message
259
inputMessage.setObjectPart("name", nameToAdd);
260             inputMessage.setObjectPart("address", addressToAdd);
261
262             // Execute the operation, obtaining a flag to indicate its success
263
operation.executeInputOnlyOperation(inputMessage);
264
265             // Start from fresh
266
operation = null;
267             inputMessage = null;
268             outputMessage = null;
269             faultMessage = null;
270
271             operation = port.createOperation("getAddressFromName");
272
273             // Create the messages
274
inputMessage = operation.createInputMessage();
275             outputMessage = operation.createOutputMessage();
276             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             // Execute the operation
283
boolean operationSucceeded =
284                 operation.executeRequestResponseOperation(
285                     inputMessage,
286                     outputMessage,
287                     faultMessage);
288
289             if (operationSucceeded) {
290                 System.out.println(
291                     "Successfull lookup of name '" + nameToLookup + "' in addressbook");
292
293                 // We can obtain the address that was found by querying the output message
294
Address addressFound = (Address) outputMessage.getObjectPart("address");
295                 System.out.println("The address found was:");
296                 System.out.println(addressFound);
297             } else {
298                 System.out.println("Failed to lookup name in addressbook");
299             }
300
301             // Check that we can't reuse an operation.
302
boolean caughtException = false;
303             try {
304                 operationSucceeded =
305                     operation.executeRequestResponseOperation(
306                         inputMessage,
307                         outputMessage,
308                         faultMessage);
309             } catch (WSIFException we) {
310                 caughtException = true;
311             }
312             assertTrue(caughtException);
313                 
314         } catch (Exception JavaDoc e) {
315             System.err.println("AddressBookTest(" + portName + ") caught exception " + e);
316             e.printStackTrace();
317             assertTrue(false);
318         } finally {
319             TestUtilities.resetDefaultProviders();
320         }
321     }
322
323     private void testAsyncOPs(WSIFService service, String JavaDoc portName, String JavaDoc name, Address addr) {
324        try {
325             WSIFPort port =
326                 (portName == null) ? service.getPort() : service.getPort(portName);
327
328             if (!port.supportsAsync() ) {
329                 return;
330             }
331
332             WSIFOperation op = port.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 + "JMSReplyTo",
344                                    TestUtilities.getWsifProperty("wsif.async.replytoq") );
345             op.setContext( context );
346
347             WSIFCorrelationId id = op.executeRequestResponseAsync(inMsg, abHandler);
348             assertTrue("null correlation id returned from async request!", id != null );
349
350             int i = 10;
351             while (i-- > 0 && !abHandler.isDone()) {
352                 System.out.println( "waiting for async responses - " + i );
353                 try {
354                     Thread.sleep(3000);
355                 } catch (InterruptedException JavaDoc ex) {
356                 }
357             }
358             assertTrue("no response from async operation!",i > 0); // no responses in time
359

360             WSIFMessage[] faults = abHandler.getFaults();
361             WSIFMessage[] outputs = abHandler.getOutputs();
362             Address respAddr = (Address) outputs[0].getObjectPart("address");
363             assertTrue("incorrect address response!", addr.equals(respAddr));
364
365        } catch (Exception JavaDoc ex) {
366           ex.printStackTrace();
367           assertTrue( "exception making async request!!", false );
368        }
369     }
370     
371 }
372
Popular Tags