Skip to content

Commit 22032c9

Browse files
committed
JavaMoney-76: Fixed issue in IMF provider. Added simple test for comparison.
1 parent e91e6df commit 22032c9

File tree

2 files changed

+67
-14
lines changed

2 files changed

+67
-14
lines changed

src/main/java/org/javamoney/moneta/convert/internal/IMFRateProvider.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ public IMFRateProvider() throws MalformedURLException {
9999
super(CONTEXT);
100100
LoaderService loader = Bootstrap.getService(LoaderService.class);
101101
loader.addLoaderListener(this, DATA_ID);
102-
loader.loadDataAsync(DATA_ID);
102+
try {
103+
loader.loadData(DATA_ID);
104+
} catch (IOException e) {
105+
LOGGER.log(Level.WARNING, "Error loading initial data from IMF provider...", e);
106+
}
103107
}
104108

105109
@Override
@@ -154,7 +158,7 @@ private void loadRatesTSV(InputStream inputStream) throws IOException, ParseExce
154158
String[] parts = line.split("\\t");
155159
CurrencyUnit currency = currenciesByName.get(parts[0]);
156160
if (Objects.isNull(currency)) {
157-
LOGGER.warning("Unknown currency from, IMF data feed: " + parts[0]);
161+
LOGGER.finest(() -> "Uninterpretable data from IMF data feed: " + parts[0]);
158162
line = pr.readLine();
159163
continue;
160164
}
@@ -173,24 +177,16 @@ private void loadRatesTSV(InputStream inputStream) throws IOException, ParseExce
173177
rateType = RateType.DEFERRED;
174178
}
175179
if (currencyToSdr) { // Currency -> SDR
176-
List<ExchangeRate> rates = this.currencyToSdr.get(currency);
177-
if (Objects.isNull(rates)) {
178-
rates = new ArrayList<>(5);
179-
newCurrencyToSdr.put(currency, rates);
180-
}
181180
ExchangeRate rate = new ExchangeRateBuilder(
182181
ConversionContextBuilder.create(CONTEXT, rateType).setTimestampMillis(toTS).build())
183-
.setBase(currency).setTerm(SDR).setFactor(new DefaultNumberValue(values[i])).build();
182+
.setBase(currency).setTerm(SDR).setFactor(new DefaultNumberValue(1d / values[i])).build();
183+
List<ExchangeRate> rates = newCurrencyToSdr.computeIfAbsent(currency, c -> new ArrayList<>(5));
184184
rates.add(rate);
185185
} else { // SDR -> Currency
186-
List<ExchangeRate> rates = this.sdrToCurrency.get(currency);
187-
if (Objects.isNull(rates)) {
188-
rates = new ArrayList<>(5);
189-
newSdrToCurrency.put(currency, rates);
190-
}
191186
ExchangeRate rate = new ExchangeRateBuilder(
192187
ConversionContextBuilder.create(CONTEXT, rateType).setTimestampMillis(fromTS).build())
193-
.setBase(SDR).setTerm(currency).setFactor(DefaultNumberValue.of(values[i])).build();
188+
.setBase(SDR).setTerm(currency).setFactor(DefaultNumberValue.of(1d / values[i])).build();
189+
List<ExchangeRate> rates = newSdrToCurrency.computeIfAbsent(currency, (c) -> new ArrayList<>(5));
194190
rates.add(rate);
195191
}
196192
}
@@ -201,6 +197,8 @@ private void loadRatesTSV(InputStream inputStream) throws IOException, ParseExce
201197
newCurrencyToSdr.values().forEach((c) -> Collections.sort(List.class.cast(c)));
202198
this.sdrToCurrency = newSdrToCurrency;
203199
this.currencyToSdr = newCurrencyToSdr;
200+
this.sdrToCurrency.forEach((c, l) -> LOGGER.finest(() -> "SDR -> " + c.getCurrencyCode() + ": " + l));
201+
this.currencyToSdr.forEach((c, l) -> LOGGER.finest(() -> c.getCurrencyCode() + " -> SDR: " + l));
204202
}
205203

206204
private Double[] parseValues(NumberFormat f, String[] parts) throws ParseException {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Copyright (c) 2012, 2014, Credit Suisse (Anatole Tresch), Werner Keil and others by the @author tag.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.javamoney.moneta.convert;
17+
18+
import org.javamoney.moneta.Money;
19+
import org.testng.annotations.Test;
20+
21+
import javax.money.convert.CurrencyConversion;
22+
import javax.money.convert.ExchangeRateProvider;
23+
import javax.money.convert.MonetaryConversions;
24+
25+
import static org.testng.AssertJUnit.assertEquals;
26+
27+
/**
28+
* Test that tries to compare the value returned by IMF and ECB provider.
29+
*/
30+
public class CurrencyProviderTest {
31+
32+
@Test
33+
public void testECB() {
34+
ExchangeRateProvider ecbRateProvider = MonetaryConversions.getExchangeRateProvider("ECB");
35+
ExchangeRateProvider imfRateProvider = MonetaryConversions.getExchangeRateProvider("IMF");
36+
37+
CurrencyConversion ecbDollarConvertion = ecbRateProvider.getCurrencyConversion("USD");
38+
CurrencyConversion imfDollarConversion = imfRateProvider.getCurrencyConversion("USD");
39+
40+
try {
41+
// Wait for IMF provider to load
42+
Thread.sleep(10000L);
43+
for (String currency : new String[]{"INR", "CHF", "BRL"}) {
44+
Money money = Money.of(10, currency);
45+
System.out.println("ECB : " + money.with(ecbDollarConvertion));
46+
System.out.println("IMF : " + money.with(imfDollarConversion));
47+
assertEquals(money.with(ecbDollarConvertion).getNumber().doubleValue(), money.with(imfDollarConversion).getNumber().doubleValue(), 0.1d);
48+
}
49+
} catch (InterruptedException e) {
50+
// This test may fail, if the network is slow or not available, so only write the exception as of now...
51+
e.printStackTrace();
52+
}
53+
54+
}
55+
}

0 commit comments

Comments
 (0)