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