Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2008 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.google.common.collect.testing;
 
 
 import java.util.List;

Base class for testers of classes (including java.util.Collection and Map) that contain elements.

Parameters:
<C> the type of the container
<E> the type of the container's contents
Author(s):
George van den Driessche
 
 public abstract class AbstractContainerTester<C, E>
     extends AbstractTester<OneSizeTestContainerGenerator<C, E>> {
   protected SampleElements<E> samples;
   protected C container;
 
   @Override public void setUp() throws Exception {
     super.setUp();
      = this.getSubjectGenerator().samples();
     resetContainer();
   }

  

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

  
Replaces the existing container under test with a new container created by the subject generator.

Returns:
the new container instance.
See also:
resetContainer(java.lang.Object) resetContainer(C)
 
   protected C resetContainer() {
   }

  
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
 
   protected C resetContainer(C newValue) {
      = newValue;
     return ;
   }

  

Parameters:
elements expected contents of container
See also:
expectContents(java.util.Collection)
 
   protected final void expectContents(E... elements) {
     expectContents(Arrays.asList(elements));
   }

  
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
  /*
   * TODO: improve this and other implementations and move out of this framework
   * for wider use
   *
   * TODO: could we incorporate the overriding logic from AbstractListTester, by
   * examining whether the features include KNOWN_ORDER?
   */
  protected void expectContents(Collection<E> expected) {
    Helpers.assertEqualIgnoringOrder(expectedactualContents());
  }
  protected void expectUnchanged() {
  }

  
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
  protected final void expectAdded(E... elements) {
    List<E> expected = Helpers.copyToList(getSampleElements());
    expected.addAll(Arrays.asList(elements));
    expectContents(expected);
  }
  protected final void expectAdded(int index, E... elements) {
    expectAdded(index, Arrays.asList(elements));
  }
  protected final void expectAdded(int indexCollection<E> elements) {
    List<E> expected = Helpers.copyToList(getSampleElements());
    expected.addAll(indexelements);
    expectContents(expected);
  }
  /*
   * TODO: if we're testing a list, we could check indexOf(). (Doing it in
   * AbstractListTester isn't enough because many tests that run on lists don't
   * extends AbstractListTester.) We could also iterate over all elements to
   * verify absence
   */
  protected void expectMissing(E... elements) {
    for (E element : elements) {
      assertFalse("Should not contain " + element,
          actualContents().contains(element));
    }
  }
  protected E[] createSamplesArray() {
    getSampleElements().toArray(array);
    return array;
  }
  protected E[] createOrderedArray() {
    getOrderedElements().toArray(array);
    return array;
  }
  public static class ArrayWithDuplicate<E> {
    public final E[] elements;
    public final E duplicate;
    private ArrayWithDuplicate(E[] elements, E duplicate) {
      this. = elements;
      this. = duplicate;
    }
  }

  

Returns:
an array of the proper size with a duplicate element. The size must be at least three.
    E[] elements = createSamplesArray();
    E duplicate = elements[(elements.length / 2) - 1];
    elements[(elements.length / 2) + 1] = duplicate;
    return new ArrayWithDuplicate<E>(elementsduplicate);
  }
  // Helper methods to improve readability of derived classes
  protected int getNumElements() {
  }
  protected Collection<E> getSampleElements(int howMany) {
    return getSubjectGenerator().getSampleElements(howMany);
  }
  protected Collection<E> getSampleElements() {
  }

  
Returns the sample elements as ordered by TestContainerGenerator.order(java.util.List). Tests should used this method only if they declare requirement com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER.
  protected List<E> getOrderedElements() {
    List<E> list = new ArrayList<E>();
    for (E e : getSubjectGenerator().order(
        new ArrayList<E>(getSampleElements()))) {
      list.add(e);
    }
    return Collections.unmodifiableList(list);
  }

  

Returns:
a suitable location for a null element, to use when initializing containers for tests that involve a null element being present.
  protected int getNullLocation() {
    return getNumElements() / 2;
  }
  @SuppressWarnings("unchecked")
    return MinimalCollection.of(..);
  }
  @SuppressWarnings("unchecked")
  protected MinimalCollection<E> emptyCollection() {
    return MinimalCollection.<E>of();
  }
New to GrepCode? Check out our FAQ X