-
-
Notifications
You must be signed in to change notification settings - Fork 765
Description
Summary
Considering the java.util.Collections.unmodifiableXXX() methods, we could add a isUnmodifiable() method to the assert classes for collections.
Implementation Proposal
Under the hood, the assertion should try to call all the collection APIs which are expected to throw UnsupportedOperationException. The assertion succeeds if all of them are throwing the expected exception, otherwise it fails.
We could define a new AbstractCollectionAssert which would inherit from AbstractIterableAssert and would be the parent class for all the other assert classes for collection subtypes. AbstractCollectionAssert would expose isUnmodifiable(). Also, Assertions.assertThat(Collection) and BDDAssertions.then(Collection) would be needed.
Below is the mapping between the collection type and the APIs to test. Each subtype would cover also the methods of the parent type. E.g., if we have a List, the assertion should also verify Collection methods.
| Instance Of | APIs to test |
|---|---|
Collection |
add(E), addAll(Collection), clear(), iterator().remove(), remove(Object), removeAll(Collection), removeIf(Predicate), retainAll(Collection) |
List |
add(int, E), addAll(int, Collection), listIterator().add(E), listIterator().remove(), listIterator().set(E), remove(int), replaceAll(UnaryOperator), set(int, E), sort(Comparator) |
NavigableSet |
descendingIterator().remove(), pollFirst(), pollLast() |
Set |
- |
SortedSet |
- |
Example
List<Integer> list = Collections.unmodifiableList(Arrays.asList(1, 2, 3));
assertThat(list).isUnmodifiable();