Manual money tests (#2338)

This commit is contained in:
Grzegorz Piwowarek 2017-07-30 07:42:16 +02:00 committed by GitHub
parent f445a4d11b
commit 34a793a0fc
1 changed files with 190 additions and 190 deletions

View File

@ -1,190 +1,190 @@
package com.baeldung.money; package com.baeldung.money;
import org.javamoney.moneta.FastMoney; import org.javamoney.moneta.FastMoney;
import org.javamoney.moneta.Money; import org.javamoney.moneta.Money;
import org.javamoney.moneta.format.CurrencyStyle; import org.javamoney.moneta.format.CurrencyStyle;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import javax.money.CurrencyUnit; import javax.money.CurrencyUnit;
import javax.money.Monetary; import javax.money.Monetary;
import javax.money.MonetaryAmount; import javax.money.MonetaryAmount;
import javax.money.UnknownCurrencyException; import javax.money.UnknownCurrencyException;
import javax.money.convert.CurrencyConversion; import javax.money.convert.CurrencyConversion;
import javax.money.convert.MonetaryConversions; import javax.money.convert.MonetaryConversions;
import javax.money.format.AmountFormatQueryBuilder; import javax.money.format.AmountFormatQueryBuilder;
import javax.money.format.MonetaryAmountFormat; import javax.money.format.MonetaryAmountFormat;
import javax.money.format.MonetaryFormats; import javax.money.format.MonetaryFormats;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
public class JavaMoneyUnitTest { public class JavaMoneyUnitManualTest {
@Test @Test
public void givenCurrencyCode_whenString_thanExist() { public void givenCurrencyCode_whenString_thanExist() {
CurrencyUnit usd = Monetary.getCurrency("USD"); CurrencyUnit usd = Monetary.getCurrency("USD");
assertNotNull(usd); assertNotNull(usd);
assertEquals(usd.getCurrencyCode(), "USD"); assertEquals(usd.getCurrencyCode(), "USD");
assertEquals(usd.getNumericCode(), 840); assertEquals(usd.getNumericCode(), 840);
assertEquals(usd.getDefaultFractionDigits(), 2); assertEquals(usd.getDefaultFractionDigits(), 2);
} }
@Test(expected = UnknownCurrencyException.class) @Test(expected = UnknownCurrencyException.class)
public void givenCurrencyCode_whenNoExist_thanThrowsError() { public void givenCurrencyCode_whenNoExist_thanThrowsError() {
Monetary.getCurrency("AAA"); Monetary.getCurrency("AAA");
} }
@Test @Test
public void givenAmounts_whenStringified_thanEquals() { public void givenAmounts_whenStringified_thanEquals() {
CurrencyUnit usd = Monetary.getCurrency("USD"); CurrencyUnit usd = Monetary.getCurrency("USD");
MonetaryAmount fstAmtUSD = Monetary MonetaryAmount fstAmtUSD = Monetary
.getDefaultAmountFactory() .getDefaultAmountFactory()
.setCurrency(usd) .setCurrency(usd)
.setNumber(200) .setNumber(200)
.create(); .create();
Money moneyof = Money.of(12, usd); Money moneyof = Money.of(12, usd);
FastMoney fastmoneyof = FastMoney.of(2, usd); FastMoney fastmoneyof = FastMoney.of(2, usd);
assertEquals("USD", usd.toString()); assertEquals("USD", usd.toString());
assertEquals("USD 200", fstAmtUSD.toString()); assertEquals("USD 200", fstAmtUSD.toString());
assertEquals("USD 12", moneyof.toString()); assertEquals("USD 12", moneyof.toString());
assertEquals("USD 2.00000", fastmoneyof.toString()); assertEquals("USD 2.00000", fastmoneyof.toString());
} }
@Test @Test
public void givenCurrencies_whenCompared_thanNotequal() { public void givenCurrencies_whenCompared_thanNotequal() {
MonetaryAmount oneDolar = Monetary MonetaryAmount oneDolar = Monetary
.getDefaultAmountFactory() .getDefaultAmountFactory()
.setCurrency("USD") .setCurrency("USD")
.setNumber(1) .setNumber(1)
.create(); .create();
Money oneEuro = Money.of(1, "EUR"); Money oneEuro = Money.of(1, "EUR");
assertFalse(oneEuro.equals(FastMoney.of(1, "EUR"))); assertFalse(oneEuro.equals(FastMoney.of(1, "EUR")));
assertTrue(oneDolar.equals(Money.of(1, "USD"))); assertTrue(oneDolar.equals(Money.of(1, "USD")));
} }
@Test(expected = ArithmeticException.class) @Test(expected = ArithmeticException.class)
public void givenAmount_whenDivided_thanThrowsException() { public void givenAmount_whenDivided_thanThrowsException() {
MonetaryAmount oneDolar = Monetary MonetaryAmount oneDolar = Monetary
.getDefaultAmountFactory() .getDefaultAmountFactory()
.setCurrency("USD") .setCurrency("USD")
.setNumber(1) .setNumber(1)
.create(); .create();
oneDolar.divide(3); oneDolar.divide(3);
fail(); // if no exception fail(); // if no exception
} }
@Test @Test
public void givenAmounts_whenSummed_thanCorrect() { public void givenAmounts_whenSummed_thanCorrect() {
List<MonetaryAmount> monetaryAmounts = Arrays.asList(Money.of(100, "CHF"), Money.of(10.20, "CHF"), Money.of(1.15, "CHF")); List<MonetaryAmount> monetaryAmounts = Arrays.asList(Money.of(100, "CHF"), Money.of(10.20, "CHF"), Money.of(1.15, "CHF"));
Money sumAmtCHF = (Money) monetaryAmounts Money sumAmtCHF = (Money) monetaryAmounts
.stream() .stream()
.reduce(Money.of(0, "CHF"), MonetaryAmount::add); .reduce(Money.of(0, "CHF"), MonetaryAmount::add);
assertEquals("CHF 111.35", sumAmtCHF.toString()); assertEquals("CHF 111.35", sumAmtCHF.toString());
} }
@Test @Test
public void givenArithmetic_whenStringified_thanEqualsAmount() { public void givenArithmetic_whenStringified_thanEqualsAmount() {
CurrencyUnit usd = Monetary.getCurrency("USD"); CurrencyUnit usd = Monetary.getCurrency("USD");
Money moneyof = Money.of(12, usd); Money moneyof = Money.of(12, usd);
MonetaryAmount fstAmtUSD = Monetary MonetaryAmount fstAmtUSD = Monetary
.getDefaultAmountFactory() .getDefaultAmountFactory()
.setCurrency(usd) .setCurrency(usd)
.setNumber(200.50) .setNumber(200.50)
.create(); .create();
MonetaryAmount oneDolar = Monetary MonetaryAmount oneDolar = Monetary
.getDefaultAmountFactory() .getDefaultAmountFactory()
.setCurrency("USD") .setCurrency("USD")
.setNumber(1) .setNumber(1)
.create(); .create();
Money subtractedAmount = Money Money subtractedAmount = Money
.of(1, "USD") .of(1, "USD")
.subtract(fstAmtUSD); .subtract(fstAmtUSD);
MonetaryAmount multiplyAmount = oneDolar.multiply(0.25); MonetaryAmount multiplyAmount = oneDolar.multiply(0.25);
MonetaryAmount divideAmount = oneDolar.divide(0.25); MonetaryAmount divideAmount = oneDolar.divide(0.25);
assertEquals("USD", usd.toString()); assertEquals("USD", usd.toString());
assertEquals("USD 1", oneDolar.toString()); assertEquals("USD 1", oneDolar.toString());
assertEquals("USD 200.5", fstAmtUSD.toString()); assertEquals("USD 200.5", fstAmtUSD.toString());
assertEquals("USD 12", moneyof.toString()); assertEquals("USD 12", moneyof.toString());
assertEquals("USD -199.5", subtractedAmount.toString()); assertEquals("USD -199.5", subtractedAmount.toString());
assertEquals("USD 0.25", multiplyAmount.toString()); assertEquals("USD 0.25", multiplyAmount.toString());
assertEquals("USD 4", divideAmount.toString()); assertEquals("USD 4", divideAmount.toString());
} }
@Test @Test
public void givenAmount_whenRounded_thanEquals() { public void givenAmount_whenRounded_thanEquals() {
MonetaryAmount fstAmtEUR = Monetary MonetaryAmount fstAmtEUR = Monetary
.getDefaultAmountFactory() .getDefaultAmountFactory()
.setCurrency("EUR") .setCurrency("EUR")
.setNumber(1.30473908) .setNumber(1.30473908)
.create(); .create();
MonetaryAmount roundEUR = fstAmtEUR.with(Monetary.getDefaultRounding()); MonetaryAmount roundEUR = fstAmtEUR.with(Monetary.getDefaultRounding());
assertEquals("EUR 1.30473908", fstAmtEUR.toString()); assertEquals("EUR 1.30473908", fstAmtEUR.toString());
assertEquals("EUR 1.3", roundEUR.toString()); assertEquals("EUR 1.3", roundEUR.toString());
} }
@Test @Test
@Ignore("Currency providers are not always available") @Ignore("Currency providers are not always available")
public void givenAmount_whenConversion_thenNotNull() { public void givenAmount_whenConversion_thenNotNull() {
MonetaryAmount oneDollar = Monetary MonetaryAmount oneDollar = Monetary
.getDefaultAmountFactory() .getDefaultAmountFactory()
.setCurrency("USD") .setCurrency("USD")
.setNumber(1) .setNumber(1)
.create(); .create();
CurrencyConversion conversionEUR = MonetaryConversions.getConversion("EUR"); CurrencyConversion conversionEUR = MonetaryConversions.getConversion("EUR");
MonetaryAmount convertedAmountUSDtoEUR = oneDollar.with(conversionEUR); MonetaryAmount convertedAmountUSDtoEUR = oneDollar.with(conversionEUR);
assertEquals("USD 1", oneDollar.toString()); assertEquals("USD 1", oneDollar.toString());
assertNotNull(convertedAmountUSDtoEUR); assertNotNull(convertedAmountUSDtoEUR);
} }
@Test @Test
public void givenLocale_whenFormatted_thanEquals() { public void givenLocale_whenFormatted_thanEquals() {
MonetaryAmount oneDollar = Monetary MonetaryAmount oneDollar = Monetary
.getDefaultAmountFactory() .getDefaultAmountFactory()
.setCurrency("USD") .setCurrency("USD")
.setNumber(1) .setNumber(1)
.create(); .create();
MonetaryAmountFormat formatUSD = MonetaryFormats.getAmountFormat(Locale.US); MonetaryAmountFormat formatUSD = MonetaryFormats.getAmountFormat(Locale.US);
String usFormatted = formatUSD.format(oneDollar); String usFormatted = formatUSD.format(oneDollar);
assertEquals("USD 1", oneDollar.toString()); assertEquals("USD 1", oneDollar.toString());
assertNotNull(formatUSD); assertNotNull(formatUSD);
assertEquals("USD1.00", usFormatted); assertEquals("USD1.00", usFormatted);
} }
@Test @Test
public void givenAmount_whenCustomFormat_thanEquals() { public void givenAmount_whenCustomFormat_thanEquals() {
MonetaryAmount oneDollar = Monetary MonetaryAmount oneDollar = Monetary
.getDefaultAmountFactory() .getDefaultAmountFactory()
.setCurrency("USD") .setCurrency("USD")
.setNumber(1) .setNumber(1)
.create(); .create();
MonetaryAmountFormat customFormat = MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder MonetaryAmountFormat customFormat = MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder
.of(Locale.US) .of(Locale.US)
.set(CurrencyStyle.NAME) .set(CurrencyStyle.NAME)
.set("pattern", "00000.00 ¤") .set("pattern", "00000.00 <EFBFBD>")
.build()); .build());
String customFormatted = customFormat.format(oneDollar); String customFormatted = customFormat.format(oneDollar);
assertNotNull(customFormat); assertNotNull(customFormat);
assertEquals("USD 1", oneDollar.toString()); assertEquals("USD 1", oneDollar.toString());
assertEquals("00001.00 US Dollar", customFormatted); assertEquals("00001.00 US Dollar", customFormatted);
} }
} }