/** * Access a MonetaryAmountFactory for each registered type. */ @Test(description = "4.2.6 Ensure MonetaryAmountFactory instances are accessible for all amount types under test.") @SpecAssertion(section = "4.2.6", id = "426-A1") public void testAccessToMonetaryAmountFactory() { for (Class type : Monetary.getAmountTypes()) { assertNotNull("Section 4.2.6: No MonetaryAmountFactory available for " + type.getName(), Monetary.getAmountFactory(type)); } }
/** * For each MonetaryAmountFactory: Check if getAmountType returns the correct type. */ @Test(description = "4.2.6 Ensure MonetaryAmountFactory instances accessible for all amount types under test return " + "correct amount type.") @SpecAssertion(section = "4.2.6", id = "426-A2") public void testMonetaryAmountFactoryReturnsCorrectType() { for (Class type : Monetary.getAmountTypes()) { AssertJUnit.assertEquals( "Section 4.2.6: MonetaryAmountFactory declares invalid amount type for " + type.getName(), type, Monetary.getAmountFactory(type).getAmountType()); } }
/** * Test divide(Double.NEGATIVE_INFINITY) function must return ZERO amount. */ @SpecAssertion(section = "4.2.2", id = "422-D16") @Test(description = "4.2.2 For each amount class, ensure divide(Double.NEGATIVE_INFINITY) return ZERO amount.") public void testDivideDoubleNEGATIVE_INFINITY() { for (Class type : Monetary.getAmountTypes()) { if (type.equals(TestAmount.class)) { continue; } MonetaryAmount mAmount1 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(10).create(); MonetaryAmount mActualResult = mAmount1.divide(Double.NEGATIVE_INFINITY); AssertJUnit.assertEquals("Section 4.2.2: ZERO amount expected on division by Double.POSITIVE_INFINITY, type was " + type.getName(), mActualResult, Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(0).create()); } }
/** * Test divide(0) function must return ZERO amount. */ @SpecAssertion(section = "4.2.2", id = "422-D16") @Test(description = "4.2.2 For each amount class, ensure divide(Double.POSITIVE_INFINITY) return ZERO amount.") public void testDivideDoublePOSITIVE_INFINITY() { for (Class type : Monetary.getAmountTypes()) { if (type.equals(TestAmount.class)) { continue; } MonetaryAmount mAmount1 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(10).create(); MonetaryAmount mActualResult = mAmount1.divide(Double.POSITIVE_INFINITY); AssertJUnit.assertEquals("Section 4.2.2: ZERO amount expected on division by Double.POSITIVE_INFINITY, type was " + type.getName(), mActualResult, Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(0).create()); } }
/** * Test remainder(null) must throw a NullPointerException */ @SpecAssertion(section = "4.2.2", id = "422-D21") @Test(description = "4.2.2 For each amount class, ensure remainder(Double.POSITIVE_INFINITY), throws ArithmeticException.") public void testRemainder_DoublePOSITIVE_INFINITY() { for (Class type : Monetary.getAmountTypes()) { if (type.equals(TestAmount.class)) { continue; } MonetaryAmount mAmount1 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(10).create(); MonetaryAmount mActualResult = mAmount1.remainder(Double.POSITIVE_INFINITY); AssertJUnit.assertEquals(Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(0).create(), mActualResult); } }
/** * Test remainder(null) must throw a NullPointerException */ @SpecAssertion(section = "4.2.2", id = "422-D21") @Test(description = "4.2.2 For each amount class, ensure remainder(Double.NEGATIVE_INFINITY), throws ArithmeticException.") public void testRemainder_DoubleNEGATIVE_INFINITY() { for (Class type : Monetary.getAmountTypes()) { if (type.equals(TestAmount.class)) { continue; } MonetaryAmount mAmount1 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(10).create(); MonetaryAmount mActualResult = mAmount1.remainder(Double.NEGATIVE_INFINITY); AssertJUnit.assertEquals(Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(0).create(), mActualResult); } }
/** * Checks if number type is not null. */ @SpecAssertion(section = "4.2.3", id = "423-A1") @Test(description = "4.2.3 Amount types do not return a NumberValue of null.") public void testReturningNumberValueIsNotNull() { for (Class type : Monetary.getAmountTypes()) { if (type.equals(TestAmount.class)) { continue; } MonetaryAmount mAmount1 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(10).create(); NumberValue result = mAmount1.getNumber(); AssertJUnit.assertNotNull("Section 4.2.3: Amount type does not return a NumberValue (null); " + type.getName(), result); } }
/** * Tests that add() correctly adds two values, using negative integers. */ @SpecAssertion(section = "4.2.2", id = "422-D1") @Test(description = "4.2.2 For each amount class, check m1.add(m2), m1 <0, m2<0.") public void testAddNegativeIntegers() { for (Class type : Monetary.getAmountTypes()) { if (type.equals(TestAmount.class)) { continue; } MonetaryAmount mAmount1 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(-10).create(); MonetaryAmount mAmount2 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(-20).create(); MonetaryAmount mActualResult = mAmount1.add(mAmount2); MonetaryAmount mExpectedResult = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(-30).create(); AssertJUnit.assertEquals("Section 4.2.2: Adding two simple ammounts", mExpectedResult, mActualResult); } }
/** * Tests that add() correctly adds two values, using fractions. */ @SpecAssertion(section = "4.2.2", id = "422-D1") @Test(description = "4.2.2 For each amount class, check m1.add(m2), m2 is fraction.") public void testAddPositiveFractions() { for (Class type : Monetary.getAmountTypes()) { if (type.equals(TestAmount.class)) { continue; } MonetaryAmount mAmount1 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(1.5).create(); MonetaryAmount mAmount2 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(2.85).create(); MonetaryAmount mActualResult = mAmount1.add(mAmount2); MonetaryAmount mExpectedResult = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(4.35).create(); AssertJUnit.assertEquals("Section 4.2.2: Adding two simple ammounts", mExpectedResult, mActualResult); } }
/** * Tests that subtract() correctly adds two values, using negative integers. */ @SpecAssertion(section = "4.2.2", id = "422-D6") @Test(description = "4.2.2 For each amount class, ensure correct subtraction of negative ints.") public void testSubtractNegativeIntegers() { for (Class type : Monetary.getAmountTypes()) { if (type.equals(TestAmount.class)) { continue; } MonetaryAmount mAmount1 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(-10).create(); MonetaryAmount mAmount2 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(-20).create(); MonetaryAmount mActualResult = mAmount1.subtract(mAmount2); MonetaryAmount mExpectedResult = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(10).create(); AssertJUnit.assertEquals("Section 4.2.2: Subtracting two simple ammounts", mExpectedResult, mActualResult); } }
/** * Tests that subtract() correctly adds two values, using fractions. */ @SpecAssertion(section = "4.2.2", id = "422-D6") @Test(description = "4.2.2 For each amount class, ensure correct subtraction of positive fractions.") public void testSubtractPositiveFractions() { for (Class type : Monetary.getAmountTypes()) { if (type.equals(TestAmount.class)) { continue; } MonetaryAmount mAmount1 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(1.5).create(); MonetaryAmount mAmount2 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(2.85).create(); MonetaryAmount mActualResult = mAmount1.subtract(mAmount2); MonetaryAmount mExpectedResult = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(-1.35).create(); AssertJUnit.assertEquals("Section 4.2.2: Adding two simple ammounts", mExpectedResult, mActualResult); } }
/** * Tests that add(0) should return itself. */ @SpecAssertion(section = "4.2.2", id = "422-D3") @Test(description = "4.2.2 For each amount class, ensure m2 = m1,add(0) -> m1==m2.") public void testAdd_Zero() { for (Class type : Monetary.getAmountTypes()) { if (type.equals(TestAmount.class)) { continue; } MonetaryAmount mAmount1 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(10).create(); MonetaryAmount mAmount2 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(0).create(); MonetaryAmount mActualResult = mAmount1.add(mAmount2); AssertJUnit.assertEquals("Section 4.2.2: Adding zero", mAmount1, mActualResult); } }
/** * Tests that subtract(0) should return itself. */ @SpecAssertion(section = "4.2.2", id = "422-D7") @Test(description = "4.2.2 For each amount class, ensure subtraction of 0 returns same instance.") public void testSubtract_Zero() { for (Class type : Monetary.getAmountTypes()) { if (type.equals(TestAmount.class)) { continue; } MonetaryAmount mAmount1 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(10).create(); MonetaryAmount mAmount2 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(0).create(); MonetaryAmount mActualResult = mAmount1.subtract(mAmount2); AssertJUnit.assertEquals("Section 4.2.2: Subtract zero", mAmount1, mActualResult); } }
/** * Tests that add() correctly adds two values, using positive integers. */ @SpecAssertion(section = "4.2.2", id = "422-D1") @Test(description = "4.2.2 For each amount class, check m1.add(m2), m1 >0, m2>0.") public void testAddPositiveIntegers() { for (Class type : Monetary.getAmountTypes()) { if (type.equals(TestAmount.class)) { continue; } MonetaryAmount mAmount1 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(10).create(); MonetaryAmount mAmount2 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(20).create(); MonetaryAmount mActualResult = mAmount1.add(mAmount2); MonetaryAmount mExpectedResult = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(30).create(); AssertJUnit.assertEquals( "Section 4.2.2: Adding two simple ammounts failed, " + mAmount1 + " + " + mAmount2 + " != " + mExpectedResult, mExpectedResult, mActualResult); } }
/** * Tests that subtract() correctly adds two values, using positive integers. */ @SpecAssertion(section = "4.2.2", id = "422-D6") @Test(description = "4.2.2 For each amount class, ensure correct subtraction of positive ints.") public void testSubtractPositiveIntegers() { for (Class type : Monetary.getAmountTypes()) { if (type.equals(TestAmount.class)) { continue; } MonetaryAmount mAmount1 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(10).create(); MonetaryAmount mAmount2 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(20).create(); MonetaryAmount mActualResult = mAmount1.subtract(mAmount2); MonetaryAmount mExpectedResult = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(-10).create(); AssertJUnit.assertEquals("Section 4.2.2: Subtracting two simple ammounts", mExpectedResult, mActualResult); } }
/** * Tests that add(), which results in an amount exceeding the max MonetaryContext throws * a MonetaryException. */ @SpecAssertion(section = "4.2.2", id = "422-D5") @Test(description = "4.2.2 For each amount class, ensure NullPointerException is thrown when calling m.add(null).") public void testAdd_Null() { for (Class type : Monetary.getAmountTypes()) { if (type.equals(TestAmount.class)) { continue; } MonetaryAmount mAmount1 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(10).create(); try { MonetaryAmount mActualResult = mAmount1.add(null); AssertJUnit.fail("Section 4.2.2: MonetaryAmount.add(null): NullPointerException expected"); } catch (NullPointerException ex) { // Expected } } }
/** * Creates an amount with the given scale. * @param scale the target scale * @return the new amount. */ public static MonetaryAmount createAmountWithScale(int scale) { MonetaryAmountFactoryQuery tgtContext = MonetaryAmountFactoryQueryBuilder.of().setMaxScale(scale).build(); MonetaryAmountFactory<?> exceedingFactory; try { exceedingFactory = Monetary.getAmountFactory(tgtContext); AssertJUnit.assertNotNull(exceedingFactory); MonetaryAmountFactory<? extends MonetaryAmount> bigFactory = Monetary.getAmountFactory(exceedingFactory.getAmountType()); return bigFactory.setCurrency("CHF").setNumber(createNumberWithScale(scale)).create(); } catch (MonetaryException e) { return null; } }
/** * Test divide() function allow to divide numbers. */ @SpecAssertion(section = "4.2.2", id = "422-D15") @Test(description = "4.2.2 For each amount class, ensure correct division.") public void testDivide() { for (Class type : Monetary.getAmountTypes()) { if (type.equals(TestAmount.class)) { continue; } MonetaryAmount m = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(10).create(); MonetaryAmount m2 = m.divide(10); AssertJUnit.assertEquals("Section 4.2.2: Currency not equal after division, type was " + type.getName(), DEFAULT_CURRENCY, m2.getCurrency().getCurrencyCode()); AssertJUnit.assertEquals("Section 4.2.2: Division result is not correct for " + type.getName(), 1, m2.getNumber().longValueExact()); } }
/** * Creates an amount with the given precision. * @param precision the target precision * @return a corresponding amount. */ public static MonetaryAmount createAmountWithPrecision(int precision) { MonetaryAmountFactoryQuery tgtContext = MonetaryAmountFactoryQueryBuilder.of().setPrecision(precision).build(); MonetaryAmountFactory<?> exceedingFactory; try { exceedingFactory = Monetary.getAmountFactory(tgtContext); AssertJUnit.assertNotNull(exceedingFactory); MonetaryAmountFactory<? extends MonetaryAmount> bigFactory = Monetary.getAmountFactory(exceedingFactory.getAmountType()); return bigFactory.setCurrency("CHF").setNumber(createNumberWithPrecision(precision)).create(); } catch (MonetaryException e) { return null; } } }
/** * @param args */ public static void main(String[] args) { MonetaryAmount amount = Monetary.getDefaultAmountFactory().setCurrency("EUR").setNumber(234).create(); ConsoleUtils.printDetails(amount); amount = Monetary.getAmountFactory(FastMoney.class).setCurrency("EUR").setNumber(234).create(); ConsoleUtils.printDetails(amount); amount = Monetary.getAmountFactory( MonetaryAmountFactoryQueryBuilder.of().setMaxScale(50).setPrecision(30).build()) .setCurrency("EUR").setNumber(234).create(); ConsoleUtils.printDetails(amount); Money amt1 = Money.of(10.1234556123456789, "USD"); FastMoney amt2 = FastMoney.of(123456789, "USD"); Money total = amt1.add(amt2).multiply(0.5) .remainder(1); ConsoleUtils.printDetails(total); }