11d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/*
21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2007 The Guava Authors
31d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
41d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License");
51d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * you may not use this file except in compliance with the License.
61d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * You may obtain a copy of the License at
71d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
81d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * http://www.apache.org/licenses/LICENSE-2.0
91d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Unless required by applicable law or agreed to in writing, software
111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS,
121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * See the License for the specific language governing permissions and
141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * limitations under the License.
151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */
161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpackage com.google.common.collect.testing;
181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Collection;
201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/**
221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Base class for collection testers.
231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>This class is GWT compatible.
251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param <E> the element type of the collection to be tested.
271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @author Kevin Bourrillion
291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */
301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpublic abstract class AbstractCollectionTester<E>
311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    extends AbstractContainerTester<Collection<E>, E> {
321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  // TODO: replace this with an accessor.
341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected Collection<E> collection;
351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override protected Collection<E> actualContents() {
371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return collection;
381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  // TODO: dispose of this once collection is encapsulated.
411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override protected Collection<E> resetContainer(Collection<E> newContents) {
421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    collection = super.resetContainer(newContents);
431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return collection;
441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /** @see AbstractContainerTester#resetContainer() */
471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected void resetCollection() {
481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    resetContainer();
491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return an array of the proper size with {@code null} inserted into the
531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * middle element.
541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected E[] createArrayWithNullElement() {
561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    E[] array = createSamplesArray();
571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    array[getNullLocation()] = null;
581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return array;
591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected void initCollectionWithNullElement() {
621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    E[] array = createArrayWithNullElement();
631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    resetContainer(getSubjectGenerator().create(array));
641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Equivalent to {@link #expectMissing(Object[]) expectMissing}{@code (null)}
681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * except that the call to {@code contains(null)} is permitted to throw a
691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code NullPointerException}.
701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param message message to use upon assertion failure
721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected void expectNullMissingWhenNullUnsupported(String message) {
741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    try {
751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      assertFalse(message, actualContents().contains(null));
761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    } catch (NullPointerException tolerated) {
771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // Tolerated
781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert}
81