Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isUnmodifiable assertion for Collections #2102

Closed
scordio opened this issue Jan 15, 2021 · 10 comments
Closed

isUnmodifiable assertion for Collections #2102

scordio opened this issue Jan 15, 2021 · 10 comments
Assignees
Labels
type: improvement A general improvement

Comments

@scordio
Copy link
Member

scordio commented Jan 15, 2021

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();
@scordio scordio added the type: improvement A general improvement label Jan 15, 2021
@HaydenMeloche
Copy link
Contributor

That would be cool! How would we assert the list is unmodifiable under the hood?
We could do:

list.getClass().isInstance(Collections.unmodifiableList(Arrays.asList("any value")))

This should work if UnmodifiableList or UnmodifiableRandomAccessList is returned.

@joel-costigliola
Copy link
Member

@HaydenMeloche that would cover the JDK types but if I use a Guava ImmutableCollection that won't work so the best implementation IMO is following the possible workaround by calling clear and catching the UnsupportedOperationException, if no exceptin is caught then fail the assertion

@HaydenMeloche
Copy link
Contributor

@joel-costigliola Fair point. I'd be willing to tackle this if you think it's worth working on :)

@joel-costigliola
Copy link
Member

Sure, go ahead, thanks!

@scordio
Copy link
Member Author

scordio commented Jan 16, 2021

@HaydenMeloche thank you! Before start working on it please let me put together a more detailed description of the approach I have in mind, so we can blame it also considering Guava. I should be able to do it today or tomorrow max.

@scordio
Copy link
Member Author

scordio commented Jan 16, 2021

I added some more details and I'll complete them during the weekend. However, this should give already a feeling so please let me know your thoughts.

I'd suggest starting the implementation of AbstractCollectionAssert which would inherit from AbstractIterableAssert and provide isUnmodifiable, covering Collection APIs only. The other types are extension points and can be done incrementally.

@HaydenMeloche
Copy link
Contributor

@scordio Thanks for the detailed explanation. I'll get started when I have some time this week. I might create a draft PR when I have something concrete so I can get feedback

@HaydenMeloche
Copy link
Contributor

hey @scordio got a chance to working on this over the weekend. I setup my assertion class as such

public abstract class AbstractCollectionAssert <SELF extends AbstractCollectionAssert<SELF, ACTUAL, ELEMENT, ELEMENT_ASSERT>,
  ACTUAL extends java.util.Collection<? extends ELEMENT>,
  ELEMENT,
  ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT, ELEMENT>>
  extends AbstractIterableAssert<SELF, ACTUAL, ELEMENT, ELEMENT_ASSERT>
  implements IndexedObjectEnumerableAssert<SELF, ELEMENT> {

I'm running into an issue now with this setup. Existing test cases that test the Iterable type are now looking for a Collection assert rather than a Iterator assert.

Ex: This test case:
https://github.com/assertj/assertj-core/blob/4fad9a03993e66fd4e2735352c22c52d206e9a1e/src/test/java/org/assertj/core/api/Assertions_assertThat_with_Iterable_Test.java#L30

Gives error:

Required type:
AbstractIterableAssert
<?,
java.lang.Iterable<? extends java.lang.Object>,
java.lang.Object,
org.assertj.core.api.ObjectAssert<java.lang.Object>>
Provided:
CollectionAssert
<Object,



no instance(s) of type variable(s) ELEMENT, T exist so that Collection<? extends ELEMENT> conforms to Iterable<?>

In Assertions.java I setup the collection assert as

  public static <ELEMENT> CollectionAssert<ELEMENT> assertThat(Collection<? extends ELEMENT> actual) {
    return AssertionsForInterfaceTypes.assertThat(actual);
  }

Since Collection extends Iterable I understand why this is happening just not sure what the recommended action would be...
any ideas/help would be appreciated :) I can push the code up to a branch if it's easier.

@scordio
Copy link
Member Author

scordio commented Jan 26, 2021

Hi @HaydenMeloche, yes please, just push/raise a draft PR and I'll have a look.

@scordio
Copy link
Member Author

scordio commented Oct 20, 2021

Closing as the implementation for collections has been done in #2315/#2328. Maps will be covered in #2381.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: improvement A general improvement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants