Coverage Summary for Class: AbstractMultimapTester (com.google.common.collect.testing.google)

Class Class, % Method, % Line, %
AbstractMultimapTester 0% (0/1) 0% (0/29) 0% (0/63)


1 /* 2  * Copyright (C) 2012 The Guava Authors 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); 5  * you may not use this file except in compliance with the License. 6  * You may obtain a copy of the License at 7  * 8  * http://www.apache.org/licenses/LICENSE-2.0 9  * 10  * Unless required by applicable law or agreed to in writing, software 11  * distributed under the License is distributed on an "AS IS" BASIS, 12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13  * See the License for the specific language governing permissions and 14  * limitations under the License. 15  */ 16  17 package com.google.common.collect.testing.google; 18  19 import static com.google.common.collect.testing.Helpers.assertEqualIgnoringOrder; 20  21 import com.google.common.annotations.GwtCompatible; 22 import com.google.common.collect.Multimap; 23 import com.google.common.collect.testing.AbstractContainerTester; 24 import com.google.common.collect.testing.Helpers; 25 import com.google.common.collect.testing.SampleElements; 26 import java.util.Arrays; 27 import java.util.Collection; 28 import java.util.Iterator; 29 import java.util.Map.Entry; 30 import org.junit.Ignore; 31  32 /** 33  * Superclass for all {@code Multimap} testers. 34  * 35  * @author Louis Wasserman 36  */ 37 @GwtCompatible 38 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 39 public abstract class AbstractMultimapTester<K, V, M extends Multimap<K, V>> 40  extends AbstractContainerTester<M, Entry<K, V>> { 41  42  private M multimap; 43  44  protected M multimap() { 45  return multimap; 46  } 47  48  /** @return an array of the proper size with {@code null} as the key of the middle element. */ 49  protected Entry<K, V>[] createArrayWithNullKey() { 50  Entry<K, V>[] array = createSamplesArray(); 51  final int nullKeyLocation = getNullLocation(); 52  final Entry<K, V> oldEntry = array[nullKeyLocation]; 53  array[nullKeyLocation] = Helpers.mapEntry(null, oldEntry.getValue()); 54  return array; 55  } 56  57  /** @return an array of the proper size with {@code null} as the value of the middle element. */ 58  protected Entry<K, V>[] createArrayWithNullValue() { 59  Entry<K, V>[] array = createSamplesArray(); 60  final int nullValueLocation = getNullLocation(); 61  final Entry<K, V> oldEntry = array[nullValueLocation]; 62  array[nullValueLocation] = Helpers.mapEntry(oldEntry.getKey(), null); 63  return array; 64  } 65  66  /** 67  * @return an array of the proper size with {@code null} as the key and value of the middle 68  * element. 69  */ 70  protected Entry<K, V>[] createArrayWithNullKeyAndValue() { 71  Entry<K, V>[] array = createSamplesArray(); 72  final int nullValueLocation = getNullLocation(); 73  array[nullValueLocation] = Helpers.mapEntry(null, null); 74  return array; 75  } 76  77  protected V getValueForNullKey() { 78  return getEntryNullReplaces().getValue(); 79  } 80  81  protected K getKeyForNullValue() { 82  return getEntryNullReplaces().getKey(); 83  } 84  85  private Entry<K, V> getEntryNullReplaces() { 86  Iterator<Entry<K, V>> entries = getSampleElements().iterator(); 87  for (int i = 0; i < getNullLocation(); i++) { 88  entries.next(); 89  } 90  return entries.next(); 91  } 92  93  protected void initMultimapWithNullKey() { 94  resetContainer(getSubjectGenerator().create((Object[]) createArrayWithNullKey())); 95  } 96  97  protected void initMultimapWithNullValue() { 98  resetContainer(getSubjectGenerator().create((Object[]) createArrayWithNullValue())); 99  } 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 }