Search in sources :

Example 6 with ExchangeRateProvider

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

the class ProviderTest method testAccess_IMF_HIST.

@Test
public void testAccess_IMF_HIST() throws InterruptedException {
    final MonetaryAmount inEuro = Money.of(10, "EUR");
    for (int i = 0; i < 100; i++) {
        try {
            final ExchangeRateProvider rateProvider = MonetaryConversions.getExchangeRateProvider("IMF-HIST");
            final CurrencyConversion dollarConversion = rateProvider.getCurrencyConversion("USD");
            final MonetaryAmount inDollar = inEuro.with(dollarConversion);
            System.out.println(String.format("RUN: %n - %s: %s ≙ %s", i, rateProvider, inEuro, inDollar));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    for (int i = 0; i < 100; i++) {
        final ExchangeRateProvider rateProvider = MonetaryConversions.getExchangeRateProvider("IMF-HIST");
        final CurrencyConversion dollarConversion = rateProvider.getCurrencyConversion("USD");
        Thread.sleep(100L);
        final MonetaryAmount inDollar = inEuro.with(dollarConversion);
        System.out.println(String.format("RUN: %n - %s: %s ≙ %s", i, rateProvider, inEuro, inDollar));
    }
}
Also used : ExchangeRateProvider(javax.money.convert.ExchangeRateProvider) MonetaryAmount(javax.money.MonetaryAmount) CurrencyConversion(javax.money.convert.CurrencyConversion) Test(org.testng.annotations.Test)

Example 7 with ExchangeRateProvider

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

the class DefaultMonetaryConversionsSingletonSpi method getProvidersToUse.

private Collection<String> getProvidersToUse(ConversionQuery query) {
    List<String> providersToUse = new ArrayList<>();
    List<String> providerNames = query.getProviderNames();
    if (providerNames.isEmpty()) {
        providerNames = getDefaultProviderChain();
        if (providerNames.isEmpty()) {
            throw new IllegalStateException("No default provider chain available.");
        }
    }
    for (String provider : providerNames) {
        ExchangeRateProvider prov = this.conversionProviders.get(provider);
        if (prov == null) {
            throw new MonetaryException("Invalid ExchangeRateProvider (not found): " + provider);
        }
        providersToUse.add(provider);
    }
    return providersToUse;
}
Also used : ExchangeRateProvider(javax.money.convert.ExchangeRateProvider) ArrayList(java.util.ArrayList) MonetaryException(javax.money.MonetaryException)

Example 8 with ExchangeRateProvider

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

the class MonetaryConversionTest method testGetExchangeRateProvider.

@Test
public void testGetExchangeRateProvider() {
    ExchangeRateProvider prov = MonetaryConversions.getExchangeRateProvider("ECB");
    assertTrue(Objects.nonNull(prov));
    assertEquals(ECBCurrentRateProvider.class, prov.getClass());
}
Also used : ExchangeRateProvider(javax.money.convert.ExchangeRateProvider) Test(org.testng.annotations.Test)

Example 9 with ExchangeRateProvider

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

the class CurrencyProviderTest method testProviderComparison.

@Test
public void testProviderComparison() {
    ExchangeRateProvider ecbRateProvider = MonetaryConversions.getExchangeRateProvider("ECB");
    ExchangeRateProvider imfRateProvider = MonetaryConversions.getExchangeRateProvider("IMF");
    CurrencyConversion ecbDollarConversion = ecbRateProvider.getCurrencyConversion("USD");
    CurrencyConversion imfDollarConversion = imfRateProvider.getCurrencyConversion("USD");
    try {
        // Wait for IMF provider to load
        Thread.sleep(10000L);
        for (String currency : new String[] { "INR", "CHF", "BRL" }) {
            Money money = Money.of(2, currency);
            System.out.println("ECB : " + money.with(ecbDollarConversion));
            System.out.println("IMF : " + money.with(imfDollarConversion));
            assertEquals("Too much difference (ECB/IMF) for " + money, money.with(ecbDollarConversion).getNumber().doubleValue(), money.with(imfDollarConversion).getNumber().doubleValue(), 0.4d);
        }
    } catch (Exception e) {
        // This test may fail, if the network is slow or not available, so only write the exception as of now...
        e.printStackTrace();
    }
}
Also used : Money(org.javamoney.moneta.Money) ExchangeRateProvider(javax.money.convert.ExchangeRateProvider) CurrencyConversion(javax.money.convert.CurrencyConversion) Test(org.testng.annotations.Test)

Example 10 with ExchangeRateProvider

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

the class ExchangeRateTypeTest method shouldReturnsECBCurrentRateProvider.

@Test
public void shouldReturnsECBCurrentRateProvider() {
    ExchangeRateProvider prov = MonetaryConversions.getExchangeRateProvider(ExchangeRateType.ECB);
    assertNotNull(prov);
    assertEquals(ECBCurrentRateProvider.class, prov.getClass());
}
Also used : ExchangeRateProvider(javax.money.convert.ExchangeRateProvider) Test(org.testng.annotations.Test)

Aggregations

ExchangeRateProvider (javax.money.convert.ExchangeRateProvider)35 Test (org.testng.annotations.Test)23 CurrencyConversion (javax.money.convert.CurrencyConversion)10 ArrayList (java.util.ArrayList)9 MonetaryAmount (javax.money.MonetaryAmount)9 MonetaryException (javax.money.MonetaryException)7 CompoundRateProvider (org.javamoney.moneta.spi.CompoundRateProvider)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ExchangeRate (javax.money.convert.ExchangeRate)2 ProviderContext (javax.money.convert.ProviderContext)2 ProviderContextBuilder (javax.money.convert.ProviderContextBuilder)2 RateType (javax.money.convert.RateType)2 HashSet (java.util.HashSet)1 Money (org.javamoney.moneta.Money)1