diff options
author | Patrick Bänziger | 2014-12-04 14:38:06 +0000 |
---|---|---|
committer | Judith Gull | 2014-12-05 15:40:59 +0000 |
commit | 129506d23267f3bc09d27712d5cac6eb10fd209b (patch) | |
tree | 8286cdc5b0d9c7b46f6c28cccde34a0f10e15cac | |
parent | 595737f60bb78deb72c846f6673ae6195ea25d91 (diff) | |
download | org.eclipse.scout.rt-129506d23267f3bc09d27712d5cac6eb10fd209b.tar.gz org.eclipse.scout.rt-129506d23267f3bc09d27712d5cac6eb10fd209b.tar.xz org.eclipse.scout.rt-129506d23267f3bc09d27712d5cac6eb10fd209b.zip |
Bug 402298: Extend unittests for CollectionUtility
Change-Id: I35fdf17af1d26b7eceeae345c18f5a04ac7db754
Signed-off-by: Patrick Bänziger <patrick.baenziger@bsiag.com>
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=402298
Reviewed-on: https://git.eclipse.org/r/37578
Reviewed-by: Judith Gull <jgu@bsiag.com>
Tested-by: Judith Gull <jgu@bsiag.com>
-rw-r--r-- | org.eclipse.scout.commons.test/src/org/eclipse/scout/commons/CollectionUtilityTest.java | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/org.eclipse.scout.commons.test/src/org/eclipse/scout/commons/CollectionUtilityTest.java b/org.eclipse.scout.commons.test/src/org/eclipse/scout/commons/CollectionUtilityTest.java index 3c1e1af73d..08f42c9a7d 100644 --- a/org.eclipse.scout.commons.test/src/org/eclipse/scout/commons/CollectionUtilityTest.java +++ b/org.eclipse.scout.commons.test/src/org/eclipse/scout/commons/CollectionUtilityTest.java @@ -11,7 +11,11 @@ package org.eclipse.scout.commons; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; import java.util.ArrayList; import java.util.Arrays; @@ -214,6 +218,67 @@ public class CollectionUtilityTest { assertEquals(2, CollectionUtility.size(createList("1", "2"))); } + @Test + public void testContainsAny() { + // Test for single valued list + assertTrue(CollectionUtility.containsAny(createList(2L, 1L), createList(1L))); + assertFalse(CollectionUtility.containsAny(createList(2L, 3L), createList(1L))); + // Test for null collections + assertFalse(CollectionUtility.containsAny(createList(2L, 3L), (Collection<?>) null)); + assertFalse(CollectionUtility.containsAny(null, createList(2L, 3L))); + assertFalse(CollectionUtility.containsAny((Collection<Long>) null, (Collection<Long>) null)); + // Test for null elements + assertTrue(CollectionUtility.containsAny(createList(null, 1L), createList(new Object[]{null}))); + } + + @Test + public void testContainsAnyArray() { + // Test for single valued list + assertTrue(CollectionUtility.containsAny(createList(2L, 1L), new Object[]{1L})); + assertFalse(CollectionUtility.containsAny(createList(2L, 3L), new Object[]{1L})); + // Test for null collections + assertFalse(CollectionUtility.containsAny(createList(2L, 3L), (Object[]) null)); + assertFalse(CollectionUtility.containsAny(null, new Object[]{2L, 3L})); + assertFalse(CollectionUtility.containsAny((Collection<Object>) null, (Object[]) null)); + + // Test for null elements + assertTrue(CollectionUtility.containsAny(createList(null, 1L), createList(new Object[]{null}))); + } + + @Test + public void testGetElement() { + // Test that the item is returned from the position - not a copy or similar + String item = "1"; + assertSame(item, CollectionUtility.getElement(createList("1", item, "1"), 1)); + // Test out of bounds + assertNull(CollectionUtility.getElement(createList(1L, 2L, 3L), -1)); + assertNull(CollectionUtility.getElement(createList(1L, 2L, 3L), 3)); + } + + @Test + public void testTruncateList() { + List<Object> list = Collections.unmodifiableList(createList(1L, 2L, 3L, 4L, 5L)); + ArrayList<Object> newList = CollectionUtility.truncateList(list, 3); + assertNotNull(newList); + assertEquals(3, newList.size()); + assertEquals(1L, list.get(0)); + assertEquals(2L, list.get(1)); + assertEquals(3L, list.get(2)); + + // Test size is equal to actual size + newList = CollectionUtility.truncateList(list, 5); + assertEquals(list, newList); // Compare collection + + // Test max size larger than actual size + newList = CollectionUtility.truncateList(list, 6); + assertEquals(list, newList); // Compare collection + + // Test null + newList = CollectionUtility.truncateList(null, 1); + assertNotNull(newList); + assertTrue(newList.isEmpty()); + } + private List<Object> createList(Object... elements) { List<Object> list = new ArrayList<Object>(); for (Object o : elements) { |