/** * 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; }
/** * 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()); } }
/** * 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); } }
MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(locale); for (MonetaryAmountFactory fact : Monetary.getAmountFactories()) { if (fact.getAmountType().equals(TestAmount.class)) { continue;
if (fact.getAmountType().equals(TestAmount.class)) { continue;
MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(locale); for (MonetaryAmountFactory fact : Monetary.getAmountFactories()) { if (fact.getAmountType().equals(TestAmount.class)) { continue;
/** * Ensure correct query function implementations, providing also * the some test implementations with the TCK. */ @Test(description = "4.2.7 Ensure correct query function, Monetary.getAmountFactories should return factory" + "for explicit acquired amount types.") @SpecAssertion(section = "4.2.7", id = "427-B4") public void testAmountQueryType() { MonetaryAmountFactoryQuery ctx = MonetaryAmountFactoryQueryBuilder.of().setTargetType(TestAmount.class).build(); Collection<MonetaryAmountFactory<?>> factories = Monetary.getAmountFactories(ctx); AssertJUnit.assertNotNull("Section 4.2.7: Amount factory query should return explicitly queried factories", factories); boolean found = false; for (MonetaryAmountFactory<?> f : factories) { if (f.getAmountType().equals(TestAmount.class)) { found = true; break; } } AssertJUnit.assertTrue("Section 4.2.7: Amount type query should return same explicitly queried factory", found); ctx = MonetaryAmountFactoryQueryBuilder.of().build(); MonetaryAmountFactory<?> factory = Monetary.getAmountFactory(ctx); AssertJUnit.assertNotNull("Section 4.2.7: Amount type must be provided", factory); }
/** * 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; } }
/** * 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; } } }