KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xpetstore > services > cart > test > CartTest


1 package xpetstore.services.cart.test;
2
3 import java.util.Collection JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 import javax.naming.InitialContext JavaDoc;
7
8 import junit.framework.TestCase;
9
10 import xpetstore.services.cart.ejb.Cart;
11 import xpetstore.services.cart.model.CartItem;
12
13
14 /**
15  * @author <a HREF="mailto:tchbansi@sourceforge.net">Herve Tchepannou</a>
16  */

17 public class CartTest
18     extends TestCase
19 {
20     //~ Constructors -----------------------------------------------------------
21

22     /**
23      * Constructor for CartTest.
24      * @param arg0
25      */

26     public CartTest( String JavaDoc arg0 )
27     {
28         super( arg0 );
29     }
30
31     //~ Methods ----------------------------------------------------------------
32

33     public void testCart( )
34     {
35         Cart cart = null;
36
37         try
38         {
39             cart = (Cart)new InitialContext JavaDoc().lookup("Cart");
40             cart.addItem( "EST-1" );
41             cart.addItem( "EST-2" );
42             cart.addItem( "EST-10" );
43             cart.addItem( "EST-1", 9 );
44             cart.removeItem( "EST-2" );
45
46             // Cart count
47
assertEquals( "cart.count", 2, cart.getCount( ) );
48
49             // Cart items
50
Collection JavaDoc items = cart.getCartItems( );
51             assertNotNull( "items=null", items );
52             assertEquals( "items.size", 2, items.size( ) );
53
54             for ( Iterator JavaDoc it = items.iterator( ); it.hasNext( ); )
55             {
56                 CartItem item = ( CartItem ) it.next( );
57
58                 if ( "EST-1".equals( item.getItemId( ) ) )
59                 {
60                     assertEquals( "item[EST-1].name", "Angelfish", item.getName( ) );
61                     assertEquals( "item[EST-1].description", "Large", item.getDescription( ) );
62                     assertEquals( "item[EST-1].productId", "FI-SW-01", item.getProductId( ) );
63                     assertEquals( "item[EST-1].unitCost", 16.50, item.getUnitCost( ), 0 );
64                     assertEquals( "item[EST-1].quantity", 10, item.getQuantity( ) );
65                     assertEquals( "item[EST-1].totalCost", 165.0, item.getTotalCost( ), 0 );
66                 }
67                 else if ( "EST-10".equals( item.getItemId( ) ) )
68                 {
69                     assertEquals( "item[EST-10].name", "Bulldog", item.getName( ) );
70                     assertEquals( "item[EST-10].description", "Spotless Female Puppy", item.getDescription( ) );
71                     assertEquals( "item[EST-10].productId", "K9-BD-01", item.getProductId( ) );
72                     assertEquals( "item[EST-10].unitCost", 28.50, item.getUnitCost( ), 0 );
73                     assertEquals( "item[EST-10].quantity", 1, item.getQuantity( ) );
74                     assertEquals( "item[EST-10].totalCost", 28.50, item.getTotalCost( ), 0 );
75                 }
76                 else
77                 {
78                     assertEquals( item.getItemId( ) + " is invalid", false, true );
79                 }
80             }
81
82             // Sub total
83
assertEquals( "subTotal", 193.5, cart.getTotal( ), 0 );
84         }
85         catch ( Exception JavaDoc e )
86         {
87             e.printStackTrace( );
88             fail( "Error=" + e.toString( ) );
89         }
90         finally
91         {
92             try
93             {
94                 if ( cart != null )
95                 {
96                     cart.remove( );
97                 }
98             }
99             catch ( Exception JavaDoc e )
100             {
101                 fail( "Unable to destroy the card. Error=" + e.toString( ) );
102             }
103         }
104     }
105
106     public void testUpdateCart( )
107     {
108         Cart cart = null;
109
110         try
111         {
112             cart = (Cart)new InitialContext JavaDoc().lookup("Cart");
113             cart.addItem( "EST-1" );
114             cart.addItem( "EST-10" );
115             cart.updateItems( new String JavaDoc[] { "EST-1", "EST-10" }, new int[] { 10, 11 } );
116
117             // Cart count
118
assertEquals( "cart.count", 2, cart.getCount( ) );
119
120             // Cart items
121
Collection JavaDoc items = cart.getCartItems( );
122             assertNotNull( "items=null", items );
123             assertEquals( "items.size", 2, items.size( ) );
124
125             for ( Iterator JavaDoc it = items.iterator( ); it.hasNext( ); )
126             {
127                 CartItem item = ( CartItem ) it.next( );
128
129                 if ( "EST-1".equals( item.getItemId( ) ) )
130                 {
131                     assertEquals( "item[EST-1].name", "Angelfish", item.getName( ) );
132                     assertEquals( "item[EST-1].description", "Large", item.getDescription( ) );
133                     assertEquals( "item[EST-1].productId", "FI-SW-01", item.getProductId( ) );
134                     assertEquals( "item[EST-1].unitCost", 16.50, item.getUnitCost( ), 0 );
135                     assertEquals( "item[EST-1].quantity", 10, item.getQuantity( ) );
136                     assertEquals( "item[EST-1].totalCost", 165.0, item.getTotalCost( ), 0 );
137                 }
138                 else if ( "EST-10".equals( item.getItemId( ) ) )
139                 {
140                     assertEquals( "item[EST-10].name", "Bulldog", item.getName( ) );
141                     assertEquals( "item[EST-10].description", "Spotless Female Puppy", item.getDescription( ) );
142                     assertEquals( "item[EST-10].productId", "K9-BD-01", item.getProductId( ) );
143                     assertEquals( "item[EST-10].unitCost", 28.50, item.getUnitCost( ), 0 );
144                     assertEquals( "item[EST-10].quantity", 11, item.getQuantity( ) );
145                     assertEquals( "item[EST-10].totalCost", 313.50, item.getTotalCost( ), 0 );
146                 }
147                 else
148                 {
149                     assertEquals( item.getItemId( ) + " is invalid", false, true );
150                 }
151             }
152
153             // Sub total
154
assertEquals( "subTotal", 478.5, cart.getTotal( ), 0 );
155         }
156         catch ( Exception JavaDoc e )
157         {
158             e.printStackTrace( );
159             fail( "Error=" + e.toString( ) );
160         }
161         finally
162         {
163             try
164             {
165                 if ( cart != null )
166                 {
167                     cart.remove( );
168                 }
169             }
170             catch ( Exception JavaDoc e )
171             {
172                 fail( "Unable to destroy the card. Error=" + e.toString( ) );
173             }
174         }
175     }
176 }
177
Popular Tags