From 1979a6e31067a18c9ede59ad4518f738512eba82 Mon Sep 17 00:00:00 2001 From: Don Jeba Date: Fri, 23 Nov 2018 00:32:09 -0700 Subject: [PATCH] [COLLECTIONS-701] StackOverflowError in SetUniqueList.add() when it receives itself. Closes #57. --- src/changes/changes.xml | 3 +++ .../collections4/list/SetUniqueList.java | 2 +- .../collections4/list/Collections701Test.java | 1 - .../collections4/list/SetUniqueListTest.java | 27 ++++++++++++++++++- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 1d5fb6b03..2cc7a7e43 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -36,6 +36,9 @@ AbstractReferenceMap made easier for subclassing; PR #51. + + StackOverflowError in SetUniqueList.add() when it receives itself. + diff --git a/src/main/java/org/apache/commons/collections4/list/SetUniqueList.java b/src/main/java/org/apache/commons/collections4/list/SetUniqueList.java index 3227e4392..b00b2d2f2 100644 --- a/src/main/java/org/apache/commons/collections4/list/SetUniqueList.java +++ b/src/main/java/org/apache/commons/collections4/list/SetUniqueList.java @@ -149,8 +149,8 @@ public class SetUniqueList extends AbstractSerializableListDecorator { public void add(final int index, final E object) { // adds element if it is not contained already if (set.contains(object) == false) { - super.add(index, object); set.add(object); + super.add(index, object); } } diff --git a/src/test/java/org/apache/commons/collections4/list/Collections701Test.java b/src/test/java/org/apache/commons/collections4/list/Collections701Test.java index 118369c4f..82bfd2668 100644 --- a/src/test/java/org/apache/commons/collections4/list/Collections701Test.java +++ b/src/test/java/org/apache/commons/collections4/list/Collections701Test.java @@ -47,7 +47,6 @@ public class Collections701Test { } @Test - @Ignore public void testSetUniqueList() { final List source = new ArrayList<>(); final List list = SetUniqueList.setUniqueList(source); diff --git a/src/test/java/org/apache/commons/collections4/list/SetUniqueListTest.java b/src/test/java/org/apache/commons/collections4/list/SetUniqueListTest.java index 24d220929..e8b6c14b6 100644 --- a/src/test/java/org/apache/commons/collections4/list/SetUniqueListTest.java +++ b/src/test/java/org/apache/commons/collections4/list/SetUniqueListTest.java @@ -139,6 +139,7 @@ public class SetUniqueListTest extends AbstractListTest { assertEquals("Size should increase after addAll", size + elements.length, getCollection().size()); } + @Override public void testCollectionIteratorRemove() { try { @@ -148,7 +149,6 @@ public class SetUniqueListTest extends AbstractListTest { extraVerify = true; } } - public void testCollections304() { final List list = new LinkedList<>(); final SetUniqueList decoratedList = SetUniqueList.setUniqueList(list); @@ -213,6 +213,31 @@ public class SetUniqueListTest extends AbstractListTest { assertFalse(subUniqueList.contains("World")); // fails } + public void testCollections701() { + final SetUniqueList uniqueList = new SetUniqueList<>(new ArrayList<>(), new HashSet<>()); + final Integer obj1 = Integer.valueOf(1); + final Integer obj2 = Integer.valueOf(2); + uniqueList.add(obj1); + uniqueList.add(obj2); + assertEquals(2, uniqueList.size()); + uniqueList.add(uniqueList); + assertEquals(3, uniqueList.size()); + final List list = new LinkedList<>(); + final SetUniqueList decoratedList = SetUniqueList.setUniqueList(list); + final String s1 = "Apple"; + final String s2 = "Lemon"; + final String s3 = "Orange"; + final String s4 = "Strawberry"; + decoratedList.add(s1); + decoratedList.add(s2); + decoratedList.add(s3); + assertEquals(3, decoratedList.size()); + decoratedList.set(1, s4); + assertEquals(3, decoratedList.size()); + decoratedList.add(decoratedList); + assertEquals(4, decoratedList.size()); + } + //----------------------------------------------------------------------- public void testFactory() { final Integer[] array = new Integer[] { Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(1) };