BAEL - 606 - Tests for JSR 354 Money implementation
This commit is contained in:
parent
1df0503b20
commit
8ffc21e8cf
|
@ -1,13 +1,9 @@
|
||||||
package com.baeldung.money;
|
package com.baeldung.money;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import org.javamoney.moneta.FastMoney;
|
||||||
import static org.junit.Assert.assertFalse;
|
import org.javamoney.moneta.Money;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import org.javamoney.moneta.format.CurrencyStyle;
|
||||||
import static org.junit.Assert.assertNull;
|
import org.junit.Test;
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
import java.util.Locale;
|
|
||||||
|
|
||||||
import javax.money.CurrencyUnit;
|
import javax.money.CurrencyUnit;
|
||||||
import javax.money.Monetary;
|
import javax.money.Monetary;
|
||||||
|
@ -19,43 +15,36 @@ 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.Locale;
|
||||||
|
|
||||||
import org.javamoney.moneta.FastMoney;
|
import static org.junit.Assert.*;
|
||||||
import org.javamoney.moneta.Money;
|
|
||||||
import org.javamoney.moneta.format.CurrencyStyle;
|
|
||||||
import org.junit.Rule;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.rules.ExpectedException;
|
|
||||||
|
|
||||||
public class JavaMoneyTest {
|
public class JavaMoneyTest {
|
||||||
@Rule
|
|
||||||
public ExpectedException thrown = ExpectedException.none();
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenCurrencyCode_whenString_thanExist() {
|
public void givenCurrencyCode_whenString_thanExist() {
|
||||||
CurrencyUnit USD = Monetary.getCurrency("USD");
|
CurrencyUnit usd = Monetary.getCurrency("USD");
|
||||||
assertNotNull(USD);
|
|
||||||
assertEquals(USD.getCurrencyCode(), "USD");
|
assertNotNull(usd);
|
||||||
assertEquals(USD.getNumericCode(), 840);
|
assertEquals(usd.getCurrencyCode(), "USD");
|
||||||
assertEquals(USD.getDefaultFractionDigits(), 2);
|
assertEquals(usd.getNumericCode(), 840);
|
||||||
|
assertEquals(usd.getDefaultFractionDigits(), 2);
|
||||||
assertFalse(Monetary.isCurrencyAvailable("AAA"));
|
assertFalse(Monetary.isCurrencyAvailable("AAA"));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = UnknownCurrencyException.class)
|
||||||
public void givenCurrencyCode_whenNoExist_thanThrowsError() {
|
public void givenCurrencyCode_whenNoExist_thanThrowsError() {
|
||||||
thrown.expect(UnknownCurrencyException.class);
|
CurrencyUnit aaa = Monetary.getCurrency("AAA");
|
||||||
CurrencyUnit AAA = Monetary.getCurrency("AAA");
|
fail(); // if no exception
|
||||||
assertNull(AAA);
|
|
||||||
throw new UnknownCurrencyException("AAA");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenAmounts_whenStrinfied_thanEquals() {
|
public void givenAmounts_whenStringified_thanEquals() {
|
||||||
CurrencyUnit USD = Monetary.getCurrency("USD");
|
CurrencyUnit USD = Monetary.getCurrency("USD");
|
||||||
MonetaryAmount fstAmtUSD = Monetary.getDefaultAmountFactory().setCurrency(USD).setNumber(200.50).create();
|
MonetaryAmount fstAmtUSD = Monetary.getDefaultAmountFactory().setCurrency(USD).setNumber(200.50).create();
|
||||||
MonetaryAmount fstAmtEUR = Monetary.getDefaultAmountFactory().setCurrency("EUR").setNumber(1.30473908).create();
|
MonetaryAmount fstAmtEUR = Monetary.getDefaultAmountFactory().setCurrency("EUR").setNumber(1.30473908).create();
|
||||||
MonetaryAmount oneDolar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create();
|
MonetaryAmount oneDolar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).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);
|
||||||
Money oneEuro = Money.of(1, "EUR");
|
Money oneEuro = Money.of(1, "EUR");
|
||||||
|
@ -67,30 +56,26 @@ public class JavaMoneyTest {
|
||||||
assertEquals("EUR 1.30473908", fstAmtEUR.toString());
|
assertEquals("EUR 1.30473908", fstAmtEUR.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.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create();
|
MonetaryAmount oneDolar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).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
|
@Test(expected = ArithmeticException.class)
|
||||||
public void givenAmount_whenDivided_thanThrowsException() {
|
public void givenAmount_whenDivided_thanThrowsException() {
|
||||||
MonetaryAmount oneDolar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create();
|
MonetaryAmount oneDolar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create();
|
||||||
thrown.expect(ArithmeticException.class);
|
oneDolar.divide(3);
|
||||||
MonetaryAmount oneDivThree = oneDolar.divide(3);
|
fail(); // if no exception
|
||||||
assertNull(oneDivThree);
|
|
||||||
throw new ArithmeticException();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenArithmetic_whenStrinfied_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.getDefaultAmountFactory().setCurrency(USD).setNumber(200.50).create();
|
MonetaryAmount fstAmtUSD = Monetary.getDefaultAmountFactory().setCurrency(USD).setNumber(200.50).create();
|
||||||
|
@ -101,10 +86,10 @@ public class JavaMoneyTest {
|
||||||
MonetaryAmount multiplyAmount = oneDolar.multiply(0.25);
|
MonetaryAmount multiplyAmount = oneDolar.multiply(0.25);
|
||||||
MonetaryAmount divideAmount = oneDolar.divide(0.25);
|
MonetaryAmount divideAmount = oneDolar.divide(0.25);
|
||||||
|
|
||||||
MonetaryAmount[] monetaryAmounts = new MonetaryAmount[] {
|
MonetaryAmount[] monetaryAmounts = new MonetaryAmount[]{
|
||||||
Money.of(100, "CHF"),
|
Money.of(100, "CHF"),
|
||||||
Money.of(10.20, "CHF"),
|
Money.of(10.20, "CHF"),
|
||||||
Money.of(1.15, "CHF"), };
|
Money.of(1.15, "CHF"),};
|
||||||
|
|
||||||
Money sumAmtCHF = Money.of(0, "CHF");
|
Money sumAmtCHF = Money.of(0, "CHF");
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue