javax.money.convert.CurrencyConversion Java Examples

The following examples show how to use javax.money.convert.CurrencyConversion. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JavaMoneyUnitManualTest.java    From tutorials with MIT License 7 votes vote down vote up
@Test
@Ignore("Currency providers are not always available")
public void givenAmount_whenConversion_thenNotNull() {
    MonetaryAmount oneDollar = Monetary
      .getDefaultAmountFactory()
      .setCurrency("USD")
      .setNumber(1)
      .create();

    CurrencyConversion conversionEUR = MonetaryConversions.getConversion("EUR");

    MonetaryAmount convertedAmountUSDtoEUR = oneDollar.with(conversionEUR);

    assertEquals("USD 1", oneDollar.toString());
    assertNotNull(convertedAmountUSDtoEUR);
}
 
Example #2
Source File: ConversionExample.java    From javamoney-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String... args) {
	String termCurrencyCode = DEFAULT_TERM_CURRENCY_CODE;
	if (args.length > 0) {
		termCurrencyCode = args[0];
	}
	final MonetaryAmount amt = Money.of(2000, "EUR");
	CurrencyConversion conv= MonetaryConversions.getConversion(termCurrencyCode, "ECB");
	System.out.println(MessageFormat.format("2000 EUR (ECB)-> {0} = {1}",
			termCurrencyCode, amt.with(conv)));
	conv= MonetaryConversions.getConversion(termCurrencyCode, "IMF");
	System.out.println(MessageFormat.format("2000 EUR (IMF)-> {0} = {1}",
			termCurrencyCode, amt.with(conv)));

	System.out.println(MessageFormat.format(
			"2000 EUR (ECB, at 5th Jan 2015)-> {0} = {1}",
			termCurrencyCode, amt.with(MonetaryConversions
					.getConversion(ConversionQueryBuilder.of()
							.setTermCurrency(termCurrencyCode)
							.set(LocalDate.of(2015, 01, 05)).build()))));
}
 
Example #3
Source File: IMFHistoricRateProviderTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSetTimeInLocalDateTime2() {

	LocalDate localDate = LocalDate.now().minusDays(90)
			.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));

	ConversionQuery conversionQuery = ConversionQueryBuilder.of()
			.setTermCurrency(EURO).set(localDate).build();
	CurrencyConversion currencyConversion = provider
			.getCurrencyConversion(conversionQuery);
	assertNotNull(currencyConversion);
	MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR);
	MonetaryAmount result = currencyConversion.apply(money);

	assertEquals(result.getCurrency(), EURO);
	assertTrue(result.getNumber().doubleValue() > 0);

}
 
Example #4
Source File: ConversionExample.java    From javamoney-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String... args) {
	String termCurrencyCode = DEFAULT_TERM_CURRENCY_CODE;
	if (args.length > 0) {
		termCurrencyCode = args[0];
	}
	final MonetaryAmount amt = Money.of(2000, "EUR");
	CurrencyConversion conv= MonetaryConversions.getConversion(termCurrencyCode, "ECB");
	System.out.println(MessageFormat.format("2000 EUR (ECB)-> {0} = {1}",
			termCurrencyCode, amt.with(conv)));
	conv= MonetaryConversions.getConversion(termCurrencyCode, "IMF");
	System.out.println(MessageFormat.format("2000 EUR (IMF)-> {0} = {1}",
			termCurrencyCode, amt.with(conv)));

	System.out.println(MessageFormat.format(
			"2000 EUR (ECB, at 5th Jan 2015)-> {0} = {1}",
			termCurrencyCode, amt.with(MonetaryConversions
					.getConversion(ConversionQueryBuilder.of()
							.setTermCurrency(termCurrencyCode)
							//.set(LocalDate.of(2015, 01, 05))
							.build()))));
}
 
Example #5
Source File: ExchangeExample.java    From javamoney-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
	ExchangeRateProvider imfRateProvider = MonetaryConversions
			.getExchangeRateProvider("IMF");
	ExchangeRateProvider ecbRateProvider = MonetaryConversions
			.getExchangeRateProvider("ECB");

	CurrencyUnit euro = Monetary.getCurrency("EUR");
	CurrencyUnit dollar = Monetary.getCurrency(Locale.US);

	CurrencyConversion ecbDollarConvertion = ecbRateProvider
			.getCurrencyConversion(dollar);

	CurrencyConversion imfDollarConvertion = imfRateProvider
			.getCurrencyConversion(dollar);

	MonetaryAmount money = Money.of(10, euro);
	System.out.println(money.with(ecbDollarConvertion));
	System.out.println(money.with(imfDollarConvertion));
}
 
