-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter24_test_utility_method_example.java
More file actions
103 lines (92 loc) · 4.02 KB
/
Copy pathchapter24_test_utility_method_example.java
File metadata and controls
103 lines (92 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
public class Chapter24Test1 extends TestCase {
public void setUp() {
}
public void tearDown() {
deleteTestObjects();
}
protected Customer findActiveCustomerWithDiscount(
BigDecimal percentDiscount) {
Address billingAddress = new Address("1222 1st St SW",
"Calgary", "Alberta",
"T2N 2V2", "Canada");
Address shippingAddress = new Address("1333 1st St SW",
"Calgary", "Alberta",
"T2N 2V2", "Canada");
return new Customer( 99, "John", "Doe",
percentDiscount,
billingAddress,
shippingAddress);
}
protected Product findCurrentProductWith3DigitPrice() {
return new Product( 88, "SomeWidget",
new BigDecimal("19.99"));
}
protected Invoice createInvoice(Customer customer) {
Invoice newInvoice = new Invoice(customer);
registerTestObject(newInvoice);
return newInvoice;
}
List testObjects;
protected void registerTestObject(Object testObject) {
testObjects.add(testObject);
}
private void deleteTestObjects() {
Iterator i = testObjects.iterator();
while (i.hasNext()) {
try {
deleteObject(i.next());
} catch (RuntimeException e) {
// Nothing to do; we just want to make sure
// we continue on to the next object in the list.
}
}
}
protected LineItem createLineItem(int quantity, BigDecimal discountPercentage,
BigDecimal extendedPrice, Product product, Invoice invoice) {
return new LineItem(quantity, discountPercentage,
extendedPrice, product.getName(), invoice.getName());
}
protected void assertLineItemsEqual(String message, LineItem expected, LineItem actual) {
// assertMessage(message)
assertEquals("inv", expected.getInv(), actual.getInv());
assertEquals("prod", expected.getProd(), actual.getProd());
assertEquals("quant", expected.getQuantity(), actual.getQuantity());
assertEquals("discount",
expected.getPercentDiscount(),
actual.getPercentDiscount());
assertEquals("unit price",
expected.getUnitPrice(),
actual.getUnitPrice());
assertEquals("extended",
expected.getExtendedPrice(),
actual.getExtendedPrice());
}
protected void assertContainsExactlyOneLineItem(Invoice invoice,
LineItem expected) {
List lineItems = invoice.getLineItems();
assertEquals("number of items", lineItems.size(), 1);
LineItem actItem = (LineItem) lineItem.get(0);
assertLineItemsEqual("", expected, actItem);
}
public void testAddItemQuantity_sevelhralQuantity_v1(){
final int QUANTITY = 5;
final BigDecimal CUSTOMER_DISCOUNT = new BigDecimal("30");
// Fixture Setup
Customer customer =
findActiveCustomerWithDiscount(CUSTOMER_DISCOUNT);
Product product = findCurrentProductWith3DigitPrice();
Invoice invoice = createInvoice(customer);
// Exercise SUT
invoice.addItemQuantity(product, QUANTITY);
// Verify Outcome
final BigDecimal BASE_PRICE = product.getUnitPrice().
multiply(new BigDecimal(QUANTITY));
final BigDecimal EXTENDED_PRICE =
BASE_PRICE.subtract(BASE_PRICE.multiply(
CUSTOMER_DISCOUNT.movePointLeft(2)));
LineItem expected =
createLineItem(QUANTITY, CUSTOMER_DISCOUNT,
EXTENDED_PRICE, product, invoice);
assertContainsExactlyOneLineItem(invoice, expected);
}
}