001/* 002 * Copyright (C) 2007 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect.testing; 018 019import static com.google.common.collect.testing.Helpers.copyToList; 020import static com.google.common.collect.testing.Helpers.mapEntry; 021 022import com.google.common.annotations.GwtCompatible; 023import java.util.Collection; 024import java.util.Iterator; 025import java.util.List; 026import java.util.ListIterator; 027import java.util.Map; 028import java.util.Map.Entry; 029import java.util.Objects; 030import org.jspecify.annotations.NullMarked; 031import org.jspecify.annotations.Nullable; 032 033/** 034 * Base class for map testers. 035 * 036 * <p>TODO: see how much of this is actually needed once Map testers are written. (It was cloned 037 * from AbstractCollectionTester.) 038 * 039 * @param <K> the key type of the map to be tested. 040 * @param <V> the value type of the map to be tested. 041 * @author George van den Driessche 042 */ 043@GwtCompatible 044@NullMarked 045public abstract class AbstractMapTester<K extends @Nullable Object, V extends @Nullable Object> 046 extends AbstractContainerTester<Map<K, V>, Entry<K, V>> { 047 protected Map<K, V> getMap() { 048 return container; 049 } 050 051 @Override 052 protected Collection<Entry<K, V>> actualContents() { 053 return getMap().entrySet(); 054 } 055 056 /** 057 * @see AbstractContainerTester#resetContainer() 058 */ 059 protected final void resetMap() { 060 resetContainer(); 061 } 062 063 protected void resetMap(Entry<K, V>[] entries) { 064 resetContainer(getSubjectGenerator().create((Object[]) entries)); 065 } 066 067 protected void expectMissingKeys(K... elements) { 068 for (K element : elements) { 069 assertFalse("Should not contain key " + element, getMap().containsKey(element)); 070 } 071 } 072 073 protected void expectMissingValues(V... elements) { 074 for (V element : elements) { 075 assertFalse("Should not contain value " + element, getMap().containsValue(element)); 076 } 077 } 078 079 /** 080 * @return an array of the proper size with {@code null} as the key of the middle element. 081 */ 082 protected Entry<K, V>[] createArrayWithNullKey() { 083 Entry<K, V>[] array = createSamplesArray(); 084 int nullKeyLocation = getNullLocation(); 085 Entry<K, V> oldEntry = array[nullKeyLocation]; 086 array[nullKeyLocation] = entry(null, oldEntry.getValue()); 087 return array; 088 } 089 090 protected V getValueForNullKey() { 091 return getEntryNullReplaces().getValue(); 092 } 093 094 protected K getKeyForNullValue() { 095 return getEntryNullReplaces().getKey(); 096 } 097 098 private Entry<K, V> getEntryNullReplaces() { 099 Iterator<Entry<K, V>> entries = getSampleElements().iterator(); 100 for (int i = 0; i < getNullLocation(); i++) { 101 entries.next(); 102 } 103 return entries.next(); 104 } 105 106 /** 107 * @return an array of the proper size with {@code null} as the value of the middle element. 108 */ 109 protected Entry<K, V>[] createArrayWithNullValue() { 110 Entry<K, V>[] array = createSamplesArray(); 111 int nullValueLocation = getNullLocation(); 112 Entry<K, V> oldEntry = array[nullValueLocation]; 113 array[nullValueLocation] = entry(oldEntry.getKey(), null); 114 return array; 115 } 116 117 protected void initMapWithNullKey() { 118 resetMap(createArrayWithNullKey()); 119 } 120 121 protected void initMapWithNullValue() { 122 resetMap(createArrayWithNullValue()); 123 } 124 125 /** 126 * Equivalent to {@link #expectMissingKeys(Object[]) expectMissingKeys} {@code (null)} except that 127 * the call to {@code contains(null)} is permitted to throw a {@code NullPointerException}. 128 * 129 * @param message message to use upon assertion failure 130 */ 131 protected void expectNullKeyMissingWhenNullKeysUnsupported(String message) { 132 try { 133 assertFalse(message, getMap().containsKey(null)); 134 } catch (NullPointerException tolerated) { 135 // Tolerated 136 } 137 } 138 139 /** 140 * Equivalent to {@link #expectMissingValues(Object[]) expectMissingValues} {@code (null)} except 141 * that the call to {@code contains(null)} is permitted to throw a {@code NullPointerException}. 142 * 143 * @param message message to use upon assertion failure 144 */ 145 protected void expectNullValueMissingWhenNullValuesUnsupported(String message) { 146 try { 147 assertFalse(message, getMap().containsValue(null)); 148 } catch (NullPointerException tolerated) { 149 // Tolerated 150 } 151 } 152 153 @Override 154 protected MinimalCollection<Entry<K, V>> createDisjointCollection() { 155 return MinimalCollection.of(e3(), e4()); 156 } 157 158 protected int getNumEntries() { 159 return getNumElements(); 160 } 161 162 protected Collection<Entry<K, V>> getSampleEntries(int howMany) { 163 return getSampleElements(howMany); 164 } 165 166 protected Collection<Entry<K, V>> getSampleEntries() { 167 return getSampleElements(); 168 } 169 170 @Override 171 protected void expectMissing(Entry<K, V>... entries) { 172 for (Entry<K, V> entry : entries) { 173 assertFalse("Should not contain entry " + entry, actualContents().contains(entry)); 174 assertFalse( 175 "Should not contain key " + entry.getKey() + " mapped to value " + entry.getValue(), 176 Objects.equals(getMap().get(entry.getKey()), entry.getValue())); 177 } 178 } 179 180 // This one-liner saves us from some ugly casts 181 protected Entry<K, V> entry(K key, V value) { 182 return mapEntry(key, value); 183 } 184 185 @Override 186 protected void expectContents(Collection<Entry<K, V>> expected) { 187 // TODO: move this to invariant checks once the appropriate hook exists? 188 super.expectContents(expected); 189 for (Entry<K, V> entry : expected) { 190 assertEquals( 191 "Wrong value for key " + entry.getKey(), entry.getValue(), getMap().get(entry.getKey())); 192 } 193 } 194 195 protected final void expectReplacement(Entry<K, V> newEntry) { 196 List<Entry<K, V>> expected = copyToList(getSampleElements()); 197 replaceValue(expected, newEntry); 198 expectContents(expected); 199 } 200 201 private void replaceValue(List<Entry<K, V>> expected, Entry<K, V> newEntry) { 202 for (ListIterator<Entry<K, V>> i = expected.listIterator(); i.hasNext(); ) { 203 if (Objects.equals(i.next().getKey(), newEntry.getKey())) { 204 i.set(newEntry); 205 return; 206 } 207 } 208 209 throw new IllegalArgumentException( 210 Platform.format("key %s not found in entries %s", newEntry.getKey(), expected)); 211 } 212 213 /** 214 * Wrapper for {@link Map#get(Object)} that forces the caller to pass in a key of the same type as 215 * the map. Besides being slightly shorter than code that uses {@link #getMap()}, it also ensures 216 * that callers don't pass an {@link Entry} by mistake. 217 */ 218 protected V get(K key) { 219 return getMap().get(key); 220 } 221 222 protected final K k0() { 223 return e0().getKey(); 224 } 225 226 protected final V v0() { 227 return e0().getValue(); 228 } 229 230 protected final K k1() { 231 return e1().getKey(); 232 } 233 234 protected final V v1() { 235 return e1().getValue(); 236 } 237 238 protected final K k2() { 239 return e2().getKey(); 240 } 241 242 protected final V v2() { 243 return e2().getValue(); 244 } 245 246 protected final K k3() { 247 return e3().getKey(); 248 } 249 250 protected final V v3() { 251 return e3().getValue(); 252 } 253 254 protected final K k4() { 255 return e4().getKey(); 256 } 257 258 protected final V v4() { 259 return e4().getValue(); 260 } 261}