|
1 | | -package com.baeldung.money; |
2 | | - |
3 | | -import org.javamoney.moneta.FastMoney; |
4 | | -import org.javamoney.moneta.Money; |
5 | | -import org.javamoney.moneta.format.CurrencyStyle; |
6 | | -import org.junit.Ignore; |
7 | | -import org.junit.Test; |
8 | | - |
9 | | -import javax.money.CurrencyUnit; |
10 | | -import javax.money.Monetary; |
11 | | -import javax.money.MonetaryAmount; |
12 | | -import javax.money.UnknownCurrencyException; |
13 | | -import javax.money.convert.CurrencyConversion; |
14 | | -import javax.money.convert.MonetaryConversions; |
15 | | -import javax.money.format.AmountFormatQueryBuilder; |
16 | | -import javax.money.format.MonetaryAmountFormat; |
17 | | -import javax.money.format.MonetaryFormats; |
18 | | -import java.util.Arrays; |
19 | | -import java.util.List; |
20 | | -import java.util.Locale; |
21 | | - |
22 | | -import static org.junit.Assert.assertEquals; |
23 | | -import static org.junit.Assert.assertFalse; |
24 | | -import static org.junit.Assert.assertNotNull; |
25 | | -import static org.junit.Assert.assertTrue; |
26 | | -import static org.junit.Assert.fail; |
27 | | - |
28 | | -public class JavaMoneyUnitTest { |
29 | | - |
30 | | - @Test |
31 | | - public void givenCurrencyCode_whenString_thanExist() { |
32 | | - CurrencyUnit usd = Monetary.getCurrency("USD"); |
33 | | - |
34 | | - assertNotNull(usd); |
35 | | - assertEquals(usd.getCurrencyCode(), "USD"); |
36 | | - assertEquals(usd.getNumericCode(), 840); |
37 | | - assertEquals(usd.getDefaultFractionDigits(), 2); |
38 | | - } |
39 | | - |
40 | | - @Test(expected = UnknownCurrencyException.class) |
41 | | - public void givenCurrencyCode_whenNoExist_thanThrowsError() { |
42 | | - Monetary.getCurrency("AAA"); |
43 | | - } |
44 | | - |
45 | | - @Test |
46 | | - public void givenAmounts_whenStringified_thanEquals() { |
47 | | - CurrencyUnit usd = Monetary.getCurrency("USD"); |
48 | | - MonetaryAmount fstAmtUSD = Monetary |
49 | | - .getDefaultAmountFactory() |
50 | | - .setCurrency(usd) |
51 | | - .setNumber(200) |
52 | | - .create(); |
53 | | - Money moneyof = Money.of(12, usd); |
54 | | - FastMoney fastmoneyof = FastMoney.of(2, usd); |
55 | | - |
56 | | - assertEquals("USD", usd.toString()); |
57 | | - assertEquals("USD 200", fstAmtUSD.toString()); |
58 | | - assertEquals("USD 12", moneyof.toString()); |
59 | | - assertEquals("USD 2.00000", fastmoneyof.toString()); |
60 | | - } |
61 | | - |
62 | | - @Test |
63 | | - public void givenCurrencies_whenCompared_thanNotequal() { |
64 | | - MonetaryAmount oneDolar = Monetary |
65 | | - .getDefaultAmountFactory() |
66 | | - .setCurrency("USD") |
67 | | - .setNumber(1) |
68 | | - .create(); |
69 | | - Money oneEuro = Money.of(1, "EUR"); |
70 | | - |
71 | | - assertFalse(oneEuro.equals(FastMoney.of(1, "EUR"))); |
72 | | - assertTrue(oneDolar.equals(Money.of(1, "USD"))); |
73 | | - } |
74 | | - |
75 | | - @Test(expected = ArithmeticException.class) |
76 | | - public void givenAmount_whenDivided_thanThrowsException() { |
77 | | - MonetaryAmount oneDolar = Monetary |
78 | | - .getDefaultAmountFactory() |
79 | | - .setCurrency("USD") |
80 | | - .setNumber(1) |
81 | | - .create(); |
82 | | - oneDolar.divide(3); |
83 | | - fail(); // if no exception |
84 | | - } |
85 | | - |
86 | | - @Test |
87 | | - public void givenAmounts_whenSummed_thanCorrect() { |
88 | | - List<MonetaryAmount> monetaryAmounts = Arrays.asList(Money.of(100, "CHF"), Money.of(10.20, "CHF"), Money.of(1.15, "CHF")); |
89 | | - |
90 | | - Money sumAmtCHF = (Money) monetaryAmounts |
91 | | - .stream() |
92 | | - .reduce(Money.of(0, "CHF"), MonetaryAmount::add); |
93 | | - |
94 | | - assertEquals("CHF 111.35", sumAmtCHF.toString()); |
95 | | - } |
96 | | - |
97 | | - @Test |
98 | | - public void givenArithmetic_whenStringified_thanEqualsAmount() { |
99 | | - CurrencyUnit usd = Monetary.getCurrency("USD"); |
100 | | - |
101 | | - Money moneyof = Money.of(12, usd); |
102 | | - MonetaryAmount fstAmtUSD = Monetary |
103 | | - .getDefaultAmountFactory() |
104 | | - .setCurrency(usd) |
105 | | - .setNumber(200.50) |
106 | | - .create(); |
107 | | - MonetaryAmount oneDolar = Monetary |
108 | | - .getDefaultAmountFactory() |
109 | | - .setCurrency("USD") |
110 | | - .setNumber(1) |
111 | | - .create(); |
112 | | - Money subtractedAmount = Money |
113 | | - .of(1, "USD") |
114 | | - .subtract(fstAmtUSD); |
115 | | - MonetaryAmount multiplyAmount = oneDolar.multiply(0.25); |
116 | | - MonetaryAmount divideAmount = oneDolar.divide(0.25); |
117 | | - |
118 | | - assertEquals("USD", usd.toString()); |
119 | | - assertEquals("USD 1", oneDolar.toString()); |
120 | | - assertEquals("USD 200.5", fstAmtUSD.toString()); |
121 | | - assertEquals("USD 12", moneyof.toString()); |
122 | | - assertEquals("USD -199.5", subtractedAmount.toString()); |
123 | | - assertEquals("USD 0.25", multiplyAmount.toString()); |
124 | | - assertEquals("USD 4", divideAmount.toString()); |
125 | | - } |
126 | | - |
127 | | - @Test |
128 | | - public void givenAmount_whenRounded_thanEquals() { |
129 | | - MonetaryAmount fstAmtEUR = Monetary |
130 | | - .getDefaultAmountFactory() |
131 | | - .setCurrency("EUR") |
132 | | - .setNumber(1.30473908) |
133 | | - .create(); |
134 | | - MonetaryAmount roundEUR = fstAmtEUR.with(Monetary.getDefaultRounding()); |
135 | | - assertEquals("EUR 1.30473908", fstAmtEUR.toString()); |
136 | | - assertEquals("EUR 1.3", roundEUR.toString()); |
137 | | - } |
138 | | - |
139 | | - @Test |
140 | | - @Ignore("Currency providers are not always available") |
141 | | - public void givenAmount_whenConversion_thenNotNull() { |
142 | | - MonetaryAmount oneDollar = Monetary |
143 | | - .getDefaultAmountFactory() |
144 | | - .setCurrency("USD") |
145 | | - .setNumber(1) |
146 | | - .create(); |
147 | | - |
148 | | - CurrencyConversion conversionEUR = MonetaryConversions.getConversion("EUR"); |
149 | | - |
150 | | - MonetaryAmount convertedAmountUSDtoEUR = oneDollar.with(conversionEUR); |
151 | | - |
152 | | - assertEquals("USD 1", oneDollar.toString()); |
153 | | - assertNotNull(convertedAmountUSDtoEUR); |
154 | | - } |
155 | | - |
156 | | - @Test |
157 | | - public void givenLocale_whenFormatted_thanEquals() { |
158 | | - MonetaryAmount oneDollar = Monetary |
159 | | - .getDefaultAmountFactory() |
160 | | - .setCurrency("USD") |
161 | | - .setNumber(1) |
162 | | - .create(); |
163 | | - MonetaryAmountFormat formatUSD = MonetaryFormats.getAmountFormat(Locale.US); |
164 | | - String usFormatted = formatUSD.format(oneDollar); |
165 | | - |
166 | | - assertEquals("USD 1", oneDollar.toString()); |
167 | | - assertNotNull(formatUSD); |
168 | | - assertEquals("USD1.00", usFormatted); |
169 | | - } |
170 | | - |
171 | | - @Test |
172 | | - public void givenAmount_whenCustomFormat_thanEquals() { |
173 | | - MonetaryAmount oneDollar = Monetary |
174 | | - .getDefaultAmountFactory() |
175 | | - .setCurrency("USD") |
176 | | - .setNumber(1) |
177 | | - .create(); |
178 | | - |
179 | | - MonetaryAmountFormat customFormat = MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder |
180 | | - .of(Locale.US) |
181 | | - .set(CurrencyStyle.NAME) |
182 | | - .set("pattern", "00000.00 ¤") |
183 | | - .build()); |
184 | | - String customFormatted = customFormat.format(oneDollar); |
185 | | - |
186 | | - assertNotNull(customFormat); |
187 | | - assertEquals("USD 1", oneDollar.toString()); |
188 | | - assertEquals("00001.00 US Dollar", customFormatted); |
189 | | - } |
190 | | -} |
| 1 | +package com.baeldung.money; |
| 2 | + |
| 3 | +import org.javamoney.moneta.FastMoney; |
| 4 | +import org.javamoney.moneta.Money; |
| 5 | +import org.javamoney.moneta.format.CurrencyStyle; |
| 6 | +import org.junit.Ignore; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +import javax.money.CurrencyUnit; |
| 10 | +import javax.money.Monetary; |
| 11 | +import javax.money.MonetaryAmount; |
| 12 | +import javax.money.UnknownCurrencyException; |
| 13 | +import javax.money.convert.CurrencyConversion; |
| 14 | +import javax.money.convert.MonetaryConversions; |
| 15 | +import javax.money.format.AmountFormatQueryBuilder; |
| 16 | +import javax.money.format.MonetaryAmountFormat; |
| 17 | +import javax.money.format.MonetaryFormats; |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Locale; |
| 21 | + |
| 22 | +import static org.junit.Assert.assertEquals; |
| 23 | +import static org.junit.Assert.assertFalse; |
| 24 | +import static org.junit.Assert.assertNotNull; |
| 25 | +import static org.junit.Assert.assertTrue; |
| 26 | +import static org.junit.Assert.fail; |
| 27 | + |
| 28 | +public class JavaMoneyUnitManualTest { |
| 29 | + |
| 30 | + @Test |
| 31 | + public void givenCurrencyCode_whenString_thanExist() { |
| 32 | + CurrencyUnit usd = Monetary.getCurrency("USD"); |
| 33 | + |
| 34 | + assertNotNull(usd); |
| 35 | + assertEquals(usd.getCurrencyCode(), "USD"); |
| 36 | + assertEquals(usd.getNumericCode(), 840); |
| 37 | + assertEquals(usd.getDefaultFractionDigits(), 2); |
| 38 | + } |
| 39 | + |
| 40 | + @Test(expected = UnknownCurrencyException.class) |
| 41 | + public void givenCurrencyCode_whenNoExist_thanThrowsError() { |
| 42 | + Monetary.getCurrency("AAA"); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void givenAmounts_whenStringified_thanEquals() { |
| 47 | + CurrencyUnit usd = Monetary.getCurrency("USD"); |
| 48 | + MonetaryAmount fstAmtUSD = Monetary |
| 49 | + .getDefaultAmountFactory() |
| 50 | + .setCurrency(usd) |
| 51 | + .setNumber(200) |
| 52 | + .create(); |
| 53 | + Money moneyof = Money.of(12, usd); |
| 54 | + FastMoney fastmoneyof = FastMoney.of(2, usd); |
| 55 | + |
| 56 | + assertEquals("USD", usd.toString()); |
| 57 | + assertEquals("USD 200", fstAmtUSD.toString()); |
| 58 | + assertEquals("USD 12", moneyof.toString()); |
| 59 | + assertEquals("USD 2.00000", fastmoneyof.toString()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void givenCurrencies_whenCompared_thanNotequal() { |
| 64 | + MonetaryAmount oneDolar = Monetary |
| 65 | + .getDefaultAmountFactory() |
| 66 | + .setCurrency("USD") |
| 67 | + .setNumber(1) |
| 68 | + .create(); |
| 69 | + Money oneEuro = Money.of(1, "EUR"); |
| 70 | + |
| 71 | + assertFalse(oneEuro.equals(FastMoney.of(1, "EUR"))); |
| 72 | + assertTrue(oneDolar.equals(Money.of(1, "USD"))); |
| 73 | + } |
| 74 | + |
| 75 | + @Test(expected = ArithmeticException.class) |
| 76 | + public void givenAmount_whenDivided_thanThrowsException() { |
| 77 | + MonetaryAmount oneDolar = Monetary |
| 78 | + .getDefaultAmountFactory() |
| 79 | + .setCurrency("USD") |
| 80 | + .setNumber(1) |
| 81 | + .create(); |
| 82 | + oneDolar.divide(3); |
| 83 | + fail(); // if no exception |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void givenAmounts_whenSummed_thanCorrect() { |
| 88 | + List<MonetaryAmount> monetaryAmounts = Arrays.asList(Money.of(100, "CHF"), Money.of(10.20, "CHF"), Money.of(1.15, "CHF")); |
| 89 | + |
| 90 | + Money sumAmtCHF = (Money) monetaryAmounts |
| 91 | + .stream() |
| 92 | + .reduce(Money.of(0, "CHF"), MonetaryAmount::add); |
| 93 | + |
| 94 | + assertEquals("CHF 111.35", sumAmtCHF.toString()); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void givenArithmetic_whenStringified_thanEqualsAmount() { |
| 99 | + CurrencyUnit usd = Monetary.getCurrency("USD"); |
| 100 | + |
| 101 | + Money moneyof = Money.of(12, usd); |
| 102 | + MonetaryAmount fstAmtUSD = Monetary |
| 103 | + .getDefaultAmountFactory() |
| 104 | + .setCurrency(usd) |
| 105 | + .setNumber(200.50) |
| 106 | + .create(); |
| 107 | + MonetaryAmount oneDolar = Monetary |
| 108 | + .getDefaultAmountFactory() |
| 109 | + .setCurrency("USD") |
| 110 | + .setNumber(1) |
| 111 | + .create(); |
| 112 | + Money subtractedAmount = Money |
| 113 | + .of(1, "USD") |
| 114 | + .subtract(fstAmtUSD); |
| 115 | + MonetaryAmount multiplyAmount = oneDolar.multiply(0.25); |
| 116 | + MonetaryAmount divideAmount = oneDolar.divide(0.25); |
| 117 | + |
| 118 | + assertEquals("USD", usd.toString()); |
| 119 | + assertEquals("USD 1", oneDolar.toString()); |
| 120 | + assertEquals("USD 200.5", fstAmtUSD.toString()); |
| 121 | + assertEquals("USD 12", moneyof.toString()); |
| 122 | + assertEquals("USD -199.5", subtractedAmount.toString()); |
| 123 | + assertEquals("USD 0.25", multiplyAmount.toString()); |
| 124 | + assertEquals("USD 4", divideAmount.toString()); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void givenAmount_whenRounded_thanEquals() { |
| 129 | + MonetaryAmount fstAmtEUR = Monetary |
| 130 | + .getDefaultAmountFactory() |
| 131 | + .setCurrency("EUR") |
| 132 | + .setNumber(1.30473908) |
| 133 | + .create(); |
| 134 | + MonetaryAmount roundEUR = fstAmtEUR.with(Monetary.getDefaultRounding()); |
| 135 | + assertEquals("EUR 1.30473908", fstAmtEUR.toString()); |
| 136 | + assertEquals("EUR 1.3", roundEUR.toString()); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + @Ignore("Currency providers are not always available") |
| 141 | + public void givenAmount_whenConversion_thenNotNull() { |
| 142 | + MonetaryAmount oneDollar = Monetary |
| 143 | + .getDefaultAmountFactory() |
| 144 | + .setCurrency("USD") |
| 145 | + .setNumber(1) |
| 146 | + .create(); |
| 147 | + |
| 148 | + CurrencyConversion conversionEUR = MonetaryConversions.getConversion("EUR"); |
| 149 | + |
| 150 | + MonetaryAmount convertedAmountUSDtoEUR = oneDollar.with(conversionEUR); |
| 151 | + |
| 152 | + assertEquals("USD 1", oneDollar.toString()); |
| 153 | + assertNotNull(convertedAmountUSDtoEUR); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void givenLocale_whenFormatted_thanEquals() { |
| 158 | + MonetaryAmount oneDollar = Monetary |
| 159 | + .getDefaultAmountFactory() |
| 160 | + .setCurrency("USD") |
| 161 | + .setNumber(1) |
| 162 | + .create(); |
| 163 | + MonetaryAmountFormat formatUSD = MonetaryFormats.getAmountFormat(Locale.US); |
| 164 | + String usFormatted = formatUSD.format(oneDollar); |
| 165 | + |
| 166 | + assertEquals("USD 1", oneDollar.toString()); |
| 167 | + assertNotNull(formatUSD); |
| 168 | + assertEquals("USD1.00", usFormatted); |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + public void givenAmount_whenCustomFormat_thanEquals() { |
| 173 | + MonetaryAmount oneDollar = Monetary |
| 174 | + .getDefaultAmountFactory() |
| 175 | + .setCurrency("USD") |
| 176 | + .setNumber(1) |
| 177 | + .create(); |
| 178 | + |
| 179 | + MonetaryAmountFormat customFormat = MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder |
| 180 | + .of(Locale.US) |
| 181 | + .set(CurrencyStyle.NAME) |
| 182 | + .set("pattern", "00000.00 �") |
| 183 | + .build()); |
| 184 | + String customFormatted = customFormat.format(oneDollar); |
| 185 | + |
| 186 | + assertNotNull(customFormat); |
| 187 | + assertEquals("USD 1", oneDollar.toString()); |
| 188 | + assertEquals("00001.00 US Dollar", customFormatted); |
| 189 | + } |
| 190 | +} |
0 commit comments