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