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