guava
201 строка · 7.7 Кб
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.
50public class CollectionAddAllTester<E extends @Nullable Object>
51extends AbstractCollectionTester<E> {
52@CollectionFeature.Require(SUPPORTS_ADD)
53public void testAddAll_supportedNothing() {
54assertFalse("addAll(nothing) should return false", collection.addAll(emptyCollection()));
55expectUnchanged();
56}
57
58@CollectionFeature.Require(absent = SUPPORTS_ADD)
59public void testAddAll_unsupportedNothing() {
60try {
61assertFalse(
62"addAll(nothing) should return false or throw", collection.addAll(emptyCollection()));
63} catch (UnsupportedOperationException tolerated) {
64}
65expectUnchanged();
66}
67
68@CollectionFeature.Require(SUPPORTS_ADD)
69public void testAddAll_supportedNonePresent() {
70assertTrue(
71"addAll(nonePresent) should return true", collection.addAll(createDisjointCollection()));
72expectAdded(e3(), e4());
73}
74
75@CollectionFeature.Require(absent = SUPPORTS_ADD)
76public void testAddAll_unsupportedNonePresent() {
77try {
78collection.addAll(createDisjointCollection());
79fail("addAll(nonePresent) should throw");
80} catch (UnsupportedOperationException expected) {
81}
82expectUnchanged();
83expectMissing(e3(), e4());
84}
85
86@CollectionFeature.Require(SUPPORTS_ADD)
87@CollectionSize.Require(absent = ZERO)
88public void testAddAll_supportedSomePresent() {
89assertTrue(
90"addAll(somePresent) should return true",
91collection.addAll(MinimalCollection.of(e3(), e0())));
92assertTrue("should contain " + e3(), collection.contains(e3()));
93assertTrue("should contain " + e0(), collection.contains(e0()));
94}
95
96@CollectionFeature.Require(absent = SUPPORTS_ADD)
97@CollectionSize.Require(absent = ZERO)
98public void testAddAll_unsupportedSomePresent() {
99try {
100collection.addAll(MinimalCollection.of(e3(), e0()));
101fail("addAll(somePresent) should throw");
102} catch (UnsupportedOperationException expected) {
103}
104expectUnchanged();
105}
106
107@CollectionFeature.Require({SUPPORTS_ADD, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
108@CollectionSize.Require(absent = ZERO)
109public void testAddAllConcurrentWithIteration() {
110try {
111Iterator<E> iterator = collection.iterator();
112assertTrue(collection.addAll(MinimalCollection.of(e3(), e0())));
113iterator.next();
114fail("Expected ConcurrentModificationException");
115} catch (ConcurrentModificationException expected) {
116// success
117}
118}
119
120@CollectionFeature.Require(absent = SUPPORTS_ADD)
121@CollectionSize.Require(absent = ZERO)
122public void testAddAll_unsupportedAllPresent() {
123try {
124assertFalse(
125"addAll(allPresent) should return false or throw",
126collection.addAll(MinimalCollection.of(e0())));
127} catch (UnsupportedOperationException tolerated) {
128}
129expectUnchanged();
130}
131
132@CollectionFeature.Require(
133value = {SUPPORTS_ADD, ALLOWS_NULL_VALUES},
134absent = RESTRICTS_ELEMENTS)
135public void testAddAll_nullSupported() {
136List<E> containsNull = singletonList(null);
137assertTrue("addAll(containsNull) should return true", collection.addAll(containsNull));
138/*
139* We need (E) to force interpretation of null as the single element of a
140* varargs array, not the array itself
141*/
142expectAdded((E) null);
143}
144
145@CollectionFeature.Require(value = SUPPORTS_ADD, absent = ALLOWS_NULL_VALUES)
146public void testAddAll_nullUnsupported() {
147List<E> containsNull = singletonList(null);
148try {
149collection.addAll(containsNull);
150fail("addAll(containsNull) should throw");
151} catch (NullPointerException expected) {
152}
153expectUnchanged();
154expectNullMissingWhenNullUnsupported(
155"Should not contain null after unsupported addAll(containsNull)");
156}
157
158@CollectionFeature.Require(SUPPORTS_ADD)
159public void testAddAll_nullCollectionReference() {
160try {
161collection.addAll(null);
162fail("addAll(null) should throw NullPointerException");
163} catch (NullPointerException expected) {
164}
165}
166
167/**
168* Returns the {@link Method} instance for {@link #testAddAll_nullUnsupported()} so that tests can
169* suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a
170* href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun bug 5045147</a> is fixed.
171*/
172@J2ktIncompatible
173@GwtIncompatible // reflection
174public static Method getAddAllNullUnsupportedMethod() {
175return Helpers.getMethod(CollectionAddAllTester.class, "testAddAll_nullUnsupported");
176}
177
178/**
179* Returns the {@link Method} instance for {@link #testAddAll_unsupportedNonePresent()} so that
180* tests can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} while we
181* figure out what to do with <a href="http://goo.gl/qJBruX">{@code ConcurrentHashMap} support for
182* {@code entrySet().add()}</a>.
183*/
184@J2ktIncompatible
185@GwtIncompatible // reflection
186public static Method getAddAllUnsupportedNonePresentMethod() {
187return Helpers.getMethod(CollectionAddAllTester.class, "testAddAll_unsupportedNonePresent");
188}
189
190/**
191* Returns the {@link Method} instance for {@link #testAddAll_unsupportedSomePresent()} so that
192* tests can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} while we
193* figure out what to do with <a href="http://goo.gl/qJBruX">{@code ConcurrentHashMap} support for
194* {@code entrySet().add()}</a>.
195*/
196@J2ktIncompatible
197@GwtIncompatible // reflection
198public static Method getAddAllUnsupportedSomePresentMethod() {
199return Helpers.getMethod(CollectionAddAllTester.class, "testAddAll_unsupportedSomePresent");
200}
201}
202