-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoneyTest.java
More file actions
114 lines (97 loc) · 3.12 KB
/
MoneyTest.java
File metadata and controls
114 lines (97 loc) · 3.12 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
104
105
106
107
108
109
110
111
112
113
114
package money;
import static org.junit.Assert.*;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.HashMap;
import org.junit.Test;
/**
* Class of Unit tests checking proper functionality of class Money
*/
public class MoneyTest {
/**
* Test method for {@link money.Money#Money(java.math.BigDecimal)}.
*/
@Test
public final void testMoneyBigDecimal() {
final BigDecimal amount = new BigDecimal("98.76");
final Money m = new Money(amount);
assertTrue(m.getAmount().equals(amount) && m.getCurrency().equals("EUR"));
}
/**
* Test method for {@link money.Money#Money(java.math.BigDecimal, java.lang.String)}.
*/
@Test
public final void testMoneyBigDecimalString() {
final BigDecimal amount = new BigDecimal("98.76");
final Money m = new Money(amount, "USD");
assertTrue(m.getAmount().equals(amount) && m.getCurrency().equals("USD"));
}
/**
* Test method for {@link money.Money#Money(java.lang.String, java.lang.String)}.
*/
@Test
public final void testMoneyStringString() {
final BigDecimal amount = new BigDecimal("98.76");
final Money m = new Money("98.76", "USD");
assertTrue(m.getAmount().equals(amount) && m.getCurrency().equals("USD"));
}
/**
* Test method for {@link money.Money#getAmount()}.
*/
@Test
public final void testGetAmount() {
final BigDecimal amount = new BigDecimal("98.76");
final Money m = new Money("98.76", "USD");
assertTrue(m.getAmount().equals(amount));
}
/**
* Test method for {@link money.Money#getCurrency()}.
*/
@Test
public final void testGetCurrency() {
final Money m = new Money("98.76", "USD");
assertTrue(m.getCurrency().equals("USD"));
}
/**
* Test method for {@link money.Money#toString()}.
*/
@Test
public final void testToString() {
final Money m = new Money("98.76", "USD");
assertTrue(m.toString().equals("USD 98.76"));
}
/**
* Test method for {@link money.Money#multiplyBy(int)}.
*/
@Test
public final void testMultiplyBy() {
final Money a = new Money("98.76", "USD");
final Money aTimes5 = a.multiplyBy(5);
assertTrue(aTimes5.toString().equals("USD 493.80"));
}
/**
* Test method for {@link money.Money#divideBy(int)}.
*/
@Test
public final void testDivideBy() {
final Money a = new Money("493.80", "USD");
final Money aAgain = a.divideBy(5);
assertTrue(aAgain.toString().equals("USD 98.76"));
}
/**
* Test method for {@link money.Money#convertToOtherCurrency(java.lang.String, java.util.HashMap)}.
*/
@Test
public final void testConvertToOtherCurrency() {
HashMap<String,BigDecimal> currencies = new HashMap<String,BigDecimal>();
MathContext mc = new MathContext(4, RoundingMode.HALF_EVEN);
currencies.put("EUR", new BigDecimal(1.0, mc));
currencies.put("USD", new BigDecimal(1.11, mc));
final Money a = new Money("98.76", "USD");
final Money b = new Money("67.89", "EUR");
final Money aConvertedToEUR = a.convertToOtherCurrency("EUR", currencies);
final Money bConvertedToUSD = b.convertToOtherCurrency("USD", currencies);
assertTrue(aConvertedToEUR.toString().equals("EUR 88.97") && bConvertedToUSD.toString().equals("USD 75.36"));
}
}