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.testers; 018 019import static com.google.common.collect.testing.Helpers.copyToList; 020import static com.google.common.collect.testing.Helpers.getMethod; 021import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES; 022import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD; 023import static com.google.common.collect.testing.features.CollectionSize.ZERO; 024import static com.google.common.collect.testing.testers.ReflectionFreeAssertThrows.assertThrows; 025 026import com.google.common.annotations.GwtCompatible; 027import com.google.common.annotations.GwtIncompatible; 028import com.google.common.annotations.J2ktIncompatible; 029import com.google.common.collect.testing.features.CollectionFeature; 030import com.google.common.collect.testing.features.CollectionSize; 031import java.lang.reflect.Method; 032import java.util.List; 033import org.junit.Ignore; 034 035/** 036 * A generic JUnit test which tests {@code add(Object)} operations on a list. Can't be invoked 037 * directly; please see {@link com.google.common.collect.testing.ListTestSuiteBuilder}. 038 * 039 * @author Chris Povirk 040 */ 041@GwtCompatible(emulated = true) 042@Ignore("test runners must not instantiate and run this directly, only via suites we build") 043// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 044@SuppressWarnings("JUnit4ClassUsedInJUnit3") 045public class ListAddTester<E> extends AbstractListTester<E> { 046 @CollectionFeature.Require(SUPPORTS_ADD) 047 @CollectionSize.Require(absent = ZERO) 048 public void testAdd_supportedPresent() { 049 assertTrue("add(present) should return true", getList().add(e0())); 050 expectAdded(e0()); 051 } 052 053 @CollectionFeature.Require(absent = SUPPORTS_ADD) 054 @CollectionSize.Require(absent = ZERO) 055 /* 056 * absent = ZERO isn't required, since unmodList.add() must 057 * throw regardless, but it keeps the method name accurate. 058 */ 059 public void testAdd_unsupportedPresent() { 060 assertThrows(UnsupportedOperationException.class, () -> getList().add(e0())); 061 } 062 063 @CollectionFeature.Require(value = {SUPPORTS_ADD, ALLOWS_NULL_VALUES}) 064 @CollectionSize.Require(absent = ZERO) 065 public void testAdd_supportedNullPresent() { 066 E[] array = createArrayWithNullElement(); 067 collection = getSubjectGenerator().create(array); 068 assertTrue("add(nullPresent) should return true", getList().add(null)); 069 070 List<E> expected = copyToList(array); 071 expected.add(null); 072 expectContents(expected); 073 } 074 075 /** 076 * Returns the {@link Method} instance for {@link #testAdd_supportedNullPresent()} so that tests 077 * can suppress it. See {@link CollectionAddTester#getAddNullSupportedMethod()} for details. 078 */ 079 @J2ktIncompatible 080 @GwtIncompatible // reflection 081 public static Method getAddSupportedNullPresentMethod() { 082 return getMethod(ListAddTester.class, "testAdd_supportedNullPresent"); 083 } 084}