001/*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect.testing;
018
019import com.google.common.annotations.GwtCompatible;
020import com.google.errorprone.annotations.CanIgnoreReturnValue;
021import java.util.Collection;
022import org.junit.Ignore;
023
024/**
025 * Base class for collection testers.
026 *
027 * @param <E> the element type of the collection to be tested.
028 * @author Kevin Bourrillion
029 */
030@GwtCompatible
031@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
032public abstract class AbstractCollectionTester<E>
033    extends AbstractContainerTester<Collection<E>, E> {
034
035  // TODO: replace this with an accessor.
036  protected Collection<E> collection;
037
038  @Override
039  protected Collection<E> actualContents() {
040    return collection;
041  }
042
043  // TODO: dispose of this once collection is encapsulated.
044  @Override
045  @CanIgnoreReturnValue
046  protected Collection<E> resetContainer(Collection<E> newContents) {
047    collection = super.resetContainer(newContents);
048    return collection;
049  }
050
051  /** @see AbstractContainerTester#resetContainer() */
052  protected void resetCollection() {
053    resetContainer();
054  }
055
056  /** @return an array of the proper size with {@code null} inserted into the middle element. */
057  protected E[] createArrayWithNullElement() {
058    E[] array = createSamplesArray();
059    array[getNullLocation()] = null;
060    return array;
061  }
062
063  protected void initCollectionWithNullElement() {
064    E[] array = createArrayWithNullElement();
065    resetContainer(getSubjectGenerator().create(array));
066  }
067
068  /**
069   * Equivalent to {@link #expectMissing(Object[]) expectMissing}{@code (null)} except that the call
070   * to {@code contains(null)} is permitted to throw a {@code NullPointerException}.
071   *
072   * @param message message to use upon assertion failure
073   */
074  protected void expectNullMissingWhenNullUnsupported(String message) {
075    try {
076      assertFalse(message, actualContents().contains(null));
077    } catch (NullPointerException tolerated) {
078      // Tolerated
079    }
080  }
081}