Search in sources :

Example 96 with MonetaryAmount

use of javax.money.MonetaryAmount in project jsr354-ri-bp by JavaMoney.

the class RoundedMoneyTest method testWithMonetaryOperator.

// /**
// * Test method for
// * {@link RoundedMoney#getAmountWhole()}.
// */
// @Test
// public void testGetAmountWhole() {
// assertEquals(1, RoundedMoney.of("XXX", 1.23455645d).getAmountWhole());
// assertEquals(1, RoundedMoney.of( 1).getAmountWhole());
// assertEquals(11, RoundedMoney.of( 11.0d).getAmountWhole());
// assertEquals(1234, RoundedMoney.of( 1234.1d).getAmountWhole());
// assertEquals(0, RoundedMoney.of( 0.0100d).getAmountWhole());
// }
// 
// /**
// * Test method for
// * {@link RoundedMoney#getAmountFractionNumerator()}.
// */
// @Test
// public void testGetAmountFractionNumerator() {
// assertEquals(0, RoundedMoney.of("XXX", new BigDecimal("1.23455645"))
// .getAmountFractionNumerator());
// assertEquals(0, RoundedMoney.of( 1).getAmountFractionNumerator());
// assertEquals(0, RoundedMoney.of( new BigDecimal("11.0"))
// .getAmountFractionNumerator());
// assertEquals(10L, RoundedMoney.of( new BigDecimal("1234.1"))
// .getAmountFractionNumerator());
// assertEquals(1L, RoundedMoney.of( new BigDecimal("0.0100"))
// .getAmountFractionNumerator());
// }
// 
// /**
// * Test method for
// * {@link
// RoundedMoney#getAmountFractionDenominator()}.
// */
// @Test
// public void testGetAmountFractionDenominator() {
// assertEquals(1, RoundedMoney.of("XXX", new BigDecimal("1.23455645"))
// .getAmountFractionDenominator());
// assertEquals(100, RoundedMoney.of( 1)
// .getAmountFractionDenominator());
// assertEquals(100, RoundedMoney.of( new BigDecimal("11.0"))
// .getAmountFractionDenominator());
// assertEquals(100L, RoundedMoney.of( new BigDecimal("1234.1"))
// .getAmountFractionDenominator());
// assertEquals(100L, RoundedMoney.of( new BigDecimal("0.0100"))
// .getAmountFractionDenominator());
// }
/**
 * Test method for
 * {@link RoundedMoney#with(javax.money.MonetaryOperator)}
 * .
 */
@Test
public void testWithMonetaryOperator() {
    MonetaryOperator adj = new MonetaryOperator() {

        @Override
        public MonetaryAmount apply(MonetaryAmount amount) {
            return amount.getFactory().setCurrency(amount.getCurrency()).setNumber(-100).create();
        }
    };
    RoundedMoney m = RoundedMoney.of(new BigDecimal("1.23645"), "USD");
    RoundedMoney a = m.with(adj);
    assertNotNull(a);
    assertNotSame(m, a);
    assertEquals(m.getCurrency(), a.getCurrency());
    assertEquals(RoundedMoney.of(-100, m.getCurrency()), a);
    adj = new MonetaryOperator() {

        @Override
        public MonetaryAmount apply(MonetaryAmount amount) {
            return amount.multiply(2).getFactory().setCurrency(Monetary.getCurrency("CHF")).create();
        }
    };
    a = m.with(adj);
    assertNotNull(a);
    assertNotSame(m, a);
    assertEquals(Monetary.getCurrency("CHF"), a.getCurrency());
    assertEquals(RoundedMoney.of(new BigDecimal("2.47"), a.getCurrency()), a);
}
Also used : MonetaryAmount(javax.money.MonetaryAmount) MonetaryOperator(javax.money.MonetaryOperator) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 97 with MonetaryAmount

use of javax.money.MonetaryAmount in project jsr354-ri-bp by JavaMoney.

the class ToStringMonetaryAmountFormatTest method executeTest.

private void executeTest(MonetaryAmount expectedMoney, MonetaryAmount a, MonetaryAmount b, ToStringMonetaryAmountFormat.ToStringMonetaryAmountFormatStyle type) {
    MonetaryAmount parserAResult = parser(a, type);
    MonetaryAmount parserBResult = parser(b, type);
    assertEquals(parserAResult, expectedMoney);
    assertEquals(parserBResult, expectedMoney);
    assertEquals(parserBResult, parserAResult);
}
Also used : MonetaryAmount(javax.money.MonetaryAmount)

Example 98 with MonetaryAmount

use of javax.money.MonetaryAmount in project jsr354-ri-bp by JavaMoney.

the class ToStringMonetaryAmountFormatTest method testBulgarianLev.

/**
 * Test related to {@see https://java.net/jira/browse/JAVAMONEY-151}.
 */
