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;
030import org.junit.Ignore;
031
032/**
033 * A generic JUnit test which tests {@code add(Object)} operations on a list. Can't be invoked
034 * directly; please see {@link com.google.common.collect.testing.ListTestSuiteBuilder}.
035 *
036 * @author Chris Povirk
037 */
038@SuppressWarnings("unchecked") // too many "unchecked generic array creations"
039@GwtCompatible(emulated = true)
040@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
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    try {
057      getList().add(e0());
058      fail("add(present) should throw");
059    } catch (UnsupportedOperationException expected) {
060    }
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 = Helpers.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  @GwtIncompatible // reflection
080  public static Method getAddSupportedNullPresentMethod() {
081    return Helpers.getMethod(ListAddTester.class, "testAdd_supportedNullPresent");
082  }
083}