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