Example #6
Source File: ECBHistoric90RateProviderTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Test(expectedExceptions = MonetaryException.class)
public void shouldSetTimeInLocalDateTime() {

	LocalDate localDate = YearMonth.of(2014, Month.JANUARY).atDay(9);

	ConversionQuery conversionQuery = ConversionQueryBuilder.of()
                       .setTermCurrency(EURO).set(localDate).build();
	CurrencyConversion currencyConversion = provider
			.getCurrencyConversion(conversionQuery);
	assertNotNull(currencyConversion);
	MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR);
	MonetaryAmount result = currencyConversion.apply(money);

	assertEquals(result.getCurrency(), EURO);
	assertTrue(result.getNumber().doubleValue() > 0);
}
 
Example #7
Source File: ECBHistoric90RateProviderTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Test(expectedExceptions = MonetaryException.class)
public void shouldReturnErrorWhenFindFromHistoricalUsingPeriod() {

	LocalDate localDate = YearMonth.of(2011, Month.JANUARY).atDay(9);

	ConversionQuery conversionQuery = ConversionQueryBuilder.of()
			.setTermCurrency(EURO)
			.set(withDaysBetween(localDate.minusDays(1), localDate))
			.build();
	CurrencyConversion currencyConversion = provider
			.getCurrencyConversion(conversionQuery);
	assertNotNull(currencyConversion);
	MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR);
	currencyConversion.apply(money);
	fail();

}
 
Example #8
Source File: IMFHistoricRateProviderTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Test(expectedExceptions = MonetaryException.class)
public void shouldReturnErrorWhenFindFromHistoricalUsingPeriod() {

	LocalDate localDate = YearMonth.of(2011, Month.JANUARY).atDay(9);

	ConversionQuery conversionQuery = ConversionQueryBuilder.of()
			.setTermCurrency(EURO)
			.set(withDaysBetween(localDate.minusDays(1), localDate))
			.build();
	CurrencyConversion currencyConversion = provider
			.getCurrencyConversion(conversionQuery);
	assertNotNull(currencyConversion);
	MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR);
	currencyConversion.apply(money);
	fail();

}
 
Example #9
Source File: USFederalReserveRateProviderTest.java    From javamoney-lib with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldHaveExchangeRates() {
    CurrencyConversion currencyConversion = provider.getCurrencyConversion(DOLLAR);
    assertNotNull(currencyConversion);
    int count = 0;
    for (Currency currency : Currency.getAvailableCurrencies()) {
        MonetaryAmount money = Money.of(BigDecimal.ONE, currency.getCurrencyCode());
        try {
            MonetaryAmount result = currencyConversion.apply(money);
            assertTrue(result.getNumber().doubleValue() > 0);
            count++;
        } catch(Exception e) {
            //not a supported currency
        }
    }
    assertTrue(count >=24);
}
 
Example #10
Source File: ECBHistoric90RateProviderTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSetTimeInLocalDateTime2() {

	LocalDate localDate = LocalDate.now().minusDays(90)
			.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));

	ConversionQuery conversionQuery = ConversionQueryBuilder.of()
			.setTermCurrency(EURO).set(localDate).build();
	CurrencyConversion currencyConversion = provider
			.getCurrencyConversion(conversionQuery);
	assertNotNull(currencyConversion);
	MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR);
	MonetaryAmount result = currencyConversion.apply(money);

	assertEquals(result.getCurrency(), EURO);
	assertTrue(result.getNumber().doubleValue() > 0);

}
 
Example #11
Source File: IMFHistoricRateProviderTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSetTimeInLocalDateTime() {

	LocalDate localDate = YearMonth.of(2014, Month.JANUARY).atDay(9);

	ConversionQuery conversionQuery = ConversionQueryBuilder.of()
			.setTermCurrency(EURO).set(localDate).build();
	CurrencyConversion currencyConversion = provider
			.getCurrencyConversion(conversionQuery);
	assertNotNull(currencyConversion);
	MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR);
	MonetaryAmount result = currencyConversion.apply(money);

	assertEquals(result.getCurrency(), EURO);
	assertTrue(result.getNumber().doubleValue() > 0);
}
 
Example #12
Source File: ECBHistoricRateProviderTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldConvertsEuroToDollar() {
    CurrencyConversion currencyConversion = provider
            .getCurrencyConversion(DOLLAR);
    assertNotNull(currencyConversion);
    MonetaryAmount money = Money.of(BigDecimal.TEN, EURO);
    MonetaryAmount result = currencyConversion.apply(money);

    assertEquals(result.getCurrency(), DOLLAR);
    assertTrue(result.getNumber().doubleValue() > 0);

}
 
