001: package org.springunit.framework.samples.jpetstore.domain;
002:
003: import java.util.Calendar;
004: import java.util.Date;
005: import java.util.GregorianCalendar;
006: import java.util.List;
007:
008: import org.springunit.framework.SpringUnitContext;
009: import org.springunit.framework.SpringUnitTest;
010:
011: /**
012: * Test of the Order domain object in the JPetStore sample application.
013: * @author Ted.Velkoff
014: *
015: */
016: public class OrderTest extends SpringUnitTest {
017:
018: public void testSetters() throws Exception {
019: Order subject = getObject("subject");
020: subject.setBillAddress1((String) getObject("billAddress1"));
021: subject.setBillAddress2((String) getObject("billAddress2"));
022: subject.setBillCity((String) getObject("billCity"));
023: subject.setBillCountry((String) getObject("billCountry"));
024: subject.setBillState((String) getObject("billState"));
025: subject
026: .setBillToFirstName((String) getObject("billToFirstName"));
027: subject.setBillToLastName((String) getObject("billToLastName"));
028: subject.setBillZip((String) getObject("billZip"));
029: subject.setCardType((String) getObject("cardType"));
030: subject.setCourier((String) getObject("courier"));
031: subject.setCreditCard((String) getObject("creditCard"));
032: subject.setExpiryDate((String) getObject("expiryDate"));
033: subject.setLineItems((List) getObject("lineItems"));
034: subject.setLocale((String) getObject("locale"));
035: subject
036: .setOrderDate(((GregorianCalendar) getObject("orderDate"))
037: .getTime());
038: subject.setShipAddress1((String) getObject("shipAddress1"));
039: subject.setShipAddress2((String) getObject("shipAddress2"));
040: subject.setShipCity((String) getObject("shipCity"));
041: subject.setShipCountry((String) getObject("shipCountry"));
042: subject.setShipState((String) getObject("shipState"));
043: subject
044: .setShipToFirstName((String) getObject("shipToFirstName"));
045: subject.setShipToLastName((String) getObject("shipToLastName"));
046: subject.setShipZip((String) getObject("shipZip"));
047: subject.setStatus((String) getObject("status"));
048: subject.setTotalPrice((Double) getObject("totalPrice"));
049: subject.setUsername((String) getObject("username"));
050: assertTrue(((String) getObject("expectedBillAddress1"))
051: .equals(subject.getBillAddress1()));
052: assertTrue(((String) getObject("expectedBillAddress2"))
053: .equals(subject.getBillAddress2()));
054: assertTrue(((String) getObject("expectedBillCity"))
055: .equals(subject.getBillCity()));
056: assertTrue(((String) getObject("expectedBillCountry"))
057: .equals(subject.getBillCountry()));
058: assertTrue(((String) getObject("expectedBillState"))
059: .equals(subject.getBillState()));
060: assertTrue(((String) getObject("expectedBillToFirstName"))
061: .equals(subject.getBillToFirstName()));
062: assertTrue(((String) getObject("expectedBillToLastName"))
063: .equals(subject.getBillToLastName()));
064: assertTrue(((String) getObject("expectedBillZip"))
065: .equals(subject.getBillZip()));
066: assertTrue(((String) getObject("expectedCardType"))
067: .equals(subject.getCardType()));
068: assertTrue(((String) getObject("expectedCourier"))
069: .equals(subject.getCourier()));
070: assertTrue(((String) getObject("expectedCreditCard"))
071: .equals(subject.getCreditCard()));
072: assertTrue(((String) getObject("expectedExpiryDate"))
073: .equals(subject.getExpiryDate()));
074: assertTrue(((List) getObject("expectedLineItems"))
075: .equals(subject.getLineItems()));
076: assertTrue(((String) getObject("expectedLocale"))
077: .equals(subject.getLocale()));
078: assertTrue((((GregorianCalendar) getObject("expectedOrderDate")))
079: .getTime().equals(subject.getOrderDate()));
080: assertTrue(((String) getObject("expectedShipAddress1"))
081: .equals(subject.getShipAddress1()));
082: assertTrue(((String) getObject("expectedShipAddress2"))
083: .equals(subject.getShipAddress2()));
084: assertTrue(((String) getObject("expectedShipCity"))
085: .equals(subject.getShipCity()));
086: assertTrue(((String) getObject("expectedShipCountry"))
087: .equals(subject.getShipCountry()));
088: assertTrue(((String) getObject("expectedShipState"))
089: .equals(subject.getShipState()));
090: assertTrue(((String) getObject("expectedShipToFirstName"))
091: .equals(subject.getShipToFirstName()));
092: assertTrue(((String) getObject("expectedShipToLastName"))
093: .equals(subject.getShipToLastName()));
094: assertTrue(((String) getObject("expectedShipZip"))
095: .equals(subject.getShipZip()));
096: assertTrue(((String) getObject("expectedStatus"))
097: .equals(subject.getStatus()));
098: assertTrue(((Double) getObject("expectedTotalPrice"))
099: .equals(subject.getTotalPrice()));
100: assertTrue(((String) getObject("expectedUsername"))
101: .equals(subject.getUsername()));
102: }
103:
104: public void testInitEmptyCart() throws Exception {
105: runInit(true);
106: }
107:
108: public void testInitNonEmptyCart() throws Exception {
109: runInit(false);
110: }
111:
112: public void testEqualsSame() throws Exception {
113: Order subject = getObject("subject");
114: Order other = getObject("other");
115: assertTrue(subject == other);
116: assertTrue(subject.equals(other));
117: }
118:
119: public void testEqualsNotSame() throws Exception {
120: Order subject = getObject("subject");
121: Order other = getObject("other");
122: assertFalse(subject == other);
123: assertTrue(subject.equals(other));
124: }
125:
126: public void testEqualsTypeMismatch() throws Exception {
127: Order subject = getObject("subject");
128: Integer other = getObject("other");
129: assertTrue(!subject.equals(other));
130: }
131:
132: public void testCompareToLessThan() throws Exception {
133: Order subject = getObject("subject");
134: Order other = getObject("other");
135: assertTrue(subject.compareTo(other) < 0);
136: }
137:
138: public void testCompareToGreaterThan() throws Exception {
139: Order subject = getObject("subject");
140: Order other = getObject("other");
141: assertTrue(subject.compareTo(other) > 0);
142: }
143:
144: public void testCompareToEquals() throws Exception {
145: Order subject = getObject("subject");
146: Order other = getObject("other");
147: assertTrue(subject.compareTo(other) == 0);
148: }
149:
150: public void testHashCodeSame() throws Exception {
151: Order subject = getObject("subject");
152: Order other = getObject("other");
153: assertTrue(subject.hashCode() == other.hashCode());
154: }
155:
156: public void testHashCodeDifferent() throws Exception {
157: Order subject = getObject("subject");
158: Order other = getObject("other");
159: assertTrue(subject.hashCode() != other.hashCode());
160: }
161:
162: public void testToString() throws Exception {
163: Order subject = getObject("subject");
164: String s = subject.toString();
165: }
166:
167: protected void runInit(boolean isEmpty) throws Exception {
168: Order subject = getObject("subject");
169: Account account = getObject("account");
170: assertNotNull(account);
171: Cart cart = getObject("cart");
172: assertNotNull(cart);
173: if (!isEmpty) {
174: fillCart(cart);
175: }
176: subject.initOrder(account, cart);
177: assertTrue(((String) getObject("expectedBillAddress1"))
178: .equals(subject.getBillAddress1()));
179: assertTrue(((String) getObject("expectedBillAddress2"))
180: .equals(subject.getBillAddress2()));
181: assertTrue(((String) getObject("expectedBillCity"))
182: .equals(subject.getBillCity()));
183: assertTrue(((String) getObject("expectedBillState"))
184: .equals(subject.getBillState()));
185: assertTrue(((String) getObject("expectedBillZip"))
186: .equals(subject.getBillZip()));
187: assertTrue(((String) getObject("expectedBillCountry"))
188: .equals(subject.getBillCountry()));
189: assertTrue(((String) getObject("expectedShipAddress1"))
190: .equals(subject.getShipAddress1()));
191: assertTrue(((String) getObject("expectedShipAddress2"))
192: .equals(subject.getShipAddress2()));
193: assertTrue(((String) getObject("expectedShipCity"))
194: .equals(subject.getShipCity()));
195: assertTrue(((String) getObject("expectedShipState"))
196: .equals(subject.getShipState()));
197: assertTrue(((String) getObject("expectedShipZip"))
198: .equals(subject.getShipZip()));
199: assertTrue(((String) getObject("expectedShipCountry"))
200: .equals(subject.getShipCountry()));
201: assertTrue(((Double) getObject("expectedTotalPrice"))
202: .equals(subject.getTotalPrice()));
203: assertTrue(((String) getObject("expectedUsername"))
204: .equals(subject.getUsername()));
205: assertTrue(((String) getObject("expectedCreditCard"))
206: .equals(subject.getCreditCard()));
207: assertTrue(((String) getObject("expectedCardType"))
208: .equals(subject.getCardType()));
209: assertTrue(((String) getObject("expectedExpiryDate"))
210: .equals(subject.getExpiryDate()));
211: assertTrue(((String) getObject("expectedCourier"))
212: .equals(subject.getCourier()));
213: assertTrue(((String) getObject("expectedLocale"))
214: .equals(subject.getLocale()));
215: assertTrue(((String) getObject("expectedStatus"))
216: .equals(subject.getStatus()));
217: GregorianCalendar expectedOrderDate = new GregorianCalendar();
218: expectedOrderDate.setTime(new Date());
219: GregorianCalendar actualOrderDate = new GregorianCalendar();
220: actualOrderDate.setTime(subject.getOrderDate());
221: assertEquals(expectedOrderDate.get(Calendar.YEAR),
222: actualOrderDate.get(Calendar.YEAR));
223: assertEquals(expectedOrderDate.get(Calendar.MONTH),
224: actualOrderDate.get(Calendar.MONTH));
225: assertEquals(expectedOrderDate.get(Calendar.DAY_OF_MONTH),
226: actualOrderDate.get(Calendar.DAY_OF_MONTH));
227: assertEquals(((Integer) getObject("expectedLineItemsCount"))
228: .intValue(), subject.getLineItems().size());
229: }
230:
231: protected void fillCart(Cart cart) throws Exception {
232: List<Item> items = getObject("items");
233: for (Item item : items) {
234: cart.addItem(item, true);
235: }
236: }
237:
238: public SpringUnitContext getOrderTest() {
239: return orderTest;
240: }
241:
242: public void setOrderTest(SpringUnitContext orderTest) {
243: this .orderTest = orderTest;
244: }
245:
246: private SpringUnitContext orderTest;
247:
248: }
|