Class AbstractContainerTester<C, E extends @Nullable Object>

java.lang.Object
junit.framework.Assert
junit.framework.TestCase
com.google.common.collect.testing.AbstractTester<OneSizeTestContainerGenerator<C,E>>
com.google.common.collect.testing.AbstractContainerTester<C,E>
Type Parameters:
C - the type of the container
E - the type of the container's contents
All Implemented Interfaces:
junit.framework.Test
Direct Known Subclasses:
AbstractCollectionTester, AbstractMapTester, AbstractMultimapTester

@GwtCompatible @NullMarked public abstract class AbstractContainerTester<C, E extends @Nullable Object> extends AbstractTester<OneSizeTestContainerGenerator<C,E>>
Base class for testers of classes (including Collection and Map) that contain elements.
Author:
George van den Driessche
  • Field Details

  • Constructor Details

    • AbstractContainerTester

      public AbstractContainerTester()
  • Method Details

    • setUp

      @OverridingMethodsMustInvokeSuper public void setUp() throws Exception
      Overrides:
      setUp in class AbstractTester<OneSizeTestContainerGenerator<C, E extends @Nullable Object>>
      Throws:
      Exception
    • actualContents

      protected abstract Collection<E> actualContents()
      Returns:
      the contents of the container under test, for use by expectContents(E...) and its friends.
    • resetContainer

      @CanIgnoreReturnValue protected C resetContainer()
      Replaces the existing container under test with a new container created by the subject generator.
      Returns:
      the new container instance.
      See Also:
    • resetContainer

      @CanIgnoreReturnValue protected C resetContainer(C newValue)
      Replaces the existing container under test with a new container. This is useful when a single test method needs to create multiple containers while retaining the ability to use expectContents(E...) and other convenience methods. The creation of multiple containers in a single method is discouraged in most cases, but it is vital to the iterator tests.
      Parameters:
      newValue - the new container instance
      Returns:
      the new container instance
    • expectContents

      protected final void expectContents(E... elements)
      Parameters:
      elements - expected contents of container
      See Also:
    • expectContents

      protected void expectContents(Collection<E> expected)
      Asserts that the collection under test contains exactly the given elements, respecting cardinality but not order. Subclasses may override this method to provide stronger assertions, e.g., to check ordering in lists, but realize that unless a test extends AbstractListTester, a call to expectContents() invokes this version.
      Parameters:
      expected - expected value of container
    • expectUnchanged

      protected void expectUnchanged()
    • expectAdded

      protected final void expectAdded(E... elements)
      Asserts that the collection under test contains exactly the elements it was initialized with plus the given elements, according to expectContents(java.util.Collection). In other words, for the default expectContents() implementation, the number of occurrences of each given element has increased by one since the test collection was created, and the number of occurrences of all other elements has not changed.

      Note: This means that a test like the following will fail if collection is a Set:

       collection.add(existingElement);
       expectAdded(existingElement);

      In this case, collection was not modified as a result of the add() call, and the test will fail because the number of occurrences of existingElement is unchanged.

      Parameters:
      elements - expected additional contents of container
    • expectAdded

      protected final void expectAdded(int index, E... elements)
    • expectAdded

      protected final void expectAdded(int index, Collection<E> elements)
    • expectMissing

      protected void expectMissing(E... elements)
    • createSamplesArray

      protected E[] createSamplesArray()
    • createOrderedArray

      protected E[] createOrderedArray()
    • createArrayWithDuplicateElement

      protected AbstractContainerTester.ArrayWithDuplicate<E> createArrayWithDuplicateElement()
      Returns:
      an array of the proper size with a duplicate element. The size must be at least three.
    • getNumElements

      protected int getNumElements()
    • getSampleElements

      protected Collection<E> getSampleElements(int howMany)
    • getSampleElements

      protected Collection<E> getSampleElements()
    • getOrderedElements

      protected List<E> getOrderedElements()
      Returns the sample elements as ordered by TestContainerGenerator.order(List). Tests should use this method only if they declare requirement CollectionFeature.KNOWN_ORDER.
    • getNullLocation

      protected int getNullLocation()
      Returns:
      a suitable location for a null element, to use when initializing containers for tests that involve a null element being present.
    • createDisjointCollection

      protected MinimalCollection<E> createDisjointCollection()
    • emptyCollection

      protected MinimalCollection<E> emptyCollection()
    • e0

      protected final E e0()
    • e1

      protected final E e1()
    • e2

      protected final E e2()
    • e3

      protected final E e3()
    • e4

      protected final E e4()