1 /* 2 * Copyright (C) 2008 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; 18 19 import com.google.common.annotations.GwtCompatible; 20 import com.google.common.collect.testing.MapInterfaceTest; 21 22 import java.util.Collection; 23 import java.util.Map; 24 25 /** 26 * Test {@link Multimap#asMap()} for an arbitrary multimap with 27 * {@link MapInterfaceTest}. 28 * 29 * @author George van den Driessche 30 * @author Jared Levy 31 */ 32 @GwtCompatible 33 public abstract class AbstractMultimapAsMapImplementsMapTest 34 extends MapInterfaceTest<String, Collection<Integer>> { 35 AbstractMultimapAsMapImplementsMapTest( boolean modifiable, boolean allowsNulls, boolean supportsIteratorRemove)36 public AbstractMultimapAsMapImplementsMapTest( 37 boolean modifiable, boolean allowsNulls, boolean supportsIteratorRemove) { 38 super(allowsNulls, allowsNulls, false, modifiable, modifiable, supportsIteratorRemove); 39 } 40 populate(Multimap<String, Integer> multimap)41 protected void populate(Multimap<String, Integer> multimap) { 42 multimap.put("one", 1); 43 multimap.put("two", 2); 44 multimap.put("two", 22); 45 multimap.put("three", 3); 46 multimap.put("three", 33); 47 multimap.put("three", 333); 48 } 49 getKeyNotInPopulatedMap()50 @Override protected String getKeyNotInPopulatedMap() 51 throws UnsupportedOperationException { 52 return "zero"; 53 } 54 getValueNotInPopulatedMap()55 @Override protected Collection<Integer> getValueNotInPopulatedMap() 56 throws UnsupportedOperationException { 57 return Lists.newArrayList(0); 58 } 59 60 /** 61 * The version of this test supplied by {@link MapInterfaceTest} fails for 62 * this particular Map implementation, because {@code map.get()} returns a 63 * view collection that changes in the course of a call to {@code remove()}. 64 * Thus, the expectation doesn't hold that {@code map.remove(x)} returns the 65 * same value which {@code map.get(x)} did immediately beforehand. 66 */ testRemove()67 @Override public void testRemove() { 68 final Map<String, Collection<Integer>> map; 69 final String keyToRemove; 70 try { 71 map = makePopulatedMap(); 72 } catch (UnsupportedOperationException e) { 73 return; 74 } 75 keyToRemove = map.keySet().iterator().next(); 76 if (supportsRemove) { 77 int initialSize = map.size(); 78 map.get(keyToRemove); 79 map.remove(keyToRemove); 80 // This line doesn't hold - see the Javadoc comments above. 81 // assertEquals(expectedValue, oldValue); 82 assertFalse(map.containsKey(keyToRemove)); 83 assertEquals(initialSize - 1, map.size()); 84 } else { 85 try { 86 map.remove(keyToRemove); 87 fail("Expected UnsupportedOperationException."); 88 } catch (UnsupportedOperationException e) { 89 // Expected. 90 } 91 } 92 assertInvariants(map); 93 } 94 } 95