• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.collect.testing.testers;
18 
19 import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
20 import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
21 import static com.google.common.collect.testing.features.CollectionFeature.RESTRICTS_ELEMENTS;
22 import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD;
23 import static com.google.common.collect.testing.features.CollectionSize.ZERO;
24 import static java.util.Collections.singletonList;
25 
26 import com.google.common.annotations.GwtCompatible;
27 import com.google.common.annotations.GwtIncompatible;
28 import com.google.common.collect.testing.AbstractCollectionTester;
29 import com.google.common.collect.testing.Helpers;
30 import com.google.common.collect.testing.MinimalCollection;
31 import com.google.common.collect.testing.features.CollectionFeature;
32 import com.google.common.collect.testing.features.CollectionSize;
33 import java.lang.reflect.Method;
34 import java.util.ConcurrentModificationException;
35 import java.util.Iterator;
36 import java.util.List;
37 import org.junit.Ignore;
38 
39 /**
40  * A generic JUnit test which tests addAll operations on a collection. Can't be invoked directly;
41  * please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
42  *
43  * @author Chris Povirk
44  * @author Kevin Bourrillion
45  */
46 @SuppressWarnings("unchecked") // too many "unchecked generic array creations"
47 @GwtCompatible(emulated = true)
48 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
49 public class CollectionAddAllTester<E> extends AbstractCollectionTester<E> {
50   @CollectionFeature.Require(SUPPORTS_ADD)
testAddAll_supportedNothing()51   public void testAddAll_supportedNothing() {
52     assertFalse("addAll(nothing) should return false", collection.addAll(emptyCollection()));
53     expectUnchanged();
54   }
55 
56   @CollectionFeature.Require(absent = SUPPORTS_ADD)
testAddAll_unsupportedNothing()57   public void testAddAll_unsupportedNothing() {
58     try {
59       assertFalse(
60           "addAll(nothing) should return false or throw", collection.addAll(emptyCollection()));
61     } catch (UnsupportedOperationException tolerated) {
62     }
63     expectUnchanged();
64   }
65 
66   @CollectionFeature.Require(SUPPORTS_ADD)
testAddAll_supportedNonePresent()67   public void testAddAll_supportedNonePresent() {
68     assertTrue(
69         "addAll(nonePresent) should return true", collection.addAll(createDisjointCollection()));
70     expectAdded(e3(), e4());
71   }
72 
73   @CollectionFeature.Require(absent = SUPPORTS_ADD)
testAddAll_unsupportedNonePresent()74   public void testAddAll_unsupportedNonePresent() {
75     try {
76       collection.addAll(createDisjointCollection());
77       fail("addAll(nonePresent) should throw");
78     } catch (UnsupportedOperationException expected) {
79     }
80     expectUnchanged();
81     expectMissing(e3(), e4());
82   }
83 
84   @CollectionFeature.Require(SUPPORTS_ADD)
85   @CollectionSize.Require(absent = ZERO)
testAddAll_supportedSomePresent()86   public void testAddAll_supportedSomePresent() {
87     assertTrue(
88         "addAll(somePresent) should return true",
89         collection.addAll(MinimalCollection.of(e3(), e0())));
90     assertTrue("should contain " + e3(), collection.contains(e3()));
91     assertTrue("should contain " + e0(), collection.contains(e0()));
92   }
93 
94   @CollectionFeature.Require(absent = SUPPORTS_ADD)
95   @CollectionSize.Require(absent = ZERO)
testAddAll_unsupportedSomePresent()96   public void testAddAll_unsupportedSomePresent() {
97     try {
98       collection.addAll(MinimalCollection.of(e3(), e0()));
99       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)
testAddAllConcurrentWithIteration()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)
testAddAll_unsupportedAllPresent()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)
testAddAll_nullSupported()133   public void testAddAll_nullSupported() {
134     List<E> containsNull = singletonList(null);
135     assertTrue("addAll(containsNull) should return true", collection.addAll(containsNull));
136     /*
137      * We need (E) to force interpretation of null as the single element of a
138      * varargs array, not the array itself
139      */
140     expectAdded((E) null);
141   }
142 
143   @CollectionFeature.Require(value = SUPPORTS_ADD, absent = ALLOWS_NULL_VALUES)
testAddAll_nullUnsupported()144   public void testAddAll_nullUnsupported() {
145     List<E> containsNull = singletonList(null);
146     try {
147       collection.addAll(containsNull);
148       fail("addAll(containsNull) should throw");
149     } catch (NullPointerException expected) {
150     }
151     expectUnchanged();
152     expectNullMissingWhenNullUnsupported(
153         "Should not contain null after unsupported addAll(containsNull)");
154   }
155 
156   @CollectionFeature.Require(SUPPORTS_ADD)
testAddAll_nullCollectionReference()157   public void testAddAll_nullCollectionReference() {
158     try {
159       collection.addAll(null);
160       fail("addAll(null) should throw NullPointerException");
161     } catch (NullPointerException expected) {
162     }
163   }
164 
165   /**
166    * Returns the {@link Method} instance for {@link #testAddAll_nullUnsupported()} so that tests can
167    * suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a
168    * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun bug 5045147</a> is fixed.
169    */
170   @GwtIncompatible // reflection
getAddAllNullUnsupportedMethod()171   public static Method getAddAllNullUnsupportedMethod() {
172     return Helpers.getMethod(CollectionAddAllTester.class, "testAddAll_nullUnsupported");
173   }
174 
175   /**
176    * Returns the {@link Method} instance for {@link #testAddAll_unsupportedNonePresent()} so that
177    * tests can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} while we
178    * figure out what to do with <a href="http://goo.gl/qJBruX">{@code ConcurrentHashMap} support for
179    * {@code entrySet().add()}</a>.
180    */
181   @GwtIncompatible // reflection
getAddAllUnsupportedNonePresentMethod()182   public static Method getAddAllUnsupportedNonePresentMethod() {
183     return Helpers.getMethod(CollectionAddAllTester.class, "testAddAll_unsupportedNonePresent");
184   }
185 
186   /**
187    * Returns the {@link Method} instance for {@link #testAddAll_unsupportedSomePresent()} so that
188    * tests can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} while we
189    * figure out what to do with <a href="http://goo.gl/qJBruX">{@code ConcurrentHashMap} support for
190    * {@code entrySet().add()}</a>.
191    */
192   @GwtIncompatible // reflection
getAddAllUnsupportedSomePresentMethod()193   public static Method getAddAllUnsupportedSomePresentMethod() {
194     return Helpers.getMethod(CollectionAddAllTester.class, "testAddAll_unsupportedSomePresent");
195   }
196 }
197