KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > shop > ShoppingCartTest


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 shop;
59
60 import java.util.Collection JavaDoc;
61 import java.util.HashMap JavaDoc;
62 import java.util.Iterator JavaDoc;
63
64 import junit.framework.Test;
65 import junit.framework.TestCase;
66 import junit.framework.TestSuite;
67
68 import org.apache.wsif.WSIFMessage;
69 import org.apache.wsif.WSIFOperation;
70 import org.apache.wsif.WSIFPort;
71 import org.apache.wsif.WSIFService;
72 import org.apache.wsif.WSIFServiceFactory;
73 import util.TestUtilities;
74
75 /**
76  * Shopping cart test scenario using Java & EJB invocation.
77  *
78  * In order to run this test, set your JNDI settings using the following variables:
79  * <UL>
80  * <LI>java.naming.provider.url (javax.naming.Context.PROVIDER_URL)</LI>
81  * <LI>java.naming.factory.initial (javax.naming.Context.INITIAL_CONTEXT_FACTORY)</LI>
82  * </UL>
83  *
84  * @author Owen Burroughs
85  */

86
87 public class ShoppingCartTest extends TestCase {
88
89     private boolean debugMode = true;
90     //private boolean debugMode = false;
91
private String JavaDoc wsdlPath;
92
93     /**
94      * Starts the application.
95      * @param args an array of command-line arguments
96      */

97     public static void main(java.lang.String JavaDoc[] args) {
98        TestUtilities.startListeners();
99        junit.textui.TestRunner.run (suite());
100        TestUtilities.stopListeners();
101     }
102
103     public static Test suite() {
104         return new TestSuite(ShoppingCartTest.class);
105     }
106
107     public ShoppingCartTest(String JavaDoc arg0) {
108         super(arg0);
109     }
110
111     protected void setUp() {
112         wsdlPath = TestUtilities.getWsdlPath("java\\test\\shop");
113     }
114
115     private void printOrder(Order order) {
116         HashMap JavaDoc items = null;
117         Iterator JavaDoc it = null;
118         Collection JavaDoc entries = null;
119         Item item = null;
120         Object JavaDoc obj = null;
121
122         debug("\nOrder " + order.getConfirmationNumber());
123         items = order.getItems();
124         entries = items.values();
125         it = entries.iterator();
126         while (it.hasNext()) {
127             debug(it.next());
128         }
129     }
130
131     private void printOrders(Orders orders) {
132         int size = orders.size();
133
134         for (int i = 0; i < size; i++) {
135             printOrder((Order) orders.get(i));
136         }
137     }
138
139     /**
140      * Tests the Java binding for the shopping cart scenario via WSDL
141      */

142     public void testWSDL_Java() throws Exception JavaDoc {
143         debug("\n*** TEST JAVA BINDING ***");
144
145         WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
146             WSIFService service =
147                 factory
148                     .getService(
149                         wsdlPath + "ShoppingCartAll.wsdl",
150                         "http://www.shoppingcart.com/definitions/ShoppingCartInterface",
151         // serviceNS,
152
"ShoppingCart_JavaService", // serviceName,
153
"http://www.shoppingcart.com/definitions/ShoppingCartInterface",
154         // portTypeNS,
155
"ShoppingCart_JavaPortType" // portTypeName
156
);
157
158         Iterator JavaDoc it = service.getAvailablePortNames();
159         {
160             System.out.println("Available ports for the service are: ");
161             while (it.hasNext()) {
162                 System.out.println((String JavaDoc) it.next());
163             }
164         }
165
166         WSIFPort port = service.getPort();
167
168         WSIFOperation operation;
169         WSIFMessage inputMessage;
170         WSIFMessage outputMessage;
171         WSIFMessage faultMessage;
172
173         String JavaDoc customerNumber;
174         String JavaDoc tempString;
175         Address address;
176         ShoppingCart_Java shoppingCart;
177         Item item;
178         CreditCardInfo creditCardInfo;
179         AirMilesContainer airMilesContainer;
180         Integer JavaDoc currentTotal = null;
181         Long JavaDoc orderConfirmationNumber;
182         Integer JavaDoc itemQuantity;
183         Object JavaDoc part;
184         boolean operationSucceeded;
185
186         // -----------------------------------------------------------------------------------------------------
187
operation = port.createOperation("createOperation");
188         inputMessage = operation.createInputMessage();
189         outputMessage = operation.createOutputMessage();
190         faultMessage = operation.createFaultMessage();
191
192         debug("\n---> Invocation: public ShoppingCart_Java(String firstName, String lastName, Address address, String customerNumber)");
193
194         tempString = "Albert";
195         inputMessage.setObjectPart("firstName", tempString);
196
197         tempString = "Einstein";
198         inputMessage.setObjectPart("lastName", tempString);
199
200         address = new Address("Berlin", "Unter den Linden");
201         inputMessage.setObjectPart("address", address);
202
203         customerNumber = "AE001";
204         inputMessage.setObjectPart("customerNumber", customerNumber);
205
206         operationSucceeded =
207             operation.executeRequestResponseOperation(
208                 inputMessage,
209                 outputMessage,
210                 faultMessage);
211
212         assertTrue("Failed to create a ShoppingCart", operationSucceeded);
213
214         // -----------------------------------------------------------------------------------------------------
215
operation = port.createOperation("addItemOperation");
216         inputMessage = operation.createInputMessage();
217         outputMessage = operation.createOutputMessage();
218         faultMessage = operation.createFaultMessage();
219
220         debug("\n---> Invocation: public int addItem(String itemNumber, Item item, String itemName, int itemQuantity)");
221
222         tempString = "100123";
223         inputMessage.setObjectPart("itemNumber", tempString);
224
225         item = new Item();
226         //inputMessage.setObjectPart("item", item);
227

228         tempString = "Pocket calculator";
229         inputMessage.setObjectPart("itemName", tempString);
230
231         itemQuantity = new Integer JavaDoc(1);
232         inputMessage.setObjectPart("itemQuantity", itemQuantity);
233
234         operationSucceeded =
235             operation.executeRequestResponseOperation(
236                 inputMessage,
237                 outputMessage,
238                 faultMessage);
239
240         assertTrue("addItem test failed", operationSucceeded);
241
242         if (operationSucceeded) {
243             assertTrue("Part is not an Item!!!",
244                 outputMessage.getObjectPart("item") instanceof Item);
245             debug(outputMessage.getObjectPart("item"));
246         } else {
247             // Cannot get here since test would have already failed!
248
}
249
250         // -----------------------------------------------------------------------------------------------------
251
operation = port.createOperation("addItemOperation");
252         inputMessage = operation.createInputMessage();
253         outputMessage = operation.createOutputMessage();
254         faultMessage = operation.createFaultMessage();
255
256         debug("\n---> Invocation: public int addItem(String itemNumber, Item item, String itemName, int itemQuantity)");
257
258         tempString = "234123";
259         inputMessage.setObjectPart("itemNumber", tempString);
260
261         item = new Item();
262         inputMessage.setObjectPart("item", item);
263
264         tempString = "Pencil";
265         inputMessage.setObjectPart("itemName", tempString);
266
267         itemQuantity = new Integer JavaDoc(12);
268         inputMessage.setObjectPart("itemQuantity", itemQuantity);
269
270         operationSucceeded =
271             operation.executeRequestResponseOperation(
272                 inputMessage,
273                 outputMessage,
274                 faultMessage);
275
276         assertTrue(
277             "addItem succeeded when it should have thrown a outOfStockException",
278             !operationSucceeded);
279
280         if (operationSucceeded) {
281             debug(outputMessage.getObjectPart("item"));
282         } else {
283             assertNotNull(
284                 "outputMessage should have contained outOfStockException",
285                 faultMessage.getObjectPart("outOfStockException"));
286
287             // Extra steps useful for debugging
288
/* part = faultMessage.getObjectPart("invalidItemException");
289                         if (part != null) {
290                             debug(faultMessage.getName() + ":\n" + part);
291                         } else {
292                             part = faultMessage.getObjectPart("outOfStockException");
293                             if (part != null) {
294                                 debug(faultMessage.getName() + ":\n" + part);
295                             } else {
296                                 debug("ERROR: Unknown fault message!");
297                             }
298                         }
299             */

300         }
301
302         // -----------------------------------------------------------------------------------------------------
303
operation = port.createOperation("addItemOperation");
304         inputMessage = operation.createInputMessage();
305         outputMessage = operation.createOutputMessage();
306         faultMessage = operation.createFaultMessage();
307
308         debug("\n---> Invocation: public int addItem(String itemNumber, Item item, String itemName, int itemQuantity)");
309
310         inputMessage.setObjectPart("itemNumber", "234123");
311
312         item = new Item();
313         inputMessage.setObjectPart("item", item);
314
315         inputMessage.setObjectPart("itemName", "Pencil");
316
317         inputMessage.setIntPart("itemQuantity", 8);
318
319         operationSucceeded =
320             operation.executeRequestResponseOperation(
321                 inputMessage,
322                 outputMessage,
323                 faultMessage);
324
325         assertTrue("addItem failed when it should have succeeded", operationSucceeded);
326
327         //Extra steps useful for debugging
328
/* part = faultMessage.getObjectPart("invalidItemException");
329                 if (operationSucceeded) {
330                     debug(outputMessage.getObjectPart("item"));
331                 } else {
332                     part = faultMessage.getObjectPart("invalidItemException");
333                     if (part != null) {
334                         debug(faultMessage.getName() + ":\n" + part);
335                     } else {
336                         part = faultMessage.getObjectPart("outOfStockException");
337                         if (part != null) {
338                             debug(faultMessage.getName() + ":\n" + part);
339                         } else {
340                             debug("ERROR: Unknown fault message!");
341                         }
342                     }
343                 }*/

344
345         // -----------------------------------------------------------------------------------------------------
346
operation = port.createOperation("submitOrderOperation");
347         inputMessage = operation.createInputMessage();
348         outputMessage = operation.createOutputMessage();
349         faultMessage = operation.createFaultMessage();
350
351         debug("\n---> Invocation: public long submitOrder(CreditCardInfo creditCardInfo, AirMilesContainer airMilesContainer)");
352
353         creditCardInfo = new CreditCardInfo();
354         inputMessage.setObjectPart("creditCardInfo", creditCardInfo);
355
356         airMilesContainer = new AirMilesContainer();
357         inputMessage.setObjectPart("airMilesContainer", airMilesContainer);
358
359         inputMessage.setLongPart("orderConfirmationNumber", 0);
360
361         operationSucceeded =
362             operation.executeRequestResponseOperation(
363                 inputMessage,
364                 outputMessage,
365                 faultMessage);
366
367         assertTrue(
368             "submitOrderOperation failed when it should have succeeded.",
369             operationSucceeded);
370
371         debug(
372             "order confirmation no. = "
373                 + outputMessage.getObjectPart("orderConfirmationNumber"));
374         debug(airMilesContainer);
375
376         // -----------------------------------------------------------------------------------------------------
377
operation = port.createOperation("queryOrdersOperation");
378         inputMessage = operation.createInputMessage();
379         outputMessage = operation.createOutputMessage();
380         faultMessage = operation.createFaultMessage();
381
382         debug("\n---> Invocation: public static Orders queryOrders(String customerNumber)");
383
384         inputMessage.setObjectPart("customerNumber", "AE001");
385
386         operationSucceeded =
387             operation.executeRequestResponseOperation(
388                 inputMessage,
389                 outputMessage,
390                 faultMessage);
391
392         assertTrue(
393             "queryOrdersOperation failed when it should have succeeded.",
394             operationSucceeded);
395
396         printOrders((Orders) outputMessage.getObjectPart("orders"));
397
398         // -----------------------------------------------------------------------------------------------------
399
}
400
401     /**
402      * Tests the EJB binding for the shopping cart scenario via WSDL
403      */

404     public void testWSDL_EJB() throws Exception JavaDoc {
405         if (!TestUtilities.areWeTesting("ejb"))
406             return;
407
408         debug("\n*** TEST EJB BINDING ***");
409
410         WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
411             WSIFService service =
412                 factory.getService(wsdlPath + "ShoppingCartAll.wsdl", // WSDL file
413
"http://www.shoppingcart.com/definitions/ShoppingCartInterface", // serviceNS,
414
"ShoppingCart_EJBService", // serviceName,
415
"http://www.shoppingcart.com/definitions/ShoppingCartInterface",
416         // portTypeNS,
417
"ShoppingCart_EJBPortType" // portTypeName
418
);
419
420         Iterator JavaDoc it = service.getAvailablePortNames();
421         {
422             System.out.println("Available ports for the service are: ");
423             while (it.hasNext()) {
424                 System.out.println((String JavaDoc) it.next());
425             }
426         }
427
428         WSIFPort port = service.getPort();
429         WSIFOperation operation;
430         WSIFMessage inputMessage;
431         WSIFMessage outputMessage;
432         WSIFMessage faultMessage;
433
434         String JavaDoc customerNumber;
435         String JavaDoc tempString;
436         Address address;
437         ShoppingCart shoppingCart;
438         Item item = null;
439         CreditCardInfo creditCardInfo;
440         AirMilesContainer airMilesContainer;
441         Integer JavaDoc currentTotal = null;
442         Long JavaDoc orderConfirmationNumber;
443         Integer JavaDoc itemQuantity;
444         Object JavaDoc part;
445         boolean operationSucceeded;
446
447         // -----------------------------------------------------------------------------------------------------
448
operation = port.createOperation("createOperation");
449         inputMessage = operation.createInputMessage();
450         outputMessage = operation.createOutputMessage();
451         faultMessage = operation.createFaultMessage();
452
453         debug("\n---> Invocation (home): public ShoppingCart create(java.lang.String firstName, java.lang.String lastName, org.apache.wsif.ejb.sample.shop.Address address, java.lang.String customerNumber) throws org.apache.wsif.ejb.sample.shop.CreateException, javax.ejb.CreateException, java.rmi.RemoteException");
454
455         inputMessage.setObjectPart("firstName", "Albert");
456
457         inputMessage.setObjectPart("lastName", "Einstein");
458
459         address = new Address("Berlin", "Unter den Linden");
460         inputMessage.setObjectPart("address", address);
461
462         inputMessage.setObjectPart("customerNumber", "AE001");
463
464         operationSucceeded =
465             operation.executeRequestResponseOperation(
466                 inputMessage,
467                 outputMessage,
468                 faultMessage);
469
470         assertTrue("createOperation failed!!", operationSucceeded);
471
472         // -----------------------------------------------------------------------------------------------------
473
operation = port.createOperation("addItemOperation");
474         inputMessage = operation.createInputMessage();
475         outputMessage = operation.createOutputMessage();
476         faultMessage = operation.createFaultMessage();
477
478         debug("\n---> Invocation (remote): public Item addItem(java.lang.String itemNumber, java.lang.String itemName, int itemQuantity) throws org.apache.wsif.ejb.sample.shop.OutOfStockException, java.rmi.RemoteException, org.apache.wsif.ejb.sample.shop.InvalidItemException");
479
480         inputMessage.setObjectPart("itemNumber", "100123");
481
482         inputMessage.setObjectPart("itemName", "Pocket calculator");
483
484         inputMessage.setIntPart("itemQuantity", 1);
485
486         operationSucceeded =
487             operation.executeRequestResponseOperation(
488                 inputMessage,
489                 outputMessage,
490                 faultMessage);
491
492         assertTrue("addItemOperation failed!!", operationSucceeded);
493
494         if (operationSucceeded) {
495             assertTrue(
496                 "addItemOperation (EJB) did not return an Item Object!!",
497                 outputMessage.getObjectPart("item") instanceof Item);
498             debug(outputMessage.getObjectPart("item"));
499         } else {
500             part = faultMessage.getObjectPart("invalidItemException");
501             if (part != null) {
502                 debug(faultMessage.getName() + ":\n" + part);
503             } else {
504                 part = faultMessage.getObjectPart("outOfStockException");
505                 if (part != null) {
506                     debug(faultMessage.getName() + ":\n" + part);
507                 } else {
508                     debug("ERROR: Unknown fault message!");
509                 }
510             }
511         }
512
513         // -----------------------------------------------------------------------------------------------------
514
operation = port.createOperation("addItemOperation");
515         inputMessage = operation.createInputMessage();
516         outputMessage = operation.createOutputMessage();
517         faultMessage = operation.createFaultMessage();
518
519         debug("\n---> Invocation (remote): public Item addItem(java.lang.String itemNumber, java.lang.String itemName, int itemQuantity) throws org.apache.wsif.ejb.sample.shop.OutOfStockException, java.rmi.RemoteException, org.apache.wsif.ejb.sample.shop.InvalidItemException");
520
521         inputMessage.setObjectPart("itemNumber", "234123");
522
523         inputMessage.setObjectPart("itemName", "Pencil");
524
525         inputMessage.setIntPart("itemQuantity", 12);
526
527         operationSucceeded =
528             operation.executeRequestResponseOperation(
529                 inputMessage,
530                 outputMessage,
531                 faultMessage);
532
533         assertTrue(
534             "addItemOperation succeeded when it should have thrown an outOfStockException",
535             !operationSucceeded);
536
537         if (operationSucceeded) {
538             assertTrue(
539                 "addItemOperation (EJB) did not return an Item Object!!",
540                 outputMessage.getObjectPart("item") instanceof Item);
541             debug(outputMessage.getObjectPart("item"));
542         } else {
543             assertNotNull(
544                 "outputMessage should have contained outOfStockException",
545                 faultMessage.getObjectPart("outOfStockException"));
546
547             /* part = faultMessage.getObjectPart("invalidItemException");
548                         if (part != null) {
549                             debug(faultMessage.getName() + ":\n" + part);
550                         } else {
551                             part = faultMessage.getObjectPart("outOfStockException");
552                             if (part != null) {
553                                 debug(faultMessage.getName() + ":\n" + part);
554                             } else {
555                                 debug("ERROR: Unknown fault message!");
556                             }
557                         }
558             */

559         }
560
561         // -----------------------------------------------------------------------------------------------------
562
operation = port.createOperation("addItemOperation");
563         inputMessage = operation.createInputMessage();
564         outputMessage = operation.createOutputMessage();
565         faultMessage = operation.createFaultMessage();
566
567         debug("\n---> Invocation (remote): public Item addItem(java.lang.String itemNumber, java.lang.String itemName, int itemQuantity) throws org.apache.wsif.ejb.sample.shop.OutOfStockException, java.rmi.RemoteException, org.apache.wsif.ejb.sample.shop.InvalidItemException");
568
569         inputMessage.setObjectPart("itemNumber", "234123");
570
571         inputMessage.setObjectPart("itemName", "Pencil");
572
573         inputMessage.setIntPart("itemQuantity", 8);
574
575         operationSucceeded =
576             operation.executeRequestResponseOperation(
577                 inputMessage,
578                 outputMessage,
579                 faultMessage);
580
581         assertTrue("addItemOperation failed!!", operationSucceeded);
582
583         if (operationSucceeded) {
584             assertTrue(
585                 "addItemOperation (EJB) did not return an Item Object!!",
586                 outputMessage.getObjectPart("item") instanceof Item);
587             debug(outputMessage.getObjectPart("item"));
588         } else {
589             part = faultMessage.getObjectPart("invalidItemException");
590             if (part != null) {
591                 debug(faultMessage.getName() + ":\n" + part);
592             } else {
593                 part = faultMessage.getObjectPart("outOfStockException");
594                 if (part != null) {
595                     debug(faultMessage.getName() + ":\n" + part);
596                 } else {
597                     debug("ERROR: Unknown fault message!");
598                 }
599             }
600         }
601
602         // -----------------------------------------------------------------------------------------------------
603
operation = port.createOperation("submitOrderOperation");
604         inputMessage = operation.createInputMessage();
605         outputMessage = operation.createOutputMessage();
606         faultMessage = operation.createFaultMessage();
607
608         debug("\n---> Invocation (remote): public SubmitOrderResult submitOrder(org.apache.wsif.ejb.sample.shop.CreditCardInfo creditCardInfo, org.apache.wsif.ejb.sample.shop.AirMilesContainer airMilesContainer) throws java.rmi.RemoteException");
609
610         creditCardInfo = new CreditCardInfo();
611         inputMessage.setObjectPart("creditCardInfo", creditCardInfo);
612
613         airMilesContainer = new AirMilesContainer();
614         inputMessage.setObjectPart("airMilesContainer", airMilesContainer);
615
616         inputMessage.setLongPart("orderConfirmationNumber", 0);
617
618         operationSucceeded =
619             operation.executeRequestResponseOperation(
620                 inputMessage,
621                 outputMessage,
622                 faultMessage);
623
624         assertTrue("submitOrderOperation failed!!", operationSucceeded);
625
626         debug(outputMessage.getObjectPart("submitOrderResult"));
627
628         // -----------------------------------------------------------------------------------------------------
629

630     }
631
632     /**
633     * Tests input only operations on the Java binding for the shopping cart scenario via WSDL
634     */

635     public void testWSDL_Java_InputOnly() throws Exception JavaDoc {
636         debug("\n*** TEST INPUT ONLY OPERATION USING JAVA BINDING ***");
637
638         WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
639             WSIFService service =
640                 factory
641                     .getService(
642                         wsdlPath + "ShoppingCartAll.wsdl",
643                         "http://www.shoppingcart.com/definitions/ShoppingCartInterface",
644         // serviceNS,
645
"ShoppingCart_JavaService", // serviceName,
646
"http://www.shoppingcart.com/definitions/ShoppingCartInterface",
647         // portTypeNS,
648
"ShoppingCart_JavaPortType" // portTypeName
649
);
650
651         WSIFPort port = service.getPort();
652
653         WSIFOperation operation;
654         WSIFMessage inputMessage;
655         WSIFMessage outputMessage;
656         WSIFMessage faultMessage;
657
658         String JavaDoc customerNumber;
659         String JavaDoc tempString;
660         Address address;
661         ShoppingCart_Java shoppingCart;
662         Item item;
663         CreditCardInfo creditCardInfo;
664         AirMilesContainer airMilesContainer;
665         Integer JavaDoc currentTotal = null;
666         Long JavaDoc orderConfirmationNumber;
667         Integer JavaDoc itemQuantity;
668         Object JavaDoc part;
669         boolean operationSucceeded;
670
671         // -----------------------------------------------------------------------------------------------------
672
operation = port.createOperation("createOperation");
673         inputMessage = operation.createInputMessage();
674
675         debug("\n---> Invocation: public ShoppingCart_Java(String firstName, String lastName, Address address, String customerNumber)");
676
677         inputMessage.setObjectPart("firstName", "Albert");
678
679         inputMessage.setObjectPart("lastName", "Einstein");
680
681         address = new Address("Berlin", "Unter den Linden");
682         inputMessage.setObjectPart("address", address);
683
684         inputMessage.setObjectPart("customerNumber", "AE001");
685
686         operation.executeInputOnlyOperation(inputMessage);
687
688         // -----------------------------------------------------------------------------------------------------
689
// First add something to the basket
690
operation = port.createOperation("addItemOperation");
691         inputMessage = operation.createInputMessage();
692         outputMessage = operation.createOutputMessage();
693         faultMessage = operation.createFaultMessage();
694
695         debug("\n---> Invocation: public int addItem(String itemNumber, Item item, String itemName, int itemQuantity)");
696
697         inputMessage.setObjectPart("itemNumber", "100123");
698
699         item = new Item();
700         inputMessage.setObjectPart("item", item);
701
702         inputMessage.setObjectPart("itemName", "Pocket calculator");
703
704         inputMessage.setIntPart("itemQuantity", 1);
705
706         operationSucceeded =
707             operation.executeRequestResponseOperation(
708                 inputMessage,
709                 outputMessage,
710                 faultMessage);
711
712         assertTrue("addItem test failed", operationSucceeded);
713
714         if (operationSucceeded) {
715             assertTrue(
716                 "addItemOperation (EJB) did not return an Item Object!!",
717                 outputMessage.getObjectPart("item") instanceof Item);
718             debug(outputMessage.getObjectPart("item"));
719             debug("Current total = " + outputMessage.getObjectPart("currentTotal"));
720         } else {
721             // Cannot get here since test would have already failed!
722
}
723
724         // -----------------------------------------------------------------------------------------------------
725
// Test an "Input Only" operation by invoking the emptyBasket method
726
operation = port.createOperation("emptyOrderOperation");
727         inputMessage = operation.createInputMessage();
728
729         debug("\n---> Invocation: public void emptyOrder(String customerNumber)");
730
731         inputMessage.setObjectPart("customerNumber", "AE001");
732
733         operation.executeInputOnlyOperation(inputMessage);
734
735         // -----------------------------------------------------------------------------------------------------
736
// Add to the basket again - the basket should be empty before this if previous operation worked!!
737
operation = port.createOperation("addItemOperation");
738         inputMessage = operation.createInputMessage();
739         outputMessage = operation.createOutputMessage();
740         faultMessage = operation.createFaultMessage();
741
742         debug("\n---> Invocation: public int addItem(String itemNumber, Item item, String itemName, int itemQuantity)");
743
744         inputMessage.setObjectPart("itemNumber", "100123");
745
746         item = new Item();
747         inputMessage.setObjectPart("item", item);
748
749         inputMessage.setObjectPart("itemName", "Pocket calculator");
750
751         inputMessage.setIntPart("itemQuantity", 5);
752
753         operationSucceeded =
754             operation.executeRequestResponseOperation(
755                 inputMessage,
756                 outputMessage,
757                 faultMessage);
758
759         assertTrue("addItem test failed", operationSucceeded);
760
761         if (operationSucceeded) {
762             assertTrue(
763                 "addItemOperation (EJB) did not return an Item Object!!",
764                 outputMessage.getObjectPart("item") instanceof Item);
765             debug(outputMessage.getObjectPart("item"));
766             debug("Current total = " + outputMessage.getObjectPart("currentTotal"));
767         } else {
768             // Cannot get here since test would have already failed!
769
}
770     }
771
772     /**
773     * Tests input only operations on the EJB binding for the shopping cart scenario via WSDL
774     */

775     public void testWSDL_EJB_InputOnly() throws Exception JavaDoc {
776         if (!TestUtilities.areWeTesting("ejb"))
777             return;
778
779         debug("\n*** TEST INPUT ONLY OPERATION USING EJB BINDING ***");
780
781         WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
782             WSIFService service =
783                 factory
784                     .getService(
785                         wsdlPath + "ShoppingCartAll.wsdl",
786                         "http://www.shoppingcart.com/definitions/ShoppingCartInterface",
787         // serviceNS,
788
"ShoppingCart_EJBService", // serviceName,
789
"http://www.shoppingcart.com/definitions/ShoppingCartInterface",
790         // portTypeNS,
791
"ShoppingCart_EJBPortType" // portTypeName
792
);
793
794         WSIFPort port = service.getPort();
795
796         WSIFOperation operation;
797         WSIFMessage inputMessage;
798         WSIFMessage outputMessage;
799         WSIFMessage faultMessage;
800
801         String JavaDoc customerNumber;
802         String JavaDoc tempString;
803         Address address;
804         ShoppingCart shoppingCart;
805         Item item = null;
806         CreditCardInfo creditCardInfo;
807         AirMilesContainer airMilesContainer;
808         Integer JavaDoc currentTotal = null;
809         Long JavaDoc orderConfirmationNumber;
810         Integer JavaDoc itemQuantity;
811         Object JavaDoc part;
812         boolean operationSucceeded;
813
814         // -----------------------------------------------------------------------------------------------------
815
operation = port.createOperation("createOperation");
816         inputMessage = operation.createInputMessage();
817         outputMessage = operation.createOutputMessage();
818         faultMessage = operation.createFaultMessage();
819
820         debug("\n---> Invocation (home): public ShoppingCart create(java.lang.String firstName, java.lang.String lastName, org.apache.wsif.ejb.sample.shop.Address address, java.lang.String customerNumber) throws org.apache.wsif.ejb.sample.shop.CreateException, javax.ejb.CreateException, java.rmi.RemoteException");
821
822         inputMessage.setObjectPart("firstName", "Albert");
823
824         inputMessage.setObjectPart("lastName", "Einstein");
825
826         address = new Address("Berlin", "Unter den Linden");
827         inputMessage.setObjectPart("address", address);
828
829         inputMessage.setObjectPart("customerNumber", "AE001");
830
831         operationSucceeded =
832             operation.executeRequestResponseOperation(
833                 inputMessage,
834                 outputMessage,
835                 faultMessage);
836
837         assertTrue("createOperation failed!!", operationSucceeded);
838
839         // -----------------------------------------------------------------------------------------------------
840
// First add something to the basket
841
operation = port.createOperation("addItemOperation");
842         inputMessage = operation.createInputMessage();
843         outputMessage = operation.createOutputMessage();
844         faultMessage = operation.createFaultMessage();
845
846         debug("\n---> Invocation (remote): public Item addItem(java.lang.String itemNumber, java.lang.String itemName, int itemQuantity) throws org.apache.wsif.ejb.sample.shop.OutOfStockException, java.rmi.RemoteException, org.apache.wsif.ejb.sample.shop.InvalidItemException");
847
848         inputMessage.setObjectPart("itemNumber", "100123");
849
850         inputMessage.setObjectPart("itemName", "Pocket calculator");
851
852         inputMessage.setIntPart("itemQuantity", 1);
853
854         operationSucceeded =
855             operation.executeRequestResponseOperation(
856                 inputMessage,
857                 outputMessage,
858                 faultMessage);
859
860         assertTrue("addItemOperation failed!!", operationSucceeded);
861
862         if (operationSucceeded) {
863             assertTrue(
864                 "addItemOperation (EJB) did not return an Item Object!!",
865                 outputMessage.getObjectPart("item") instanceof Item);
866             debug(outputMessage.getObjectPart("item"));
867         } else {
868             part = faultMessage.getObjectPart("invalidItemException");
869             if (part != null) {
870                 debug(faultMessage.getName() + ":\n" + part);
871             } else {
872                 part = faultMessage.getObjectPart("outOfStockException");
873                 if (part != null) {
874                     debug(faultMessage.getName() + ":\n" + part);
875                 } else {
876                     debug("ERROR: Unknown fault message!");
877                 }
878             }
879         }
880
881         // -----------------------------------------------------------------------------------------------------
882
// Test an "Input Only" operation by invoking the emptyBasket method
883
operation = port.createOperation("emptyOrderOperation");
884         inputMessage = operation.createInputMessage();
885
886         debug("\n---> Invocation: public void emptyOrder(String customerNumber)");
887
888         inputMessage.setObjectPart("customerNumber", "AE001");
889
890         operation.executeInputOnlyOperation(inputMessage);
891
892         // -----------------------------------------------------------------------------------------------------
893
// Add to the basket again - the basket should be empty before this if previous operation worked!!
894
operation = port.createOperation("addItemOperation");
895         inputMessage = operation.createInputMessage();
896         outputMessage = operation.createOutputMessage();
897         faultMessage = operation.createFaultMessage();
898
899         debug("\n---> Invocation (remote): public Item addItem(java.lang.String itemNumber, java.lang.String itemName, int itemQuantity) throws org.apache.wsif.ejb.sample.shop.OutOfStockException, java.rmi.RemoteException, org.apache.wsif.ejb.sample.shop.InvalidItemException");
900
901         inputMessage.setObjectPart("itemNumber", "100123");
902
903         inputMessage.setObjectPart("itemName", "Pocket calculator");
904
905         inputMessage.setIntPart("itemQuantity", 5);
906
907         operationSucceeded =
908             operation.executeRequestResponseOperation(
909                 inputMessage,
910                 outputMessage,
911                 faultMessage);
912
913         assertTrue("addItemOperation failed!!", operationSucceeded);
914
915         if (operationSucceeded) {
916             assertTrue(
917                 "addItemOperation (EJB) did not return an Item Object!!",
918                 outputMessage.getObjectPart("item") instanceof Item);
919             debug(outputMessage.getObjectPart("item"));
920         } else {
921             part = faultMessage.getObjectPart("invalidItemException");
922             if (part != null) {
923                 debug(faultMessage.getName() + ":\n" + part);
924             } else {
925                 part = faultMessage.getObjectPart("outOfStockException");
926                 if (part != null) {
927                     debug(faultMessage.getName() + ":\n" + part);
928                 } else {
929                     debug("ERROR: Unknown fault message!");
930                 }
931             }
932         }
933     }
934
935     private void debug(Object JavaDoc s) {
936         if (debugMode)
937             System.out.println(s);
938     }
939
940 }
941
Popular Tags