Search in sources :

Example 16 with CurrencyUnit

use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.

the class RoudingMonetaryAmountOperatorTest method shouldReturnPositiveValueUsingRoudingType.

@Test
public void shouldReturnPositiveValueUsingRoudingType() {
    operator = new RoudingMonetaryAmountOperator(RoundingMode.HALF_EVEN);
    CurrencyUnit currency = Monetary.getCurrency("EUR");
    MonetaryAmount money = Money.parse("EUR 2.3523");
    MonetaryAmount result = operator.apply(money);
    assertEquals(result.getCurrency(), currency);
    assertEquals(result.getNumber().doubleValue(), 2.35);
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) MonetaryAmount(javax.money.MonetaryAmount) Test(org.testng.annotations.Test)

Example 17 with CurrencyUnit

use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.

the class ScaleRoundedOperatorTest method shouldRoundedMonetaryOperatorWhenTheImplementationIsMoney.

@Test
public void shouldRoundedMonetaryOperatorWhenTheImplementationIsMoney() {
    int scale = 4;
    CurrencyUnit real = Monetary.getCurrency("BRL");
    MonetaryAmount money = Money.of(BigDecimal.valueOf(35.34567), real);
    ScaleRoundedOperator monetaryOperator = ScaleRoundedOperator.of(scale, RoundingMode.HALF_EVEN);
    MonetaryAmount result = monetaryOperator.apply(money);
    assertTrue(RoundedMoney.class.isInstance(result));
    assertEquals(result.getCurrency(), real);
    assertEquals(result.getNumber().getScale(), scale);
    assertEquals(BigDecimal.valueOf(35.3457), result.getNumber().numberValue(BigDecimal.class));
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) MonetaryAmount(javax.money.MonetaryAmount) RoundedMoney(org.javamoney.moneta.RoundedMoney) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 18 with CurrencyUnit

use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.

the class MonetaryOperatorsTest method shouldRoudingUsingRoundingModeAndScale.

@Test
public void shouldRoudingUsingRoundingModeAndScale() {
    CurrencyUnit euro = Monetary.getCurrency("EUR");
    MonetaryAmount money = Money.parse("EUR 2.355432");
    MonetaryAmount result = MonetaryOperators.rounding(RoundingMode.HALF_EVEN, 4).apply(money);
    assertNotNull(result);
    assertEquals(result.getCurrency(), euro);
    assertEquals(Double.valueOf(2.3554), result.getNumber().doubleValue());
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) MonetaryAmount(javax.money.MonetaryAmount) Test(org.testng.annotations.Test)

Example 19 with CurrencyUnit

use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.

the class MoneyUtils method checkAmountParameter.

/**
 * Method to check if a currency is compatible with this amount instance.
 *
 * @param amount       The monetary amount to be compared to, never null.
 * @param currencyUnit the currency unit to compare, never null.
 * @throws MonetaryException If the amount is null, or the amount's {@link CurrencyUnit} is not
 *                           compatible, meaning has a different value of
 *                           {@link CurrencyUnit#getCurrencyCode()}).
 */
public static void checkAmountParameter(MonetaryAmount amount, CurrencyUnit currencyUnit) {
    Objects.requireNonNull(amount, "Amount must not be null.");
    final CurrencyUnit amountCurrency = amount.getCurrency();
    if (!(currencyUnit.getCurrencyCode().equals(amountCurrency.getCurrencyCode()))) {
        throw new MonetaryException("Currency mismatch: " + currencyUnit + '/' + amountCurrency);
    }
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) MonetaryException(javax.money.MonetaryException)

Example 20 with CurrencyUnit

use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.

the class BuildableCurrencyUnitTest method testSerialization.

/**
 * Tests that currencies built by {@link CurrencyUnitBuilder} are serializable.
 */
@Test
public void testSerialization() throws ClassNotFoundException, IOException {
    CurrencyUnit currencyUnit = CurrencyUnitBuilder.of("SDR", "serialization-test").setDefaultFractionDigits(3).build(false);
    CurrencyUnit copy = (CurrencyUnit) deserailize(serailize(currencyUnit));
    assertEquals(currencyUnit, copy);
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) Test(org.testng.annotations.Test)

Aggregations

CurrencyUnit (javax.money.CurrencyUnit)116 Test (org.testng.annotations.Test)78 MonetaryAmount (javax.money.MonetaryAmount)57 BigDecimal (java.math.BigDecimal)14 Currency (java.util.Currency)8 MonetaryException (javax.money.MonetaryException)6 ExchangeRate (javax.money.convert.ExchangeRate)6 RoundedMoney (org.javamoney.moneta.RoundedMoney)6 IOException (java.io.IOException)5 MathContext (java.math.MathContext)5 MonetaryParseException (javax.money.format.MonetaryParseException)5 Money (org.javamoney.moneta.Money)4 ExchangeRateBuilder (org.javamoney.moneta.convert.ExchangeRateBuilder)3 DefaultNumberValue (org.javamoney.moneta.spi.DefaultNumberValue)3 Test (org.junit.Test)3 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 LocalDate (java.time.LocalDate)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2