/** * Returns the maximal {@link MonetaryContext} supported, for requests that exceed these maximal * capabilities, an {@link ArithmeticException} must be thrown. * * @return the maximal {@link MonetaryContext} supported, never {@code null} */ default MonetaryContext getMaximalMonetaryContext() { return getDefaultMonetaryContext(); }
/** * For each MonetaryAmount Factory: Bad Case: Create negative amounts from a factory with an invalid currency. */ @Test(description = "4.2.6 Bad case: For each MonetaryAmount Factory: Create negative amounts, with invalid " + "currency, expect MonetaryException.") @SpecAssertion(section = "4.2.6", id = "426-D4") public void testMonetaryAmountFactoryCreateNegativeInvalidCurrency_BadCase() { for (Class type : Monetary.getAmountTypes()) { if (type.equals(TestAmount.class)) { continue; } MonetaryAmountFactory f = Monetary.getAmountFactory(type); try { if (f.getDefaultMonetaryContext().getPrecision() == 0) { f.setNumber(TestUtils.createNumberWithPrecision(5).negate()); } else { f.setNumber(TestUtils.createNumberWithPrecision(f.getDefaultMonetaryContext().getPrecision()) .negate()); } f.setCurrency("Section 4.2.6: FooBar_foobar_fOobAr_foObaR"); f.create(); AssertJUnit.fail("Section 4.2.6: MonetaryAmountFactory must throw a MonetaryException, " + "when an amount with an invalid " + "currency is tried to be created, type: " + type.getName()); } catch (MonetaryException e) { // OK } } }
if (f.getDefaultMonetaryContext().getMaxScale() < 5) { MonetaryAmount[] divideAndRemainder = money1.divideAndRemainder(new BigDecimal("0.6")); AssertJUnit.assertEquals("Section 4.2.2: divideAndRemainder(0.6)[0] failed for " + type.getName(),
if (f.getDefaultMonetaryContext().getPrecision() == 0) { f.setNumber(TestUtils.createNumberWithPrecision(5).negate()); } else { f.setNumber(TestUtils.createNumberWithPrecision(f.getDefaultMonetaryContext().getPrecision()) .negate());
if (f.getDefaultMonetaryContext().getPrecision() == 0) { f.setNumber(TestUtils.createNumberWithPrecision(5)); } else { f.setNumber(TestUtils.createNumberWithPrecision(f.getDefaultMonetaryContext().getPrecision()));
/** * Call getFactory(),of a new MonetaryAmount instance with a new monetary context, a * new number and a new currency. The instances must be non equal. */ @SpecAssertion(section = "4.2.2", id = "422-B6") @Test(description = "4.2.2 For each amount class, check new amounts are not equal for different currencies and " + "contexts.") public void testMonetaryAmountFactories_CreateWithMonetaryContextNumberAndCurrency() { for (Class type : Monetary.getAmountTypes()) { if (type.equals(TestAmount.class)) { continue; } MonetaryAmountFactory<MonetaryAmount> f = Monetary.getAmountFactory(type); MonetaryContext mc1 = f.getDefaultMonetaryContext(); MonetaryContext mc2 = f.getMaximalMonetaryContext(); MonetaryAmount m1 = f.setCurrency("CHF").setContext(mc1).setNumber(10).create(); MonetaryAmount m2 = f.setCurrency("USD").setContext(mc2).setNumber(11).create(); AssertJUnit.assertNotSame("Section 4.2.2: Expected not same for " + type.getName(), m1, m2); AssertJUnit.assertTrue("Section 4.2.2: Expected isEqualTo==true for " + type.getName(), m1.isEqualTo(m1)); AssertJUnit.assertTrue("Section 4.2.2: Expected isEqualTo==true for " + type.getName(), m2.isEqualTo(m2)); AssertJUnit.assertTrue("Section 4.2.2: Expected equality for " + type.getName(), m1.equals(m1)); AssertJUnit.assertTrue("Section 4.2.2: Expected equality for " + type.getName(), m2.equals(m2)); } }
for (Class type : Monetary.getAmountTypes()) { MonetaryAmountFactory<?> f = Monetary.getAmountFactory(type); MonetaryContext defCtx = f.getDefaultMonetaryContext(); MonetaryContext maxCtx = f.getMaximalMonetaryContext(); if (f.getDefaultMonetaryContext().getMaxScale() > -1) { assertTrue( "Section 4.2.6: MonetaryAmountFactory maximal MonetaryContext cannot be less capable than the" + " default " + "(maxScale default/max=" + f.getDefaultMonetaryContext().getMaxScale() + '/' + f.getMaximalMonetaryContext().getMaxScale() + " for " + type.getName(), maxCtx.getMaxScale() == -1 || defCtx.getMaxScale() <= maxCtx.getMaxScale()); if (f.getDefaultMonetaryContext().getMaxScale() == -1) { assertTrue( "Section 4.2.6: MonetaryAmountFactory maximal MonetaryContext cannot be less capable than the" + " default " + "(maxScale default/max=" + f.getDefaultMonetaryContext().getMaxScale() + '/' + f.getMaximalMonetaryContext().getMaxScale() + " for " + type.getName(), maxCtx.getMaxScale() == -1); if (f.getDefaultMonetaryContext().getPrecision() > 0) { assertTrue( "Section 4.2.6: MonetaryAmountFactory maximal MonetaryContext cannot be less capable than the" + " default " + "(precision default/max=" + f.getDefaultMonetaryContext().getPrecision() + '/' + f.getMaximalMonetaryContext().getPrecision() + " for " + type.getName(),
if (f.getDefaultMonetaryContext().getPrecision() == 0) { f.setNumber(TestUtils.createNumberWithPrecision(5)); } else { f.setNumber(TestUtils.createNumberWithPrecision(f.getDefaultMonetaryContext().getPrecision()));
/** * For each MonetaryAmountFactory: Check if capabilities of default MonetaryContext are less, or equal * than Max * MonetaryContext. */ @Test(description = "4.2.6 Ensure MonetaryAmountFactory instances accessible for all amount types under test return " + "correct min/max MonetaryContext.") @SpecAssertion(section = "4.2.6", id = "426-A3") public void testMonetaryAmountFactoryMinMaxCapabilities() { for (Class type : Monetary.getAmountTypes()) { MonetaryAmountFactory<?> f = Monetary.getAmountFactory(type); MonetaryContext defCtx = f.getDefaultMonetaryContext(); MonetaryContext maxCts = f.getMaximalMonetaryContext(); assertTrue("Section 4.2.6: MonetaryAmountFactory default/max declares invalid precisions for " + type.getName(), maxCts.getPrecision() == 0 || defCtx.getPrecision() <= maxCts.getPrecision()); assertTrue( "Section 4.2.6: MonetaryAmountFactory default/max declares invalid scales for " + type.getName(), maxCts.getMaxScale() == -1 || defCtx.getMaxScale() <= maxCts.getMaxScale()); } }
if (f.getDefaultMonetaryContext().getPrecision() == 0) { f.setNumber(TestUtils.createNumberWithPrecision(5).negate()); } else { f.setNumber(TestUtils.createNumberWithPrecision(f.getDefaultMonetaryContext().getPrecision()) .negate());
/** * Call getFactory(),of a new MonetaryAmount instance * with a new monetary context(if possible-check the max context). The * instances must be non equal and have the same currency and number value. */ @SpecAssertion(section = "4.2.2", id = "422-B5") @Test(description = "4.2.2 For each amount class, check new amounts with explcit MonetaryContext.") public void testMonetaryAmountFactories_CreateWithMonetaryContext() { for (Class type : Monetary.getAmountTypes()) { MonetaryAmountFactory<MonetaryAmount> f = Monetary.getAmountFactory(type); MonetaryContext mc1 = f.getDefaultMonetaryContext(); MonetaryContext mc2 = f.getMaximalMonetaryContext(); MonetaryAmount m1; MonetaryAmount m2; if (mc1.equals(mc2)) { // In this cases both amount must be equals m1 = f.setCurrency("CHF").setContext(mc1).setNumber(10).create(); m2 = f.setCurrency("CHF").setContext(mc2).setNumber(10).create(); AssertJUnit.assertNotSame("Section 4.2.2: Expected non equal instances for " + type.getName(), m1, m2); } else { // In this cases both amount must be non equals m1 = f.setCurrency("CHF").setContext(mc1).setNumber(10).create(); m2 = f.setCurrency("CHF").setContext(mc2).setNumber(10).create(); AssertJUnit.assertNotSame("Section 4.2.2: Expected non equal instances for " + type.getName(), m1, m2); } AssertJUnit.assertTrue("Section 4.2.2: Expected equality for " + type.getName(), m1.equals(m1)); AssertJUnit.assertTrue("Section 4.2.2: Expected equality for " + type.getName(), m2.equals(m2)); } }
f.setNumber(TestUtils.createNumberWithPrecision(f.getDefaultMonetaryContext().getPrecision())); f.setCurrency("FooBar_foobar_fOobAr_foObaR"); f.create();
MonetaryContext defCtx = f.getDefaultMonetaryContext(); MonetaryContext maxCtx = f.getMaximalMonetaryContext(); MonetaryContext mc = f.setNumber(1).create().getContext();
MonetaryContext mc1 = f.getDefaultMonetaryContext(); MonetaryContext mc2 = f.getMaximalMonetaryContext(); MonetaryAmount m1;