1 package xpetstore.services.cart.test; 2 3 import java.util.Collection ; 4 import java.util.Iterator ; 5 6 import javax.naming.InitialContext ; 7 8 import junit.framework.TestCase; 9 10 import xpetstore.services.cart.ejb.Cart; 11 import xpetstore.services.cart.model.CartItem; 12 13 14 17 public class CartTest 18 extends TestCase 19 { 20 22 26 public CartTest( String arg0 ) 27 { 28 super( arg0 ); 29 } 30 31 33 public void testCart( ) 34 { 35 Cart cart = null; 36 37 try 38 { 39 cart = (Cart)new InitialContext ().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 assertEquals( "cart.count", 2, cart.getCount( ) ); 48 49 Collection items = cart.getCartItems( ); 51 assertNotNull( "items=null", items ); 52 assertEquals( "items.size", 2, items.size( ) ); 53 54 for ( Iterator 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 assertEquals( "subTotal", 193.5, cart.getTotal( ), 0 ); 84 } 85 catch ( Exception 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 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 ().lookup("Cart"); 113 cart.addItem( "EST-1" ); 114 cart.addItem( "EST-10" ); 115 cart.updateItems( new String [] { "EST-1", "EST-10" }, new int[] { 10, 11 } ); 116 117 assertEquals( "cart.count", 2, cart.getCount( ) ); 119 120 Collection items = cart.getCartItems( ); 122 assertNotNull( "items=null", items ); 123 assertEquals( "items.size", 2, items.size( ) ); 124 125 for ( Iterator 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 assertEquals( "subTotal", 478.5, cart.getTotal( ), 0 ); 155 } 156 catch ( Exception 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 e ) 171 { 172 fail( "Unable to destroy the card. Error=" + e.toString( ) ); 173 } 174 } 175 } 176 } 177 | Popular Tags |