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();
Summary
Considering the
java.util.Collections.unmodifiableXXX()methods, we could add aisUnmodifiable()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
AbstractCollectionAssertwhich would inherit fromAbstractIterableAssertand would be the parent class for all the other assert classes for collection subtypes.AbstractCollectionAssertwould exposeisUnmodifiable(). Also,Assertions.assertThat(Collection)andBDDAssertions.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 verifyCollectionmethods.Collectionadd(E),addAll(Collection),clear(),iterator().remove(),remove(Object),removeAll(Collection),removeIf(Predicate),retainAll(Collection)Listadd(int, E),addAll(int, Collection),listIterator().add(E),listIterator().remove(),listIterator().set(E),remove(int),replaceAll(UnaryOperator),set(int, E),sort(Comparator)NavigableSetdescendingIterator().remove(),pollFirst(),pollLast()SetSortedSetExample