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