Example #13
Source File: IMFRateProviderTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnsSameBrazilianValue() {
    CurrencyConversion currencyConversion = provider
            .getCurrencyConversion(BRAZILIAN_REAL);
    assertNotNull(currencyConversion);
    MonetaryAmount money = Money.of(BigDecimal.TEN, BRAZILIAN_REAL);
    MonetaryAmount result = currencyConversion.apply(money);

    assertEquals(result.getCurrency(), BRAZILIAN_REAL);
    assertEquals(result.getNumber().numberValue(BigDecimal.class),
            BigDecimal.TEN);

}
 
Example #14
Source File: ECBHistoricRateProviderTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldConvertsBrazilianToDollar() {
    CurrencyConversion currencyConversion = provider
            .getCurrencyConversion(DOLLAR);
    assertNotNull(currencyConversion);
    MonetaryAmount money = Money.of(BigDecimal.TEN, BRAZILIAN_REAL);
    MonetaryAmount result = currencyConversion.apply(money);

    assertEquals(result.getCurrency(), DOLLAR);
    assertTrue(result.getNumber().doubleValue() > 0);

}
 
Example #15
Source File: ECBHistoricRateProviderTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldConvertsDollarToBrazilian() {
    CurrencyConversion currencyConversion = provider
            .getCurrencyConversion(BRAZILIAN_REAL);
    assertNotNull(currencyConversion);
    MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR);
    MonetaryAmount result = currencyConversion.apply(money);

    assertEquals(result.getCurrency(), BRAZILIAN_REAL);
    assertTrue(result.getNumber().doubleValue() > 0);

}
 
Example #16
Source File: IMFHistoricRateProviderTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnsSameDollarValue() {
    CurrencyConversion currencyConversion = provider.getCurrencyConversion(DOLLAR);
    assertNotNull(currencyConversion);
    MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR);
    MonetaryAmount result = currencyConversion.apply(money);

    assertEquals(result.getCurrency(), DOLLAR);
    assertEquals(result.getNumber().numberValue(BigDecimal.class),
            BigDecimal.TEN);

}
 
Example #17
Source File: IMFRateProviderTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnsSameEuroValue() {
    CurrencyConversion currencyConversion = provider
            .getCurrencyConversion(EURO);
    assertNotNull(currencyConversion);
    MonetaryAmount money = Money.of(BigDecimal.TEN, EURO);
    MonetaryAmount result = currencyConversion.apply(money);

    assertEquals(result.getCurrency(), EURO);
    assertEquals(result.getNumber().numberValue(BigDecimal.class),
            BigDecimal.TEN);

}
 
Example #18
Source File: IMFRateProviderTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldConvertsEuroToDollar() {
    CurrencyConversion currencyConversion = provider
            .getCurrencyConversion(DOLLAR);
    assertNotNull(currencyConversion);
    MonetaryAmount money = Money.of(BigDecimal.TEN, EURO);
    MonetaryAmount result = currencyConversion.apply(money);

    assertEquals(result.getCurrency(), DOLLAR);
    assertTrue(result.getNumber().doubleValue() > 0);

}
 
Example #19
Source File: MonetaryConversionTest.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testMonetaryConversionsIMF() {
	final MonetaryAmount amt = Money.of(2000, "EUR");
	CurrencyConversion conv= MonetaryConversions.getConversion("CHF", "IMF");
	MonetaryAmount converted = amt.with(conv);
	assertNotNull(converted);
}
 
Example #20
Source File: IMFRateProviderTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldConvertsBrazilianToDollar() {
    CurrencyConversion currencyConversion = provider
            .getCurrencyConversion(DOLLAR);
    assertNotNull(currencyConversion);
    MonetaryAmount money = Money.of(BigDecimal.TEN, BRAZILIAN_REAL);
    MonetaryAmount result = currencyConversion.apply(money);

    assertEquals(result.getCurrency(), DOLLAR);
    assertTrue(result.getNumber().doubleValue() > 0);

}
 
Example #21
Source File: ECBCurrentRateProviderTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnsSameEuroValue() {
    CurrencyConversion currencyConversion = provider
            .getCurrencyConversion(EURO);
    assertNotNull(currencyConversion);
    MonetaryAmount money = Money.of(BigDecimal.TEN, EURO);
    MonetaryAmount result = currencyConversion.apply(money);

    assertEquals(result.getCurrency(), EURO);
    assertEquals(result.getNumber().numberValue(BigDecimal.class),
            BigDecimal.TEN);

}
 
