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;
41
@SuppressWarnings("JUnit4ClassUsedInJUnit3")
42
@ElementTypesAreNonnullByDefault
43
public abstract class AbstractMapTester<K extends @Nullable Object, V extends @Nullable Object>
44
extends AbstractContainerTester<Map<K, V>, Entry<K, V>> {
45
protected Map<K, V> getMap() {
50
protected Collection<Entry<K, V>> actualContents() {
51
return getMap().entrySet();
55
protected final void resetMap() {
59
protected void resetMap(Entry<K, V>[] entries) {
60
resetContainer(getSubjectGenerator().create((Object[]) entries));
63
protected void expectMissingKeys(K... elements) {
64
for (K element : elements) {
65
assertFalse("Should not contain key " + element, getMap().containsKey(element));
69
protected void expectMissingValues(V... elements) {
70
for (V element : elements) {
71
assertFalse("Should not contain value " + element, getMap().containsValue(element));
76
protected Entry<K, V>[] createArrayWithNullKey() {
77
Entry<K, V>[] array = createSamplesArray();
78
int nullKeyLocation = getNullLocation();
79
Entry<K, V> oldEntry = array[nullKeyLocation];
80
array[nullKeyLocation] = entry(null, oldEntry.getValue());
84
protected V getValueForNullKey() {
85
return getEntryNullReplaces().getValue();
88
protected K getKeyForNullValue() {
89
return getEntryNullReplaces().getKey();
92
private Entry<K, V> getEntryNullReplaces() {
93
Iterator<Entry<K, V>> entries = getSampleElements().iterator();
94
for (int i = 0; i < getNullLocation(); i++) {
97
return entries.next();
101
protected Entry<K, V>[] createArrayWithNullValue() {
102
Entry<K, V>[] array = createSamplesArray();
103
int nullValueLocation = getNullLocation();
104
Entry<K, V> oldEntry = array[nullValueLocation];
105
array[nullValueLocation] = entry(oldEntry.getKey(), null);
109
protected void initMapWithNullKey() {
110
resetMap(createArrayWithNullKey());
113
protected void initMapWithNullValue() {
114
resetMap(createArrayWithNullValue());
123
protected void expectNullKeyMissingWhenNullKeysUnsupported(String message) {
125
assertFalse(message, getMap().containsKey(null));
126
} catch (NullPointerException tolerated) {
137
protected void expectNullValueMissingWhenNullValuesUnsupported(String message) {
139
assertFalse(message, getMap().containsValue(null));
140
} catch (NullPointerException tolerated) {
146
protected MinimalCollection<Entry<K, V>> createDisjointCollection() {
147
return MinimalCollection.of(e3(), e4());
150
protected int getNumEntries() {
151
return getNumElements();
154
protected Collection<Entry<K, V>> getSampleEntries(int howMany) {
155
return getSampleElements(howMany);
158
protected Collection<Entry<K, V>> getSampleEntries() {
159
return getSampleElements();
163
protected void expectMissing(Entry<K, V>... entries) {
164
for (Entry<K, V> entry : entries) {
165
assertFalse("Should not contain entry " + entry, actualContents().contains(entry));
167
"Should not contain key " + entry.getKey() + " mapped to value " + entry.getValue(),
168
equal(getMap().get(entry.getKey()), entry.getValue()));
172
private static boolean equal(@Nullable Object a, @Nullable Object b) {
173
return a == b || (a != null && a.equals(b));
177
protected Entry<K, V> entry(K key, V value) {
178
return Helpers.mapEntry(key, value);
182
protected void expectContents(Collection<Entry<K, V>> expected) {
184
super.expectContents(expected);
185
for (Entry<K, V> entry : expected) {
187
"Wrong value for key " + entry.getKey(), entry.getValue(), getMap().get(entry.getKey()));
191
protected final void expectReplacement(Entry<K, V> newEntry) {
192
List<Entry<K, V>> expected = Helpers.copyToList(getSampleElements());
193
replaceValue(expected, newEntry);
194
expectContents(expected);
197
private void replaceValue(List<Entry<K, V>> expected, Entry<K, V> newEntry) {
198
for (ListIterator<Entry<K, V>> i = expected.listIterator(); i.hasNext(); ) {
199
if (Helpers.equal(i.next().getKey(), newEntry.getKey())) {
205
throw new IllegalArgumentException(
206
Platform.format("key %s not found in entries %s", newEntry.getKey(), expected));
214
protected V get(K key) {
215
return getMap().get(key);
218
protected final K k0() {
219
return e0().getKey();
222
protected final V v0() {
223
return e0().getValue();
226
protected final K k1() {
227
return e1().getKey();
230
protected final V v1() {
231
return e1().getValue();
234
protected final K k2() {
235
return e2().getKey();
238
protected final V v2() {
239
return e2().getValue();
242
protected final K k3() {
243
return e3().getKey();
246
protected final V v3() {
247
return e3().getValue();
250
protected final K k4() {
251
return e4().getKey();
254
protected final V v4() {
255
return e4().getValue();