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