Java
208 строк · 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@SuppressWarnings("JUnit4ClassUsedInJUnit3")
55@ElementTypesAreNonnullByDefault
56public class MapPutAllTester<K extends @Nullable Object, V extends @Nullable Object>
57extends AbstractMapTester<K, V> {
58private List<Entry<K, V>> containsNullKey;
59private List<Entry<K, V>> containsNullValue;
60
61@Override
62public void setUp() throws Exception {
63super.setUp();
64containsNullKey = singletonList(entry(null, v3()));
65containsNullValue = singletonList(entry(k3(), null));
66}
67
68@MapFeature.Require(SUPPORTS_PUT)
69public void testPutAll_supportedNothing() {
70getMap().putAll(emptyMap());
71expectUnchanged();
72}
73
74@MapFeature.Require(absent = SUPPORTS_PUT)
75public void testPutAll_unsupportedNothing() {
76try {
77getMap().putAll(emptyMap());
78} catch (UnsupportedOperationException tolerated) {
79}
80expectUnchanged();
81}
82
83@MapFeature.Require(SUPPORTS_PUT)
84public void testPutAll_supportedNonePresent() {
85putAll(createDisjointCollection());
86expectAdded(e3(), e4());
87}
88
89@MapFeature.Require(absent = SUPPORTS_PUT)
90public void testPutAll_unsupportedNonePresent() {
91try {
92putAll(createDisjointCollection());
93fail("putAll(nonePresent) should throw");
94} catch (UnsupportedOperationException expected) {
95}
96expectUnchanged();
97expectMissing(e3(), e4());
98}
99
100@MapFeature.Require(SUPPORTS_PUT)
101@CollectionSize.Require(absent = ZERO)
102public void testPutAll_supportedSomePresent() {
103putAll(MinimalCollection.of(e3(), e0()));
104expectAdded(e3());
105}
106
107@MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_PUT})
108@CollectionSize.Require(absent = ZERO)
109public void testPutAllSomePresentConcurrentWithEntrySetIteration() {
110try {
111Iterator<Entry<K, V>> iterator = getMap().entrySet().iterator();
112putAll(MinimalCollection.of(e3(), e0()));
113iterator.next();
114fail("Expected ConcurrentModificationException");
115} catch (ConcurrentModificationException expected) {
116// success
117}
118}
119
120@MapFeature.Require(absent = SUPPORTS_PUT)
121@CollectionSize.Require(absent = ZERO)
122public void testPutAll_unsupportedSomePresent() {
123try {
124putAll(MinimalCollection.of(e3(), e0()));
125fail("putAll(somePresent) should throw");
126} catch (UnsupportedOperationException expected) {
127}
128expectUnchanged();
129}
130
131@MapFeature.Require(absent = SUPPORTS_PUT)
132@CollectionSize.Require(absent = ZERO)
133public void testPutAll_unsupportedAllPresent() {
134try {
135putAll(MinimalCollection.of(e0()));
136} catch (UnsupportedOperationException tolerated) {
137}
138expectUnchanged();
139}
140
141@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
142public void testPutAll_nullKeySupported() {
143putAll(containsNullKey);
144expectAdded(containsNullKey.get(0));
145}
146
147@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS)
148public void testPutAll_nullKeyUnsupported() {
149try {
150putAll(containsNullKey);
151fail("putAll(containsNullKey) should throw");
152} catch (NullPointerException expected) {
153}
154expectUnchanged();
155expectNullKeyMissingWhenNullKeysUnsupported(
156"Should not contain null key after unsupported putAll(containsNullKey)");
157}
158
159@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
160public void testPutAll_nullValueSupported() {
161putAll(containsNullValue);
162expectAdded(containsNullValue.get(0));
163}
164
165@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
166public void testPutAll_nullValueUnsupported() {
167try {
168putAll(containsNullValue);
169fail("putAll(containsNullValue) should throw");
170} catch (NullPointerException expected) {
171}
172expectUnchanged();
173expectNullValueMissingWhenNullValuesUnsupported(
174"Should not contain null value after unsupported putAll(containsNullValue)");
175}
176
177@MapFeature.Require(SUPPORTS_PUT)
178public void testPutAll_nullCollectionReference() {
179try {
180getMap().putAll(null);
181fail("putAll(null) should throw NullPointerException");
182} catch (NullPointerException expected) {
183}
184}
185
186private Map<K, V> emptyMap() {
187return Collections.emptyMap();
188}
189
190private void putAll(Iterable<Entry<K, V>> entries) {
191Map<K, V> map = new LinkedHashMap<>();
192for (Entry<K, V> entry : entries) {
193map.put(entry.getKey(), entry.getValue());
194}
195getMap().putAll(map);
196}
197
198/**
199* Returns the {@link Method} instance for {@link #testPutAll_nullKeyUnsupported()} so that tests
200* can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a
201* href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun bug 5045147</a> is fixed.
202*/
203@J2ktIncompatible
204@GwtIncompatible // reflection
205public static Method getPutAllNullKeyUnsupportedMethod() {
206return Helpers.getMethod(MapPutAllTester.class, "testPutAll_nullKeyUnsupported");
207}
208}
209