@Test
public void testBulgarianLev() {
    MonetaryAmount money = Money.of(1123000.50, "BGN");
    Locale locale = new Locale("", "BG");
    MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder.of(locale).set(CurrencyStyle.SYMBOL).build());
    assertEquals(format.format(money), "1 123 000,50 лв");
    format = MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder.of(locale).set(CurrencyStyle.CODE).build());
    assertEquals(format.format(money), "1 123 000,50 BGN");
}
Also used : Locale(java.util.Locale) MonetaryAmount(javax.money.MonetaryAmount) MonetaryAmountFormat(javax.money.format.MonetaryAmountFormat) BeforeTest(org.testng.annotations.BeforeTest) Test(org.testng.annotations.Test)

Example 99 with MonetaryAmount

use of javax.money.MonetaryAmount in project jsr354-ri-bp by JavaMoney.

the class FastMoneyTest method testWithNumber.

/**
 * Test method for {@link FastMoney#getFactory()#setNumber(java.lang.Number)} .
 */
@Test
public void testWithNumber() {
    FastMoney[] moneys = new FastMoney[] { FastMoney.of(100, "CHF"), FastMoney.of(34242344, "CHF"), FastMoney.of(new BigDecimal("23123213.435"), "CHF"), FastMoney.of(new BigDecimal("-23123213.435"), "CHF"), FastMoney.of(-23123213, "CHF"), FastMoney.of(0, "CHF") };
    FastMoney s = FastMoney.of(10, "CHF");
    MonetaryAmount[] moneys2 = new MonetaryAmount[] { s.getFactory().setNumber(100).create(), s.getFactory().setNumber(34242344).create(), s.getFactory().setNumber(new BigDecimal("23123213.435")).create(), s.getFactory().setNumber(new BigDecimal("-23123213.435")).create(), s.getFactory().setNumber(-23123213).create(), s.getFactory().setNumber(0).create() };
    for (int i = 0; i < moneys.length; i++) {
        assertEquals(moneys[i], moneys2[i], "with(Number) failed.");
    }
}
Also used : MonetaryAmount(javax.money.MonetaryAmount) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 100 with MonetaryAmount

use of javax.money.MonetaryAmount in project jsr354-ri-bp by JavaMoney.

the class FastMoneyTest method testWithMonetaryOperator.

/**
 * Test method for {@link FastMoney#with(javax.money.MonetaryOperator)} .
 */
@Test
public void testWithMonetaryOperator() {
    MonetaryOperator adj = new MonetaryOperator() {

        @Override
        public MonetaryAmount apply(MonetaryAmount amount) {
            return FastMoney.of(-100, amount.getCurrency());
        }
    };
    FastMoney m = FastMoney.of(new BigDecimal("1.2345"), "XXX");
    FastMoney a = m.with(adj);
    assertNotNull(a);
    assertNotSame(m, a);
    assertEquals(m.getCurrency(), a.getCurrency());
    assertEquals(FastMoney.of(-100, m.getCurrency()), a);
    adj = new MonetaryOperator() {

        @Override
        public MonetaryAmount apply(MonetaryAmount amount) {
            return amount.multiply(2).getFactory().setCurrency(Monetary.getCurrency("CHF")).create();
        }
    };
    a = m.with(adj);
    assertNotNull(a);
    assertNotSame(m, a);
    assertEquals(Monetary.getCurrency("CHF"), a.getCurrency());
    assertEquals(FastMoney.of(1.2345 * 2, a.getCurrency()), a);
}
Also used : MonetaryAmount(javax.money.MonetaryAmount) MonetaryOperator(javax.money.MonetaryOperator) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Aggregations

MonetaryAmount (javax.money.MonetaryAmount)275 Test (org.testng.annotations.Test)260 CurrencyConversion (javax.money.convert.CurrencyConversion)88 BeforeTest (org.testng.annotations.BeforeTest)83 BigDecimal (java.math.BigDecimal)64 CurrencyUnit (javax.money.CurrencyUnit)57 ConversionQuery (javax.money.convert.ConversionQuery)10 RoundedMoney (org.javamoney.moneta.RoundedMoney)10 LocalDate (java.time.LocalDate)9 ExchangeRateProvider (javax.money.convert.ExchangeRateProvider)9 Test (org.junit.Test)9 FastMoney (org.javamoney.moneta.FastMoney)6 Money (org.javamoney.moneta.Money)6 MathContext (java.math.MathContext)4 MonetaryOperator (javax.money.MonetaryOperator)4 MonetaryAmountFormat (javax.money.format.MonetaryAmountFormat)4 MonetaryAmountFactory (javax.money.MonetaryAmountFactory)3 BookingBean (com.github.drbookings.model.data.BookingBean)2 Calendar (java.util.Calendar)2 GregorianCalendar (java.util.GregorianCalendar)2