Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Money and Currency API examples #1318

Merged
merged 1 commit into from
Mar 7, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 68 additions & 20 deletions core-java/src/test/java/com/baeldung/money/JavaMoneyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
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.*;
Expand All @@ -40,7 +42,11 @@ public void givenCurrencyCode_whenNoExist_thanThrowsError() {
@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);

Expand All @@ -52,7 +58,11 @@ public void givenAmounts_whenStringified_thanEquals() {

@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")));
Expand All @@ -61,20 +71,22 @@ public void givenCurrencies_whenCompared_thanNotequal() {

@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());
}
Expand All @@ -84,9 +96,19 @@ public void givenArithmetic_whenStringified_thanEqualsAmount() {
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);

Expand All @@ -101,19 +123,31 @@ public void givenArithmetic_whenStringified_thanEqualsAmount() {

@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());
}

@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());
Expand All @@ -126,11 +160,25 @@ public void givenAmount_whenCustomFormat_thanEquals() {
@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();

CurrencyConversion convEUR = MonetaryConversions.getConversion(ConversionQueryBuilder.of().setTermCurrency("EUR").build());
CurrencyConversion convUSD = MonetaryConversions.getConversion(ConversionQueryBuilder.of().setTermCurrency(USD).build());
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 conversionUSD = MonetaryConversions.getConversion("USD");
CurrencyConversion conversionEUR = MonetaryConversions.getConversion("EUR");
Expand Down