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