guava
207 строк · 7.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.CollectionSize.ZERO;
20import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
21import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
22import static com.google.common.collect.testing.features.MapFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
23import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
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.AbstractMapTester;
30import com.google.common.collect.testing.Helpers;
31import com.google.common.collect.testing.MinimalCollection;
32import com.google.common.collect.testing.features.CollectionSize;
33import com.google.common.collect.testing.features.MapFeature;
34import java.lang.reflect.Method;
35import java.util.Collections;
36import java.util.ConcurrentModificationException;
37import java.util.Iterator;
38import java.util.LinkedHashMap;
39import java.util.List;
40import java.util.Map;
41import java.util.Map.Entry;
42import org.checkerframework.checker.nullness.qual.Nullable;
43import org.junit.Ignore;
44
45/**
46* A generic JUnit test which tests {@code putAll} operations on a map. Can't be invoked directly;
47* please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
48*
49* @author Chris Povirk
50* @author Kevin Bourrillion
51*/
52@GwtCompatible(emulated = true)
53@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
54@ElementTypesAreNonnullByDefault
55public class MapPutAllTester<K extends @Nullable Object, V extends @Nullable Object>
56extends AbstractMapTester<K, V> {
57private List<Entry<K, V>> containsNullKey;
58private List<Entry<K, V>> containsNullValue;
59
60@Override
61public void setUp() throws Exception {
62super.setUp();
63containsNullKey = singletonList(entry(null, v3()));
64containsNullValue = singletonList(entry(k3(), null));
65}
66
67@MapFeature.Require(SUPPORTS_PUT)
68public void testPutAll_supportedNothing() {
69getMap().putAll(emptyMap());
70expectUnchanged();
71}
72
73@MapFeature.Require(absent = SUPPORTS_PUT)
74public void testPutAll_unsupportedNothing() {
75try {
76getMap().putAll(emptyMap());
77} catch (UnsupportedOperationException tolerated) {
78}
79expectUnchanged();
80}
81
82@MapFeature.Require(SUPPORTS_PUT)
83public void testPutAll_supportedNonePresent() {
84putAll(createDisjointCollection());
85expectAdded(e3(), e4());
86}
87
88@MapFeature.Require(absent = SUPPORTS_PUT)
89public void testPutAll_unsupportedNonePresent() {
90try {
91putAll(createDisjointCollection());
92fail("putAll(nonePresent) should throw");
93} catch (UnsupportedOperationException expected) {
94}
95expectUnchanged();
96expectMissing(e3(), e4());
97}
98
99@MapFeature.Require(SUPPORTS_PUT)
100@CollectionSize.Require(absent = ZERO)
101public void testPutAll_supportedSomePresent() {
102putAll(MinimalCollection.of(e3(), e0()));
103expectAdded(e3());
104}
105
106@MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_PUT})
107@CollectionSize.Require(absent = ZERO)
108public void testPutAllSomePresentConcurrentWithEntrySetIteration() {
109try {
110Iterator<Entry<K, V>> iterator = getMap().entrySet().iterator();
111putAll(MinimalCollection.of(e3(), e0()));
112iterator.next();
113fail("Expected ConcurrentModificationException");
114} catch (ConcurrentModificationException expected) {
115// success
116}
117}
118
119@MapFeature.Require(absent = SUPPORTS_PUT)
120@CollectionSize.Require(absent = ZERO)
121public void testPutAll_unsupportedSomePresent() {
122try {
123putAll(MinimalCollection.of(e3(), e0()));
124fail("putAll(somePresent) should throw");
125} catch (UnsupportedOperationException expected) {
126}
127expectUnchanged();
128}
129
130@MapFeature.Require(absent = SUPPORTS_PUT)
131@CollectionSize.Require(absent = ZERO)
132public void testPutAll_unsupportedAllPresent() {
133try {
134putAll(MinimalCollection.of(e0()));
135} catch (UnsupportedOperationException tolerated) {
136}
137expectUnchanged();
138}
139
140@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
141public void testPutAll_nullKeySupported() {
142putAll(containsNullKey);
143expectAdded(containsNullKey.get(0));
144}
145
146@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS)
147public void testPutAll_nullKeyUnsupported() {
148try {
149putAll(containsNullKey);
150fail("putAll(containsNullKey) should throw");
151} catch (NullPointerException expected) {
152}
153expectUnchanged();
154expectNullKeyMissingWhenNullKeysUnsupported(
155"Should not contain null key after unsupported putAll(containsNullKey)");
156}
157
158@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
159public void testPutAll_nullValueSupported() {
160putAll(containsNullValue);
161expectAdded(containsNullValue.get(0));
162}
163
164@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
165public void testPutAll_nullValueUnsupported() {
166try {
167putAll(containsNullValue);
168fail("putAll(containsNullValue) should throw");
169} catch (NullPointerException expected) {
170}
171expectUnchanged();
172expectNullValueMissingWhenNullValuesUnsupported(
173"Should not contain null value after unsupported putAll(containsNullValue)");
174}
175
176@MapFeature.Require(SUPPORTS_PUT)
177public void testPutAll_nullCollectionReference() {
178try {
179getMap().putAll(null);
180fail("putAll(null) should throw NullPointerException");
181} catch (NullPointerException expected) {
182}
183}
184
185private Map<K, V> emptyMap() {
186return Collections.emptyMap();
187}
188
189private void putAll(Iterable<Entry<K, V>> entries) {
190Map<K, V> map = new LinkedHashMap<>();
191for (Entry<K, V> entry : entries) {
192map.put(entry.getKey(), entry.getValue());
193}
194getMap().putAll(map);
195}
196
197/**
198* Returns the {@link Method} instance for {@link #testPutAll_nullKeyUnsupported()} so that tests
199* can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a
200* href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun bug 5045147</a> is fixed.
201*/
202@J2ktIncompatible
203@GwtIncompatible // reflection
204public static Method getPutAllNullKeyUnsupportedMethod() {
205return Helpers.getMethod(MapPutAllTester.class, "testPutAll_nullKeyUnsupported");
206}
207}
208