@Override public MonetaryAmount parse(String text, Locale locale) throws ParseException { CurrencyUnit currencyUnit = Monetary.getCurrency(locale); Number numberValue = this.numberFormatter.parse(text, locale); return Monetary.getDefaultAmountFactory().setNumber(numberValue).setCurrency(currencyUnit).create(); } }
/** * 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)); } }
/** * Print several amounts, created using the default factory, but * also a test instance, provided by the TCK, to ensure no * implementation * dependencies on the implementation. */ @SpecAssertion(section = "4.4.1", id = "441-A2") @Test(description = "4.4.1 Formats amounts using all available locales.") public void testFormattingIsIndependentOfImplementation() { for (Locale locale : MonetaryFormats.getAvailableLocales()) { MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(locale); Set<String> formatsProcuced = new HashSet<>(); for (MonetaryAmountFactory fact : Monetary.getAmountFactories()) { if (fact.getAmountType().equals(TestAmount.class)) { continue; } MonetaryAmount amt = fact.setCurrency("USD").setNumber(10.5).create(); String formatProduced = format.format(amt); assertNotNull(formatProduced, "No MonetaryAmountFormat returned from MonetaryFormats." + "getMonetaryFormat(Locale,String...) with supported Locale: " + locale); assertFalse(formatProduced.isEmpty(), "MonetaryAmountFormat returned empty String for " + amt); formatsProcuced.add(formatProduced); } assertFalse(formatsProcuced.isEmpty(), "No formatted amount available. Are there no amount?"); assertFalse(formatsProcuced.size() > 1, "Formatter produces different output for different amount classes(+" + format.getClass() + "): " + formatsProcuced); } }
@Override public MonetaryAmount apply(MonetaryAmount value) { return value.getFactory().setCurrency(term).create(); }
if (f.getDefaultMonetaryContext().getPrecision() == 0) { f.setNumber(TestUtils.createNumberWithPrecision(5).negate()); } else { f.setNumber(TestUtils.createNumberWithPrecision(f.getDefaultMonetaryContext().getPrecision()) .negate()); f.create(); AssertJUnit.fail("Section 4.2.6: MonetaryAmountFactory must throw a MonetaryException, " + "when an amount without a currency is" +
/** * Uses an arbitrary {@link MonetaryAmount} to initialize this factory. Properties reused are: * <ul> * <li>CurrencyUnit,</li> * <li>Number value,</li> * <li>MonetaryContext.</li> * </ul> * * @param amount the amount to be used, not {@code null}. * @return this factory instance, for chaining. * @throws MonetaryException when the {@link MonetaryContext} implied by {@code amount.getContext()} * exceeds the capabilities supported by this factory type. */ default MonetaryAmountFactory<T> setAmount(MonetaryAmount amount) { setCurrency(amount.getCurrency()); setNumber(amount.getNumber()); setContext(amount.getContext()); return this; }
f.setCurrency("USD"); MonetaryContext maxCtx = f.getMaximalMonetaryContext(); if (maxCtx.getPrecision() != 0) { f.setContext(MonetaryContextBuilder.of(MonetaryAmount.class).setPrecision(maxCtx.getPrecision() + 1) .build()); AssertJUnit.fail("Section 4.2.6: Factory should throw MonetaryException for invalid context " + f.setContext(MonetaryContextBuilder.of(MonetaryAmount.class).setMaxScale(maxCtx.getMaxScale() + 1) .build()); AssertJUnit.fail("Section 4.2.6: Factory should throw MonetaryException for invalid context " +
/** * Sets the {@link CurrencyUnit} to be used. * * @param currencyCode the currencyCode of the currency to be used, not {@code null}. The currency code * will be resolved using {@link Monetary#getCurrency(String, String...)}. * @return This factory instance, for chaining. * @throws UnknownCurrencyException if the {@code currencyCode} is not resolvable. */ default MonetaryAmountFactory<T> setCurrency(String currencyCode) { return setCurrency(Monetary.getCurrency(currencyCode)); }
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(),
/** * Executes the query and returns the {@link javax.money.MonetaryAmount} implementation type found, * if there is only one type. * If multiple types match the query, the first one is selected. * * @param query the factory query, not null. * @return the type found, or null. */ default Class<? extends MonetaryAmount> getAmountType(MonetaryAmountFactoryQuery query) { MonetaryAmountFactory<?> f = getAmountFactory(query); if (f != null) { return f.getAmountType(); } return null; }
factories.forEach(f -> f.setContext(context)); selection.sort(CONTEXT_COMPARATOR); factories.add(selection.get(0).createMonetaryAmountFactory());
/** * 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(); }
/** * 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)); } }
/** * 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; } }
@Override public MonetaryAmount apply(MonetaryAmount amount) { Objects.requireNonNull(amount, "Amount required."); return amount.getFactory().setCurrency(currency).create(); }
if (f.getDefaultMonetaryContext().getPrecision() == 0) { f.setNumber(TestUtils.createNumberWithPrecision(5)); } else { f.setNumber(TestUtils.createNumberWithPrecision(f.getDefaultMonetaryContext().getPrecision())); f.create(); AssertJUnit.fail("Section 4.2.6: MonetaryAmountFactory must throw a MonetaryException, " + "when a positive amount without a currency" +
/** * 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()); } }
/** * 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()); } }