Skip to content

Commit

Permalink
JavaMoney-76: Fixed issue in IMF provider. Added simple test for comp…
Browse files Browse the repository at this point in the history
…arison.
  • Loading branch information
atsticks committed Feb 5, 2015
1 parent e91e6df commit 22032c9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ public IMFRateProvider() throws MalformedURLException {
super(CONTEXT);
LoaderService loader = Bootstrap.getService(LoaderService.class);
loader.addLoaderListener(this, DATA_ID);
loader.loadDataAsync(DATA_ID);
try {
loader.loadData(DATA_ID);
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Error loading initial data from IMF provider...", e);
}
}

@Override
Expand Down Expand Up @@ -154,7 +158,7 @@ private void loadRatesTSV(InputStream inputStream) throws IOException, ParseExce
String[] parts = line.split("\\t");
CurrencyUnit currency = currenciesByName.get(parts[0]);
if (Objects.isNull(currency)) {
LOGGER.warning("Unknown currency from, IMF data feed: " + parts[0]);
LOGGER.finest(() -> "Uninterpretable data from IMF data feed: " + parts[0]);
line = pr.readLine();
continue;
}
Expand All @@ -173,24 +177,16 @@ private void loadRatesTSV(InputStream inputStream) throws IOException, ParseExce
rateType = RateType.DEFERRED;
}
if (currencyToSdr) { // Currency -> SDR
List<ExchangeRate> rates = this.currencyToSdr.get(currency);
if (Objects.isNull(rates)) {
rates = new ArrayList<>(5);
newCurrencyToSdr.put(currency, rates);
}
ExchangeRate rate = new ExchangeRateBuilder(
ConversionContextBuilder.create(CONTEXT, rateType).setTimestampMillis(toTS).build())
.setBase(currency).setTerm(SDR).setFactor(new DefaultNumberValue(values[i])).build();
.setBase(currency).setTerm(SDR).setFactor(new DefaultNumberValue(1d / values[i])).build();
List<ExchangeRate> rates = newCurrencyToSdr.computeIfAbsent(currency, c -> new ArrayList<>(5));
rates.add(rate);
} else { // SDR -> Currency
List<ExchangeRate> rates = this.sdrToCurrency.get(currency);
if (Objects.isNull(rates)) {
rates = new ArrayList<>(5);
newSdrToCurrency.put(currency, rates);
}
ExchangeRate rate = new ExchangeRateBuilder(
ConversionContextBuilder.create(CONTEXT, rateType).setTimestampMillis(fromTS).build())
.setBase(SDR).setTerm(currency).setFactor(DefaultNumberValue.of(values[i])).build();
.setBase(SDR).setTerm(currency).setFactor(DefaultNumberValue.of(1d / values[i])).build();
List<ExchangeRate> rates = newSdrToCurrency.computeIfAbsent(currency, (c) -> new ArrayList<>(5));
rates.add(rate);
}
}
Expand All @@ -201,6 +197,8 @@ private void loadRatesTSV(InputStream inputStream) throws IOException, ParseExce
newCurrencyToSdr.values().forEach((c) -> Collections.sort(List.class.cast(c)));
this.sdrToCurrency = newSdrToCurrency;
this.currencyToSdr = newCurrencyToSdr;
this.sdrToCurrency.forEach((c, l) -> LOGGER.finest(() -> "SDR -> " + c.getCurrencyCode() + ": " + l));
this.currencyToSdr.forEach((c, l) -> LOGGER.finest(() -> c.getCurrencyCode() + " -> SDR: " + l));
}

private Double[] parseValues(NumberFormat f, String[] parts) throws ParseException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright (c) 2012, 2014, Credit Suisse (Anatole Tresch), Werner Keil and others by the @author tag.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.javamoney.moneta.convert;

import org.javamoney.moneta.Money;
import org.testng.annotations.Test;

import javax.money.convert.CurrencyConversion;
import javax.money.convert.ExchangeRateProvider;
import javax.money.convert.MonetaryConversions;

import static org.testng.AssertJUnit.assertEquals;

/**
* Test that tries to compare the value returned by IMF and ECB provider.
*/
public class CurrencyProviderTest {

@Test
public void testECB() {
ExchangeRateProvider ecbRateProvider = MonetaryConversions.getExchangeRateProvider("ECB");
ExchangeRateProvider imfRateProvider = MonetaryConversions.getExchangeRateProvider("IMF");

CurrencyConversion ecbDollarConvertion = 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(10, currency);
System.out.println("ECB : " + money.with(ecbDollarConvertion));
System.out.println("IMF : " + money.with(imfDollarConversion));
assertEquals(money.with(ecbDollarConvertion).getNumber().doubleValue(), money.with(imfDollarConversion).getNumber().doubleValue(), 0.1d);
}
} catch (InterruptedException e) {
// This test may fail, if the network is slow or not available, so only write the exception as of now...
e.printStackTrace();
}

}
}

0 comments on commit 22032c9

Please sign in to comment.