Merge pull request #1344 from eugenp/money

ESG-606 - refining tests
This commit is contained in:
slavisa-baeldung 2017-03-09 14:17:00 +01:00 committed by GitHub
commit 6e00fd16d5
1 changed files with 67 additions and 82 deletions

View File

@ -43,10 +43,10 @@ public class JavaMoneyTest {
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);
@ -59,10 +59,10 @@ public class JavaMoneyTest {
@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")));
@ -72,10 +72,10 @@ public class JavaMoneyTest {
@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
} }
@ -85,8 +85,8 @@ public class JavaMoneyTest {
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());
} }
@ -97,18 +97,18 @@ public class JavaMoneyTest {
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);
@ -124,78 +124,63 @@ public class JavaMoneyTest {
@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
public void givenAmount_whenCustomFormat_thanEquals() { public void givenAmount_whenConversion_thenNotNull() {
MonetaryAmount oneDolar = Monetary MonetaryAmount oneDollar = Monetary
.getDefaultAmountFactory() .getDefaultAmountFactory()
.setCurrency("USD") .setCurrency("USD")
.setNumber(1) .setNumber(1)
.create(); .create();
MonetaryAmountFormat formatUSD = MonetaryFormats.getAmountFormat(Locale.US);
String usFormatted = formatUSD.format(oneDolar);
MonetaryAmountFormat customFormat = MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder CurrencyConversion conversionEUR = MonetaryConversions.getConversion("EUR");
.of(Locale.US)
.set(CurrencyStyle.NAME)
.set("pattern", "00000.00 ¤")
.build());
String customFormatted = customFormat.format(oneDolar);
assertEquals("USD 1", oneDolar.toString()); MonetaryAmount convertedAmountUSDtoEUR = oneDollar.with(conversionEUR);
assertNotNull(formatUSD);
assertNotNull(customFormat); assertEquals("USD 1", oneDollar.toString());
assertEquals("USD1.00", usFormatted); assertNotNull(convertedAmountUSDtoEUR);
assertEquals("00001.00 US Dollar", customFormatted);
} }
@Test @Test
public void givenAmount_whenConversion_thenNotNull() { public void givenLocale_whenFormatted_thanEquals() {
CurrencyUnit USD = Monetary.getCurrency("USD"); MonetaryAmount oneDollar = Monetary
MonetaryAmount oneDolar = Monetary .getDefaultAmountFactory()
.getDefaultAmountFactory() .setCurrency("USD")
.setCurrency("USD") .setNumber(1)
.setNumber(1) .create();
.create(); MonetaryAmountFormat formatUSD = MonetaryFormats.getAmountFormat(Locale.US);
MonetaryAmount fstAmtEUR = Monetary String usFormatted = formatUSD.format(oneDollar);
.getDefaultAmountFactory()
.setCurrency("EUR")
.setNumber(1.30473908)
.create();
CurrencyConversion convEUR = MonetaryConversions.getConversion(ConversionQueryBuilder assertEquals("USD 1", oneDollar.toString());
.of() assertNotNull(formatUSD);
.setTermCurrency("EUR") assertEquals("USD1.00", usFormatted);
.build()); }
CurrencyConversion convUSD = MonetaryConversions.getConversion(ConversionQueryBuilder
.of()
.setTermCurrency(USD)
.build());
CurrencyConversion conversionUSD = MonetaryConversions.getConversion("USD"); @Test
CurrencyConversion conversionEUR = MonetaryConversions.getConversion("EUR"); public void givenAmount_whenCustomFormat_thanEquals() {
MonetaryAmount oneDollar = Monetary
.getDefaultAmountFactory()
.setCurrency("USD")
.setNumber(1)
.create();
MonetaryAmount convertedAmountEURtoUSD = fstAmtEUR.with(conversionUSD); MonetaryAmountFormat customFormat = MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder
MonetaryAmount convertedAmountEURtoUSD2 = fstAmtEUR.with(convUSD); .of(Locale.US)
MonetaryAmount convertedAmountUSDtoEUR = oneDolar.with(conversionEUR); .set(CurrencyStyle.NAME)
MonetaryAmount convertedAmountUSDtoEUR2 = oneDolar.with(convEUR); .set("pattern", "00000.00 ¤")
.build());
String customFormatted = customFormat.format(oneDollar);
assertEquals("USD", USD.toString()); assertNotNull(customFormat);
assertEquals("USD 1", oneDolar.toString()); assertEquals("USD 1", oneDollar.toString());
assertEquals("EUR 1.30473908", fstAmtEUR.toString()); assertEquals("00001.00 US Dollar", customFormatted);
assertNotNull(convEUR);
assertNotNull(convUSD);
assertNotNull(convertedAmountEURtoUSD);
assertNotNull(convertedAmountEURtoUSD2);
assertNotNull(convertedAmountUSDtoEUR);
assertNotNull(convertedAmountUSDtoEUR2);
} }
} }