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