Refactor Money and Currency API examples
This commit is contained in:
parent
e1d136368b
commit
4f51b3634f
|
@ -15,6 +15,8 @@ import javax.money.convert.MonetaryConversions;
|
|||
import javax.money.format.AmountFormatQueryBuilder;
|
||||
import javax.money.format.MonetaryAmountFormat;
|
||||
import javax.money.format.MonetaryFormats;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
@ -40,7 +42,11 @@ public class JavaMoneyTest {
|
|||
@Test
|
||||
public void givenAmounts_whenStringified_thanEquals() {
|
||||
CurrencyUnit usd = Monetary.getCurrency("USD");
|
||||
MonetaryAmount fstAmtUSD = Monetary.getDefaultAmountFactory().setCurrency(usd).setNumber(200).create();
|
||||
MonetaryAmount fstAmtUSD = Monetary
|
||||
.getDefaultAmountFactory()
|
||||
.setCurrency(usd)
|
||||
.setNumber(200)
|
||||
.create();
|
||||
Money moneyof = Money.of(12, usd);
|
||||
FastMoney fastmoneyof = FastMoney.of(2, usd);
|
||||
|
||||
|
@ -52,7 +58,11 @@ public class JavaMoneyTest {
|
|||
|
||||
@Test
|
||||
public void givenCurrencies_whenCompared_thanNotequal() {
|
||||
MonetaryAmount oneDolar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create();
|
||||
MonetaryAmount oneDolar = Monetary
|
||||
.getDefaultAmountFactory()
|
||||
.setCurrency("USD")
|
||||
.setNumber(1)
|
||||
.create();
|
||||
Money oneEuro = Money.of(1, "EUR");
|
||||
|
||||
assertFalse(oneEuro.equals(FastMoney.of(1, "EUR")));
|
||||
|
@ -61,20 +71,22 @@ public class JavaMoneyTest {
|
|||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void givenAmount_whenDivided_thanThrowsException() {
|
||||
MonetaryAmount oneDolar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create();
|
||||
MonetaryAmount oneDolar = Monetary
|
||||
.getDefaultAmountFactory()
|
||||
.setCurrency("USD")
|
||||
.setNumber(1)
|
||||
.create();
|
||||
oneDolar.divide(3);
|
||||
fail(); // if no exception
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAmounts_whenSummed_thanCorrect() {
|
||||
MonetaryAmount[] monetaryAmounts = new MonetaryAmount[]{
|
||||
Money.of(100, "CHF"), Money.of(10.20, "CHF"), Money.of(1.15, "CHF")};
|
||||
List<MonetaryAmount> monetaryAmounts = Arrays.asList(Money.of(100, "CHF"), Money.of(10.20, "CHF"), Money.of(1.15, "CHF"));
|
||||
|
||||
Money sumAmtCHF = Money.of(0, "CHF");
|
||||
for (MonetaryAmount monetaryAmount : monetaryAmounts) {
|
||||
sumAmtCHF = sumAmtCHF.add(monetaryAmount);
|
||||
}
|
||||
Money sumAmtCHF = (Money) monetaryAmounts
|
||||
.stream()
|
||||
.reduce(Money.of(0, "CHF"), MonetaryAmount::add);
|
||||
|
||||
assertEquals("CHF 111.35", sumAmtCHF.toString());
|
||||
}
|
||||
|
@ -84,9 +96,19 @@ public class JavaMoneyTest {
|
|||
CurrencyUnit usd = Monetary.getCurrency("USD");
|
||||
|
||||
Money moneyof = Money.of(12, usd);
|
||||
MonetaryAmount fstAmtUSD = Monetary.getDefaultAmountFactory().setCurrency(usd).setNumber(200.50).create();
|
||||
MonetaryAmount oneDolar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create();
|
||||
Money subtractedAmount = Money.of(1, "USD").subtract(fstAmtUSD);
|
||||
MonetaryAmount fstAmtUSD = Monetary
|
||||
.getDefaultAmountFactory()
|
||||
.setCurrency(usd)
|
||||
.setNumber(200.50)
|
||||
.create();
|
||||
MonetaryAmount oneDolar = Monetary
|
||||
.getDefaultAmountFactory()
|
||||
.setCurrency("USD")
|
||||
.setNumber(1)
|
||||
.create();
|
||||
Money subtractedAmount = Money
|
||||
.of(1, "USD")
|
||||
.subtract(fstAmtUSD);
|
||||
MonetaryAmount multiplyAmount = oneDolar.multiply(0.25);
|
||||
MonetaryAmount divideAmount = oneDolar.divide(0.25);
|
||||
|
||||
|
@ -101,7 +123,11 @@ public class JavaMoneyTest {
|
|||
|
||||
@Test
|
||||
public void givenAmount_whenRounded_thanEquals() {
|
||||
MonetaryAmount fstAmtEUR = Monetary.getDefaultAmountFactory().setCurrency("EUR").setNumber(1.30473908).create();
|
||||
MonetaryAmount fstAmtEUR = Monetary
|
||||
.getDefaultAmountFactory()
|
||||
.setCurrency("EUR")
|
||||
.setNumber(1.30473908)
|
||||
.create();
|
||||
MonetaryAmount roundEUR = fstAmtEUR.with(Monetary.getDefaultRounding());
|
||||
assertEquals("EUR 1.30473908", fstAmtEUR.toString());
|
||||
assertEquals("EUR 1.3", roundEUR.toString());
|
||||
|
@ -109,11 +135,19 @@ public class JavaMoneyTest {
|
|||
|
||||
@Test
|
||||
public void givenAmount_whenCustomFormat_thanEquals() {
|
||||
MonetaryAmount oneDolar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create();
|
||||
MonetaryAmount oneDolar = Monetary
|
||||
.getDefaultAmountFactory()
|
||||
.setCurrency("USD")
|
||||
.setNumber(1)
|
||||
.create();
|
||||
MonetaryAmountFormat formatUSD = MonetaryFormats.getAmountFormat(Locale.US);
|
||||
String usFormatted = formatUSD.format(oneDolar);
|
||||
|
||||
MonetaryAmountFormat customFormat = MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder.of(Locale.US).set(CurrencyStyle.NAME).set("pattern", "00000.00 ¤").build());
|
||||
MonetaryAmountFormat customFormat = MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder
|
||||
.of(Locale.US)
|
||||
.set(CurrencyStyle.NAME)
|
||||
.set("pattern", "00000.00 ¤")
|
||||
.build());
|
||||
String customFormatted = customFormat.format(oneDolar);
|
||||
|
||||
assertEquals("USD 1", oneDolar.toString());
|
||||
|
@ -126,11 +160,25 @@ public class JavaMoneyTest {
|
|||
@Test
|
||||
public void givenAmount_whenConversion_thenNotNull() {
|
||||
CurrencyUnit USD = Monetary.getCurrency("USD");
|
||||
MonetaryAmount oneDolar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create();
|
||||
MonetaryAmount fstAmtEUR = Monetary.getDefaultAmountFactory().setCurrency("EUR").setNumber(1.30473908).create();
|
||||
MonetaryAmount oneDolar = Monetary
|
||||
.getDefaultAmountFactory()
|
||||
.setCurrency("USD")
|
||||
.setNumber(1)
|
||||
.create();
|
||||
MonetaryAmount fstAmtEUR = Monetary
|
||||
.getDefaultAmountFactory()
|
||||
.setCurrency("EUR")
|
||||
.setNumber(1.30473908)
|
||||
.create();
|
||||
|
||||
CurrencyConversion convEUR = MonetaryConversions.getConversion(ConversionQueryBuilder.of().setTermCurrency("EUR").build());
|
||||
CurrencyConversion convUSD = MonetaryConversions.getConversion(ConversionQueryBuilder.of().setTermCurrency(USD).build());
|
||||
CurrencyConversion convEUR = MonetaryConversions.getConversion(ConversionQueryBuilder
|
||||
.of()
|
||||
.setTermCurrency("EUR")
|
||||
.build());
|
||||
CurrencyConversion convUSD = MonetaryConversions.getConversion(ConversionQueryBuilder
|
||||
.of()
|
||||
.setTermCurrency(USD)
|
||||
.build());
|
||||
|
||||
CurrencyConversion conversionUSD = MonetaryConversions.getConversion("USD");
|
||||
CurrencyConversion conversionEUR = MonetaryConversions.getConversion("EUR");
|
||||
|
|
Loading…
Reference in New Issue