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.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
022import static com.google.common.collect.testing.features.CollectionFeature.RESTRICTS_ELEMENTS;
023import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD;
024import static com.google.common.collect.testing.features.CollectionSize.ZERO;
025import static com.google.common.collect.testing.testers.ReflectionFreeAssertThrows.assertThrows;
026
027import com.google.common.annotations.GwtCompatible;
028import com.google.common.annotations.GwtIncompatible;
029import com.google.common.annotations.J2ktIncompatible;
030import com.google.common.collect.testing.AbstractCollectionTester;
031import com.google.common.collect.testing.features.CollectionFeature;
032import com.google.common.collect.testing.features.CollectionSize;
033import java.lang.reflect.Method;
034import java.util.ConcurrentModificationException;
035import java.util.Iterator;
036import org.junit.Ignore;
037
038/**
039 * A generic JUnit test which tests {@code add} operations on a collection. Can't be invoked
040 * directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
041 *
042 * @author Chris Povirk
043 * @author Kevin Bourrillion
044 */
045@GwtCompatible(emulated = true)
046@Ignore("test runners must not instantiate and run this directly, only via suites we build")
047// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
048@SuppressWarnings("JUnit4ClassUsedInJUnit3")
049public class CollectionAddTester<E> extends AbstractCollectionTester<E> {
050  @CollectionFeature.Require(SUPPORTS_ADD)
051  public void testAdd_supportedNotPresent() {
052    assertTrue("add(notPresent) should return true", collection.add(e3()));
053    expectAdded(e3());
054  }
055
056  @CollectionFeature.Require(absent = SUPPORTS_ADD)
057  public void testAdd_unsupportedNotPresent() {
058    assertThrows(UnsupportedOperationException.class, () -> collection.add(e3()));
059    expectUnchanged();
060    expectMissing(e3());
061  }
062
063  @CollectionFeature.Require(absent = SUPPORTS_ADD)
064  @CollectionSize.Require(absent = ZERO)
065  public void testAdd_unsupportedPresent() {
066    try {
067      assertFalse("add(present) should return false or throw", collection.add(e0()));
068    } catch (UnsupportedOperationException tolerated) {
069    }
070    expectUnchanged();
071  }
072
073  @CollectionFeature.Require(
074      value = {SUPPORTS_ADD, ALLOWS_NULL_VALUES},
075      absent = RESTRICTS_ELEMENTS)
076  public void testAdd_nullSupported() {
077    assertTrue("add(null) should return true", collection.add(null));
078    expectAdded((E) null);
079  }
080
081  @CollectionFeature.Require(value = SUPPORTS_ADD, absent = ALLOWS_NULL_VALUES)
082  public void testAdd_nullUnsupported() {
083    assertThrows(NullPointerException.class, () -> collection.add(null));
084    expectUnchanged();
085    expectNullMissingWhenNullUnsupported("Should not contain null after unsupported add(null)");
086  }
087
088  @CollectionFeature.Require({SUPPORTS_ADD, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
089  @CollectionSize.Require(absent = ZERO)
090  public void testAddConcurrentWithIteration() {
091    assertThrows(
092        ConcurrentModificationException.class,
093        () -> {
094          Iterator<E> iterator = collection.iterator();
095          assertTrue(collection.add(e3()));
096          iterator.next();
097        });
098  }
099
100  /**
101   * Returns the {@link Method} instance for {@link #testAdd_nullSupported()} so that tests of
102   * {@link java.util.Collections#checkedCollection(java.util.Collection, Class)} can suppress it
103   * with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a
104   * href="https://bugs.openjdk.org/browse/JDK-6409434">JDK-6409434</a> is fixed. It's unclear
105   * whether nulls were to be permitted or forbidden, but presumably the eventual fix will be to
106   * permit them, as it seems more likely that code would depend on that behavior than on the other.
107   * Thus, we say the bug is in add(), which fails to support null.
108   */
109  @J2ktIncompatible
110  @GwtIncompatible // reflection
111  public static Method getAddNullSupportedMethod() {
112    return getMethod(CollectionAddTester.class, "testAdd_nullSupported");
113  }
114
115  /**
116   * Returns the {@link Method} instance for {@link #testAdd_nullSupported()} so that tests of
117   * {@link java.util.TreeSet} can suppress it with {@code
118   * FeatureSpecificTestSuiteBuilder.suppressing()} until <a
119   * href="https://bugs.openjdk.org/browse/JDK-5045147">JDK-5045147</a> is fixed.
120   */
121  @J2ktIncompatible
122  @GwtIncompatible // reflection
123  public static Method getAddNullUnsupportedMethod() {
124    return getMethod(CollectionAddTester.class, "testAdd_nullUnsupported");
125  }
126
127  /**
128   * Returns the {@link Method} instance for {@link #testAdd_unsupportedNotPresent()} so that tests
129   * can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} while we figure out
130   * what to do with <a
131   * href="https://github.com/openjdk/jdk/blob/c25c4896ad9ef031e3cddec493aef66ff87c48a7/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java#L4830">{@code
132   * ConcurrentHashMap} support for {@code entrySet().add()}</a>.
133   */
134  @J2ktIncompatible
135  @GwtIncompatible // reflection
136  public static Method getAddUnsupportedNotPresentMethod() {
137    return getMethod(CollectionAddTester.class, "testAdd_unsupportedNotPresent");
138  }
139}