/** * Gets the amount in major units as a {@code long}. * <p> * This returns the monetary amount in terms of the major units of the * currency, truncating the amount if necessary. For example, 'EUR 2.35' * will return 2, and 'BHD -1.345' will return -1. * <p> * This method matches the API of {@link java.math.BigDecimal}. * * @return the major units part of the amount * @throws ArithmeticException if the amount is too large for a {@code long} */ @Override public Long queryFrom(MonetaryAmount amount) { Objects.requireNonNull(amount, "Amount required."); return amount.with(downRounding).getNumber().longValueExact(); } }
AssertJUnit.assertTrue( "Section 4.2.2: Multiplication with 1.5 does not return correct value for " + type.getName(), mActualResult.getNumber().longValueExact() == 15); mActualResult = mAmount1.multiply(Double.valueOf(-1.5)); AssertJUnit.assertTrue( "Section 4.2.2: Multiplication with -3 does not return correct value for " + type.getName(), mActualResult.getNumber().longValueExact() == -15); mActualResult = mAmount1.multiply(0.0); AssertJUnit.assertTrue( "Section 4.2.2: Multiplication with 0 does not return correct value for " + type.getName(), mActualResult.getNumber().longValueExact() == 0); mActualResult = mAmount1.multiply(1.0); AssertJUnit.assertTrue( "Section 4.2.2: Multiplication with 0 does not return correct value for " + type.getName(), mActualResult.getNumber().longValueExact() == 10); AssertJUnit.assertTrue( "Section 4.2.2: Multiplication with 0 does not return identity value for " + type.getName(),
AssertJUnit.assertTrue( "Section 4.2.2: Multiplication with 2 does not return correct value for " + type.getName(), mActualResult.getNumber().longValueExact() == 20); mActualResult = mAmount1.multiply(Double.valueOf(-3)); AssertJUnit.assertTrue( "Section 4.2.2: Multiplication with -3 does not return correct value for " + type.getName(), mActualResult.getNumber().longValueExact() == -30); mActualResult = mAmount1.multiply(BigDecimal.ONE); AssertJUnit.assertTrue( "Section 4.2.2: Multiplication with 1 does not return correct value for " + type.getName(), mActualResult.getNumber().longValueExact() == 10); AssertJUnit.assertTrue( "Section 4.2.2: Multiplication with 1 does not return identity value for " + type.getName(),
private long centAmountOf(final MonetaryAmount totalPrice) { return totalPrice.multiply(100).getNumber().longValueExact(); }
type.getName(), 0L, result.longValue()); try { result.longValueExact(); AssertJUnit.fail("Section 4.2.3: Number value (long, exact) should throw ArithmeticException for " + num + ", type; " +
/** * 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()); } }
(long) num, result.intValue()); try { result.longValueExact(); AssertJUnit .fail("Section 4.2.3: Number value (long, exact -> truncated) must throw ArithemticException on truncation for " +
AssertJUnit .assertEquals("Section 4.2.3: Exact number (long) (double) returned is not correct for " + type.getName(), num, result.longValueExact());
(long) num, result.intValue()); try { result.longValueExact(); AssertJUnit .fail("Section 4.2.3: Number value (long, exact -> truncated) must throw ArithemticException on truncation for " +
result.longValue()); AssertJUnit.assertEquals("Section 4.2.3: Exact number (long) returned is not correct for " + type.getName(), num, result.longValueExact());
/** * Test successful conversion for possible currency pairs.<p> * Hint: you may only check for rate factory, when using a hardcoded ExchangeRateProvider, such a provider * must be also implemented and registered as an SPI. */ @Test(description = "4.3.2 Test successful conversion for CHF -> FOO, using TestRateProvider.") @SpecAssertion(id = "432-A1", section = "4.3.2") public void testConversion() { CurrencyUnit cu = new TestCurrencyUnit("FOO"); CurrencyConversion conv = MonetaryConversions.getConversion(cu, "TestRateProvider"); MonetaryAmount m = Monetary.getDefaultAmountFactory().setNumber(10).setCurrency("CHF").create(); MonetaryAmount m2 = m.with(conv); AssertJUnit.assertEquals(m2.getCurrency().getCurrencyCode(), "FOO"); AssertJUnit.assertEquals(20L, m2.getNumber().longValueExact()); m2 = m.with(conv); AssertJUnit.assertEquals(m2.getCurrency().getCurrencyCode(), "FOO"); AssertJUnit.assertEquals(20L, m2.getNumber().longValueExact()); }