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