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