View Javadoc
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  
17  package com.google.common.collect.testing.testers;
18  
19  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
20  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
21  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
22  import static com.google.common.collect.testing.features.MapFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
23  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
24  import static java.util.Collections.singletonList;
25  
26  import com.google.common.annotations.GwtCompatible;
27  import com.google.common.annotations.GwtIncompatible;
28  import com.google.common.collect.testing.AbstractMapTester;
29  import com.google.common.collect.testing.Helpers;
30  import com.google.common.collect.testing.MinimalCollection;
31  import com.google.common.collect.testing.features.CollectionSize;
32  import com.google.common.collect.testing.features.MapFeature;
33  import java.lang.reflect.Method;
34  import java.util.Collections;
35  import java.util.ConcurrentModificationException;
36  import java.util.Iterator;
37  import java.util.LinkedHashMap;
38  import java.util.List;
39  import java.util.Map;
40  import java.util.Map.Entry;
41  
42  /**
43   * A generic JUnit test which tests {@code putAll} operations on a map. Can't be
44   * invoked directly; please see
45   * {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
46   *
47   * @author Chris Povirk
48   * @author Kevin Bourrillion
49   */
50  @SuppressWarnings("unchecked") // too many "unchecked generic array creations"
51  @GwtCompatible(emulated = true)
52  public class MapPutAllTester<K, V> extends AbstractMapTester<K, V> {
53    private List<Entry<K, V>> containsNullKey;
54    private List<Entry<K, V>> containsNullValue;
55  
56    @Override
57    public void setUp() throws Exception {
58      super.setUp();
59      containsNullKey = singletonList(entry(null, v3()));
60      containsNullValue = singletonList(entry(k3(), 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(e3(), 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(e3(), e4());
93    }
94  
95    @MapFeature.Require(SUPPORTS_PUT)
96    @CollectionSize.Require(absent = ZERO)
97    public void testPutAll_supportedSomePresent() {
98      putAll(MinimalCollection.of(e3(), e0()));
99      expectAdded(e3());
100   }
101 
102   @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_PUT})
103   @CollectionSize.Require(absent = ZERO)
104   public void testPutAllSomePresentConcurrentWithEntrySetIteration() {
105     try {
106       Iterator<Entry<K, V>> iterator = getMap().entrySet().iterator();
107       putAll(MinimalCollection.of(e3(), e0()));
108       iterator.next();
109       fail("Expected ConcurrentModificationException");
110     } catch (ConcurrentModificationException expected) {
111       // success
112     }
113   }
114 
115   @MapFeature.Require(absent = SUPPORTS_PUT)
116   @CollectionSize.Require(absent = ZERO)
117   public void testPutAll_unsupportedSomePresent() {
118     try {
119       putAll(MinimalCollection.of(e3(), e0()));
120       fail("putAll(somePresent) should throw");
121     } catch (UnsupportedOperationException expected) {
122     }
123     expectUnchanged();
124   }
125 
126   @MapFeature.Require(absent = SUPPORTS_PUT)
127   @CollectionSize.Require(absent = ZERO)
128   public void testPutAll_unsupportedAllPresent() {
129     try {
130       putAll(MinimalCollection.of(e0()));
131     } catch (UnsupportedOperationException tolerated) {
132     }
133     expectUnchanged();
134   }
135 
136   @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
137   public void testPutAll_nullKeySupported() {
138     putAll(containsNullKey);
139     expectAdded(containsNullKey.get(0));
140   }
141 
142   @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS)
143   public void testPutAll_nullKeyUnsupported() {
144     try {
145       putAll(containsNullKey);
146       fail("putAll(containsNullKey) should throw");
147     } catch (NullPointerException expected) {
148     }
149     expectUnchanged();
150     expectNullKeyMissingWhenNullKeysUnsupported(
151         "Should not contain null key after unsupported putAll(containsNullKey)");
152   }
153 
154   @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
155   public void testPutAll_nullValueSupported() {
156     putAll(containsNullValue);
157     expectAdded(containsNullValue.get(0));
158   }
159 
160   @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
161   public void testPutAll_nullValueUnsupported() {
162     try {
163       putAll(containsNullValue);
164       fail("putAll(containsNullValue) should throw");
165     } catch (NullPointerException expected) {
166     }
167     expectUnchanged();
168     expectNullValueMissingWhenNullValuesUnsupported(
169         "Should not contain null value after unsupported putAll(containsNullValue)");
170   }
171 
172   @MapFeature.Require(SUPPORTS_PUT)
173   public void testPutAll_nullCollectionReference() {
174     try {
175       getMap().putAll(null);
176       fail("putAll(null) should throw NullPointerException");
177     } catch (NullPointerException expected) {
178     }
179   }
180 
181   private Map<K, V> emptyMap() {
182     return Collections.emptyMap();
183   }
184 
185   private void putAll(Iterable<Entry<K, V>> entries) {
186     Map<K, V> map = new LinkedHashMap<>();
187     for (Entry<K, V> entry : entries) {
188       map.put(entry.getKey(), entry.getValue());
189     }
190     getMap().putAll(map);
191   }
192 
193   /**
194    * Returns the {@link Method} instance for {@link
195    * #testPutAll_nullKeyUnsupported()} so that tests can suppress it with {@code
196    * FeatureSpecificTestSuiteBuilder.suppressing()} until <a
197    * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun
198    * bug 5045147</a> is fixed.
199    */
200   @GwtIncompatible // reflection
201   public static Method getPutAllNullKeyUnsupportedMethod() {
202     return Helpers.getMethod(MapPutAllTester.class, "testPutAll_nullKeyUnsupported");
203   }
204 }