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