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.getMethod;
020import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
021import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD;
022import static com.google.common.collect.testing.features.CollectionSize.ZERO;
023
024import com.google.common.annotations.GwtCompatible;
025import com.google.common.annotations.GwtIncompatible;
026import com.google.common.annotations.J2ktIncompatible;
027import com.google.common.collect.testing.features.CollectionFeature;
028import com.google.common.collect.testing.features.CollectionSize;
029import java.lang.reflect.Method;
030import org.junit.Ignore;
031
032/**
033 * A generic JUnit test which tests add operations on a set. Can't be invoked directly; please see
034 * {@link com.google.common.collect.testing.SetTestSuiteBuilder}.
035 *
036 * @author Kevin Bourrillion
037 */
038@GwtCompatible(emulated = true)
039@Ignore("test runners must not instantiate and run this directly, only via suites we build")
040// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
041@SuppressWarnings("JUnit4ClassUsedInJUnit3")
042public class SetAddTester<E> extends AbstractSetTester<E> {
043  @CollectionFeature.Require(SUPPORTS_ADD)
044  @CollectionSize.Require(absent = ZERO)
045  public void testAdd_supportedPresent() {
046    assertFalse("add(present) should return false", getSet().add(e0()));
047    expectUnchanged();
048  }
049
050  @CollectionFeature.Require(value = {SUPPORTS_ADD, ALLOWS_NULL_VALUES})
051  @CollectionSize.Require(absent = ZERO)
052  public void testAdd_supportedNullPresent() {
053    E[] array = createArrayWithNullElement();
054    collection = getSubjectGenerator().create(array);
055    assertFalse("add(nullPresent) should return false", getSet().add(null));
056    expectContents(array);
057  }
058
059  /**
060   * Returns the {@link Method} instance for {@link #testAdd_supportedNullPresent()} so that tests
061   * can suppress it. See {@link CollectionAddTester#getAddNullSupportedMethod()} for details.
062   */
063  @J2ktIncompatible
064  @GwtIncompatible // reflection
065  public static Method getAddSupportedNullPresentMethod() {
066    return getMethod(SetAddTester.class, "testAdd_supportedNullPresent");
067  }
068}