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.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
021import static com.google.common.collect.testing.features.CollectionFeature.RESTRICTS_ELEMENTS;
022import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD;
023import static com.google.common.collect.testing.features.CollectionSize.ZERO;
024import static java.util.Collections.singletonList;
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.AbstractCollectionTester;
030import com.google.common.collect.testing.Helpers;
031import com.google.common.collect.testing.MinimalCollection;
032import com.google.common.collect.testing.features.CollectionFeature;
033import com.google.common.collect.testing.features.CollectionSize;
034import java.lang.reflect.Method;
035import java.util.ConcurrentModificationException;
036import java.util.Iterator;
037import java.util.List;
038import org.checkerframework.checker.nullness.qual.Nullable;
039import org.junit.Ignore;
040
041/**
042 * A generic JUnit test which tests addAll operations on a collection. Can't be invoked directly;
043 * please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
044 *
045 * @author Chris Povirk
046 * @author Kevin Bourrillion
047 */
048@GwtCompatible(emulated = true)
049@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
050@SuppressWarnings("JUnit4ClassUsedInJUnit3")
051public class CollectionAddAllTester<E extends @Nullable Object>
052    extends AbstractCollectionTester<E> {
053  @CollectionFeature.Require(SUPPORTS_ADD)
054  public void testAddAll_supportedNothing() {
055    assertFalse("addAll(nothing) should return false", collection.addAll(emptyCollection()));
056    expectUnchanged();
057  }
058
059  @CollectionFeature.Require(absent = SUPPORTS_ADD)
060  public void testAddAll_unsupportedNothing() {
061    try {
062      assertFalse(
063          "addAll(nothing) should return false or throw", collection.addAll(emptyCollection()));
064    } catch (UnsupportedOperationException tolerated) {
065    }
066    expectUnchanged();
067  }
068
069  @CollectionFeature.Require(SUPPORTS_ADD)
070  public void testAddAll_supportedNonePresent() {
071    assertTrue(
072        "addAll(nonePresent) should return true", collection.addAll(createDisjointCollection()));
073    expectAdded(e3(), e4());
074  }
075
076  @CollectionFeature.Require(absent = SUPPORTS_ADD)
077  public void testAddAll_unsupportedNonePresent() {
078    try {
079      collection.addAll(createDisjointCollection());
080      fail("addAll(nonePresent) should throw");
081    } catch (UnsupportedOperationException expected) {
082    }
083    expectUnchanged();
084    expectMissing(e3(), e4());
085  }
086
087  @CollectionFeature.Require(SUPPORTS_ADD)
088  @CollectionSize.Require(absent = ZERO)
089  public void testAddAll_supportedSomePresent() {
090    assertTrue(
091        "addAll(somePresent) should return true",
092        collection.addAll(MinimalCollection.of(e3(), e0())));
093    assertTrue("should contain " + e3(), collection.contains(e3()));
094    assertTrue("should contain " + e0(), collection.contains(e0()));
095  }
096
097  @CollectionFeature.Require(absent = SUPPORTS_ADD)
098  @CollectionSize.Require(absent = ZERO)
099  public void testAddAll_unsupportedSomePresent() {
100    try {
101      collection.addAll(MinimalCollection.of(e3(), e0()));
102      fail("addAll(somePresent) should throw");
103    } catch (UnsupportedOperationException expected) {
104    }
105    expectUnchanged();
106  }
107
108  @CollectionFeature.Require({SUPPORTS_ADD, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
109  @CollectionSize.Require(absent = ZERO)
110  public void testAddAllConcurrentWithIteration() {
111    try {
112      Iterator<E> iterator = collection.iterator();
113      assertTrue(collection.addAll(MinimalCollection.of(e3(), e0())));
114      iterator.next();
115      fail("Expected ConcurrentModificationException");
116    } catch (ConcurrentModificationException expected) {
117      // success
118    }
119  }
120
121  @CollectionFeature.Require(absent = SUPPORTS_ADD)
122  @CollectionSize.Require(absent = ZERO)
123  public void testAddAll_unsupportedAllPresent() {
124    try {
125      assertFalse(
126          "addAll(allPresent) should return false or throw",
127          collection.addAll(MinimalCollection.of(e0())));
128    } catch (UnsupportedOperationException tolerated) {
129    }
130    expectUnchanged();
131  }
132
133  @CollectionFeature.Require(
134      value = {SUPPORTS_ADD, ALLOWS_NULL_VALUES},
135      absent = RESTRICTS_ELEMENTS)
136  public void testAddAll_nullSupported() {
137    List<E> containsNull = singletonList(null);
138    assertTrue("addAll(containsNull) should return true", collection.addAll(containsNull));
139    /*
140     * We need (E) to force interpretation of null as the single element of a
141     * varargs array, not the array itself
142     */
143    expectAdded((E) null);
144  }
145
146  @CollectionFeature.Require(value = SUPPORTS_ADD, absent = ALLOWS_NULL_VALUES)
147  public void testAddAll_nullUnsupported() {
148    List<E> containsNull = singletonList(null);
149    try {
150      collection.addAll(containsNull);
151      fail("addAll(containsNull) should throw");
152    } catch (NullPointerException expected) {
153    }
154    expectUnchanged();
155    expectNullMissingWhenNullUnsupported(
156        "Should not contain null after unsupported addAll(containsNull)");
157  }
158
159  @CollectionFeature.Require(SUPPORTS_ADD)
160  public void testAddAll_nullCollectionReference() {
161    try {
162      collection.addAll(null);
163      fail("addAll(null) should throw NullPointerException");
164    } catch (NullPointerException expected) {
165    }
166  }
167
168  /**
169   * Returns the {@link Method} instance for {@link #testAddAll_nullUnsupported()} so that tests can
170   * suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a
171   * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun bug 5045147</a> is fixed.
172   */
173  @J2ktIncompatible
174  @GwtIncompatible // reflection
175  public static Method getAddAllNullUnsupportedMethod() {
176    return Helpers.getMethod(CollectionAddAllTester.class, "testAddAll_nullUnsupported");
177  }
178
179  /**
180   * Returns the {@link Method} instance for {@link #testAddAll_unsupportedNonePresent()} so that
181   * tests can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} while we
182   * figure out what to do with <a
183   * href="https://github.com/openjdk/jdk/blob/c25c4896ad9ef031e3cddec493aef66ff87c48a7/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java#L4830">{@code
184   * ConcurrentHashMap} support for {@code entrySet().add()}</a>.
185   */
186  @J2ktIncompatible
187  @GwtIncompatible // reflection
188  public static Method getAddAllUnsupportedNonePresentMethod() {
189    return Helpers.getMethod(CollectionAddAllTester.class, "testAddAll_unsupportedNonePresent");
190  }
191
192  /**
193   * Returns the {@link Method} instance for {@link #testAddAll_unsupportedSomePresent()} so that
194   * tests can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} while we
195   * figure out what to do with <a
196   * href="https://github.com/openjdk/jdk/blob/c25c4896ad9ef031e3cddec493aef66ff87c48a7/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java#L4830">{@code
197   * ConcurrentHashMap} support for {@code entrySet().add()}</a>.
198   */
199  @J2ktIncompatible
200  @GwtIncompatible // reflection
201  public static Method getAddAllUnsupportedSomePresentMethod() {
202    return Helpers.getMethod(CollectionAddAllTester.class, "testAddAll_unsupportedSomePresent");
203  }
204}