Search in sources :

Example 6 with MonetaryOperator

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

the class MonetaryRoundedFactoryBuilderTest method shouldReturnScaleMathContextOperator.

@Test
public void shouldReturnScaleMathContextOperator() {
    int precision = 6;
    int scale = 3;
    RoundingMode roundingMode = RoundingMode.HALF_EVEN;
    MonetaryRoundedFactory factory = MonetaryRoundedFactory.withRoundingMode(roundingMode).withScale(scale).withPrecision(precision).build();
    assertNotNull(factory);
    MonetaryOperator roundingOperator = factory.getRoundingOperator();
    assertNotNull(roundingOperator);
    assertTrue(PrecisionScaleRoundedOperator.class.isInstance(roundingOperator));
    PrecisionScaleRoundedOperator result = PrecisionScaleRoundedOperator.class.cast(roundingOperator);
    assertEquals(precision, result.getMathContext().getPrecision());
    assertEquals(scale, result.getScale());
    assertEquals(roundingMode, result.getMathContext().getRoundingMode());
}
Also used : RoundingMode(java.math.RoundingMode) MonetaryOperator(javax.money.MonetaryOperator) Test(org.testng.annotations.Test)

Example 7 with MonetaryOperator

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

the class MonetaryRoundedFactoryBuilderTest method shouldReturnMathContextScaleOperator.

@Test
public void shouldReturnMathContextScaleOperator() {
    int precision = 6;
    int scale = 3;
    RoundingMode roundingMode = RoundingMode.HALF_EVEN;
    MonetaryRoundedFactory factory = MonetaryRoundedFactory.withRoundingMode(roundingMode).withPrecision(precision).withScale(scale).build();
    assertNotNull(factory);
    MonetaryOperator roundingOperator = factory.getRoundingOperator();
    assertNotNull(roundingOperator);
    assertTrue(PrecisionScaleRoundedOperator.class.isInstance(roundingOperator));
    PrecisionScaleRoundedOperator result = PrecisionScaleRoundedOperator.class.cast(roundingOperator);
    assertEquals(precision, result.getMathContext().getPrecision());
    assertEquals(scale, result.getScale());
    assertEquals(roundingMode, result.getMathContext().getRoundingMode());
}
Also used : RoundingMode(java.math.RoundingMode) MonetaryOperator(javax.money.MonetaryOperator) Test(org.testng.annotations.Test)

Example 8 with MonetaryOperator

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

the class MonetaryRoundedFactoryBuilderTest method shouldReturnErrorWhenMonetaryOperatorIsNull.

@Test(expectedExceptions = NullPointerException.class)
public void shouldReturnErrorWhenMonetaryOperatorIsNull() {
    MonetaryOperator roundingOperator = null;
    MonetaryRoundedFactory.of(roundingOperator);
    fail();
}
Also used : MonetaryOperator(javax.money.MonetaryOperator) Test(org.testng.annotations.Test)

Example 9 with MonetaryOperator

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

the class MonetaryRoundedFactoryBuilderTest method shouldReturnScaleRoundingOperator.

@Test
public void shouldReturnScaleRoundingOperator() {
    int scale = 6;
    RoundingMode roundingMode = RoundingMode.HALF_EVEN;
    MonetaryRoundedFactory factory = MonetaryRoundedFactory.withRoundingMode(roundingMode).withScale(scale).build();
    assertNotNull(factory);
    MonetaryOperator roundingOperator = factory.getRoundingOperator();
    assertNotNull(roundingOperator);
    assertTrue(ScaleRoundedOperator.class.isInstance(roundingOperator));
    ScaleRoundedOperator result = ScaleRoundedOperator.class.cast(roundingOperator);
    assertEquals(scale, result.getScale());
    assertEquals(roundingMode, result.getRoundingMode());
}
Also used : RoundingMode(java.math.RoundingMode) MonetaryOperator(javax.money.MonetaryOperator) Test(org.testng.annotations.Test)

Example 10 with MonetaryOperator

use of javax.money.MonetaryOperator 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)

Aggregations

MonetaryOperator (javax.money.MonetaryOperator)20 Test (org.testng.annotations.Test)20 RoundingMode (java.math.RoundingMode)9 MonetaryAmount (javax.money.MonetaryAmount)5 BigDecimal (java.math.BigDecimal)3 MathContext (java.math.MathContext)3 Assert.assertEquals (org.testng.Assert.assertEquals)2 Assert.assertNotNull (org.testng.Assert.assertNotNull)2 Assert.assertTrue (org.testng.Assert.assertTrue)2 Assert.fail (org.testng.Assert.fail)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 MethodHandles (java.lang.invoke.MethodHandles)1 BigInteger (java.math.BigInteger)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 CurrencyUnit (javax.money.CurrencyUnit)1