Moti Asayag has uploaded a new change for review. Change subject: engine: Replace return type of ReplacementUtils ......................................................................
engine: Replace return type of ReplacementUtils To ease usability of ReplacementUtils.replaceWith* return value, it is changed from a static array into a Collection. Change-Id: If0733f3bb5fec70896a685452125c67d1f14784f Signed-off-by: Moti Asayag <[email protected]> --- M backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ReplacementUtils.java M backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/ReplacementUtilsTest.java 2 files changed, 36 insertions(+), 21 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/61/10761/1 diff --git a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ReplacementUtils.java b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ReplacementUtils.java index 87f54f8..02531ad 100644 --- a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ReplacementUtils.java +++ b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ReplacementUtils.java @@ -2,6 +2,8 @@ import java.text.MessageFormat; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; import java.util.List; import org.apache.commons.lang.StringUtils; @@ -13,16 +15,19 @@ /** * 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. + * 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 an array of two elements contains the property name and its replacement items and a property for its + * @return a collection of two elements contains the property name and its replacement items and a property for its * total size. */ - public static String[] replaceWith(String propertyName, List<Object> items) { + public static Collection<String> replaceWith(String propertyName, List<Object> items) { int size = Math.min(MAX_NUMBER_OF_PRINTED_ITEMS, items.size()); List<String> printedItems = new ArrayList<String>(size); @@ -34,22 +39,26 @@ printedItems.add("\t..."); } - return new String[] { MessageFormat.format("${0} {1}", propertyName, StringUtils.join(printedItems, ",\n")), - MessageFormat.format("${0}_COUNTER {1}", propertyName, items.size()) }; + return new ArrayList<String>( + Arrays.asList(MessageFormat.format("${0} {1}", propertyName, StringUtils.join(printedItems, ",\n")), + MessageFormat.format("${0}_COUNTER {1}", propertyName, items.size()))); } /** * 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. + * 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 an array of two elements contains the property name and its replacement items and a property for its + * @return a collection of two elements contains the property name and its replacement items and a property for its * total size. */ - public static <T extends Nameable> String[] replaceWithNameable(String propertyName, List<T> items) { + public static <T extends Nameable> Collection<String> replaceWithNameable(String propertyName, List<T> items) { List<Object> printedItems = new ArrayList<Object>(items.size()); for (Nameable itemName : items) { diff --git a/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/ReplacementUtilsTest.java b/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/ReplacementUtilsTest.java index f90ae55..6e3b57f 100644 --- a/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/ReplacementUtilsTest.java +++ b/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/ReplacementUtilsTest.java @@ -5,7 +5,9 @@ import static org.junit.Assert.assertTrue; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; +import java.util.Iterator; import java.util.List; import org.junit.Test; @@ -20,7 +22,7 @@ @Test public void replaceWithSingleItem() { List<Object> items = Collections.<Object> singletonList(PROPERTY_VALUE); - String[] messageItems = ReplacementUtils.replaceWith(PROPERTY_NAME, items); + Collection<String> messageItems = ReplacementUtils.replaceWith(PROPERTY_NAME, items); validateMessageItems(messageItems, items); } @@ -35,23 +37,24 @@ }; List<Nameable> items = Collections.<Nameable> singletonList(item); - String[] messageItems = ReplacementUtils.replaceWithNameable(PROPERTY_NAME, items); + Collection<String> messageItems = ReplacementUtils.replaceWithNameable(PROPERTY_NAME, items); validateMessageItems(messageItems, items); } @Test public void replaceWithEmptyCollection() { - String[] messageItems = ReplacementUtils.replaceWith(PROPERTY_NAME, Collections.emptyList()); + Collection<String> messageItems = ReplacementUtils.replaceWith(PROPERTY_NAME, Collections.emptyList()); validateMessageContainsProperties(messageItems); } @Test public void replaceWithMoreThanMaxItems() { List<Object> items = createItems(); - String[] messageItems = ReplacementUtils.replaceWith(PROPERTY_NAME, items); + Collection<String> messageItems = ReplacementUtils.replaceWith(PROPERTY_NAME, items); validateMessageContainsProperties(messageItems); - validateMessageDoesNotContainUnexpectedItems(messageItems[0], items); - assertTrue(messageItems[1].contains(String.valueOf(items.size()))); + Iterator<String> messageIterator = messageItems.iterator(); + validateMessageDoesNotContainUnexpectedItems(messageIterator.next(), items); + assertTrue(messageIterator.next().contains(String.valueOf(items.size()))); } private void validateMessageDoesNotContainUnexpectedItems(String message, List<Object> items) { @@ -74,16 +77,19 @@ return PROPERTY_NAME + String.valueOf(id); } - private void validateMessageContainsProperties(String[] messageItems) { + private void validateMessageContainsProperties(Collection<String> messageItems) { assertNotNull(messageItems); - assertTrue(messageItems.length > 0); - assertTrue(messageItems[0].contains(PROPERTY_NAME)); - assertTrue(messageItems[1].contains(PROPERTY_COUNTER_NAME)); + assertFalse(messageItems.isEmpty()); + assertTrue(messageItems.size() == 2); + Iterator<String> messageIterator = messageItems.iterator(); + assertTrue(messageIterator.next().contains(PROPERTY_NAME)); + assertTrue(messageIterator.next().contains(PROPERTY_COUNTER_NAME)); } - private <T> void validateMessageItems(String[] messageItems, List<T> items) { + private <T> void validateMessageItems(Collection<String> messageItems, List<T> items) { validateMessageContainsProperties(messageItems); - assertTrue(messageItems[0].contains(PROPERTY_VALUE)); - assertTrue(messageItems[1].contains(String.valueOf(items.size()))); + Iterator<String> messageIterator = messageItems.iterator(); + assertTrue(messageIterator.next().contains(PROPERTY_VALUE)); + assertTrue(messageIterator.next().contains(String.valueOf(items.size()))); } } -- To view, visit http://gerrit.ovirt.org/10761 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: If0733f3bb5fec70896a685452125c67d1f14784f Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Moti Asayag <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
