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 java.util.Collection;
021import org.junit.Ignore;
022
023/**
024 * Base class for collection testers.
025 *
026 * @param <E> the element type of the collection to be tested.
027 * @author Kevin Bourrillion
028 */
029@GwtCompatible
030@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
031public abstract class AbstractCollectionTester<E>
032    extends AbstractContainerTester<Collection<E>, E> {
033
034  // TODO: replace this with an accessor.
035  protected Collection<E> collection;
036
037  @Override
038  protected Collection<E> actualContents() {
039    return collection;
040  }
041
042  // TODO: dispose of this once collection is encapsulated.
043  @Override
044  protected Collection<E> resetContainer(Collection<E> newContents) {
045    collection = super.resetContainer(newContents);
046    return collection;
047  }
048
049  /** @see AbstractContainerTester#resetContainer() */
050  protected void resetCollection() {
051    resetContainer();
052  }
053
054  /** @return an array of the proper size with {@code null} inserted into the middle element. */
055  protected E[] createArrayWithNullElement() {
056    E[] array = createSamplesArray();
057    array[getNullLocation()] = null;
058    return array;
059  }
060
061  protected void initCollectionWithNullElement() {
062    E[] array = createArrayWithNullElement();
063    resetContainer(getSubjectGenerator().create(array));
064  }
065
066  /**
067   * Equivalent to {@link #expectMissing(Object[]) expectMissing}{@code (null)} except that the call
068   * to {@code contains(null)} is permitted to throw a {@code NullPointerException}.
069   *
070   * @param message message to use upon assertion failure
071   */
072  protected void expectNullMissingWhenNullUnsupported(String message) {
073    try {
074      assertFalse(message, actualContents().contains(null));
075    } catch (NullPointerException tolerated) {
076      // Tolerated
077    }
078  }
079}