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));
}
}
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;
}
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());
}
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();
}
}
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());
}
Aggregations