KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xpetstore > services > petstore > test > PetstoreTest


1 package xpetstore.services.petstore.test;
2
3 import java.util.Date JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6
7 import javax.naming.InitialContext JavaDoc;
8
9 import javax.ejb.FinderException JavaDoc;
10
11 import junit.framework.TestCase;
12
13 import xpetstore.domain.catalog.ejb.Category;
14 import xpetstore.domain.catalog.ejb.Item;
15 import xpetstore.domain.catalog.ejb.Product;
16 import xpetstore.domain.customer.ejb.Customer;
17 import xpetstore.domain.order.ejb.OrderItem;
18 import xpetstore.domain.order.ejb.Order;
19 import xpetstore.domain.signon.ejb.Account;
20
21 import xpetstore.services.petstore.ejb.Petstore;
22
23 import xpetstore.util.Page;
24
25
26 /**
27  * @author <a HREF="mailto:tchbansi@sourceforge.net">Herve Tchepannou</a>
28  */

29 public class PetstoreTest
30     extends TestCase
31 {
32     //~ Static fields/initializers ---------------------------------------------
33

34     private static Petstore _petstore;
35
36     //~ Constructors -----------------------------------------------------------
37

38     public PetstoreTest( String JavaDoc arg0 )
39     {
40         super( arg0 );
41     }
42
43     //~ Methods ----------------------------------------------------------------
44

45     //================================================
46
// signon Test cases
47
//================================================
48
public void testAuthenticate( )
49     {
50         boolean result;
51
52         try
53         {
54             result = _petstore.authenticate( "user1", "password1" );
55             assertEquals( "Unable to authenticate 'user1'", true, result );
56         }
57         catch ( Exception JavaDoc e )
58         {
59             fail( "Unable to authenticate. Error=" + e.toString( ) );
60         }
61     }
62
63     //================================================
64
// Customer Test cases
65
//================================================
66
public void testCreateCustomer( )
67     {
68         try
69         {
70             String JavaDoc userId = "CST-" + ( System.currentTimeMillis( ) % 100000 );
71             Account act0 = new Account( userId, "password" );
72             Customer cst0 = new Customer( null, "firstname", "lastname", userId + "@foo.com", "123-456", "en", "street1.1", "street1.2", "city", "CA", "A1A-1A1", "US", "111-111-111", "Visa", "01-11" );
73
74             //System.out.println( "creating customer=" + cst0 );
75
String JavaDoc id = _petstore.createCustomer( cst0 );
76             Customer cst = _petstore.getCustomer( id );
77             Account act = cst.getAccount( );
78
79             assertEquals( "firstname", "firstname", cst.getFirstname( ) );
80             assertEquals( "lastname", "lastname", cst.getLastname( ) );
81             assertEquals( "telephone", "123-456", cst.getTelephone( ) );
82             assertEquals( "email", userId + "@foo.com", cst.getEmail( ) );
83             assertEquals( "language", "en", cst.getLanguage( ) );
84             assertEquals( "street1", "street1.1", cst.getStreet1( ) );
85             assertEquals( "street2", "street1.2", cst.getStreet2( ) );
86             assertEquals( "city", "city", cst.getCity( ) );
87             assertEquals( "zipcode", "A1A-1A1", cst.getZipcode( ) );
88             assertEquals( "state", "CA", cst.getState( ) );
89             assertEquals( "country", "US", cst.getCountry( ) );
90             assertEquals( "creditCardType", "Visa", cst.getCreditCardType( ) );
91             assertEquals( "creditCardNumber", "111-111-111", cst.getCreditCardNumber( ) );
92             assertEquals( "creditCardExpiryDate", "01-11", cst.getCreditCardExpiryDate( ) );
93             assertEquals( "account.userId", userId, act.getUserId( ) );
94             assertEquals( "account.password", "password", act.getPassword( ) );
95         }
96         catch ( Exception JavaDoc e )
97         {
98             fail( "Unable to create the user. Error=" + e.toString( ) );
99         }
100     }
101
102     public void testUpdateCustomer( )
103     {
104         try
105         {
106             String JavaDoc userId = "CST-" + ( System.currentTimeMillis( ) % 1000000 );
107             Account act0 = new Account( userId, "password" );
108             Customer cst0 = new Customer( null, "firstnam", "lastnam", userId + "@foo.com", "222-22", "f", "street2.", "street2.", "city", "O", "A1A-1A1", "C", "111-111-111", "Visa", "01-21" );
109
110             //System.out.println( "creating customer=" + cst0 );
111
String JavaDoc id = _petstore.createCustomer( cst0 );
112
113             Account act1 = new Account( userId, "password1" );
114             Customer cst1 = new Customer( id, "firstname2", "lastname2", userId + "-2@foo.com", "222-222", "fr", "street2.1", "street2.2", "city2", "ON", "A2A-2A2", "CA", "222-222-222", "Amex", "02-22" );
115             cst1.setAccount( act1 );
116
117             //System.out.println( "updating customer=" + cst1 );
118
_petstore.updateCustomer( cst1 );
119
120             Customer cst = _petstore.getCustomer( id );
121             Account act = cst.getAccount( );
122
123             //System.out.println( "customer=" + cst );
124
assertEquals( "firstname", "firstname2", cst.getFirstname( ) );
125             assertEquals( "lastname", "lastname2", cst.getLastname( ) );
126             assertEquals( "telephone", "222-222", cst.getTelephone( ) );
127             assertEquals( "email", userId + "-2@foo.com", cst.getEmail( ) );
128             assertEquals( "language", "fr", cst.getLanguage( ) );
129             assertEquals( "street1", "street2.1", cst.getStreet1( ) );
130             assertEquals( "street2", "street2.2", cst.getStreet2( ) );
131             assertEquals( "city", "city2", cst.getCity( ) );
132             assertEquals( "zipcode", "A2A-2A2", cst.getZipcode( ) );
133             assertEquals( "state", "ON", cst.getState( ) );
134             assertEquals( "country", "CA", cst.getCountry( ) );
135             assertEquals( "creditCardType", "Amex", cst.getCreditCardType( ) );
136             assertEquals( "creditCardNumber", "222-222-222", cst.getCreditCardNumber( ) );
137             assertEquals( "creditCardExpiryDate", "02-22", cst.getCreditCardExpiryDate( ) );
138             assertEquals( "account.userId", userId, act.getUserId( ) );
139             assertEquals( "account.password", "password1", act.getPassword( ) );
140         }
141         catch ( Exception JavaDoc e )
142         {
143             fail( "Unable to create the customer. Error=" + e.toString( ) );
144         }
145     }
146
147     //================================================
148
// Categories Test cases
149
//================================================
150
public void testGetCategory( )
151     {
152         try
153         {
154             Category cat = _petstore.getCategory( "FISH" );
155             assertNotNull( "category[FISH] not found", cat );
156             assertEquals( "categoryId", "FISH", cat.getCategoryId( ) );
157             assertEquals( "name", "Fish", cat.getName( ) );
158             assertEquals( "description", "description of FISH", cat.getDescription( ) );
159         }
160         catch ( Exception JavaDoc e )
161         {
162             fail( "Error=" + e.toString( ) );
163         }
164     }
165
166     public void testGetCategories( )
167     {
168         try
169         {
170             Page pg = _petstore.getCategories( 0, 10 );
171             assertNotNull( "page", pg );
172             assertEquals( "page.size", 5, pg.getSize( ) );
173         }
174         catch ( Exception JavaDoc e )
175         {
176             fail( "Error=" + e.toString( ) );
177         }
178     }
179
180     //================================================
181
// Products Test cases
182
//================================================
183
public void testGetProduct( )
184     {
185         try
186         {
187             Product prod = _petstore.getProduct( "K9-DL-01" );
188             assertNotNull( "product[K9-DL-01] not found", prod );
189             assertEquals( "productId", "K9-DL-01", prod.getProductId( ) );
190             assertEquals( "name", "Dalmation", prod.getName( ) );
191             assertEquals( "description", "Great dog for a fire station", prod.getDescription( ) );
192         }
193         catch ( Exception JavaDoc e )
194         {
195             fail( "Error=" + e.toString( ) );
196         }
197     }
198
199     public void testGetProducts( )
200     {
201         try
202         {
203             Page pg = _petstore.getProducts( "FISH", 0, 10 );
204             assertNotNull( "page", pg );
205             assertEquals( "page.size", 4, pg.getSize( ) );
206         }
207         catch ( Exception JavaDoc e )
208         {
209             fail( "Error=" + e.toString( ) );
210         }
211     }
212
213     public void testSearchProducts( )
214     {
215         try
216         {
217             Page pg = _petstore.searchProducts( "fish", 0, 10 );
218             assertNotNull( "page", pg );
219             assertEquals( "page.size", 4, pg.getSize( ) );
220         }
221         catch ( Exception JavaDoc e )
222         {
223             fail( "Error=" + e.toString( ) );
224         }
225     }
226
227     //================================================
228
// Products Test cases
229
//================================================
230
public void testGetItem( )
231     {
232         try
233         {
234             Item item = _petstore.getItem( "EST-1" );
235             assertNotNull( "Item[EST-1] not found", item );
236             assertEquals( "productId", "EST-1", item.getItemId( ) );
237             assertEquals( "name", "Large", item.getDescription( ) );
238             assertEquals( "listPrice", 16.50, item.getListPrice( ), 0 );
239             assertEquals( "unitCost", 10.00, item.getUnitCost( ), 0 );
240             assertEquals( "itemPath", "fish1.jpg", item.getImagePath( ) );
241         }
242         catch ( Exception JavaDoc e )
243         {
244             fail( "Error=" + e.toString( ) );
245         }
246     }
247
248     public void testGetItems( )
249     {
250         try
251         {
252             Page pg = _petstore.getItems( "FI-SW-02", 0, 10 );
253             assertNotNull( "page", pg );
254             assertEquals( "page.size", 2, pg.getSize( ) );
255         }
256         catch ( Exception JavaDoc e )
257         {
258             fail( "Error=" + e.toString( ) );
259         }
260     }
261
262     //================================================
263
// Order Test cases
264
//================================================
265
public void testOrder( )
266     {
267         try
268         {
269             // Fill the cart
270
HashMap JavaDoc map = new HashMap JavaDoc( );
271             map.put( "EST-1", new Integer JavaDoc( 10 ) );
272             map.put( "EST-10", new Integer JavaDoc( 1 ) );
273
274             // Customer
275
String JavaDoc userId = "user1";
276
277             // Order
278
Date JavaDoc now = new Date JavaDoc( );
279             Integer JavaDoc orderUId = _petstore.createOrder( userId, now, map );
280             Order order = _petstore.getOrder( orderUId );
281
282             //System.out.println( "order=" + order );
283
assertNotNull( "order=null", order );
284             assertEquals( "order.orderUID", orderUId, order.getOrderUId( ) );
285             assertNotNull( "order.date=null", order.getOrderDate( ) );
286             assertEquals( "order.date", now.getTime( ) / 100000, order.getOrderDate( ).getTime( ) / 100000, 1000 );
287             assertEquals( "street1", "street1.1", order.getStreet1( ) );
288             assertEquals( "street2", "street1.2", order.getStreet2( ) );
289             assertEquals( "city", "city1", order.getCity( ) );
290             assertEquals( "state", "ST1", order.getState( ) );
291             assertEquals( "zipcode", "A1B-1C1", order.getZipcode( ) );
292             assertEquals( "country", "US", order.getCountry( ) );
293             assertEquals( "creditCardType", "Visa", order.getCreditCardType( ) );
294             assertEquals( "creditCardNumber", "111-111-111", order.getCreditCardNumber( ) );
295             assertEquals( "creditCardExpiryDate", "01-11", order.getCreditCardExpiryDate( ) );
296             assertNotNull( "order.status=null", order.getStatus( ) );
297         }
298         catch ( Exception JavaDoc e )
299         {
300             fail( "Error=" + e.toString( ) );
301         }
302     }
303
304     public void testOrderItems( )
305     {
306         try
307         {
308             // Fill the cart
309
HashMap JavaDoc map = new HashMap JavaDoc( );
310             map.put( "EST-1", new Integer JavaDoc( 10 ) );
311             map.put( "EST-10", new Integer JavaDoc( 1 ) );
312
313             // Customer
314
String JavaDoc userId = "user1";
315
316             // Order
317
Date JavaDoc now = new Date JavaDoc( );
318             Integer JavaDoc orderUId = _petstore.createOrder( userId, now, map );
319
320             // Order items
321
Page page = _petstore.getOrderItems( orderUId, 0, Integer.MAX_VALUE );
322             assertEquals( "page.size", 2, page.getSize( ) );
323
324             Iterator JavaDoc it = page.getList( ).iterator( );
325
326             for ( int i = 0; it.hasNext( ); )
327             {
328                 String JavaDoc prefix = "order#" + i;
329                 OrderItem value = ( OrderItem ) it.next( );
330                 Item item = value.getItem( );
331
332                 assertNotNull( prefix + ".item", value.getItem( ) );
333
334                 if ( "EST-1".equals( item.getItemId( ) ) )
335                 {
336                     assertEquals( prefix + ".quantity", 10, value.getQuantity( ) );
337                     assertEquals( prefix + ".unitPrice", 16.5, value.getUnitPrice( ), 0 );
338                 }
339                 else if ( "EST-10".equals( item.getItemId( ) ) )
340                 {
341                     assertEquals( prefix + ".quantity", 1, value.getQuantity( ) );
342                     assertEquals( prefix + ".unitPrice", 28.5, value.getUnitPrice( ), 0 );
343                 }
344                 else
345                 {
346                     fail( prefix + ".id is invalid: " + item.getItemId( ) );
347                 }
348             }
349         }
350         catch ( Exception JavaDoc e )
351         {
352             fail( "Error=" + e.toString( ) );
353         }
354     }
355
356     public void testMyOrders( )
357     {
358         try
359         {
360             // Fill the cart
361
HashMap JavaDoc map = new HashMap JavaDoc( );
362             map.put( "EST-1", new Integer JavaDoc( 10 ) );
363             map.put( "EST-10", new Integer JavaDoc( 1 ) );
364
365             // Customer
366
String JavaDoc userId = "CST-" + ( System.currentTimeMillis( ) % 1000000 );
367             Account act0 = new Account( userId, "password" );
368             Customer cst0 = new Customer( null, "firstname", "lastname", userId + "@localdomain", "123-456", "en", "street1.1", "street1.2", "city", "CA", "A1A-1A1", "US", "111-111-111", "Visa", "01-01-11" );
369             cst0.setAccount( act0 );
370             _petstore.createCustomer( cst0 );
371
372             // Order
373
Date JavaDoc now = new Date JavaDoc( );
374             _petstore.createOrder( userId, now, map );
375
376             _petstore.createOrder( userId, now, map );
377
378             Page orders = _petstore.getCustomerOrders( userId, 0, 10 );
379             assertNotNull( "customer[" + userId + "] - orders=null", orders );
380             assertEquals( "customer[" + userId + "] - orders.size", 1, orders.getSize( ) );
381         }
382         catch ( Exception JavaDoc e )
383         {
384             fail( "Error=" + e.toString( ) );
385         }
386     }
387
388     //================================================
389
// junit.framework.TestCase functions
390
//================================================
391
protected void setUp( )
392     {
393         if ( _petstore == null )
394         {
395             try
396             {
397                 System.out.println( "Creating the Petstore object" );
398                 _petstore = (Petstore)new InitialContext JavaDoc().lookup("Petstore");
399             }
400             catch ( Exception JavaDoc e )
401             {
402                 e.printStackTrace( );
403                 fail( "Unable to create the Petstore object. Error=" + e.toString( ) );
404             }
405         }
406     }
407 }
408
Popular Tags