use of javax.money.CurrencyUnit in project jsr354-ri 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);
}
use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.
the class ExtractorMinorPartOperatorTest method shouldReturnNegativeValue.
@Test
public void shouldReturnNegativeValue() {
CurrencyUnit currency = Monetary.getCurrency("BHD");
MonetaryAmount money = Money.parse("BHD -1.345");
MonetaryAmount result = operator.apply(money);
assertEquals(result.getCurrency(), currency);
assertEquals(result.getNumber().doubleValue(), -0.345);
}
use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.
the class MonetaryOperatorsTest method shouldRoudingUsingScale.
@Test
public void shouldRoudingUsingScale() {
CurrencyUnit euro = Monetary.getCurrency("EUR");
MonetaryAmount money = Money.parse("EUR 2.355432");
MonetaryAmount result = MonetaryOperators.rounding(4).apply(money);
assertNotNull(result);
assertEquals(result.getCurrency(), euro);
assertEquals(Double.valueOf(2.3554), result.getNumber().doubleValue());
}
use of javax.money.CurrencyUnit in project jsr354-ri 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 (Objects.nonNull(token)) {
if (token.trim().isEmpty()) {
context.consume(token);
token = context.lookupNextToken();
continue;
}
break;
}
if (token == null) {
throw new MonetaryException("Error parsing CurrencyUnit: no input.");
}
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 (Objects.nonNull(cur)) {
context.setParsedCurrency(cur);
}
} catch (Exception e) {
throw new MonetaryException("Error parsing CurrencyUnit.", e);
}
}
use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.
the class CurrenciesTest method testCurrencyValues.
/**
* Test method for
* {@link javax.money.Monetary#getCurrency(java.lang.String, String...)} .
*/
@Test
public void testCurrencyValues() {
Currency jdkCurrency = Currency.getInstance("CHF");
CurrencyUnit cur = Monetary.getCurrency("CHF");
assertNotNull(cur);
assertEquals(jdkCurrency.getCurrencyCode(), cur.getCurrencyCode());
assertEquals(jdkCurrency.getNumericCode(), cur.getNumericCode());
assertEquals(jdkCurrency.getDefaultFractionDigits(), cur.getDefaultFractionDigits());
}
Aggregations