1 /* 2 * Copyright (C) 2007 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.collect.testing.testers; 18 19 import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES; 20 import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD; 21 import static com.google.common.collect.testing.features.CollectionSize.ZERO; 22 23 import com.google.common.annotations.GwtCompatible; 24 import com.google.common.annotations.GwtIncompatible; 25 import com.google.common.collect.testing.Helpers; 26 import com.google.common.collect.testing.features.CollectionFeature; 27 import com.google.common.collect.testing.features.CollectionSize; 28 29 import java.lang.reflect.Method; 30 import java.util.List; 31 32 /** 33 * A generic JUnit test which tests {@code add(Object)} operations on a list. 34 * Can't be invoked directly; please see 35 * {@link com.google.common.collect.testing.ListTestSuiteBuilder}. 36 * 37 * @author Chris Povirk 38 */ 39 @SuppressWarnings("unchecked") // too many "unchecked generic array creations" 40 @GwtCompatible(emulated = true) 41 public class ListAddTester<E> extends AbstractListTester<E> { 42 @CollectionFeature.Require(SUPPORTS_ADD) 43 @CollectionSize.Require(absent = ZERO) 44 public void testAdd_supportedPresent() { 45 assertTrue("add(present) should return true", getList().add(samples.e0)); 46 expectAdded(samples.e0); 47 } 48 49 @CollectionFeature.Require(absent = SUPPORTS_ADD) 50 @CollectionSize.Require(absent = ZERO) 51 /* 52 * absent = ZERO isn't required, since unmodList.add() must 53 * throw regardless, but it keeps the method name accurate. 54 */ 55 public void testAdd_unsupportedPresent() { 56 try { 57 getList().add(samples.e0); 58 fail("add(present) should throw"); 59 } catch (UnsupportedOperationException expected) { 60 } 61 } 62 63 @CollectionFeature.Require(value = {SUPPORTS_ADD, ALLOWS_NULL_VALUES}) 64 @CollectionSize.Require(absent = ZERO) 65 public void testAdd_supportedNullPresent() { 66 E[] array = createArrayWithNullElement(); 67 collection = getSubjectGenerator().create(array); 68 assertTrue("add(nullPresent) should return true", getList().add(null)); 69 70 List<E> expected = Helpers.copyToList(array); 71 expected.add(null); 72 expectContents(expected); 73 } 74 75 /** 76 * Returns the {@link Method} instance for 77 * {@link #testAdd_supportedNullPresent()} so that tests can suppress it. See 78 * {@link CollectionAddTester#getAddNullSupportedMethod()} for details. 79 */ 80 @GwtIncompatible("reflection") 81 public static Method getAddSupportedNullPresentMethod() { 82 return Helpers.getMethod(ListAddTester.class, "testAdd_supportedNullPresent"); 83 } 84 } 85