forked from JavaMoney/jsr354-ri
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…ney#385 This commit introduces a `failFast` parameter to the `getExchangeRate` method in the `CompoundRateProvider` class to work around the limitations described in issue JavaMoney#385. The `failFast` parameter helps mitigate the problem by allowing the caller to specify whether the method should return immediately on failure. Due to current API constraints, we cannot modify the `getExchangeRate` signature in the `ExchangeRateProvider` interface without causing breaking changes.
- Loading branch information
Showing
3 changed files
with
89 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
moneta-core/src/test/java/org/javamoney/moneta/spi/CompoundRateProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.javamoney.moneta.spi; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
import javax.money.convert.*; | ||
|
||
import java.time.DayOfWeek; | ||
import java.time.LocalDate; | ||
import java.time.temporal.TemporalAdjusters; | ||
|
||
import static org.testng.Assert.*; | ||
|
||
public class CompoundRateProviderTest { | ||
|
||
@Test | ||
public void testGetExchangeRate() { | ||
String baseCurrency = "EUR"; | ||
String termCurrency = "GBP"; | ||
LocalDate date = lastWeekTuesday(); | ||
ConversionQuery conversionQuery = ConversionQueryBuilder.of() | ||
.setBaseCurrency(baseCurrency) | ||
.setTermCurrency(termCurrency) | ||
.set(date) | ||
.build(); | ||
|
||
ExchangeRateProvider compoundRateProvider = MonetaryConversions.getExchangeRateProvider("ECB", "ECB-HIST90"); | ||
assertNotNull(compoundRateProvider.getExchangeRate(conversionQuery)); | ||
} | ||
|
||
@Test | ||
public void testGetExchangeRateAllProvidersFail() { | ||
String baseCurrency = "EUR"; | ||
String termCurrency = "GBP"; | ||
LocalDate date = lastWeekTuesday(); | ||
ConversionQuery conversionQuery = ConversionQueryBuilder.of() | ||
.setBaseCurrency(baseCurrency) | ||
.setTermCurrency(termCurrency) | ||
.set(date) | ||
.build(); | ||
|
||
ExchangeRateProvider compoundRateProvider = MonetaryConversions.getExchangeRateProvider("ECB", "IMF"); | ||
assertThrows(CurrencyConversionException.class, () -> compoundRateProvider.getExchangeRate(conversionQuery)); | ||
} | ||
|
||
@Test | ||
public void testGetExchangeRateFailingFast() { | ||
String baseCurrency = "EUR"; | ||
String termCurrency = "GBP"; | ||
LocalDate date = lastWeekTuesday(); | ||
ConversionQuery conversionQuery = ConversionQueryBuilder.of() | ||
.setBaseCurrency(baseCurrency) | ||
.setTermCurrency(termCurrency) | ||
.set(date) | ||
.build(); | ||
|
||
// Need to cast to CompoundRateProvider to access the getExchangeRate method that takes a boolean parameter | ||
CompoundRateProvider compoundRateProvider = (CompoundRateProvider) MonetaryConversions.getExchangeRateProvider("ECB", "ECB-HIST90"); | ||
assertThrows(CurrencyConversionException.class, () -> compoundRateProvider.getExchangeRate(conversionQuery, true)); | ||
} | ||
|
||
private LocalDate lastWeekTuesday() { | ||
LocalDate today = LocalDate.now(); | ||
LocalDate lastTuesday = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.TUESDAY)); | ||
return lastTuesday.minusWeeks(1); | ||
} | ||
} |