Search in sources :

Example 41 with CurrencyUnit

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

the class ExtractorMajorPartQueryTest method shouldReturnWhenTheValueIsBiggerThanLong.

@Test(expectedExceptions = ArithmeticException.class)
public void shouldReturnWhenTheValueIsBiggerThanLong() {
    CurrencyUnit real = Monetary.getCurrency("BRL");
    MonetaryAmount monetaryAmount = Money.of(Long.MAX_VALUE, real);
    query.queryFrom(monetaryAmount.add(Money.of(10, real)));
    fail();
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) MonetaryAmount(javax.money.MonetaryAmount) Test(org.testng.annotations.Test)

Example 42 with CurrencyUnit

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

the class ExtractorMinorPartOperatorTest method shouldReturnPositiveValue.

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

Example 43 with CurrencyUnit

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

the class CurrencyToken method parse.

/**
 * Parses a currency from the given {@link ParseContext}. Depending on the
 * current {@link CurrencyStyle} it interprets the next non empty token,
 * either as
 * <ul>
 * <li>currency code
 * <li>currency symbol
 * </ul>
 * Parsing of localized currency names or numeric code is not supported.
 *
 * @throws UnsupportedOperationException if the {@link CurrencyStyle} is configured to us currency
 *                                       names, or numeric codes for formatting.
 */
@Override
public void parse(ParseContext context) throws MonetaryParseException {
    String token = context.lookupNextToken();
    while (token != null) {
        if (!token.trim().isEmpty()) {
            break;
        }
        context.consume(token);
        token = context.lookupNextToken();
    }
    if (token == null) {
        throw new MonetaryException("Error parsing CurrencyUnit: token expected");
    }
    try {
        CurrencyUnit cur;
        switch(style) {
            case CODE:
                if (!Monetary.isCurrencyAvailable(token)) {
                    // Perhaps blank is missing between currency code and number...
                    String subCurrency = parseCurrencyCode(token);
                    cur = Monetary.getCurrency(subCurrency);
                    context.consume(subCurrency);
                } else {
                    cur = Monetary.getCurrency(token);
                    context.consume(token);
                }
                break;
            case SYMBOL:
                if (token.startsWith("$")) {
                    throw new MonetaryParseException("$ is not a unique currency symbol.", token, context.getErrorIndex());
                } else if (token.startsWith("€")) {
                    cur = Monetary.getCurrency("EUR");
                    context.consume("€");
                } else if (token.startsWith("£")) {
                    cur = Monetary.getCurrency("GBP");
                    context.consume("£");
                } else {
                    cur = Monetary.getCurrency(token);
                    context.consume(token);
                }
                context.setParsedCurrency(cur);
                break;
            case NAME:
            case NUMERIC_CODE:
            default:
                throw new UnsupportedOperationException("Not yet implemented");
        }
        if (cur != null) {
            context.setParsedCurrency(cur);
        }
    } catch (Exception e) {
        throw new MonetaryException("Error parsing CurrencyUnit.", e);
    }
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) MonetaryParseException(javax.money.format.MonetaryParseException) MonetaryException(javax.money.MonetaryException) IOException(java.io.IOException) MonetaryParseException(javax.money.format.MonetaryParseException) MonetaryException(javax.money.MonetaryException)

Example 44 with CurrencyUnit

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

the class CurrenciesTest method testGetMultipleInstancesString.

/**
 * Test method for
 * {@link javax.money.Monetary#getCurrency(java.lang.String, String...)}.
 * .
 */
@Test
public void testGetMultipleInstancesString() {
    CurrencyUnit cur = Monetary.getCurrency("USD");
    CurrencyUnit cur2 = Monetary.getCurrency("USD");
    assertNotNull(cur2);
    assertTrue(cur == cur2);
    Currency jdkCurrency = Currency.getInstance("USD");
    assertEquals(jdkCurrency.getCurrencyCode(), cur.getCurrencyCode());
    assertEquals(jdkCurrency.getNumericCode(), cur.getNumericCode());
    assertEquals(jdkCurrency.getDefaultFractionDigits(), cur.getDefaultFractionDigits());
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) Currency(java.util.Currency) Test(org.testng.annotations.Test)

Example 45 with CurrencyUnit

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

the class CurrenciesTest method testGetDifferentCurrencyCodes.

/**
 * Test method for
 * {@link javax.money.Monetary#getCurrency(java.lang.String, String...)}.
 */
@Test
public void testGetDifferentCurrencyCodes() {
    CurrencyUnit cur = Monetary.getCurrency("USD");
    assertEquals("USD", cur.getCurrencyCode());
    cur = Monetary.getCurrency("EUR");
    assertEquals("EUR", cur.getCurrencyCode());
}
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