From cbc5db55408dae5d8744307a5eb379df36d55427 Mon Sep 17 00:00:00 2001 From: Stefano Cordio Date: Tue, 7 Sep 2021 23:21:41 +0200 Subject: [PATCH 1/2] [COLLECTIONS-799]: Prevent modifications by pollFirst and pollLast --- pom.xml | 3 + .../set/UnmodifiableNavigableSet.java | 16 +++++ .../set/UnmodifiableNavigableSetTest.java | 60 ++++++------------- 3 files changed, 37 insertions(+), 42 deletions(-) diff --git a/pom.xml b/pom.xml index 02272cd07..ad4348e82 100644 --- a/pom.xml +++ b/pom.xml @@ -455,6 +455,9 @@ Chen Guoping + + Stefano Cordio + diff --git a/src/main/java/org/apache/commons/collections4/set/UnmodifiableNavigableSet.java b/src/main/java/org/apache/commons/collections4/set/UnmodifiableNavigableSet.java index 48bfe39d0..fcdc5b673 100644 --- a/src/main/java/org/apache/commons/collections4/set/UnmodifiableNavigableSet.java +++ b/src/main/java/org/apache/commons/collections4/set/UnmodifiableNavigableSet.java @@ -107,6 +107,22 @@ public final class UnmodifiableNavigableSet throw new UnsupportedOperationException(); } + /** + * @since 4.5 + */ + @Override + public E pollFirst() { + throw new UnsupportedOperationException(); + } + + /** + * @since 4.5 + */ + @Override + public E pollLast() { + throw new UnsupportedOperationException(); + } + @Override public boolean retainAll(final Collection coll) { throw new UnsupportedOperationException(); diff --git a/src/test/java/org/apache/commons/collections4/set/UnmodifiableNavigableSetTest.java b/src/test/java/org/apache/commons/collections4/set/UnmodifiableNavigableSetTest.java index d335994d8..51e379246 100644 --- a/src/test/java/org/apache/commons/collections4/set/UnmodifiableNavigableSetTest.java +++ b/src/test/java/org/apache/commons/collections4/set/UnmodifiableNavigableSetTest.java @@ -27,6 +27,8 @@ import junit.framework.Test; import org.apache.commons.collections4.BulkTest; +import static org.junit.jupiter.api.Assertions.assertThrows; + /** * Extension of {@link AbstractNavigableSetTest} for exercising the * {@link UnmodifiableNavigableSet} implementation. @@ -48,7 +50,7 @@ public class UnmodifiableNavigableSetTest extends AbstractNavigableSetTest //------------------------------------------------------------------- @Override public NavigableSet makeObject() { - return UnmodifiableNavigableSet.unmodifiableNavigableSet(new TreeSet()); + return UnmodifiableNavigableSet.unmodifiableNavigableSet(new TreeSet<>()); } @Override @@ -95,11 +97,7 @@ public class UnmodifiableNavigableSetTest extends AbstractNavigableSetTest public void testDecorateFactory() { final NavigableSet set = makeFullCollection(); assertSame(set, UnmodifiableNavigableSet.unmodifiableNavigableSet(set)); - - try { - UnmodifiableNavigableSet.unmodifiableNavigableSet(null); - fail(); - } catch (final NullPointerException ex) {} + assertThrows(NullPointerException.class, () -> UnmodifiableNavigableSet.unmodifiableNavigableSet(null)); } /** @@ -107,48 +105,26 @@ public class UnmodifiableNavigableSetTest extends AbstractNavigableSetTest */ @SuppressWarnings("unchecked") public void verifyUnmodifiable(final Set set) { - try { - set.add((E) "value"); - fail("Expecting UnsupportedOperationException."); - } catch (final UnsupportedOperationException e) { - // expected - } - try { - set.addAll(new TreeSet()); - fail("Expecting UnsupportedOperationException."); - } catch (final UnsupportedOperationException e) { - // expected - } - try { - set.clear(); - fail("Expecting UnsupportedOperationException."); - } catch (final UnsupportedOperationException e) { - // expected - } - try { - set.remove("x"); - fail("Expecting UnsupportedOperationException."); - } catch (final UnsupportedOperationException e) { - // expected - } - try { - set.removeAll(array); - fail("Expecting UnsupportedOperationException."); - } catch (final UnsupportedOperationException e) { - // expected - } - try { - set.retainAll(array); - fail("Expecting UnsupportedOperationException."); - } catch (final UnsupportedOperationException e) { - // expected + assertThrows(UnsupportedOperationException.class, () -> set.add((E) "value")); + assertThrows(UnsupportedOperationException.class, () -> set.addAll(new TreeSet<>())); + assertThrows(UnsupportedOperationException.class, () -> set.clear()); + assertThrows(UnsupportedOperationException.class, () -> set.iterator().remove()); + assertThrows(UnsupportedOperationException.class, () -> set.remove("x")); + assertThrows(UnsupportedOperationException.class, () -> set.removeAll(array)); + assertThrows(UnsupportedOperationException.class, () -> set.removeIf(element -> true)); + assertThrows(UnsupportedOperationException.class, () -> set.retainAll(array)); + + if (set instanceof NavigableSet) { + final NavigableSet navigableSet = (NavigableSet) set; + assertThrows(UnsupportedOperationException.class, () -> navigableSet.pollFirst()); + assertThrows(UnsupportedOperationException.class, () -> navigableSet.pollLast()); } } public void testComparator() { setupSet(); final Comparator c = set.comparator(); - assertTrue("natural order, so comparator should be null", c == null); + assertNull("natural order, so comparator should be null", c); } From e6f72fe910990913401173e02ac33d5b85b5d775 Mon Sep 17 00:00:00 2001 From: "Bruno P. Kinoshita" Date: Sat, 9 Oct 2021 14:53:13 +1300 Subject: [PATCH 2/2] [COLLECTIONS-799] Changelog --- src/changes/changes.xml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 9e20e5db3..1cf9dd6b1 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -79,6 +79,9 @@ SetUniqueList.createSetBasedOnList doesn't add list elements to return value. + + UnmodifiableNavigableSet can be modified by pollFirst() and pollLast(). + Add tests for MapUtils.