2
* Copyright (C) 2007 The Guava Authors
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package com.google.common.collect.testing;
19
import com.google.common.annotations.GwtCompatible;
20
import java.util.Collection;
21
import java.util.Iterator;
23
import java.util.ListIterator;
25
import java.util.Map.Entry;
26
import org.checkerframework.checker.nullness.qual.Nullable;
27
import org.junit.Ignore;
30
* Base class for map testers.
32
* <p>TODO: see how much of this is actually needed once Map testers are written. (It was cloned
33
* from AbstractCollectionTester.)
35
* @param <K> the key type of the map to be tested.
36
* @param <V> the value type of the map to be tested.
37
* @author George van den Driessche
40
@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
41
@ElementTypesAreNonnullByDefault
42
public abstract class AbstractMapTester<K extends @Nullable Object, V extends @Nullable Object>
43
extends AbstractContainerTester<Map<K, V>, Entry<K, V>> {
44
protected Map<K, V> getMap() {
49
protected Collection<Entry<K, V>> actualContents() {
50
return getMap().entrySet();
53
/** @see AbstractContainerTester#resetContainer() */
54
protected final void resetMap() {
58
protected void resetMap(Entry<K, V>[] entries) {
59
resetContainer(getSubjectGenerator().create((Object[]) entries));
62
protected void expectMissingKeys(K... elements) {
63
for (K element : elements) {
64
assertFalse("Should not contain key " + element, getMap().containsKey(element));
68
protected void expectMissingValues(V... elements) {
69
for (V element : elements) {
70
assertFalse("Should not contain value " + element, getMap().containsValue(element));
74
/** @return an array of the proper size with {@code null} as the key of the middle element. */
75
protected Entry<K, V>[] createArrayWithNullKey() {
76
Entry<K, V>[] array = createSamplesArray();
77
int nullKeyLocation = getNullLocation();
78
Entry<K, V> oldEntry = array[nullKeyLocation];
79
array[nullKeyLocation] = entry(null, oldEntry.getValue());
83
protected V getValueForNullKey() {
84
return getEntryNullReplaces().getValue();
87
protected K getKeyForNullValue() {
88
return getEntryNullReplaces().getKey();
91
private Entry<K, V> getEntryNullReplaces() {
92
Iterator<Entry<K, V>> entries = getSampleElements().iterator();
93
for (int i = 0; i < getNullLocation(); i++) {
96
return entries.next();
99
/** @return an array of the proper size with {@code null} as the value of the middle element. */
100
protected Entry<K, V>[] createArrayWithNullValue() {
101
Entry<K, V>[] array = createSamplesArray();
102
int nullValueLocation = getNullLocation();
103
Entry<K, V> oldEntry = array[nullValueLocation];
104
array[nullValueLocation] = entry(oldEntry.getKey(), null);
108
protected void initMapWithNullKey() {
109
resetMap(createArrayWithNullKey());
112
protected void initMapWithNullValue() {
113
resetMap(createArrayWithNullValue());
117
* Equivalent to {@link #expectMissingKeys(Object[]) expectMissingKeys} {@code (null)} except that
118
* the call to {@code contains(null)} is permitted to throw a {@code NullPointerException}.
120
* @param message message to use upon assertion failure
122
protected void expectNullKeyMissingWhenNullKeysUnsupported(String message) {
124
assertFalse(message, getMap().containsKey(null));
125
} catch (NullPointerException tolerated) {
131
* Equivalent to {@link #expectMissingValues(Object[]) expectMissingValues} {@code (null)} except
132
* that the call to {@code contains(null)} is permitted to throw a {@code NullPointerException}.
134
* @param message message to use upon assertion failure
136
protected void expectNullValueMissingWhenNullValuesUnsupported(String message) {
138
assertFalse(message, getMap().containsValue(null));
139
} catch (NullPointerException tolerated) {
145
protected MinimalCollection<Entry<K, V>> createDisjointCollection() {
146
return MinimalCollection.of(e3(), e4());
149
protected int getNumEntries() {
150
return getNumElements();
153
protected Collection<Entry<K, V>> getSampleEntries(int howMany) {
154
return getSampleElements(howMany);
157
protected Collection<Entry<K, V>> getSampleEntries() {
158
return getSampleElements();
162
protected void expectMissing(Entry<K, V>... entries) {
163
for (Entry<K, V> entry : entries) {
164
assertFalse("Should not contain entry " + entry, actualContents().contains(entry));
166
"Should not contain key " + entry.getKey() + " mapped to value " + entry.getValue(),
167
equal(getMap().get(entry.getKey()), entry.getValue()));
171
private static boolean equal(@Nullable Object a, @Nullable Object b) {
172
return a == b || (a != null && a.equals(b));
175
// This one-liner saves us from some ugly casts
176
protected Entry<K, V> entry(K key, V value) {
177
return Helpers.mapEntry(key, value);
181
protected void expectContents(Collection<Entry<K, V>> expected) {
182
// TODO: move this to invariant checks once the appropriate hook exists?
183
super.expectContents(expected);
184
for (Entry<K, V> entry : expected) {
186
"Wrong value for key " + entry.getKey(), entry.getValue(), getMap().get(entry.getKey()));
190
protected final void expectReplacement(Entry<K, V> newEntry) {
191
List<Entry<K, V>> expected = Helpers.copyToList(getSampleElements());
192
replaceValue(expected, newEntry);
193
expectContents(expected);
196
private void replaceValue(List<Entry<K, V>> expected, Entry<K, V> newEntry) {
197
for (ListIterator<Entry<K, V>> i = expected.listIterator(); i.hasNext(); ) {
198
if (Helpers.equal(i.next().getKey(), newEntry.getKey())) {
204
throw new IllegalArgumentException(
205
Platform.format("key %s not found in entries %s", newEntry.getKey(), expected));
209
* Wrapper for {@link Map#get(Object)} that forces the caller to pass in a key of the same type as
210
* the map. Besides being slightly shorter than code that uses {@link #getMap()}, it also ensures
211
* that callers don't pass an {@link Entry} by mistake.
213
protected V get(K key) {
214
return getMap().get(key);
217
protected final K k0() {
218
return e0().getKey();
221
protected final V v0() {
222
return e0().getValue();
225
protected final K k1() {
226
return e1().getKey();
229
protected final V v1() {
230
return e1().getValue();
233
protected final K k2() {
234
return e2().getKey();
237
protected final V v2() {
238
return e2().getValue();
241
protected final K k3() {
242
return e3().getKey();
245
protected final V v3() {
246
return e3().getValue();
249
protected final K k4() {
250
return e4().getKey();
253
protected final V v4() {
254
return e4().getValue();