/** * Replace a property defined within a message with a bounded number of elements.<br> * In addition, if a counter appears in the message, it will be replaced with the elements size:<br> * <ul> * <li>The elements' size property name is expected to be {propertyName}_COUNTER</li> * </ul> * * @param propertyName * the property name which represents the collection. * @param items * the collection of items to be shown in the message. * @return a mutable collection contains two elements:<br> * <ul> * <li>The property name and its replacement items.</li> * <li>The property counter name and the items size.</li> * </ul> */ public static Collection<String> replaceWith(String propertyName, Collection<?> items) { return replaceWith(propertyName, items, DEFAULT_SEPARATOR, DEFAULT_MAX_NUMBER_OF_PRINTED_ITEMS); }
/** * Replace a property defined within a message with a bounded number of elements.<br> * In addition, if a counter appears in the message, it will be replaced with the elements size:<br> * <ul> * <li>The elements' size property name is expected to be {propertyName}_COUNTER</li> * </ul> * * @param propertyName * the property name which represents the collection. * @param items * the collection of items to be shown in the message. * @param separator * the separator that will separate between the elements. * @return a mutable collection contains two elements:<br> * <ul> * <li>The property name and its replacement items.</li> * <li>The property counter name and the items size.</li> * </ul> */ public static Collection<String> replaceWith(String propertyName, Collection<?> items, String separator) { return replaceWith(propertyName, items, separator, DEFAULT_MAX_NUMBER_OF_PRINTED_ITEMS); }
@Override public List<String> render() { final List<String> violationMessages = new ArrayList<>(); violationMessages.add(violationName); violationMessages.addAll(ReplacementUtils.replaceWith(violationName + LIST_SUFFIX, violatingEntities)); return violationMessages; }
@Override public List<String> render() { final List<String> result = new ArrayList<>(details.size()); for (Entry<String, Set<String>> detailEntry : details.entrySet()) { final String detailName = detailEntry.getKey(); final Set<String> detailValues = detailEntry.getValue(); result.addAll(ReplacementUtils.replaceWith(detailName, detailValues)); } return result; } }
public static Collection<String> getListVariableAssignmentString(EngineMessage engineMessage, Collection<?> values) { return ReplacementUtils.replaceWith(ReplacementUtils.getListVariableName(engineMessage), values); }
/** * Replace a property defined within a message with a bounded number of elements of {@link Nameable}.<br> * In addition, if a counter appears in the message, it will be replaced with the elements size:<br> * <ul> * <li>The elements' size property name is expected to be {propertyName}_COUNTER</li> * </ul> * * @param propertyName * the property name which represents the collection * @param items * the collection of items to be shown in the message * @return a mutable collection contains two elements:<br> * <ul> * <li>The property name and its replacement items.</li> * <li>The property counter name and the items size.</li> * </ul> */ public static <T extends Nameable> Collection<String> replaceWithNameable(String propertyName, Collection<T> items) { List<Object> printedItems = new ArrayList<>(items.size()); for (Nameable itemName : items) { printedItems.add(itemName.getName()); } return replaceWith(propertyName, printedItems); }
@Test public void replaceWithEmptyCollection() { Collection<String> replacements = ReplacementUtils.replaceWith(PROPERTY_NAME, Collections.emptyList()); validateReplacementsContainsExpectedProperties(replacements, Collections.emptyList()); }
@Test public void replaceWithSingleItem() { List<Object> items = Collections.singletonList(PROPERTY_VALUE); validateReplacements(ReplacementUtils.replaceWith(PROPERTY_NAME, items), items); }
@Test public void separatorNotEmpty() { List<Object> items = createItems(); String separator = ""; assertThrows(IllegalArgumentException.class, () -> ReplacementUtils.replaceWith(PROPERTY_NAME, items, separator , 5)); }
@Test public void failZeroValuesToShow() { List<Object> items = createItems(); String separator = ", "; assertThrows(IllegalArgumentException.class, () -> ReplacementUtils.replaceWith(PROPERTY_NAME, items, separator , 0)); }
@Test public void failNegativeNumOfValuesToShow() { List<Object> items = createItems(); String separator = ", "; assertThrows(IllegalArgumentException.class, () -> ReplacementUtils.replaceWith(PROPERTY_NAME, items, separator , -5)); }
@Test public void replaceWithNullItem() { List<Object> items = Collections.singletonList(null); Collection<String> replacements = ReplacementUtils.replaceWith(PROPERTY_NAME, items); validateReplacementsContainsExpectedProperties(replacements, items); assertTrue(validateReplacementContains(replacements, "null")); }
@Test public void separatorNotNull() { List<Object> items = createItems(); String separator = null; assertThrows(IllegalArgumentException.class, () -> ReplacementUtils.replaceWith(PROPERTY_NAME, items, separator, 5)); }
@Test public void replaceWithCustomSeparator() { List<Object> items = createItems(); String separator = ","; Collection<String> replacements = ReplacementUtils.replaceWith(PROPERTY_NAME, items, separator); assertTrue(validateReplacementElementCount(replacements, separator, ReplacementUtils.DEFAULT_MAX_NUMBER_OF_PRINTED_ITEMS)); }
@Test public void containLowerThanDefaultNumberOfElements() { List<Object> items = createItems(); String separator = "sep"; // Less than the default number of elements to show. int numOfElementsToShow = 3; Collection<String> replacements = ReplacementUtils.replaceWith(PROPERTY_NAME, items, separator , numOfElementsToShow); assertTrue(validateReplacementElementCount(replacements, separator, numOfElementsToShow)); }
@Test public void replaceWithMoreThanMaxItems() { List<Object> items = createItems(); Collection<String> replacements = ReplacementUtils.replaceWith(PROPERTY_NAME, items); validateReplacementsContainsExpectedProperties(replacements, items); validateReplacementsDoNotContainUnexpectedItems(replacements, items); }
@Test public void containBiggerThanDefaultNumberOfElements() { List<Object> items = createItems(); String separator = "sep"; // More than the default number of elements to show. int numOfElementsToShow = 8; Collection<String> replacements = ReplacementUtils.replaceWith(PROPERTY_NAME, items, separator , numOfElementsToShow); assertTrue(validateReplacementElementCount(replacements, separator, numOfElementsToShow)); }