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