Example #22
Source File: ECBHistoricRateProviderTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldConvertsDollarToEuro() {
    CurrencyConversion currencyConversion = provider
            .getCurrencyConversion(EURO);
    assertNotNull(currencyConversion);
    MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR);
    MonetaryAmount result = currencyConversion.apply(money);

    assertEquals(result.getCurrency(), EURO);
    assertTrue(result.getNumber().doubleValue() > 0);

}
 
Example #23
Source File: ECBHistoricRateProviderTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnsSameEuroValue() {
    CurrencyConversion currencyConversion = provider
            .getCurrencyConversion(EURO);
    assertNotNull(currencyConversion);
    MonetaryAmount money = Money.of(BigDecimal.TEN, EURO);
    MonetaryAmount result = currencyConversion.apply(money);

    assertEquals(result.getCurrency(), EURO);
    assertEquals(result.getNumber().numberValue(BigDecimal.class),
            BigDecimal.TEN);

}
 
Example #24
Source File: ECBHistoricRateProviderTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnsSameBrazilianValue() {
    CurrencyConversion currencyConversion = provider
            .getCurrencyConversion(BRAZILIAN_REAL);
    assertNotNull(currencyConversion);
    MonetaryAmount money = Money.of(BigDecimal.TEN, BRAZILIAN_REAL);
    MonetaryAmount result = currencyConversion.apply(money);

    assertEquals(result.getCurrency(), BRAZILIAN_REAL);
    assertEquals(result.getNumber().numberValue(BigDecimal.class),
            BigDecimal.TEN);

}
 
Example #25
Source File: ECBHistoricRateProviderTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnsSameDollarValue() {
    CurrencyConversion currencyConversion = provider.getCurrencyConversion(DOLLAR);
    assertNotNull(currencyConversion);
    MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR);
    MonetaryAmount result = currencyConversion.apply(money);

    assertEquals(result.getCurrency(), DOLLAR);
    assertEquals(result.getNumber().numberValue(BigDecimal.class),
            BigDecimal.TEN);

}
 
Example #26
Source File: ECBCurrentRateProviderTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldConvertsDollarToBrazilian() {
    CurrencyConversion currencyConversion = provider
            .getCurrencyConversion(BRAZILIAN_REAL);
    assertNotNull(currencyConversion);
    MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR);
    MonetaryAmount result = currencyConversion.apply(money);

    assertEquals(result.getCurrency(), BRAZILIAN_REAL);
    assertTrue(result.getNumber().doubleValue() > 0);

}
 
Example #27
Source File: ECBCurrentRateProviderTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldConvertsBrazilianToDollar() {
    CurrencyConversion currencyConversion = provider
            .getCurrencyConversion(DOLLAR);
    assertNotNull(currencyConversion);
    MonetaryAmount money = Money.of(BigDecimal.TEN, BRAZILIAN_REAL);
    MonetaryAmount result = currencyConversion.apply(money);

    assertEquals(result.getCurrency(), DOLLAR);
    assertTrue(result.getNumber().doubleValue() > 0);

}
 
Example #28
Source File: ECBCurrentRateProviderTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldConvertsEuroToDollar() {
    CurrencyConversion currencyConversion = provider
            .getCurrencyConversion(DOLLAR);
    assertNotNull(currencyConversion);
    MonetaryAmount money = Money.of(BigDecimal.TEN, EURO);
    MonetaryAmount result = currencyConversion.apply(money);

    assertEquals(result.getCurrency(), DOLLAR);
    assertTrue(result.getNumber().doubleValue() > 0);

}
 
Example #29
Source File: ECBCurrentRateProviderTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldConvertsDollarToEuro() {
    CurrencyConversion currencyConversion = provider
            .getCurrencyConversion(EURO);
    assertNotNull(currencyConversion);
    MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR);
    MonetaryAmount result = currencyConversion.apply(money);

    assertEquals(result.getCurrency(), EURO);
    assertTrue(result.getNumber().doubleValue() > 0);

}
 
Example #30
Source File: MonetaryConversionTest.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testMonetaryConversionsECB() {
	final MonetaryAmount amt = Money.of(2000, "EUR");
	CurrencyConversion conv= MonetaryConversions.getConversion("CHF", "ECB");
	MonetaryAmount converted = amt.with(conv);
	assertNotNull(converted);
}