Java
204 строки · 8.0 Кб
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
17package com.google.common.collect.testing.testers;
18
19import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
20import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
21import static com.google.common.collect.testing.features.CollectionFeature.RESTRICTS_ELEMENTS;
22import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD;
23import static com.google.common.collect.testing.features.CollectionSize.ZERO;
24import static java.util.Collections.singletonList;
25
26import com.google.common.annotations.GwtCompatible;
27import com.google.common.annotations.GwtIncompatible;
28import com.google.common.annotations.J2ktIncompatible;
29import com.google.common.collect.testing.AbstractCollectionTester;
30import com.google.common.collect.testing.Helpers;
31import com.google.common.collect.testing.MinimalCollection;
32import com.google.common.collect.testing.features.CollectionFeature;
33import com.google.common.collect.testing.features.CollectionSize;
34import java.lang.reflect.Method;
35import java.util.ConcurrentModificationException;
36import java.util.Iterator;
37import java.util.List;
38import org.checkerframework.checker.nullness.qual.Nullable;
39import org.junit.Ignore;
40
41/**
42* A generic JUnit test which tests addAll operations on a collection. Can't be invoked directly;
43* please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
44*
45* @author Chris Povirk
46* @author Kevin Bourrillion
47*/
48@GwtCompatible(emulated = true)
49@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
50@SuppressWarnings("JUnit4ClassUsedInJUnit3")
51public class CollectionAddAllTester<E extends @Nullable Object>
52extends AbstractCollectionTester<E> {
53@CollectionFeature.Require(SUPPORTS_ADD)
54public void testAddAll_supportedNothing() {
55assertFalse("addAll(nothing) should return false", collection.addAll(emptyCollection()));
56expectUnchanged();
57}
58
59@CollectionFeature.Require(absent = SUPPORTS_ADD)
60public void testAddAll_unsupportedNothing() {
61try {
62assertFalse(
63"addAll(nothing) should return false or throw", collection.addAll(emptyCollection()));
64} catch (UnsupportedOperationException tolerated) {
65}
66expectUnchanged();
67}
68
69@CollectionFeature.Require(SUPPORTS_ADD)
70public void testAddAll_supportedNonePresent() {
71assertTrue(
72"addAll(nonePresent) should return true", collection.addAll(createDisjointCollection()));
73expectAdded(e3(), e4());
74}
75
76@CollectionFeature.Require(absent = SUPPORTS_ADD)
77public void testAddAll_unsupportedNonePresent() {
78try {
79collection.addAll(createDisjointCollection());
80fail("addAll(nonePresent) should throw");
81} catch (UnsupportedOperationException expected) {
82}
83expectUnchanged();
84expectMissing(e3(), e4());
85}
86
87@CollectionFeature.Require(SUPPORTS_ADD)
88@CollectionSize.Require(absent = ZERO)
89public void testAddAll_supportedSomePresent() {
90assertTrue(
91"addAll(somePresent) should return true",
92collection.addAll(MinimalCollection.of(e3(), e0())));
93assertTrue("should contain " + e3(), collection.contains(e3()));
94assertTrue("should contain " + e0(), collection.contains(e0()));
95}
96
97@CollectionFeature.Require(absent = SUPPORTS_ADD)
98@CollectionSize.Require(absent = ZERO)
99public void testAddAll_unsupportedSomePresent() {
100try {
101collection.addAll(MinimalCollection.of(e3(), e0()));
102fail("addAll(somePresent) should throw");
103} catch (UnsupportedOperationException expected) {
104}
105expectUnchanged();
106}
107
108@CollectionFeature.Require({SUPPORTS_ADD, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
109@CollectionSize.Require(absent = ZERO)
110public void testAddAllConcurrentWithIteration() {
111try {
112Iterator<E> iterator = collection.iterator();
113assertTrue(collection.addAll(MinimalCollection.of(e3(), e0())));
114iterator.next();
115fail("Expected ConcurrentModificationException");
116} catch (ConcurrentModificationException expected) {
117// success
118}
119}
120
121@CollectionFeature.Require(absent = SUPPORTS_ADD)
122@CollectionSize.Require(absent = ZERO)
123public void testAddAll_unsupportedAllPresent() {
124try {
125assertFalse(
126"addAll(allPresent) should return false or throw",
127collection.addAll(MinimalCollection.of(e0())));
128} catch (UnsupportedOperationException tolerated) {
129}
130expectUnchanged();
131}
132
133@CollectionFeature.Require(
134value = {SUPPORTS_ADD, ALLOWS_NULL_VALUES},
135absent = RESTRICTS_ELEMENTS)
136public void testAddAll_nullSupported() {
137List<E> containsNull = singletonList(null);
138assertTrue("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*/
143expectAdded((E) null);
144}
145
146@CollectionFeature.Require(value = SUPPORTS_ADD, absent = ALLOWS_NULL_VALUES)
147public void testAddAll_nullUnsupported() {
148List<E> containsNull = singletonList(null);
149try {
150collection.addAll(containsNull);
151fail("addAll(containsNull) should throw");
152} catch (NullPointerException expected) {
153}
154expectUnchanged();
155expectNullMissingWhenNullUnsupported(
156"Should not contain null after unsupported addAll(containsNull)");
157}
158
159@CollectionFeature.Require(SUPPORTS_ADD)
160public void testAddAll_nullCollectionReference() {
161try {
162collection.addAll(null);
163fail("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
175public static Method getAddAllNullUnsupportedMethod() {
176return 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
188public static Method getAddAllUnsupportedNonePresentMethod() {
189return 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
201public static Method getAddAllUnsupportedSomePresentMethod() {
202return Helpers.getMethod(CollectionAddAllTester.class, "testAddAll_unsupportedSomePresent");
203}
204}
205