KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junit > samples > money > MoneyTest


1 package junit.samples.money;
2
3 import junit.framework.*;
4
5 public class MoneyTest extends TestCase {
6     private Money f12CHF;
7     private Money f14CHF;
8     private Money f7USD;
9     private Money f21USD;
10
11     private IMoney fMB1;
12     private IMoney fMB2;
13
14     public static void main(String JavaDoc args[]) {
15         junit.textui.TestRunner.run(MoneyTest.class);
16     }
17     protected void setUp() {
18         f12CHF= new Money(12, "CHF");
19         f14CHF= new Money(14, "CHF");
20         f7USD= new Money( 7, "USD");
21         f21USD= new Money(21, "USD");
22
23         fMB1= MoneyBag.create(f12CHF, f7USD);
24         fMB2= MoneyBag.create(f14CHF, f21USD);
25     }
26     public void testBagMultiply() {
27         // {[12 CHF][7 USD]} *2 == {[24 CHF][14 USD]}
28
IMoney expected= MoneyBag.create(new Money(24, "CHF"), new Money(14, "USD"));
29         assertEquals(expected, fMB1.multiply(2));
30         assertEquals(fMB1, fMB1.multiply(1));
31         assertTrue(fMB1.multiply(0).isZero());
32     }
33     public void testBagNegate() {
34         // {[12 CHF][7 USD]} negate == {[-12 CHF][-7 USD]}
35
IMoney expected= MoneyBag.create(new Money(-12, "CHF"), new Money(-7, "USD"));
36         assertEquals(expected, fMB1.negate());
37     }
38     public void testBagSimpleAdd() {
39         // {[12 CHF][7 USD]} + [14 CHF] == {[26 CHF][7 USD]}
40
IMoney expected= MoneyBag.create(new Money(26, "CHF"), new Money(7, "USD"));
41         assertEquals(expected, fMB1.add(f14CHF));
42     }
43     public void testBagSubtract() {
44         // {[12 CHF][7 USD]} - {[14 CHF][21 USD] == {[-2 CHF][-14 USD]}
45
IMoney expected= MoneyBag.create(new Money(-2, "CHF"), new Money(-14, "USD"));
46         assertEquals(expected, fMB1.subtract(fMB2));
47     }
48     public void testBagSumAdd() {
49         // {[12 CHF][7 USD]} + {[14 CHF][21 USD]} == {[26 CHF][28 USD]}
50
IMoney expected= MoneyBag.create(new Money(26, "CHF"), new Money(28, "USD"));
51         assertEquals(expected, fMB1.add(fMB2));
52     }
53     public void testIsZero() {
54         assertTrue(fMB1.subtract(fMB1).isZero());
55         assertTrue(MoneyBag.create(new Money (0, "CHF"), new Money (0, "USD")).isZero());
56     }
57     public void testMixedSimpleAdd() {
58         // [12 CHF] + [7 USD] == {[12 CHF][7 USD]}
59
IMoney expected= MoneyBag.create(f12CHF, f7USD);
60         assertEquals(expected, f12CHF.add(f7USD));
61     }
62     public void testBagNotEquals() {
63         IMoney bag= MoneyBag.create(f12CHF, f7USD);
64         assertFalse(bag.equals(new Money(12, "DEM").add(f7USD)));
65     }
66     public void testMoneyBagEquals() {
67         assertTrue(!fMB1.equals(null));
68
69         assertEquals(fMB1, fMB1);
70         IMoney equal= MoneyBag.create(new Money(12, "CHF"), new Money(7, "USD"));
71         assertTrue(fMB1.equals(equal));
72         assertTrue(!fMB1.equals(f12CHF));
73         assertTrue(!f12CHF.equals(fMB1));
74         assertTrue(!fMB1.equals(fMB2));
75     }
76     public void testMoneyBagHash() {
77         IMoney equal= MoneyBag.create(new Money(12, "CHF"), new Money(7, "USD"));
78         assertEquals(fMB1.hashCode(), equal.hashCode());
79     }
80     public void testMoneyEquals() {
81         assertTrue(!f12CHF.equals(null));
82         Money equalMoney= new Money(12, "CHF");
83         assertEquals(f12CHF, f12CHF);
84         assertEquals(f12CHF, equalMoney);
85         assertEquals(f12CHF.hashCode(), equalMoney.hashCode());
86         assertTrue(!f12CHF.equals(f14CHF));
87     }
88     public void testMoneyHash() {
89         assertTrue(!f12CHF.equals(null));
90         Money equal= new Money(12, "CHF");
91         assertEquals(f12CHF.hashCode(), equal.hashCode());
92     }
93     public void testSimplify() {
94         IMoney money= MoneyBag.create(new Money(26, "CHF"), new Money(28, "CHF"));
95         assertEquals(new Money(54, "CHF"), money);
96     }
97     public void testNormalize2() {
98         // {[12 CHF][7 USD]} - [12 CHF] == [7 USD]
99
Money expected= new Money(7, "USD");
100         assertEquals(expected, fMB1.subtract(f12CHF));
101     }
102     public void testNormalize3() {
103         // {[12 CHF][7 USD]} - {[12 CHF][3 USD]} == [4 USD]
104
IMoney ms1= MoneyBag.create(new Money(12, "CHF"), new Money(3, "USD"));
105         Money expected= new Money(4, "USD");
106         assertEquals(expected, fMB1.subtract(ms1));
107     }
108     public void testNormalize4() {
109         // [12 CHF] - {[12 CHF][3 USD]} == [-3 USD]
110
IMoney ms1= MoneyBag.create(new Money(12, "CHF"), new Money(3, "USD"));
111         Money expected= new Money(-3, "USD");
112         assertEquals(expected, f12CHF.subtract(ms1));
113     }
114     public void testPrint() {
115         assertEquals("[12 CHF]", f12CHF.toString());
116     }
117     public void testSimpleAdd() {
118         // [12 CHF] + [14 CHF] == [26 CHF]
119
Money expected= new Money(26, "CHF");
120         assertEquals(expected, f12CHF.add(f14CHF));
121     }
122     public void testSimpleBagAdd() {
123         // [14 CHF] + {[12 CHF][7 USD]} == {[26 CHF][7 USD]}
124
IMoney expected= MoneyBag.create(new Money(26, "CHF"), new Money(7, "USD"));
125         assertEquals(expected, f14CHF.add(fMB1));
126     }
127     public void testSimpleMultiply() {
128         // [14 CHF] *2 == [28 CHF]
129
Money expected= new Money(28, "CHF");
130         assertEquals(expected, f14CHF.multiply(2));
131     }
132     public void testSimpleNegate() {
133         // [14 CHF] negate == [-14 CHF]
134
Money expected= new Money(-14, "CHF");
135         assertEquals(expected, f14CHF.negate());
136     }
137     public void testSimpleSubtract() {
138         // [14 CHF] - [12 CHF] == [2 CHF]
139
Money expected= new Money(2, "CHF");
140         assertEquals(expected, f14CHF.subtract(f12CHF));
141     }
142 }
Popular Tags