/** * Test TCK custom roundings. */ @Test(expectedExceptions = NullPointerException.class, description = "4.2.7 Ensure NullPointerException is thrown for Monetary.getRounding((String) null).") @SpecAssertion(section = "4.2.7", id = "427-C4") public void testCustomRoundings_Null() { Monetary.getRounding((String) null); }
/** * Access roundings using a RoundingContext, that is null. */ @Test(expectedExceptions = NullPointerException.class, description = "4.2.7 Ensure NullPointerException is thrown for " + "'Monetary.getRounding((RoundingContext) null)'.") @SpecAssertion(section = "4.2.7", id = "427-C2") public void testAccessRoundingsWithMonetaryContext_Null() { Monetary.getRounding(null); }
/** * Access roundings using all defined currencies, including TCK * custom currencies. */ @Test(expectedExceptions = NullPointerException.class, description = "4.2.7 Expected NullPointerException accessing a rounding with " + "'Monetary.getRounding(null)'.") @SpecAssertion(section = "4.2.7", id = "427-C1") public void testAccessRoundingsForCustomCurrencies_Explicit_Null() { Monetary.getRounding((CurrencyUnit) null); }
/** * Test TCK custom roundings. */ @Test(expectedExceptions = MonetaryException.class, description = "4.2.7 Ensure MonetaryException is thrown for " + "accessing invalid named rounding.") @SpecAssertion(section = "4.2.7", id = "427-C4") public void testCustomRoundings_Foo() { assertNotNull("Section 4.2.7 Expected custom rounding with name 'foo'.", Monetary.getRounding("foo")); }
@Override public MonetaryAmount apply(MonetaryAmount amount) { MonetaryRounding r = Monetary.getRounding(amount.getCurrency()); return r.apply(amount); }
/** * Test TCK custom roundings. */ @Test(description = "4.2.7 Access custom roundings and ensure correct functionality.") @SpecAssertion(section = "4.2.7", id = "427-C4") public void testCustomRoundings() { MonetaryOperator r = Monetary.getRounding("NOSCALE"); AssertJUnit.assertNotNull(r); MonetaryAmount m = new TestMonetaryAmountBuilder().setNumber(new BigDecimal("12.123456789101222232323")).setCurrency("CHF") .create(); AssertJUnit.assertEquals("Section 4.2.7 Expected CHF 12 for NOSCALE operator on " + m, "CHF 12", m.with(r).toString()); }
@Deprecated public RoundedMoney(Number number, CurrencyUnit currency, MathContext mathContext) { Objects.requireNonNull(currency, "Currency is required."); this.currency = currency; this.rounding = Monetary.getRounding(RoundingQueryBuilder.of().set(mathContext).build()); this.monetaryContext = DEFAULT_MONETARY_CONTEXT.toBuilder().set("MonetaryRounding", rounding).set(mathContext) .build(); Objects.requireNonNull(number, "Number is required."); checkNumber(number); this.number = MoneyUtils.getBigDecimal(number, monetaryContext); }
MonetaryOperator r = Monetary.getRounding(cu); MonetaryAmount m = new TestMonetaryAmountBuilder().setNumber(new BigDecimal("12.123456789101222232323")).setCurrency(cu) r = Monetary.getRounding(cu); } else { try { r = Monetary.getRounding(cu); AssertJUnit.assertNotNull(r); } catch (MonetaryException e) {
private MonetaryOperator createUsingMonetaryContext( MonetaryContext context, MonetaryContextBuilder monetaryContextBuilder) { MathContext mathContext = context.get(MathContext.class); if (Objects.isNull(mathContext)) { RoundingMode roundingMode = context.get(RoundingMode.class); if (Objects.nonNull(roundingMode)) { int scale = ofNullable(context.getInt("scale")).orElse(SCALE_DEFAULT); monetaryContextBuilder.set(roundingMode); monetaryContextBuilder.set("scale", scale); return Monetary.getRounding(RoundingQueryBuilder.of() .setScale(scale).set(roundingMode).build()); } else { return Monetary.getDefaultRounding(); } } else { monetaryContextBuilder.set(mathContext.getRoundingMode()); monetaryContextBuilder.set("scale", SCALE_DEFAULT); return Monetary.getRounding(RoundingQueryBuilder.of().set(mathContext) .setScale(SCALE_DEFAULT).build()); } } }
/** * Access roundings using a MonetaryContext. Use different * MathContext/RoundingMode, as an attribute, when running * on the JDK. */ @Test(description = "4.2.7 Ensure correct MonetaryRounding returned for a mathematical RoundingQuery.") @SpecAssertion(section = "4.2.7", id = "427-C2") public void testAccessRoundingsWithRoundingContext() { RoundingQuery ctx = RoundingQueryBuilder.of().setScale(1).set(RoundingMode.UP).build(); MonetaryOperator r = Monetary.getRounding(ctx); AssertJUnit.assertNotNull("Section 4.2.7: No rounding provided for RoundingQuery: " + ctx, r); MonetaryAmount m = new TestMonetaryAmountBuilder().setNumber(new BigDecimal("12.123456789101222232323")).setCurrency("CHF") .create(); AssertJUnit.assertEquals("Section 4.2.7: Invalid rounding provided for RoundingQuery: " + ctx, "CHF 12.2", m.with(r).toString()); }