From b78b81bc46222d70040d2a27ef2799f3f47a4af6 Mon Sep 17 00:00:00 2001 From: dota17 <50514813+dota17@users.noreply.github.com> Date: Mon, 4 Nov 2019 07:58:39 +0800 Subject: [PATCH 1/2] Add junit for getWithNull and subList in the LazyListTest. (#103) * Add junit for getWithNull and subList in the LazyListTest. * Add junit for add, addAll, remove, subList, isFull and maxSize in the FixedSizeListTest. * Add junit for subList in the PredicatedListTest. * Add junit for subList in the TransformedListTest. * Remove the kinds of //---... lines. --- .../collections4/list/FixedSizeListTest.java | 60 +++++++++++++++++++ .../collections4/list/LazyListTest.java | 48 +++++++++++++++ .../collections4/list/PredicatedListTest.java | 22 +++++++ .../list/TransformedListTest.java | 24 ++++++++ 4 files changed, 154 insertions(+) diff --git a/src/test/java/org/apache/commons/collections4/list/FixedSizeListTest.java b/src/test/java/org/apache/commons/collections4/list/FixedSizeListTest.java index 0c112ffbb..2eda80f73 100644 --- a/src/test/java/org/apache/commons/collections4/list/FixedSizeListTest.java +++ b/src/test/java/org/apache/commons/collections4/list/FixedSizeListTest.java @@ -83,4 +83,64 @@ public class FixedSizeListTest extends AbstractListTest { Assert.assertEquals("Modifying an the underlying list is allowed", sizeBefore + 1, fixedSizeList.size()); } + + private FixedSizeList initFixedSizeList() { + List decoratedList = new ArrayList<>(); + decoratedList.add("item 1"); + decoratedList.add("item 2"); + // + return FixedSizeList.fixedSizeList(decoratedList); + } + + public void testAdd() { + FixedSizeList fixedSizeList = initFixedSizeList(); + + try { + fixedSizeList.add(2,"New Value"); + fail(); + } catch (UnsupportedOperationException ex) {} + } + + + public void testAddAll() { + FixedSizeList fixedSizeList = initFixedSizeList(); + + List addList = new ArrayList<>(); + addList.add("item 3"); + addList.add("item 4"); + + try { + fixedSizeList.addAll(2, addList); + fail(); + } catch (UnsupportedOperationException ex) {} + } + + public void testRemove() { + FixedSizeList fixedSizeList = initFixedSizeList(); + + try { + fixedSizeList.remove(1); + fail(); + } catch (UnsupportedOperationException ex) {} + } + + public void testSubList() { + FixedSizeList fixedSizeList = initFixedSizeList(); + + List subFixedSizeList = fixedSizeList.subList(1, 1); + Assert.assertNotNull(subFixedSizeList); + Assert.assertEquals(0, subFixedSizeList.size()); + } + + public void testIsFull() { + FixedSizeList fixedSizeList = initFixedSizeList(); + + Assert.assertTrue(fixedSizeList.isFull()); + } + + public void testMaxSize() { + FixedSizeList fixedSizeList = initFixedSizeList(); + + Assert.assertEquals(2, fixedSizeList.maxSize()); + } } diff --git a/src/test/java/org/apache/commons/collections4/list/LazyListTest.java b/src/test/java/org/apache/commons/collections4/list/LazyListTest.java index 26adbe02a..1403901f6 100644 --- a/src/test/java/org/apache/commons/collections4/list/LazyListTest.java +++ b/src/test/java/org/apache/commons/collections4/list/LazyListTest.java @@ -98,6 +98,54 @@ public class LazyListTest extends AbstractObjectTest { assertNotNull(fourthElement); } + public void testGetWithNull() { + final List hours = Arrays.asList(7, 5, 8, 2); + final Transformer transformer = input -> LocalDateTime.now().withHour(hours.get(input)); + List list = new LazyList<>(new ArrayList<>(), transformer); + LocalDateTime fourthElement = list.get(3); + assertFalse(list.isEmpty()); + assertNotNull(fourthElement); + list.remove(3); + list.add(3,null); + fourthElement = list.get(3); + assertNotNull(fourthElement); + } + public void testSubListWitheFactory() { + final Factory dateFactory = LocalDateTime::now; + List list = new LazyList<>(new ArrayList<>(), dateFactory); + LocalDateTime fourthElement = list.get(3); + assertFalse(list.isEmpty()); + assertNotNull(fourthElement); + testSubList(list); + } + + public void testSubListWithTransformer() { + final List hours = Arrays.asList(7, 5, 8, 2); + final Transformer transformer = input -> LocalDateTime.now().withHour(hours.get(input)); + List list = new LazyList<>(new ArrayList<>(), transformer); + LocalDateTime fourthElement = list.get(3); + assertFalse(list.isEmpty()); + assertNotNull(fourthElement); + testSubList(list); + } + + private void testSubList(List list) { + List subList = list.subList(1, 3); + assertFalse(subList.isEmpty()); + assertNotNull(subList); + assertEquals(2, subList.size()); + + subList = list.subList(0, 1); + assertFalse(subList.isEmpty()); + assertEquals(1, subList.size()); + + subList = list.subList(1, 1); + assertTrue(subList.isEmpty()); + + subList = list.subList(0, list.size()); + assertFalse(subList.isEmpty()); + assertEquals(list.size(), subList.size()); + } } diff --git a/src/test/java/org/apache/commons/collections4/list/PredicatedListTest.java b/src/test/java/org/apache/commons/collections4/list/PredicatedListTest.java index 8ef6a413e..478124f02 100644 --- a/src/test/java/org/apache/commons/collections4/list/PredicatedListTest.java +++ b/src/test/java/org/apache/commons/collections4/list/PredicatedListTest.java @@ -130,6 +130,28 @@ public class PredicatedListTest extends AbstractListTest { list.contains("three")); } + public void testSubList() { + final List list = makeTestList(); + list.add((E) "zero"); + //subList without any element of list + List subList = list.subList(0, 0); + assertNotNull(subList); + assertEquals(0, subList.size()); + + //subList with one element oif list + subList = list.subList(0, 1); + assertEquals(1, subList.size()); + + final List elements = new ArrayList<>(); + elements.add((E) "one"); + elements.add((E) "two"); + elements.add((E) "three"); + list.addAll(1, elements); + //subList with all elements of list + subList = list.subList(0, list.size()); + assertEquals(list.size(), subList.size()); + } + @Override public String getCompatibilityVersion() { return "4"; diff --git a/src/test/java/org/apache/commons/collections4/list/TransformedListTest.java b/src/test/java/org/apache/commons/collections4/list/TransformedListTest.java index 10d28ebd4..1ef159618 100644 --- a/src/test/java/org/apache/commons/collections4/list/TransformedListTest.java +++ b/src/test/java/org/apache/commons/collections4/list/TransformedListTest.java @@ -127,6 +127,30 @@ public class TransformedListTest extends AbstractListTest { assertEquals(true, list.remove(Integer.valueOf((String) els[0]))); } + public void testSubList() { + final List list = makeObject(); + List subList = list.subList(0, 0); + assertNotNull(subList); + list.add((E) "zero"); + //subList without any element of list + subList = list.subList(0, 0); + assertNotNull(subList); + assertEquals(0, subList.size()); + + //subList with one element oif list + subList = list.subList(0, 1); + assertEquals(1, subList.size()); + + final List elements = new ArrayList<>(); + elements.add((E) "one"); + elements.add((E) "two"); + elements.add((E) "three"); + list.addAll(1, elements); + //subList with all elements of list + subList = list.subList(0, list.size()); + assertEquals(list.size(), subList.size()); + } + @Override public String getCompatibilityVersion() { return "4"; From 81adcf9238a33b1a9337f0970fc388b68d24bd82 Mon Sep 17 00:00:00 2001 From: dota17 <50514813+dota17@users.noreply.github.com> Date: Mon, 4 Nov 2019 08:00:35 +0800 Subject: [PATCH 2/2] Add test cases for ArrayListValuedHashMapTest (#106) * Add test cases for ArrayListValuedHashMapTest to cover ArrayListValuedHashMap and AbstractListValuedMap * remove import * in java file --- .../multimap/ArrayListValuedHashMapTest.java | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/src/test/java/org/apache/commons/collections4/multimap/ArrayListValuedHashMapTest.java b/src/test/java/org/apache/commons/collections4/multimap/ArrayListValuedHashMapTest.java index 260947c5a..0cbfbb48b 100644 --- a/src/test/java/org/apache/commons/collections4/multimap/ArrayListValuedHashMapTest.java +++ b/src/test/java/org/apache/commons/collections4/multimap/ArrayListValuedHashMapTest.java @@ -18,6 +18,10 @@ package org.apache.commons.collections4.multimap; import java.util.List; import java.util.ListIterator; +import java.util.Map; +import java.util.HashMap; +import java.util.Arrays; +import java.util.ArrayList; import junit.framework.Test; @@ -145,6 +149,99 @@ public class ArrayListValuedHashMapTest extends AbstractMultiValuedMapTest assertNotSame(map1.hashCode(), map2.hashCode()); } + public void testArrayListValuedHashMap() { + ListValuedMap listMap = null; + ListValuedMap listMap1 = null; + Map map = new HashMap<>(); + Map map1 = new HashMap<>(); + map.put((K) "A", (V) "W"); + map.put((K) "B", (V) "X"); + map.put((K) "C", (V) "F"); + + listMap = new ArrayListValuedHashMap<>(map); + assertEquals(1, listMap.get((K) "A").size()); + assertEquals(1, listMap.get((K) "B").size()); + assertEquals(1, listMap.get((K) "C").size()); + + listMap1 = new ArrayListValuedHashMap<>(map1); + assertEquals("{}", listMap1.toString()); + } + + public void testTrimToSize(){ + final ArrayListValuedHashMap listMap = new ArrayListValuedHashMap<>(4); + + assertEquals("{}", listMap.toString()); + listMap.put((K) "A", (V) "W"); + listMap.put((K) "A", (V) "X"); + listMap.put((K) "B", (V) "F"); + assertEquals(2, listMap.get((K) "A").size()); + assertEquals(1, listMap.get((K) "B").size()); + + listMap.trimToSize(); + assertEquals(2, listMap.get((K) "A").size()); + assertEquals(1, listMap.get((K) "B").size()); + } + + public void testWrappedListAdd() { + final ListValuedMap listMap = makeObject(); + List listA = listMap.get((K) "A"); + listA.add(0, (V) "W"); + listA.add(1, (V) "X"); + listA.add(2, (V) "F"); + assertEquals("{A=[W, X, F]}", listMap.toString()); + listMap.get((K) "A").set(1, (V) "Q"); + assertEquals("{A=[W, Q, F]}", listMap.toString()); + } + + public void testWrappedListAddAll() { + final ListValuedMap listMap = makeObject(); + List listA = listMap.get((K) "A"); + List list = Arrays.asList((V) "W", (V) "X", (V) "F"); + listA.addAll(0, list); + assertEquals("{A=[W, X, F]}", listMap.toString()); + + List list1 = Arrays.asList((V) "Q", (V) "Q", (V) "L"); + listA.addAll(3, list1); + assertEquals("{A=[W, X, F, Q, Q, L]}", listMap.toString()); + assertEquals("W", listMap.get((K) "A").get(0)); + assertEquals("X", listMap.get((K) "A").get(1)); + assertEquals("F", listMap.get((K) "A").get(2)); + assertEquals("Q", listMap.get((K) "A").get(3)); + assertEquals("Q", listMap.get((K) "A").get(4)); + assertEquals("L", listMap.get((K) "A").get(5)); + assertEquals(0, listMap.get((K) "A").indexOf("W")); + assertEquals(2, listMap.get((K) "A").indexOf("F")); + assertEquals(-1, listMap.get((K) "A").indexOf("C")); + assertEquals(3, listMap.get((K) "A").indexOf("Q")); + assertEquals(4, listMap.get((K) "A").lastIndexOf("Q")); + assertEquals(-1, listMap.get((K) "A").lastIndexOf("A")); + + List list2 = new ArrayList<>(); + listMap.get((K) "B").addAll(0, list2); + assertEquals("{A=[W, X, F, Q, Q, L]}", listMap.toString()); + List list3 = listMap.get((K) "A").subList(1, 4); + assertEquals(3, list3.size()); + assertEquals("Q", list3.get(2)); + } + + public void testValuesListIteratorMethods(){ + final ListValuedMap listMap = makeObject(); + List listA = listMap.get((K) "A"); + List list = Arrays.asList((V) "W", (V) "X", (V) "F", (V) "Q", (V) "Q", (V)"F"); + listA.addAll(0, list); + ListIterator it = listMap.get((K) "A").listIterator(1); + assertTrue(it.hasNext()); + assertEquals("X", it.next()); + assertEquals("F", it.next()); + assertTrue(it.hasPrevious()); + assertEquals("F", it.previous()); + assertEquals(2, it.nextIndex()); + assertEquals(1, it.previousIndex()); + it.set((V) "Z"); + assertEquals("Z", it.next()); + assertEquals("Q", it.next()); + } + // public void testCreate() throws Exception { // writeExternalFormToDisk((java.io.Serializable) makeObject(), // "src/test/resources/data/test/ArrayListValuedHashMap.emptyCollection.version4.1.obj");