1
2
3 package com.pnpconsult.zeiterfassung.model;
4
5 import java.text.ParseException;
6
7 import junit.framework.TestCase;
8
9 import com.pnpconsult.zeiterfassung.util.DateUtils;
10
11 /***
12 * @author <a href="mailto:powerpete@users.sf.net">Moritz Petersen </a>
13 * @version $Id: BillTest.java,v 1.2 2004/07/21 22:13:08 powerpete Exp $
14 */
15 public class BillTest extends TestCase
16 {
17
18 public BillTest(String arg0)
19 {
20 super(arg0);
21 }
22
23 public void testGetLabel() throws Exception
24 {
25 Bill bill = new Bill();
26 assertLabel("", bill);
27 Project project = new Project();
28 project.setName("Foo");
29 bill.setProject(project);
30 assertLabel("Foo", bill);
31 project.setNumber("08/15");
32 assertLabel("Foo (08/15)", bill);
33 Customer customer = new Customer();
34 customer.setName("Bar");
35 project.setCustomer(customer);
36 assertLabel("Bar - Foo (08/15)", bill);
37 bill.setStartDate(DateUtils.parse("14.1.2004"));
38 assertLabel("Bar - Foo (08/15) - Januar 2004", bill);
39 bill.setEndDate(DateUtils.parse("13.2.2004"));
40 assertLabel("Bar - Foo (08/15) - Januar 2004 - Februar 2004", bill);
41 }
42
43 private void assertLabel(String string, Bill bill)
44 {
45 assertEquals(string, bill.getLabel());
46 }
47
48 public void testCompareTo() throws Exception
49 {
50 assertCompareTo(new Bill(), new Bill(), 0);
51
52 Bill b1 = newBill("Foo", null, null, null, null);
53 Bill b2 = newBill("Bar", null, null, null, null);
54 assertCompareTo(b1, b2, 1);
55
56 b1 = newBill("Foo", "AAA", null, null, null);
57 b2 = newBill("Bar", "ZZZ", null, null, null);
58 assertCompareTo(b1, b2, -1);
59
60 b1 = newBill("Foo", "AAA", "22.02.2004", null, null);
61 b2 = newBill("Bar", "ZZZ", "11.03.2004", null, null);
62 assertCompareTo(b1, b2, 1);
63
64 b1 = newBill("Foo", "AAA", "22.02.2004", null, null);
65 b2 = newBill("Bar", "ZZZ", null, null, "11.03.2004");
66 assertCompareTo(b1, b2, 1);
67
68 b1 = newBill("Foo", "AAA", "22.02.2004", null, "11.03.2004");
69 b2 = newBill("Bar", "ZZZ", "11.03.2004", null, "22.02.2004");
70 assertCompareTo(b1, b2, -1);
71 }
72
73 private Bill newBill(
74 String projectName,
75 String customerName,
76 String startDate,
77 String endDate,
78 String billingDate) throws ParseException
79 {
80 Bill bill = new Bill();
81 Project project = new Project();
82 project.setName(projectName);
83 if (customerName != null)
84 {
85 Customer customer = new Customer();
86 customer.setName(customerName);
87 project.setCustomer(customer);
88 }
89 bill.setProject(project);
90 if (startDate != null)
91 {
92 bill.setStartDate(DateUtils.parse(startDate));
93 }
94 if (endDate != null)
95 {
96 bill.setEndDate(DateUtils.parse(endDate));
97 }
98 if (billingDate != null)
99 {
100 bill.setBillingDate(DateUtils.parse(billingDate));
101 }
102 return bill;
103 }
104
105 private void assertCompareTo(Comparable b1, Comparable b2, int expected)
106 {
107 int actual = b1.compareTo(b2);
108 if (expected < 0)
109 {
110 assertTrue("Expected "
111 + b1
112 + " < "
113 + b2
114 + " but actual was: "
115 + actual, actual < 0);
116 }
117 else if (expected > 0)
118 {
119 assertTrue("Expected "
120 + b1
121 + " > "
122 + b2
123 + " but actual was: "
124 + actual, actual > 0);
125 }
126 else
127 {
128 assertTrue("Expected "
129 + b1
130 + " = "
131 + b2
132 + " but actual was: "
133 + actual, actual == 0);
134 }
135 }
136 }