Search in sources :

Example 1 with MonetaryOperator

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

the class FastMoneyTest method testWithMonetaryOperator.

/**
 * Test method for {@link org.javamoney.moneta.FastMoney#with(javax.money.MonetaryOperator)} .
 */
@Test
public void testWithMonetaryOperator() {
    MonetaryOperator adj = amount -> 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 = amount -> 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 : Monetary(javax.money.Monetary) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Assert.fail(org.testng.Assert.fail) MethodHandles(java.lang.invoke.MethodHandles) CurrencyUnit(javax.money.CurrencyUnit) ObjectInputStream(java.io.ObjectInputStream) Assert.assertEquals(org.testng.Assert.assertEquals) MonetaryQuery(javax.money.MonetaryQuery) IOException(java.io.IOException) Test(org.testng.annotations.Test) Assert.assertNotNull(org.testng.Assert.assertNotNull) Logger(java.util.logging.Logger) MonetaryOperator(javax.money.MonetaryOperator) Level(java.util.logging.Level) BigDecimal(java.math.BigDecimal) Assert.assertNotSame(org.testng.Assert.assertNotSame) ByteArrayInputStream(java.io.ByteArrayInputStream) Assert(org.testng.Assert) Assert.assertTrue(org.testng.Assert.assertTrue) ObjectOutputStream(java.io.ObjectOutputStream) BigInteger(java.math.BigInteger) Assert.assertFalse(org.testng.Assert.assertFalse) MonetaryAmount(javax.money.MonetaryAmount) MonetaryOperator(javax.money.MonetaryOperator) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 2 with MonetaryOperator

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

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

the class MonetaryRoundedFactoryBuilderTest method shouldReturnTheSameMonetaryOperator.

@Test
public void shouldReturnTheSameMonetaryOperator() {
    MonetaryOperator monetaryOperator = m -> m;
    MonetaryRoundedFactory factory = MonetaryRoundedFactory.of(monetaryOperator);
    assertNotNull(factory);
    assertEquals(monetaryOperator, factory.getRoundingOperator());
}
Also used : MathContext(java.math.MathContext) Assert.fail(org.testng.Assert.fail) Assert.assertEquals(org.testng.Assert.assertEquals) Assert.assertTrue(org.testng.Assert.assertTrue) Test(org.testng.annotations.Test) Assert.assertNotNull(org.testng.Assert.assertNotNull) MonetaryOperator(javax.money.MonetaryOperator) RoundingMode(java.math.RoundingMode) MonetaryOperator(javax.money.MonetaryOperator) Test(org.testng.annotations.Test)

Example 4 with MonetaryOperator

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

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

the class DefaultMonetaryRoundedFactoryTest method shouldReturnGetRoudingOperator.

@Test
public void shouldReturnGetRoudingOperator() {
    MonetaryOperator roundingOperator = factory.getRoundingOperator();
    assertEquals(identical, roundingOperator);
}
Also used : MonetaryOperator(javax.money.MonetaryOperator) 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