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