-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #347 from Shenker93/master
Incorrect scale rounding fixed in AbstractCurrencyConversion
- Loading branch information
Showing
2 changed files
with
126 additions
and
4 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
125 changes: 125 additions & 0 deletions
125
moneta-core/src/test/java/org/javamoney/moneta/spi/CurrencyConversionTest.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,125 @@ | ||
package org.javamoney.moneta.spi; | ||
|
||
import org.javamoney.moneta.FastMoney; | ||
import org.javamoney.moneta.Money; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
|
||
import javax.money.CurrencyUnit; | ||
import javax.money.Monetary; | ||
import javax.money.MonetaryAmount; | ||
import javax.money.convert.*; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
|
||
public class CurrencyConversionTest { | ||
|
||
private AbstractRateProvider exactRateProvider; | ||
private AbstractRateProvider integerRateProvider; | ||
|
||
private final static String BASE_CURRENCY = "USD"; | ||
private final static String TERM_CURRENCY = "UAH"; | ||
|
||
|
||
private interface ExchangeRateStub extends ExchangeRate { | ||
|
||
@Override | ||
default ConversionContext getContext() { | ||
return ConversionContext.OTHER_CONVERSION; | ||
} | ||
|
||
@Override | ||
default CurrencyUnit getBaseCurrency() { | ||
return Monetary.getCurrency(BASE_CURRENCY); | ||
} | ||
|
||
@Override | ||
default CurrencyUnit getCurrency() { | ||
return Monetary.getCurrency(TERM_CURRENCY); | ||
} | ||
|
||
@Override | ||
default List<ExchangeRate> getExchangeRateChain() { | ||
return Collections.emptyList(); | ||
} | ||
} | ||
|
||
@BeforeClass | ||
private void init() { | ||
|
||
exactRateProvider = new AbstractRateProvider(ProviderContext.of("test_ctx_accurate")) { | ||
@Override | ||
public ExchangeRate getExchangeRate(ConversionQuery conversionQuery) { | ||
return (ExchangeRateStub) () -> new DefaultNumberValue(new BigDecimal("28.456")); | ||
} | ||
}; | ||
|
||
integerRateProvider = new AbstractRateProvider(ProviderContext.of("test_ctx_estimated")) { | ||
@Override | ||
public ExchangeRate getExchangeRate(ConversionQuery conversionQuery) { | ||
return (ExchangeRateStub) () -> new DefaultNumberValue(28); | ||
} | ||
}; | ||
} | ||
|
||
@Test | ||
public void testMoneyConversion() { | ||
|
||
testMoneyConversionHelper( | ||
Money.of(0, BASE_CURRENCY), | ||
Money.of(1000, BASE_CURRENCY), | ||
Money.of(new BigDecimal("1000.345"), BASE_CURRENCY) | ||
); | ||
} | ||
|
||
@Test | ||
public void testFastMoneyConversion() { | ||
|
||
testMoneyConversionHelper( | ||
FastMoney.of(0, BASE_CURRENCY), | ||
FastMoney.of(1000, BASE_CURRENCY), | ||
FastMoney.of(new BigDecimal("1000.345"), BASE_CURRENCY) | ||
); | ||
} | ||
|
||
private void testMoneyConversionHelper(MonetaryAmount zeroAmount, MonetaryAmount roundedAmount, MonetaryAmount amountWithCents) { | ||
|
||
assertEquals(zeroAmount.with( | ||
exactRateProvider.getCurrencyConversion(TERM_CURRENCY) | ||
).toString(), | ||
"UAH 0.00" | ||
); | ||
|
||
assertEquals( | ||
amountWithCents.with( | ||
exactRateProvider.getCurrencyConversion(TERM_CURRENCY) | ||
).toString(), | ||
"UAH 28465.82" | ||
); | ||
|
||
assertEquals( | ||
roundedAmount.with( | ||
exactRateProvider.getCurrencyConversion(TERM_CURRENCY) | ||
).toString(), | ||
"UAH 28456.00" | ||
); | ||
|
||
assertEquals( | ||
amountWithCents.with( | ||
integerRateProvider.getCurrencyConversion(TERM_CURRENCY) | ||
).toString(), | ||
"UAH 28009.66" | ||
); | ||
|
||
assertEquals( | ||
roundedAmount.with( | ||
integerRateProvider.getCurrencyConversion(TERM_CURRENCY) | ||
).toString(), | ||
"UAH 28000.00" | ||
); | ||
} | ||
} |