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