001/* 002 * Copyright (C) 2012 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.google; 018 019import static com.google.common.collect.testing.Helpers.assertEqualIgnoringOrder; 020 021import com.google.common.annotations.GwtCompatible; 022import com.google.common.collect.Multimap; 023import com.google.common.collect.testing.AbstractContainerTester; 024import com.google.common.collect.testing.Helpers; 025import com.google.common.collect.testing.SampleElements; 026import com.google.errorprone.annotations.CanIgnoreReturnValue; 027import java.util.Arrays; 028import java.util.Collection; 029import java.util.Iterator; 030import java.util.Map.Entry; 031import org.checkerframework.checker.nullness.qual.Nullable; 032import org.junit.Ignore; 033 034/** 035 * Superclass for all {@code Multimap} testers. 036 * 037 * @author Louis Wasserman 038 */ 039@GwtCompatible 040@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 041@ElementTypesAreNonnullByDefault 042public abstract class AbstractMultimapTester< 043 K extends @Nullable Object, V extends @Nullable Object, M extends Multimap<K, V>> 044 extends AbstractContainerTester<M, Entry<K, V>> { 045 046 private M multimap; 047 048 protected M multimap() { 049 return multimap; 050 } 051 052 /** 053 * @return an array of the proper size with {@code null} as the key of the middle element. 054 */ 055 protected Entry<K, V>[] createArrayWithNullKey() { 056 Entry<K, V>[] array = createSamplesArray(); 057 int nullKeyLocation = getNullLocation(); 058 Entry<K, V> oldEntry = array[nullKeyLocation]; 059 array[nullKeyLocation] = Helpers.mapEntry(null, oldEntry.getValue()); 060 return array; 061 } 062 063 /** 064 * @return an array of the proper size with {@code null} as the value of the middle element. 065 */ 066 protected Entry<K, V>[] createArrayWithNullValue() { 067 Entry<K, V>[] array = createSamplesArray(); 068 int nullValueLocation = getNullLocation(); 069 Entry<K, V> oldEntry = array[nullValueLocation]; 070 array[nullValueLocation] = Helpers.mapEntry(oldEntry.getKey(), null); 071 return array; 072 } 073 074 /** 075 * @return an array of the proper size with {@code null} as the key and value of the middle 076 * element. 077 */ 078 protected Entry<K, V>[] createArrayWithNullKeyAndValue() { 079 Entry<K, V>[] array = createSamplesArray(); 080 int nullValueLocation = getNullLocation(); 081 array[nullValueLocation] = Helpers.mapEntry(null, null); 082 return array; 083 } 084 085 protected V getValueForNullKey() { 086 return getEntryNullReplaces().getValue(); 087 } 088 089 protected K getKeyForNullValue() { 090 return getEntryNullReplaces().getKey(); 091 } 092 093 private Entry<K, V> getEntryNullReplaces() { 094 Iterator<Entry<K, V>> entries = getSampleElements().iterator(); 095 for (int i = 0; i < getNullLocation(); i++) { 096 entries.next(); 097 } 098 return entries.next(); 099 } 100 101 protected void initMultimapWithNullKey() { 102 resetContainer(getSubjectGenerator().create((Object[]) createArrayWithNullKey())); 103 } 104 105 protected void initMultimapWithNullValue() { 106 resetContainer(getSubjectGenerator().create((Object[]) createArrayWithNullValue())); 107 } 108 109 protected void initMultimapWithNullKeyAndValue() { 110 resetContainer(getSubjectGenerator().create((Object[]) createArrayWithNullKeyAndValue())); 111 } 112 113 protected SampleElements<K> sampleKeys() { 114 return ((TestMultimapGenerator<K, V, ? extends Multimap<K, V>>) 115 getSubjectGenerator().getInnerGenerator()) 116 .sampleKeys(); 117 } 118 119 protected SampleElements<V> sampleValues() { 120 return ((TestMultimapGenerator<K, V, ? extends Multimap<K, V>>) 121 getSubjectGenerator().getInnerGenerator()) 122 .sampleValues(); 123 } 124 125 @Override 126 protected Collection<Entry<K, V>> actualContents() { 127 return multimap.entries(); 128 } 129 130 // TODO: dispose of this once collection is encapsulated. 131 @Override 132 @CanIgnoreReturnValue 133 protected M resetContainer(M newContents) { 134 multimap = super.resetContainer(newContents); 135 return multimap; 136 } 137 138 @CanIgnoreReturnValue 139 protected Multimap<K, V> resetContainer(Entry<K, V>... newContents) { 140 multimap = super.resetContainer(getSubjectGenerator().create((Object[]) newContents)); 141 return multimap; 142 } 143 144 /** 145 * @see AbstractContainerTester#resetContainer() 146 */ 147 protected void resetCollection() { 148 resetContainer(); 149 } 150 151 protected void assertGet(K key, V... values) { 152 assertGet(key, Arrays.asList(values)); 153 } 154 155 protected void assertGet(K key, Collection<? extends V> values) { 156 assertEqualIgnoringOrder(values, multimap().get(key)); 157 158 if (!values.isEmpty()) { 159 assertEqualIgnoringOrder(values, multimap().asMap().get(key)); 160 assertFalse(multimap().isEmpty()); 161 } else { 162 assertNull(multimap().asMap().get(key)); 163 } 164 165 assertEquals(values.size(), multimap().get(key).size()); 166 167 assertEquals(values.size() > 0, multimap().containsKey(key)); 168 assertEquals(values.size() > 0, multimap().keySet().contains(key)); 169 assertEquals(values.size() > 0, multimap().keys().contains(key)); 170 } 171 172 protected final K k0() { 173 return e0().getKey(); 174 } 175 176 protected final V v0() { 177 return e0().getValue(); 178 } 179 180 protected final K k1() { 181 return e1().getKey(); 182 } 183 184 protected final V v1() { 185 return e1().getValue(); 186 } 187 188 protected final K k2() { 189 return e2().getKey(); 190 } 191 192 protected final V v2() { 193 return e2().getValue(); 194 } 195 196 protected final K k3() { 197 return e3().getKey(); 198 } 199 200 protected final V v3() { 201 return e3().getValue(); 202 } 203 204 protected final K k4() { 205 return e4().getKey(); 206 } 207 208 protected final V v4() { 209 return e4().getValue(); 210 } 211}