01: package org.springunit.framework.samples.jpetstore.domain;
02:
03: import java.util.List;
04:
05: import org.springframework.beans.support.PagedListHolder;
06: import org.springunit.framework.SpringUnitContext;
07: import org.springunit.framework.SpringUnitTest;
08:
09: public class CartTest extends SpringUnitTest {
10:
11: public void testAddItemFirstTime() throws Exception {
12: runAddItem();
13: }
14:
15: public void testAddItemSecondTime() throws Exception {
16: runAddItem();
17: }
18:
19: protected void runAddItem() throws Exception {
20: Cart subject = getObject("subject");
21: initSubject(subject);
22: assertEquals(((Integer) getObject("expectedNumberOfItems"))
23: .intValue(), subject.getNumberOfItems());
24: }
25:
26: public void testRemoveItemNullCartItem() throws Exception {
27: Cart subject = getObject("subject");
28: initSubject(subject);
29: List<? extends Item> items = getObject("items");
30: List<Integer> itemIds = getObject("itemIds");
31: for (int i = 0; i < items.size(); i++) {
32: items.get(i).setId(itemIds.get(i));
33: }
34: for (Item item : items) {
35: subject.addItem(item, item.getQuantity() > 0);
36: }
37: for (Integer itemId : itemIds) {
38: Item removed = subject.removeItemById(itemId);
39: assertTrue(removed == null
40: || removed.getId().equals(itemId));
41: assertFalse(subject.containsItemId(itemId));
42: }
43: }
44:
45: public void testQuantities() throws Exception {
46: Cart subject = getObject("subject");
47: initSubject(subject);
48: List<? extends Item> items = getObject("items");
49: Integer incrItemId = getObject("incrItemId");
50: subject.incrementQuantityByItemId(incrItemId);
51: Integer setItemId = getObject("setItemId");
52: Integer setQuantity = getObject("setQuantity");
53: subject.setQuantityByItemId(setItemId, setQuantity);
54: }
55:
56: protected void initSubject(Cart subject) throws Exception {
57: List<Item> items = getObject("items");
58: List<Integer> itemIds = getObject("itemIds");
59: for (int i = 0; i < items.size(); i++) {
60: items.get(i).setId(itemIds.get(i));
61: }
62: List<Boolean> isInStock = getObject("isInStock");
63: int i = 0;
64: for (Item item : items) {
65: subject.addItem(item, isInStock.get(i++));
66: assertTrue(subject.containsItemId(item.getId()));
67: }
68: }
69:
70: public void testCartItemList() throws Exception {
71: Cart subject = getObject("subject");
72: initSubject(subject);
73: PagedListHolder list = subject.getCartItemList();
74: assertEquals(((Integer) getObject("expectedNumberOfItems"))
75: .intValue(), list.getSource().size());
76: }
77:
78: public SpringUnitContext getCartTest() {
79: return cartTest;
80: }
81:
82: public void setCartTest(SpringUnitContext cartTest) {
83: this .cartTest = cartTest;
84: }
85:
86: private SpringUnitContext cartTest;
87:
88: }
|