1 //package junit.samples.money;
2 package com.cenqua.samples.money;
4 import junit.framework.TestCase;
7 public class MoneyBagTest extends TestCase {
16 public static void main(String args[]) {
17 junit.textui.TestRunner.run(MoneyBagTest.class);
20 protected void setUp() {
21 f12CHF = new Money(12, "CHF");
22 f14CHF = new Money(14, "CHF");
23 f7USD = new Money(7, "USD");
24 f21USD = new Money(21, "USD");
26 fMB1 = MoneyBag.create(f12CHF, f7USD);
27 fMB2 = MoneyBag.create(f14CHF, f21USD);
30 protected void tearDown() throws Exception {
31 // call toString, guranteed to be only called from here.
32 // this is for testing, coverage by test.
36 public void testBagMultiply() {
37 // {[12 CHF][7 USD]} *2 == {[24 CHF][14 USD]}
38 IMoney expected = MoneyBag.create(new Money(24, "CHF"), new Money(14, "USD"));
39 assertEquals(expected, fMB1.multiply(2));
40 assertEquals(fMB1, fMB1.multiply(1));
41 assertTrue(fMB1.multiply(0).isZero());
44 public void testBagNegate() {
45 // {[12 CHF][7 USD]} negate == {[-12 CHF][-7 USD]}
46 IMoney expected = MoneyBag.create(new Money(-12, "CHF"), new Money(-7, "USD"));
47 assertEquals(expected, fMB1.negate());
50 public void testBagSimpleAdd() {
51 // {[12 CHF][7 USD]} + [14 CHF] == {[26 CHF][7 USD]}
52 IMoney expected = MoneyBag.create(new Money(26, "CHF"), new Money(7, "USD"));
53 assertEquals(expected, fMB1.add(f14CHF));
56 public void testBagSubtract() {
57 // {[12 CHF][7 USD]} - {[14 CHF][21 USD] == {[-2 CHF][-14 USD]}
58 IMoney expected = MoneyBag.create(new Money(-2, "CHF"), new Money(-14, "USD"));
59 assertEquals(expected, fMB1.subtract(fMB2));
62 public void testBagSumAdd() {
63 // {[12 CHF][7 USD]} + {[14 CHF][21 USD]} == {[26 CHF][28 USD]}
64 IMoney expected = MoneyBag.create(new Money(26, "CHF"), new Money(28, "USD"));
65 assertEquals(expected, fMB1.add(fMB2));
68 public void testIsZero() {
69 assertTrue(fMB1.subtract(fMB1).isZero());
70 assertTrue(MoneyBag.create(new Money(0, "CHF"), new Money(0, "USD")).isZero());
73 public void testMixedSimpleAdd() {
74 // [12 CHF] + [7 USD] == {[12 CHF][7 USD]}
75 IMoney expected = MoneyBag.create(f12CHF, f7USD);
76 assertEquals(expected, f12CHF.add(f7USD));
79 public void testBagNotEquals() {
80 IMoney bag = MoneyBag.create(f12CHF, f7USD);
81 assertFalse(bag.equals(new Money(12, "DEM").add(f7USD)));
84 public void testMoneyBagEquals() {
85 assertTrue(!fMB1.equals(null));
87 assertEquals(fMB1, fMB1);
88 IMoney equal = MoneyBag.create(new Money(12, "CHF"), new Money(7, "USD"));
89 assertTrue(fMB1.equals(equal));
90 assertTrue(!fMB1.equals(f12CHF));
91 assertTrue(!f12CHF.equals(fMB1));
92 assertTrue(!fMB1.equals(fMB2));
95 public void testMoneyBagHash() {
96 IMoney equal = MoneyBag.create(new Money(12, "CHF"), new Money(7, "USD"));
97 assertEquals(fMB1.hashCode(), equal.hashCode());
100 public void testMoneyEquals() {
101 assertTrue(!f12CHF.equals(null));
102 Money equalMoney = new Money(12, "CHF");
103 assertEquals(f12CHF, f12CHF);
104 assertEquals(f12CHF, equalMoney);
105 assertEquals(f12CHF.hashCode(), equalMoney.hashCode());
106 assertTrue(!f12CHF.equals(f14CHF));
109 public void testMoneyHash() {
110 assertTrue(!f12CHF.equals(null));
111 Money equal = new Money(12, "CHF");
112 assertEquals(f12CHF.hashCode(), equal.hashCode());
115 public void testSimplify() {
116 IMoney money = MoneyBag.create(new Money(26, "CHF"), new Money(28, "CHF"));
117 assertEquals(new Money(54, "CHF"), money);
120 public void testNormalize2() {
121 // {[12 CHF][7 USD]} - [12 CHF] == [7 USD]
122 Money expected = new Money(7, "USD");
123 assertEquals(expected, fMB1.subtract(f12CHF));
126 public void testNormalize3() {
127 // {[12 CHF][7 USD]} - {[12 CHF][3 USD]} == [4 USD]
128 IMoney ms1 = MoneyBag.create(new Money(12, "CHF"), new Money(3, "USD"));
129 Money expected = new Money(4, "USD");
130 assertEquals(expected, fMB1.subtract(ms1));
133 public void testNormalize4() {
134 // [12 CHF] - {[12 CHF][3 USD]} == [-3 USD]
135 IMoney ms1 = MoneyBag.create(new Money(12, "CHF"), new Money(3, "USD"));
136 Money expected = new Money(-3, "USD");
137 assertEquals(expected, f12CHF.subtract(ms1));
140 public void testPrint() {
141 assertEquals("[12 CHF]", f12CHF.toString());
144 public void testSimpleAdd() {
145 // [12 CHF] + [14 CHF] == [26 CHF]
146 Money expected = new Money(26, "CHF");
147 assertEquals(expected, f12CHF.add(f14CHF));
150 public void testSimpleBagAdd() {
151 // [14 CHF] + {[12 CHF][7 USD]} == {[26 CHF][7 USD]}
152 IMoney expected = MoneyBag.create(new Money(26, "CHF"), new Money(7, "USD"));
153 assertEquals(expected, f14CHF.add(fMB1));
156 public void testSimpleMultiply() {
157 // [14 CHF] *2 == [28 CHF]
158 Money expected = new Money(28, "CHF");
159 assertEquals(expected, f14CHF.multiply(2));
162 public void testSimpleNegate() {
163 // [14 CHF] negate == [-14 CHF]
164 Money expected = new Money(-14, "CHF");
165 assertEquals(expected, f14CHF.negate());
168 public void testSimpleSubtract() {
169 // [14 CHF] - [12 CHF] == [2 CHF]
170 Money expected = new Money(2, "CHF");
171 assertEquals(expected, f14CHF.subtract(f12CHF));