Class UnmodifiableCollectionTests


  • @GwtCompatible
    public class UnmodifiableCollectionTests
    extends java.lang.Object
    A series of tests that support asserting that collections cannot be modified, either through direct or indirect means.
    Author:
    Robert Konigsberg
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <E extends @Nullable java.lang.Object>
      void
      assertCollectionIsUnmodifiable​(java.util.Collection<E> collection, E sampleElement)
      Verifies that a collection is immutable.
      static void assertIteratorIsUnmodifiable​(java.util.Iterator<?> iterator)
      Verifies that an Iterator is unmodifiable.
      static void assertIteratorsInOrder​(java.util.Iterator<?> expectedIterator, java.util.Iterator<?> actualIterator)
      Asserts that two iterators contain elements in tandem.
      static void assertMapEntryIsUnmodifiable​(java.util.Map.Entry<?,​?> entry)  
      static <K extends @Nullable java.lang.Object,​V extends @Nullable java.lang.Object>
      void
      assertMultimapIsUnmodifiable​(com.google.common.collect.Multimap<K,​V> multimap, K sampleKey, V sampleValue)
      Verifies that a multimap is immutable.
      static <E extends @Nullable java.lang.Object>
      void
      assertMultisetIsUnmodifiable​(com.google.common.collect.Multiset<E> multiset, E sampleElement)
      Verifies that a multiset is immutable.
      static <E extends @Nullable java.lang.Object>
      void
      assertSetIsUnmodifiable​(java.util.Set<E> set, E sampleElement)
      Verifies that a set is immutable.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • assertIteratorIsUnmodifiable

        public static void assertIteratorIsUnmodifiable​(java.util.Iterator<?> iterator)
        Verifies that an Iterator is unmodifiable.

        This test only works with iterators that iterate over a finite set.

      • assertIteratorsInOrder

        public static void assertIteratorsInOrder​(java.util.Iterator<?> expectedIterator,
                                                  java.util.Iterator<?> actualIterator)
        Asserts that two iterators contain elements in tandem.

        This test only works with iterators that iterate over a finite set.

      • assertCollectionIsUnmodifiable

        public static <E extends @Nullable java.lang.Object> void assertCollectionIsUnmodifiable​(java.util.Collection<E> collection,
                                                                                                 E sampleElement)
        Verifies that a collection is immutable.

        A collection is considered immutable if:

        1. All its mutation methods result in UnsupportedOperationException, and do not change the underlying contents.
        2. All methods that return objects that can indirectly mutate the collection throw UnsupportedOperationException when those mutators are called.
        Parameters:
        collection - the presumed-immutable collection
        sampleElement - an element of the same type as that contained by collection. collection may or may not have sampleElement as a member.
      • assertSetIsUnmodifiable

        public static <E extends @Nullable java.lang.Object> void assertSetIsUnmodifiable​(java.util.Set<E> set,
                                                                                          E sampleElement)
        Verifies that a set is immutable.

        A set is considered immutable if:

        1. All its mutation methods result in UnsupportedOperationException, and do not change the underlying contents.
        2. All methods that return objects that can indirectly mutate the set throw UnsupportedOperationException when those mutators are called.
        Parameters:
        set - the presumed-immutable set
        sampleElement - an element of the same type as that contained by set. set may or may not have sampleElement as a member.
      • assertMultisetIsUnmodifiable

        public static <E extends @Nullable java.lang.Object> void assertMultisetIsUnmodifiable​(com.google.common.collect.Multiset<E> multiset,
                                                                                               E sampleElement)
        Verifies that a multiset is immutable.

        A multiset is considered immutable if:

        1. All its mutation methods result in UnsupportedOperationException, and do not change the underlying contents.
        2. All methods that return objects that can indirectly mutate the multiset throw UnsupportedOperationException when those mutators are called.
        Parameters:
        multiset - the presumed-immutable multiset
        sampleElement - an element of the same type as that contained by multiset. multiset may or may not have sampleElement as a member.
      • assertMultimapIsUnmodifiable

        public static <K extends @Nullable java.lang.Object,​V extends @Nullable java.lang.Object> void assertMultimapIsUnmodifiable​(com.google.common.collect.Multimap<K,​V> multimap,
                                                                                                                                          K sampleKey,
                                                                                                                                          V sampleValue)
        Verifies that a multimap is immutable.

        A multimap is considered immutable if:

        1. All its mutation methods result in UnsupportedOperationException, and do not change the underlying contents.
        2. All methods that return objects that can indirectly mutate the multimap throw UnsupportedOperationException when those mutators
        Parameters:
        multimap - the presumed-immutable multimap
        sampleKey - a key of the same type as that contained by multimap. multimap may or may not have sampleKey as a key.
        sampleValue - a key of the same type as that contained by multimap. multimap may or may not have sampleValue as a key.