Search in sources :

Example 16 with MonetaryOperator

use of javax.money.MonetaryOperator in project jsr354-ri 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 17 with MonetaryOperator

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

the class MoneyTest method testWithMonetaryOperator.

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

        @Override
        public MonetaryAmount apply(MonetaryAmount amount) {
            return Money.of(-100, amount.getCurrency());
        }
    };
    Money m = Money.of(1.23455645d, "XXX");
    Money a = m.with(adj);
    assertNotNull(a);
    assertNotSame(m, a);
    assertEquals(m.getCurrency(), a.getCurrency());
    assertEquals(Money.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(Money.of(1.23455645d * 2, a.getCurrency()), a);
}
Also used : MonetaryAmount(javax.money.MonetaryAmount) MonetaryOperator(javax.money.MonetaryOperator) Test(org.testng.annotations.Test)

Example 18 with MonetaryOperator

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

the class MonetaryOperatorsTest method testPercentToString.

/**
 * Test method for
 * {@link org.javamoney.moneta.function.MonetaryOperators#percent(java.lang.Number)}
 * .
 */
@Test
public void testPercentToString() {
    MonetaryOperator p = MonetaryOperators.percent((short) 25);
    assertTrue(p.toString().contains("25"));
    assertTrue(p.toString().contains("%"));
}
Also used : MonetaryOperator(javax.money.MonetaryOperator) Test(org.testng.annotations.Test)

Example 19 with MonetaryOperator

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

the class MonetaryRoundedFactoryBuilderTest method shouldReturnTheSameMonetaryOperator.

@Test
public void shouldReturnTheSameMonetaryOperator() {
    MonetaryOperator monetaryOperator = new MonetaryOperator() {

        @Override
        public MonetaryAmount apply(MonetaryAmount amount) {
            return amount;
        }
    };
    MonetaryRoundedFactory factory = MonetaryRoundedFactory.of(monetaryOperator);
    assertNotNull(factory);
    assertEquals(monetaryOperator, factory.getRoundingOperator());
}
Also used : MonetaryAmount(javax.money.MonetaryAmount) MonetaryOperator(javax.money.MonetaryOperator) Test(org.testng.annotations.Test)

Example 20 with MonetaryOperator

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

the class MonetaryRoundedFactoryBuilderTest method shouldReturnMathContextOperator.

@Test
public void shouldReturnMathContextOperator() {
    int precision = 6;
    RoundingMode roundingMode = RoundingMode.HALF_EVEN;
    MonetaryRoundedFactory factory = MonetaryRoundedFactory.withRoundingMode(roundingMode).withPrecision(precision).build();
    assertNotNull(factory);
    MonetaryOperator roundingOperator = factory.getRoundingOperator();
    assertNotNull(roundingOperator);
    assertTrue(PrecisionContextRoundedOperator.class.isInstance(roundingOperator));
    MathContext result = PrecisionContextRoundedOperator.class.cast(roundingOperator).getMathContext();
    assertEquals(precision, result.getPrecision());
    assertEquals(roundingMode, result.getRoundingMode());
}
Also used : RoundingMode(java.math.RoundingMode) MonetaryOperator(javax.money.MonetaryOperator) MathContext(java.math.MathContext) 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